Skip to content

Commit

Permalink
Code cleanup, refactored code to reduce length of functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
sagaofsilence committed May 9, 2024
1 parent de4fec3 commit 99d6dd9
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -424,6 +425,13 @@ private Map<Long, String> getUserWithParallelRequests(
}
}

handleFutureResults(futuresMap, userResponseMap);

return userResponseMap;
}

private void handleFutureResults(
Map<Long, Future<SimpleHttpResponse>> futuresMap, Map<Long, String> userResponseMap) {
log.debug("Got {} futures.", futuresMap.size());

for (Map.Entry<Long, Future<SimpleHttpResponse>> futureEntry : futuresMap.entrySet()) {
Expand All @@ -441,8 +449,6 @@ private Map<Long, String> getUserWithParallelRequests(
userResponseMap.put(currentUserId, message);
}
}

return userResponseMap;
}

/**
Expand Down Expand Up @@ -478,18 +484,8 @@ public Map<Integer, String> 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<SimpleHttpResponse> future =
closeableHttpAsyncClient.execute(
httpGetRequest, new SimpleHttpResponseCallback(httpGetRequest, ""));
executeInterceptorRequest(closeableHttpAsyncClient, httpGetRequest, i, httpHost);
futuresMap.put(i, future);
} catch (RequestProcessingException e) {
userResponseMap.put(i, e.getMessage());
Expand All @@ -501,6 +497,32 @@ public Map<Integer, String> executeRequestsWithInterceptors(
throw new RequestProcessingException(message, e);
}

handleInterceptorFutureResults(futuresMap, userResponseMap);

return userResponseMap;
}

private Future<SimpleHttpResponse> 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<Integer, Future<SimpleHttpResponse>> futuresMap, Map<Integer, String> userResponseMap) {
log.debug("Got {} futures.", futuresMap.size());

for (Map.Entry<Integer, Future<SimpleHttpResponse>> futureEntry : futuresMap.entrySet()) {
Expand All @@ -514,8 +536,6 @@ public Map<Integer, String> executeRequestsWithInterceptors(
userResponseMap.put(currentRequestId, message);
}
}

return userResponseMap;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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;
}
}

0 comments on commit 99d6dd9

Please sign in to comment.