From e6c71b63fcfe9b2dd3c98821e8650cac9dd72445 Mon Sep 17 00:00:00 2001 From: Christopher O'Connell Date: Tue, 15 Oct 2019 12:14:08 -0400 Subject: [PATCH] 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 --- .../aggregator-service/pom.xml | 4 --- .../ProductInformationClientImpl.java | 28 ++++++++--------- .../ProductInventoryClientImpl.java | 29 +++++++++-------- api-gateway/api-gateway-service/pom.xml | 4 --- .../iluwatar/api/gateway/ImageClientImpl.java | 30 +++++++++++------- .../iluwatar/api/gateway/PriceClientImpl.java | 31 ++++++++++++------- pom.xml | 6 ---- 7 files changed, 64 insertions(+), 68 deletions(-) diff --git a/aggregator-microservices/aggregator-service/pom.xml b/aggregator-microservices/aggregator-service/pom.xml index 62695d9e9..d6ebc823c 100644 --- a/aggregator-microservices/aggregator-service/pom.xml +++ b/aggregator-microservices/aggregator-service/pom.xml @@ -53,10 +53,6 @@ mockito-core test - - org.apache.httpcomponents - httpclient - diff --git a/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInformationClientImpl.java b/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInformationClientImpl.java index 2588fac81..b42b5b55a 100644 --- a/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInformationClientImpl.java +++ b/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInformationClientImpl.java @@ -22,18 +22,16 @@ */ package com.iluwatar.aggregator.microservices; -import org.apache.http.client.ClientProtocolException; -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 java.io.IOException; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; -import java.io.IOException; - /** * An adapter to communicate with information micro-service. */ @@ -45,15 +43,15 @@ public class ProductInformationClientImpl implements ProductInformationClient { @Override public String getProductTitle() { String response = null; - try (CloseableHttpClient httpClient = HttpClients.createDefault()) { - HttpGet httpGet = new HttpGet("http://localhost:51515/information"); - try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) { - response = EntityUtils.toString(httpResponse.getEntity()); - } - } catch (ClientProtocolException cpe) { - LOGGER.error("ClientProtocolException Occured", cpe); + HttpRequest request = HttpRequest.newBuilder().GET().uri(URI.create("http://localhost:51515/information")).build(); + HttpClient client = HttpClient.newHttpClient(); + try { + HttpResponse httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString()); + response = httpResponse.body(); } catch (IOException ioe) { LOGGER.error("IOException Occurred", ioe); + } catch (InterruptedException ie) { + LOGGER.error("InterruptedException Occurred", ie); } return response; } diff --git a/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInventoryClientImpl.java b/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInventoryClientImpl.java index 586699f85..59a050c9e 100644 --- a/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInventoryClientImpl.java +++ b/aggregator-microservices/aggregator-service/src/main/java/com/iluwatar/aggregator/microservices/ProductInventoryClientImpl.java @@ -22,18 +22,16 @@ */ package com.iluwatar.aggregator.microservices; -import org.apache.http.client.ClientProtocolException; -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 java.io.IOException; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; -import java.io.IOException; - /** * An adapter to communicate with inventory micro-service. */ @@ -45,15 +43,16 @@ public class ProductInventoryClientImpl implements ProductInventoryClient { @Override public int getProductInventories() { String response = "0"; - try (CloseableHttpClient httpClient = HttpClients.createDefault()) { - HttpGet httpGet = new HttpGet("http://localhost:51516/inventories"); - try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) { - response = EntityUtils.toString(httpResponse.getEntity()); - } - } catch (ClientProtocolException cpe) { - LOGGER.error("ClientProtocolException Occured", cpe); + + HttpRequest request = HttpRequest.newBuilder().GET().uri(URI.create("http://localhost:51516/inventories")).build(); + HttpClient client = HttpClient.newHttpClient(); + try { + HttpResponse httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString()); + response = httpResponse.body(); } catch (IOException ioe) { LOGGER.error("IOException Occurred", ioe); + } catch (InterruptedException ie) { + LOGGER.error("InterruptedException Occurred", ie); } return Integer.parseInt(response); } diff --git a/api-gateway/api-gateway-service/pom.xml b/api-gateway/api-gateway-service/pom.xml index 8821d6b4f..fa3e7286b 100644 --- a/api-gateway/api-gateway-service/pom.xml +++ b/api-gateway/api-gateway-service/pom.xml @@ -39,10 +39,6 @@ org.springframework spring-webmvc - - org.apache.httpcomponents - httpclient - org.springframework.boot spring-boot-starter-web diff --git a/api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/ImageClientImpl.java b/api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/ImageClientImpl.java index e94657a6c..dddd0dc20 100644 --- a/api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/ImageClientImpl.java +++ b/api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/ImageClientImpl.java @@ -22,14 +22,14 @@ */ 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.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 @@ -38,19 +38,25 @@ import java.io.IOException; public class ImageClientImpl implements ImageClient { /** * Makes a simple HTTP Get request to the Image microservice + * * @return The path to the image */ @Override public String getImagePath() { String response = null; - try (CloseableHttpClient httpClient = HttpClients.createDefault()) { - HttpGet httpGet = new HttpGet("http://localhost:50005/image-path"); - try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) { - response = EntityUtils.toString(httpResponse.getEntity()); - } + + HttpClient httpClient = HttpClient.newHttpClient(); + HttpRequest httpGet = HttpRequest.newBuilder().GET().uri(URI.create("http://localhost:50005/image-path")).build(); + + try { + HttpResponse httpResponse = httpClient.send(httpGet, BodyHandlers.ofString()); + response = httpResponse.body(); } catch (IOException e) { e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); } + return response; } } diff --git a/api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/PriceClientImpl.java b/api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/PriceClientImpl.java index 4a645b809..25d7f0b11 100644 --- a/api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/PriceClientImpl.java +++ b/api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/PriceClientImpl.java @@ -22,14 +22,14 @@ */ 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.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 @@ -38,19 +38,26 @@ import java.io.IOException; public class PriceClientImpl implements PriceClient { /** * Makes a simple HTTP Get request to the Price microservice + * * @return The price of the product */ @Override public String getPrice() { + String response = null; - try (CloseableHttpClient httpClient = HttpClients.createDefault()) { - HttpGet httpGet = new HttpGet("http://localhost:50006/price"); - try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) { - response = EntityUtils.toString(httpResponse.getEntity()); - } + + HttpClient httpClient = HttpClient.newHttpClient(); + HttpRequest httpGet = HttpRequest.newBuilder().GET().uri(URI.create("http://localhost:50006/price")).build(); + + try { + HttpResponse httpResponse = httpClient.send(httpGet, BodyHandlers.ofString()); + response = httpResponse.body(); } catch (IOException e) { e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); } + return response; } } diff --git a/pom.xml b/pom.xml index 9e61e716b..d330bbfcb 100644 --- a/pom.xml +++ b/pom.xml @@ -48,7 +48,6 @@ 2.24.0 19.0 1.10.19 - 4.5.10 2.22 4.0 3.3.0 @@ -216,11 +215,6 @@ spring-webmvc ${spring.version} - - org.apache.httpcomponents - httpclient - ${apache-httpcomponents.version} - com.h2database h2