988: Replaced all of the Apache HttpClients with Java's java.net.http (#1003)

* 988: Took out the apache http component from root pom.xml

* 988: Updated the aggregator sub projects to use java.net.http instead of apache

* 988: Updated the api-gateway-service sub projects to use java.net.http instead of apache

* Applied the code style formatter
This commit is contained in:
Christopher O'Connell
2019-10-15 12:14:08 -04:00
committed by Ilkka Seppälä
parent 7e698a90dd
commit e6c71b63fc
7 changed files with 64 additions and 68 deletions

View File

@ -53,10 +53,6 @@
<artifactId>mockito-core</artifactId> <artifactId>mockito-core</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -22,18 +22,16 @@
*/ */
package com.iluwatar.aggregator.microservices; package com.iluwatar.aggregator.microservices;
import org.apache.http.client.ClientProtocolException; import java.io.IOException;
import org.apache.http.client.methods.CloseableHttpResponse; import java.net.URI;
import org.apache.http.client.methods.HttpGet; import java.net.http.HttpClient;
import org.apache.http.impl.client.CloseableHttpClient; import java.net.http.HttpRequest;
import org.apache.http.impl.client.HttpClients; import java.net.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.IOException;
/** /**
* An adapter to communicate with information micro-service. * An adapter to communicate with information micro-service.
*/ */
@ -45,15 +43,15 @@ public class ProductInformationClientImpl implements ProductInformationClient {
@Override @Override
public String getProductTitle() { public String getProductTitle() {
String response = null; String response = null;
try (CloseableHttpClient httpClient = HttpClients.createDefault()) { HttpRequest request = HttpRequest.newBuilder().GET().uri(URI.create("http://localhost:51515/information")).build();
HttpGet httpGet = new HttpGet("http://localhost:51515/information"); HttpClient client = HttpClient.newHttpClient();
try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) { try {
response = EntityUtils.toString(httpResponse.getEntity()); HttpResponse<String> httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString());
} response = httpResponse.body();
} catch (ClientProtocolException cpe) {
LOGGER.error("ClientProtocolException Occured", cpe);
} catch (IOException ioe) { } catch (IOException ioe) {
LOGGER.error("IOException Occurred", ioe); LOGGER.error("IOException Occurred", ioe);
} catch (InterruptedException ie) {
LOGGER.error("InterruptedException Occurred", ie);
} }
return response; return response;
} }

View File

@ -22,18 +22,16 @@
*/ */
package com.iluwatar.aggregator.microservices; package com.iluwatar.aggregator.microservices;
import org.apache.http.client.ClientProtocolException; import java.io.IOException;
import org.apache.http.client.methods.CloseableHttpResponse; import java.net.URI;
import org.apache.http.client.methods.HttpGet; import java.net.http.HttpClient;
import org.apache.http.impl.client.CloseableHttpClient; import java.net.http.HttpRequest;
import org.apache.http.impl.client.HttpClients; import java.net.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.IOException;
/** /**
* An adapter to communicate with inventory micro-service. * An adapter to communicate with inventory micro-service.
*/ */
@ -45,15 +43,16 @@ public class ProductInventoryClientImpl implements ProductInventoryClient {
@Override @Override
public int getProductInventories() { public int getProductInventories() {
String response = "0"; String response = "0";
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpGet httpGet = new HttpGet("http://localhost:51516/inventories"); HttpRequest request = HttpRequest.newBuilder().GET().uri(URI.create("http://localhost:51516/inventories")).build();
try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) { HttpClient client = HttpClient.newHttpClient();
response = EntityUtils.toString(httpResponse.getEntity()); try {
} HttpResponse<String> httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString());
} catch (ClientProtocolException cpe) { response = httpResponse.body();
LOGGER.error("ClientProtocolException Occured", cpe);
} catch (IOException ioe) { } catch (IOException ioe) {
LOGGER.error("IOException Occurred", ioe); LOGGER.error("IOException Occurred", ioe);
} catch (InterruptedException ie) {
LOGGER.error("InterruptedException Occurred", ie);
} }
return Integer.parseInt(response); return Integer.parseInt(response);
} }

View File

@ -39,10 +39,6 @@
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId> <artifactId>spring-webmvc</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>

View File

@ -22,14 +22,14 @@
*/ */
package com.iluwatar.api.gateway; package com.iluwatar.api.gateway;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;
import java.io.IOException; import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import org.springframework.stereotype.Component;
/** /**
* An adapter to communicate with the Image microservice * An adapter to communicate with the Image microservice
@ -38,19 +38,25 @@ import java.io.IOException;
public class ImageClientImpl implements ImageClient { public class ImageClientImpl implements ImageClient {
/** /**
* Makes a simple HTTP Get request to the Image microservice * Makes a simple HTTP Get request to the Image microservice
*
* @return The path to the image * @return The path to the image
*/ */
@Override @Override
public String getImagePath() { public String getImagePath() {
String response = null; String response = null;
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpGet httpGet = new HttpGet("http://localhost:50005/image-path"); HttpClient httpClient = HttpClient.newHttpClient();
try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) { HttpRequest httpGet = HttpRequest.newBuilder().GET().uri(URI.create("http://localhost:50005/image-path")).build();
response = EntityUtils.toString(httpResponse.getEntity());
} try {
HttpResponse<String> httpResponse = httpClient.send(httpGet, BodyHandlers.ofString());
response = httpResponse.body();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} }
return response; return response;
} }
} }

View File

@ -22,14 +22,14 @@
*/ */
package com.iluwatar.api.gateway; package com.iluwatar.api.gateway;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;
import java.io.IOException; import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import org.springframework.stereotype.Component;
/** /**
* An adapter to communicate with the Price microservice * An adapter to communicate with the Price microservice
@ -38,19 +38,26 @@ import java.io.IOException;
public class PriceClientImpl implements PriceClient { public class PriceClientImpl implements PriceClient {
/** /**
* Makes a simple HTTP Get request to the Price microservice * Makes a simple HTTP Get request to the Price microservice
*
* @return The price of the product * @return The price of the product
*/ */
@Override @Override
public String getPrice() { public String getPrice() {
String response = null; String response = null;
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpGet httpGet = new HttpGet("http://localhost:50006/price"); HttpClient httpClient = HttpClient.newHttpClient();
try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) { HttpRequest httpGet = HttpRequest.newBuilder().GET().uri(URI.create("http://localhost:50006/price")).build();
response = EntityUtils.toString(httpResponse.getEntity());
} try {
HttpResponse<String> httpResponse = httpClient.send(httpGet, BodyHandlers.ofString());
response = httpResponse.body();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} }
return response; return response;
} }
} }

View File

@ -48,7 +48,6 @@
<camel.version>2.24.0</camel.version> <camel.version>2.24.0</camel.version>
<guava.version>19.0</guava.version> <guava.version>19.0</guava.version>
<mockito.version>1.10.19</mockito.version> <mockito.version>1.10.19</mockito.version>
<apache-httpcomponents.version>4.5.10</apache-httpcomponents.version>
<htmlunit.version>2.22</htmlunit.version> <htmlunit.version>2.22</htmlunit.version>
<guice.version>4.0</guice.version> <guice.version>4.0</guice.version>
<mongo-java-driver.version>3.3.0</mongo-java-driver.version> <mongo-java-driver.version>3.3.0</mongo-java-driver.version>
@ -216,11 +215,6 @@
<artifactId>spring-webmvc</artifactId> <artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version> <version>${spring.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${apache-httpcomponents.version}</version>
</dependency>
<dependency> <dependency>
<groupId>com.h2database</groupId> <groupId>com.h2database</groupId>
<artifactId>h2</artifactId> <artifactId>h2</artifactId>