From 4ff7f8bd78b33c5c8b137b892de17161ca70f686 Mon Sep 17 00:00:00 2001 From: dev-mlb <19797865+dev-mlb@users.noreply.github.com> Date: Sun, 10 Sep 2023 08:07:24 -0400 Subject: [PATCH 1/2] update httpclient call to use a response handler --- src/main/java/emissary/client/EmissaryClient.java | 12 ++---------- src/main/java/emissary/client/EmissaryResponse.java | 12 ++++++++++++ .../emissary/directory/HeartbeatManagerTest.java | 6 ++++-- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/main/java/emissary/client/EmissaryClient.java b/src/main/java/emissary/client/EmissaryClient.java index 323e66352d..a638a21d5e 100644 --- a/src/main/java/emissary/client/EmissaryClient.java +++ b/src/main/java/emissary/client/EmissaryClient.java @@ -1,5 +1,6 @@ package emissary.client; +import emissary.client.EmissaryResponse.EmissaryResponseResponseHandler; import emissary.config.ConfigUtil; import emissary.config.Configurator; @@ -18,12 +19,9 @@ import org.apache.hc.client5.http.impl.auth.BasicAuthCache; import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; -import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager; import org.apache.hc.client5.http.protocol.HttpClientContext; -import org.apache.hc.core5.http.HttpEntity; -import org.apache.hc.core5.http.io.entity.EntityUtils; import org.apache.hc.core5.http.message.BasicClassicHttpResponse; import org.apache.hc.core5.util.Timeout; import org.eclipse.jetty.http.HttpStatus; @@ -192,7 +190,6 @@ public EmissaryResponse send(final HttpUriRequestBase method, @Nullable final Co } catch (URISyntaxException e) { LOGGER.debug("Sending {} and failed to retrieve URI", method.getMethod()); } - EmissaryResponse er; HttpClientContext localContext = HttpClientContext.create(); localContext.setAttribute(HttpClientContext.AUTH_CACHE, EmissaryClient.AUTH_CACHE); @@ -207,12 +204,7 @@ public EmissaryResponse send(final HttpUriRequestBase method, @Nullable final Co // to use a different context and request config per request method.setConfig(requestConfig); CloseableHttpClient thisClient = getHttpClient(); - try (CloseableHttpResponse response = thisClient.execute(method, localContext)) { - HttpEntity entity = response.getEntity(); - er = new EmissaryResponse(response); - EntityUtils.consume(entity); - } - return er; + return thisClient.execute(method, localContext, new EmissaryResponseResponseHandler()); } catch (IOException e) { LOGGER.debug("Problem processing request:", e); BasicClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.INTERNAL_SERVER_ERROR_500, e.getMessage()); diff --git a/src/main/java/emissary/client/EmissaryResponse.java b/src/main/java/emissary/client/EmissaryResponse.java index d7728ffebf..001779df09 100644 --- a/src/main/java/emissary/client/EmissaryResponse.java +++ b/src/main/java/emissary/client/EmissaryResponse.java @@ -8,6 +8,8 @@ import org.apache.hc.core5.http.Header; import org.apache.hc.core5.http.HttpEntity; import org.apache.hc.core5.http.HttpHeaders; +import org.apache.hc.core5.http.io.HttpClientResponseHandler; +import org.apache.hc.core5.http.io.entity.EntityUtils; import org.eclipse.jetty.http.HttpStatus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -117,5 +119,15 @@ private T makeErrorEntity(String msg, Class mapper) { return r; } + public static class EmissaryResponseResponseHandler implements HttpClientResponseHandler { + + @Override + public EmissaryResponse handleResponse(ClassicHttpResponse response) throws IOException { + HttpEntity entity = response.getEntity(); + EmissaryResponse er = new EmissaryResponse(response); + EntityUtils.consume(entity); + return er; + } + } } diff --git a/src/test/java/emissary/directory/HeartbeatManagerTest.java b/src/test/java/emissary/directory/HeartbeatManagerTest.java index 191fc438d0..6201ae0483 100644 --- a/src/test/java/emissary/directory/HeartbeatManagerTest.java +++ b/src/test/java/emissary/directory/HeartbeatManagerTest.java @@ -49,7 +49,7 @@ void testSlowHeartbeat() throws IOException { // peer didn't respond before the timeout, throws org.apache.http.NoHttpResponseException which is still and // IOException CloseableHttpClient mockClient = mock(CloseableHttpClient.class); - when(mockClient.execute(any(HttpUriRequest.class), any(HttpContext.class))).thenThrow( + when(mockClient.execute(any(HttpUriRequest.class), any(HttpContext.class), any())).thenThrow( new NoHttpResponseException("localhost:1222 failed to respond")); EmissaryClient client = new EmissaryClient(mockClient); @@ -66,7 +66,6 @@ void testUnauthorizedHeartbeat() throws IOException { CloseableHttpResponse mockResponse = mock(CloseableHttpResponse.class); HttpEntity mockHttpEntity = mock(HttpEntity.class); - when(mockClient.execute(any(HttpUriRequest.class), any(HttpContext.class))).thenReturn(mockResponse); when(mockResponse.getCode()).thenReturn(401); when(mockResponse.getEntity()).thenReturn(mockHttpEntity); String responseString = "Unauthorized heartbeat man"; @@ -75,6 +74,9 @@ void testUnauthorizedHeartbeat() throws IOException { Header[] headers = new Header[] {header1}; when(mockResponse.getHeaders(any())).thenReturn(headers); + EmissaryResponse resp = new EmissaryResponse(mockResponse); + when(mockClient.execute(any(HttpUriRequest.class), any(HttpContext.class), any())).thenReturn(resp); + EmissaryClient client = new EmissaryClient(mockClient); String fromPlace = "EMISSARY_DIRECTORY_SERVICES.DIRECTORY.STUDY.http://localhost:8001/DirectoryPlace"; From b3c6f2225a1899b412877d42dda47f951b0e0f33 Mon Sep 17 00:00:00 2001 From: dev-mlb <19797865+dev-mlb@users.noreply.github.com> Date: Thu, 14 Sep 2023 11:46:05 -0400 Subject: [PATCH 2/2] fix emissary response handler name --- src/main/java/emissary/client/EmissaryClient.java | 4 ++-- src/main/java/emissary/client/EmissaryResponse.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/emissary/client/EmissaryClient.java b/src/main/java/emissary/client/EmissaryClient.java index a638a21d5e..1fd0d9425a 100644 --- a/src/main/java/emissary/client/EmissaryClient.java +++ b/src/main/java/emissary/client/EmissaryClient.java @@ -1,6 +1,6 @@ package emissary.client; -import emissary.client.EmissaryResponse.EmissaryResponseResponseHandler; +import emissary.client.EmissaryResponse.EmissaryResponseHandler; import emissary.config.ConfigUtil; import emissary.config.Configurator; @@ -204,7 +204,7 @@ public EmissaryResponse send(final HttpUriRequestBase method, @Nullable final Co // to use a different context and request config per request method.setConfig(requestConfig); CloseableHttpClient thisClient = getHttpClient(); - return thisClient.execute(method, localContext, new EmissaryResponseResponseHandler()); + return thisClient.execute(method, localContext, new EmissaryResponseHandler()); } catch (IOException e) { LOGGER.debug("Problem processing request:", e); BasicClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.INTERNAL_SERVER_ERROR_500, e.getMessage()); diff --git a/src/main/java/emissary/client/EmissaryResponse.java b/src/main/java/emissary/client/EmissaryResponse.java index 001779df09..4aa4be0079 100644 --- a/src/main/java/emissary/client/EmissaryResponse.java +++ b/src/main/java/emissary/client/EmissaryResponse.java @@ -119,7 +119,7 @@ private T makeErrorEntity(String msg, Class mapper) { return r; } - public static class EmissaryResponseResponseHandler implements HttpClientResponseHandler { + public static class EmissaryResponseHandler implements HttpClientResponseHandler { @Override public EmissaryResponse handleResponse(ClassicHttpResponse response) throws IOException {