Skip to content

Commit e6c71b6

Browse files
oconnelciluwatar
authored andcommitted
988: Replaced all of the Apache HttpClients with Java's java.net.http (iluwatar#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
1 parent 7e698a9 commit e6c71b6

File tree

7 files changed

+64
-68
lines changed

7 files changed

+64
-68
lines changed

aggregator-microservices/aggregator-service/pom.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@
5353
<artifactId>mockito-core</artifactId>
5454
<scope>test</scope>
5555
</dependency>
56-
<dependency>
57-
<groupId>org.apache.httpcomponents</groupId>
58-
<artifactId>httpclient</artifactId>
59-
</dependency>
6056
</dependencies>
6157

6258
<build>

aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInformationClientImpl.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,16 @@
2222
*/
2323
package com.iluwatar.aggregator.microservices;
2424

25-
import org.apache.http.client.ClientProtocolException;
26-
import org.apache.http.client.methods.CloseableHttpResponse;
27-
import org.apache.http.client.methods.HttpGet;
28-
import org.apache.http.impl.client.CloseableHttpClient;
29-
import org.apache.http.impl.client.HttpClients;
30-
import org.apache.http.util.EntityUtils;
25+
import java.io.IOException;
26+
import java.net.URI;
27+
import java.net.http.HttpClient;
28+
import java.net.http.HttpRequest;
29+
import java.net.http.HttpResponse;
30+
3131
import org.slf4j.Logger;
3232
import org.slf4j.LoggerFactory;
3333
import org.springframework.stereotype.Component;
3434

35-
import java.io.IOException;
36-
3735
/**
3836
* An adapter to communicate with information micro-service.
3937
*/
@@ -45,15 +43,15 @@ public class ProductInformationClientImpl implements ProductInformationClient {
4543
@Override
4644
public String getProductTitle() {
4745
String response = null;
48-
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
49-
HttpGet httpGet = new HttpGet("http://localhost:51515/information");
50-
try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) {
51-
response = EntityUtils.toString(httpResponse.getEntity());
52-
}
53-
} catch (ClientProtocolException cpe) {
54-
LOGGER.error("ClientProtocolException Occured", cpe);
46+
HttpRequest request = HttpRequest.newBuilder().GET().uri(URI.create("http://localhost:51515/information")).build();
47+
HttpClient client = HttpClient.newHttpClient();
48+
try {
49+
HttpResponse<String> httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString());
50+
response = httpResponse.body();
5551
} catch (IOException ioe) {
5652
LOGGER.error("IOException Occurred", ioe);
53+
} catch (InterruptedException ie) {
54+
LOGGER.error("InterruptedException Occurred", ie);
5755
}
5856
return response;
5957
}

aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInventoryClientImpl.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,16 @@
2222
*/
2323
package com.iluwatar.aggregator.microservices;
2424

25-
import org.apache.http.client.ClientProtocolException;
26-
import org.apache.http.client.methods.CloseableHttpResponse;
27-
import org.apache.http.client.methods.HttpGet;
28-
import org.apache.http.impl.client.CloseableHttpClient;
29-
import org.apache.http.impl.client.HttpClients;
30-
import org.apache.http.util.EntityUtils;
25+
import java.io.IOException;
26+
import java.net.URI;
27+
import java.net.http.HttpClient;
28+
import java.net.http.HttpRequest;
29+
import java.net.http.HttpResponse;
30+
3131
import org.slf4j.Logger;
3232
import org.slf4j.LoggerFactory;
3333
import org.springframework.stereotype.Component;
3434

35-
import java.io.IOException;
36-
3735
/**
3836
* An adapter to communicate with inventory micro-service.
3937
*/
@@ -45,15 +43,16 @@ public class ProductInventoryClientImpl implements ProductInventoryClient {
4543
@Override
4644
public int getProductInventories() {
4745
String response = "0";
48-
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
49-
HttpGet httpGet = new HttpGet("http://localhost:51516/inventories");
50-
try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) {
51-
response = EntityUtils.toString(httpResponse.getEntity());
52-
}
53-
} catch (ClientProtocolException cpe) {
54-
LOGGER.error("ClientProtocolException Occured", cpe);
46+
47+
HttpRequest request = HttpRequest.newBuilder().GET().uri(URI.create("http://localhost:51516/inventories")).build();
48+
HttpClient client = HttpClient.newHttpClient();
49+
try {
50+
HttpResponse<String> httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString());
51+
response = httpResponse.body();
5552
} catch (IOException ioe) {
5653
LOGGER.error("IOException Occurred", ioe);
54+
} catch (InterruptedException ie) {
55+
LOGGER.error("InterruptedException Occurred", ie);
5756
}
5857
return Integer.parseInt(response);
5958
}

api-gateway/api-gateway-service/pom.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@
3939
<groupId>org.springframework</groupId>
4040
<artifactId>spring-webmvc</artifactId>
4141
</dependency>
42-
<dependency>
43-
<groupId>org.apache.httpcomponents</groupId>
44-
<artifactId>httpclient</artifactId>
45-
</dependency>
4642
<dependency>
4743
<groupId>org.springframework.boot</groupId>
4844
<artifactId>spring-boot-starter-web</artifactId>

api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/ImageClientImpl.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
*/
2323
package com.iluwatar.api.gateway;
2424

25-
import org.apache.http.client.methods.CloseableHttpResponse;
26-
import org.apache.http.client.methods.HttpGet;
27-
import org.apache.http.impl.client.CloseableHttpClient;
28-
import org.apache.http.impl.client.HttpClients;
29-
import org.apache.http.util.EntityUtils;
30-
import org.springframework.stereotype.Component;
31-
3225
import java.io.IOException;
26+
import java.net.URI;
27+
import java.net.http.HttpClient;
28+
import java.net.http.HttpRequest;
29+
import java.net.http.HttpResponse;
30+
import java.net.http.HttpResponse.BodyHandlers;
31+
32+
import org.springframework.stereotype.Component;
3333

3434
/**
3535
* An adapter to communicate with the Image microservice
@@ -38,19 +38,25 @@
3838
public class ImageClientImpl implements ImageClient {
3939
/**
4040
* Makes a simple HTTP Get request to the Image microservice
41+
*
4142
* @return The path to the image
4243
*/
4344
@Override
4445
public String getImagePath() {
4546
String response = null;
46-
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
47-
HttpGet httpGet = new HttpGet("http://localhost:50005/image-path");
48-
try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) {
49-
response = EntityUtils.toString(httpResponse.getEntity());
50-
}
47+
48+
HttpClient httpClient = HttpClient.newHttpClient();
49+
HttpRequest httpGet = HttpRequest.newBuilder().GET().uri(URI.create("http://localhost:50005/image-path")).build();
50+
51+
try {
52+
HttpResponse<String> httpResponse = httpClient.send(httpGet, BodyHandlers.ofString());
53+
response = httpResponse.body();
5154
} catch (IOException e) {
5255
e.printStackTrace();
56+
} catch (InterruptedException e) {
57+
e.printStackTrace();
5358
}
59+
5460
return response;
5561
}
5662
}

api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/PriceClientImpl.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
*/
2323
package com.iluwatar.api.gateway;
2424

25-
import org.apache.http.client.methods.CloseableHttpResponse;
26-
import org.apache.http.client.methods.HttpGet;
27-
import org.apache.http.impl.client.CloseableHttpClient;
28-
import org.apache.http.impl.client.HttpClients;
29-
import org.apache.http.util.EntityUtils;
30-
import org.springframework.stereotype.Component;
31-
3225
import java.io.IOException;
26+
import java.net.URI;
27+
import java.net.http.HttpClient;
28+
import java.net.http.HttpRequest;
29+
import java.net.http.HttpResponse;
30+
import java.net.http.HttpResponse.BodyHandlers;
31+
32+
import org.springframework.stereotype.Component;
3333

3434
/**
3535
* An adapter to communicate with the Price microservice
@@ -38,19 +38,26 @@
3838
public class PriceClientImpl implements PriceClient {
3939
/**
4040
* Makes a simple HTTP Get request to the Price microservice
41+
*
4142
* @return The price of the product
4243
*/
4344
@Override
4445
public String getPrice() {
46+
4547
String response = null;
46-
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
47-
HttpGet httpGet = new HttpGet("http://localhost:50006/price");
48-
try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) {
49-
response = EntityUtils.toString(httpResponse.getEntity());
50-
}
48+
49+
HttpClient httpClient = HttpClient.newHttpClient();
50+
HttpRequest httpGet = HttpRequest.newBuilder().GET().uri(URI.create("http://localhost:50006/price")).build();
51+
52+
try {
53+
HttpResponse<String> httpResponse = httpClient.send(httpGet, BodyHandlers.ofString());
54+
response = httpResponse.body();
5155
} catch (IOException e) {
5256
e.printStackTrace();
57+
} catch (InterruptedException e) {
58+
e.printStackTrace();
5359
}
60+
5461
return response;
5562
}
5663
}

pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
<camel.version>2.24.0</camel.version>
4949
<guava.version>19.0</guava.version>
5050
<mockito.version>1.10.19</mockito.version>
51-
<apache-httpcomponents.version>4.5.10</apache-httpcomponents.version>
5251
<htmlunit.version>2.22</htmlunit.version>
5352
<guice.version>4.0</guice.version>
5453
<mongo-java-driver.version>3.3.0</mongo-java-driver.version>
@@ -216,11 +215,6 @@
216215
<artifactId>spring-webmvc</artifactId>
217216
<version>${spring.version}</version>
218217
</dependency>
219-
<dependency>
220-
<groupId>org.apache.httpcomponents</groupId>
221-
<artifactId>httpclient</artifactId>
222-
<version>${apache-httpcomponents.version}</version>
223-
</dependency>
224218
<dependency>
225219
<groupId>com.h2database</groupId>
226220
<artifactId>h2</artifactId>

0 commit comments

Comments
 (0)