diff --git a/apache-http-client/src/main/java/io/refactoring/http5/client/example/async/helper/UserAsyncHttpRequestHelper.java b/apache-http-client/src/main/java/io/refactoring/http5/client/example/async/helper/UserAsyncHttpRequestHelper.java index 982cf2892..3cf21799e 100644 --- a/apache-http-client/src/main/java/io/refactoring/http5/client/example/async/helper/UserAsyncHttpRequestHelper.java +++ b/apache-http-client/src/main/java/io/refactoring/http5/client/example/async/helper/UserAsyncHttpRequestHelper.java @@ -8,6 +8,7 @@ import io.refactoring.http5.client.example.helper.BaseHttpRequestHelper; import io.refactoring.http5.client.example.model.User; import java.net.URI; +import java.net.URISyntaxException; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.security.KeyManagementException; @@ -424,6 +425,13 @@ private Map getUserWithParallelRequests( } } + handleFutureResults(futuresMap, userResponseMap); + + return userResponseMap; + } + + private void handleFutureResults( + Map> futuresMap, Map userResponseMap) { log.debug("Got {} futures.", futuresMap.size()); for (Map.Entry> futureEntry : futuresMap.entrySet()) { @@ -441,8 +449,6 @@ private Map getUserWithParallelRequests( userResponseMap.put(currentUserId, message); } } - - return userResponseMap; } /** @@ -478,18 +484,8 @@ public Map executeRequestsWithInterceptors( .build(); for (int i = 0; i < count; i++) { try { - // Update request - httpGetRequest.removeHeaders("x-req-exec-number"); - httpGetRequest.addHeader("x-req-exec-number", String.valueOf(i)); - log.debug( - "Executing {} request: {} on host {}", - httpGetRequest.getMethod(), - httpGetRequest.getUri(), - httpHost); - final Future future = - closeableHttpAsyncClient.execute( - httpGetRequest, new SimpleHttpResponseCallback(httpGetRequest, "")); + executeInterceptorRequest(closeableHttpAsyncClient, httpGetRequest, i, httpHost); futuresMap.put(i, future); } catch (RequestProcessingException e) { userResponseMap.put(i, e.getMessage()); @@ -501,6 +497,32 @@ public Map executeRequestsWithInterceptors( throw new RequestProcessingException(message, e); } + handleInterceptorFutureResults(futuresMap, userResponseMap); + + return userResponseMap; + } + + private Future executeInterceptorRequest( + CloseableHttpAsyncClient closeableHttpAsyncClient, + SimpleHttpRequest httpGetRequest, + int i, + HttpHost httpHost) + throws URISyntaxException { + // Update request + httpGetRequest.removeHeaders("x-req-exec-number"); + httpGetRequest.addHeader("x-req-exec-number", String.valueOf(i)); + log.debug( + "Executing {} request: {} on host {}", + httpGetRequest.getMethod(), + httpGetRequest.getUri(), + httpHost); + + return closeableHttpAsyncClient.execute( + httpGetRequest, new SimpleHttpResponseCallback(httpGetRequest, "")); + } + + private void handleInterceptorFutureResults( + Map> futuresMap, Map userResponseMap) { log.debug("Got {} futures.", futuresMap.size()); for (Map.Entry> futureEntry : futuresMap.entrySet()) { @@ -514,8 +536,6 @@ public Map executeRequestsWithInterceptors( userResponseMap.put(currentRequestId, message); } } - - return userResponseMap; } /** diff --git a/apache-http-client/src/main/java/io/refactoring/http5/client/example/config/interceptor/UserResponseAsyncExecChainHandler.java b/apache-http-client/src/main/java/io/refactoring/http5/client/example/config/interceptor/UserResponseAsyncExecChainHandler.java index 446b6eaea..210c70adf 100644 --- a/apache-http-client/src/main/java/io/refactoring/http5/client/example/config/interceptor/UserResponseAsyncExecChainHandler.java +++ b/apache-http-client/src/main/java/io/refactoring/http5/client/example/config/interceptor/UserResponseAsyncExecChainHandler.java @@ -32,30 +32,9 @@ public void execute( && httpRequest.containsHeader("x-req-exec-number")) { final String path = httpRequest.getPath(); if (StringUtils.startsWith(path, "/api/users/")) { - final Header baseNumberHeader = httpRequest.getFirstHeader("x-base-number"); - final String baseNumberStr = baseNumberHeader.getValue(); - int baseNumber = Integer.parseInt(baseNumberStr); - - final Header reqExecNumberHeader = httpRequest.getFirstHeader("x-req-exec-number"); - final String reqExecNumberStr = reqExecNumberHeader.getValue(); - int reqExecNumber = Integer.parseInt(reqExecNumberStr); - - // check if user id is multiple of base value - if (reqExecNumber % baseNumber == 0) { - final String reasonPhrase = "Multiple of " + baseNumber; - final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK, reasonPhrase); - final ByteBuffer content = - ByteBuffer.wrap(reasonPhrase.getBytes(StandardCharsets.US_ASCII)); - final AsyncDataConsumer asyncDataConsumer = - asyncExecCallback.handleResponse( - response, new BasicEntityDetails(content.remaining(), ContentType.TEXT_PLAIN)); - asyncDataConsumer.consume(content); - asyncDataConsumer.streamEnd(null); - requestHandled = true; - } + requestHandled = handleUserRequest(httpRequest, asyncExecCallback); } } - if (!requestHandled) { asyncExecChain.proceed(httpRequest, asyncEntityProducer, scope, asyncExecCallback); } @@ -65,4 +44,31 @@ public void execute( throw new RequestProcessingException(msg, ex); } } + + private boolean handleUserRequest(HttpRequest httpRequest, AsyncExecCallback asyncExecCallback) + throws HttpException, IOException { + boolean requestHandled = false; + final Header baseNumberHeader = httpRequest.getFirstHeader("x-base-number"); + final String baseNumberStr = baseNumberHeader.getValue(); + int baseNumber = Integer.parseInt(baseNumberStr); + + final Header reqExecNumberHeader = httpRequest.getFirstHeader("x-req-exec-number"); + final String reqExecNumberStr = reqExecNumberHeader.getValue(); + int reqExecNumber = Integer.parseInt(reqExecNumberStr); + + // check if user id is multiple of base value + if (reqExecNumber % baseNumber == 0) { + final String reasonPhrase = "Multiple of " + baseNumber; + final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK, reasonPhrase); + final ByteBuffer content = ByteBuffer.wrap(reasonPhrase.getBytes(StandardCharsets.US_ASCII)); + final BasicEntityDetails entityDetails = + new BasicEntityDetails(content.remaining(), ContentType.TEXT_PLAIN); + final AsyncDataConsumer asyncDataConsumer = + asyncExecCallback.handleResponse(response, entityDetails); + asyncDataConsumer.consume(content); + asyncDataConsumer.streamEnd(null); + requestHandled = true; + } + return requestHandled; + } }