Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update httpclient call to use a response handler #575

Merged
merged 2 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions src/main/java/emissary/client/EmissaryClient.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package emissary.client;

import emissary.client.EmissaryResponse.EmissaryResponseHandler;
import emissary.config.ConfigUtil;
import emissary.config.Configurator;

Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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 EmissaryResponseHandler());
} catch (IOException e) {
LOGGER.debug("Problem processing request:", e);
BasicClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.INTERNAL_SERVER_ERROR_500, e.getMessage());
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/emissary/client/EmissaryResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -117,5 +119,15 @@ private <T extends BaseEntity> T makeErrorEntity(String msg, Class<T> mapper) {
return r;
}

public static class EmissaryResponseHandler implements HttpClientResponseHandler<EmissaryResponse> {

@Override
public EmissaryResponse handleResponse(ClassicHttpResponse response) throws IOException {
HttpEntity entity = response.getEntity();
EmissaryResponse er = new EmissaryResponse(response);
EntityUtils.consume(entity);
return er;
}
}

}
6 changes: 4 additions & 2 deletions src/test/java/emissary/directory/HeartbeatManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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";
Expand All @@ -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";
Expand Down
Loading