Skip to content

Commit

Permalink
Refactor HTTP response handling and test assertions
Browse files Browse the repository at this point in the history
Replaced redundant response entity consumption logic with a dedicated method to improve code clarity and maintainability. Simplified test assertions by directly casting response entities to strings, ensuring consistency across test cases.
  • Loading branch information
Gcolon021 committed Nov 4, 2024
1 parent c6a09af commit 04400ba
Show file tree
Hide file tree
Showing 2 changed files with 226 additions and 288 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,7 @@ public ResourceInfo info(String rsURL, QueryRequest queryRequest) {
} catch (JsonProcessingException e) {
throw new NotAuthorizedException("Unable to encode resource credentials", e);
} finally {
if (resourcesResponse != null) {
try {
EntityUtils.consume(resourcesResponse.getEntity());
} catch (IOException e) {
logger.error("Failed to close HttpResponse entity", e);
}
}
closeHttpResponse(resourcesResponse);
}
}

Expand Down Expand Up @@ -116,13 +110,7 @@ public PaginatedSearchResult<?> searchConceptValues(
} catch (URISyntaxException e) {
throw new ApplicationException("rsURL invalid : " + rsURL, e);
} finally {
if (resourcesResponse != null) {
try {
EntityUtils.consume(resourcesResponse.getEntity());
} catch (IOException e) {
logger.error("Failed to close HttpResponse entity", e);
}
}
closeHttpResponse(resourcesResponse);
}
}

Expand Down Expand Up @@ -156,13 +144,7 @@ public SearchResults search(String rsURL, QueryRequest searchQueryRequest) {
// TODO Write custom exception
throw new ProtocolException("Unable to serialize search query", e);
} finally {
if (resourcesResponse != null) {
try {
EntityUtils.consume(resourcesResponse.getEntity());
} catch (IOException e) {
logger.error("Failed to close HttpResponse entity", e);
}
}
closeHttpResponse(resourcesResponse);
}
}

Expand Down Expand Up @@ -193,18 +175,13 @@ public QueryStatus query(String rsURL, QueryRequest dataQueryRequest) {
logger.error("Unable to encode data query");
throw new ProtocolException("Unable to encode data query", e);
} finally {
if (resourcesResponse != null) {
try {
EntityUtils.consume(resourcesResponse.getEntity());
} catch (IOException e) {
logger.error("Failed to close HttpResponse entity", e);
}
}
closeHttpResponse(resourcesResponse);
}
}

public QueryStatus queryStatus(String rsURL, String queryId, QueryRequest queryRequest) {
logger.debug("Calling ResourceWebClient query()");
HttpResponse resourcesResponse = null;
try {
if (queryRequest == null) {
throw new ProtocolException(ProtocolException.MISSING_DATA);
Expand All @@ -222,7 +199,7 @@ public QueryStatus queryStatus(String rsURL, String queryId, QueryRequest queryR
String body = json.writeValueAsString(queryRequest);
logger.debug(httpClientUtil.composeURL(rsURL, pathName));
logger.debug(body);
HttpResponse resourcesResponse = httpClientUtil.retrievePostResponse(
resourcesResponse = httpClientUtil.retrievePostResponse(
httpClientUtil.composeURL(rsURL, pathName), createHeaders(queryRequest.getResourceCredentials()), body
);
if (resourcesResponse.getStatusLine().getStatusCode() != 200) {
Expand All @@ -233,11 +210,25 @@ public QueryStatus queryStatus(String rsURL, String queryId, QueryRequest queryR
} catch (JsonProcessingException e) {
logger.error("Unable to encode resource credentials");
throw new ProtocolException("Unable to encode resource credentials", e);
} finally {
closeHttpResponse(resourcesResponse);
}
}

private void closeHttpResponse(HttpResponse resourcesResponse) {
if (resourcesResponse != null) {
try {
EntityUtils.consume(resourcesResponse.getEntity());
} catch (IOException e) {
logger.error("Failed to close HttpResponse entity", e);
}
}
}

public Response queryResult(String rsURL, String queryId, QueryRequest queryRequest) {
logger.debug("Calling ResourceWebClient query()");

HttpResponse resourcesResponse = null;
try {
if (queryRequest == null) {
throw new ProtocolException(ProtocolException.MISSING_DATA);
Expand All @@ -253,19 +244,21 @@ public Response queryResult(String rsURL, String queryId, QueryRequest queryRequ
}
String pathName = "/query/" + queryId + "/result";
String body = json.writeValueAsString(queryRequest);
HttpResponse resourcesResponse = httpClientUtil.retrievePostResponse(
httpClientUtil.composeURL(rsURL, pathName), createHeaders(queryRequest.getResourceCredentials()), body
resourcesResponse = httpClientUtil.retrievePostResponse(
HttpClientUtil.composeURL(rsURL, pathName), createHeaders(queryRequest.getResourceCredentials()), body
);

String content = httpClientUtil.readObjectFromResponse(resourcesResponse);
if (resourcesResponse.getStatusLine().getStatusCode() != 200) {
logger.error("ResourceRS did not return a 200");
httpClientUtil.throwResponseError(resourcesResponse, rsURL);
HttpClientUtil.throwResponseError(resourcesResponse, rsURL);
}
return Response.ok(resourcesResponse.getEntity().getContent()).build();
return Response.ok(content).build();
} catch (JsonProcessingException e) {
logger.error("Unable to encode resource credentials");
throw new NotAuthorizedException("Unable to encode resource credentials", e);
} catch (IOException e) {
throw new ResourceInterfaceException("Error getting results", e);
} finally {
closeHttpResponse(resourcesResponse);
}
}

Expand All @@ -290,30 +283,26 @@ public Response queryResultSignedUrl(String rsURL, String queryId, QueryRequest
resourcesResponse = httpClientUtil.retrievePostResponse(
httpClientUtil.composeURL(rsURL, pathName), createHeaders(queryRequest.getResourceCredentials()), body
);

if (resourcesResponse.getStatusLine().getStatusCode() != 200) {
logger.error("ResourceRS did not return a 200");
httpClientUtil.throwResponseError(resourcesResponse, rsURL);
}
return Response.ok(resourcesResponse.getEntity().getContent()).build();

String content = httpClientUtil.readObjectFromResponse(resourcesResponse);
return Response.ok(content).build();
} catch (JsonProcessingException e) {
logger.error("Unable to encode resource credentials");
throw new NotAuthorizedException("Unable to encode resource credentials", e);
} catch (IOException e) {
throw new ResourceInterfaceException("Error getting results", e);
} finally {
if (resourcesResponse != null) {
try {
EntityUtils.consume(resourcesResponse.getEntity());
} catch (IOException e) {
logger.error("Failed to close HttpResponse entity", e);
}
}
closeHttpResponse(resourcesResponse);
}
}


public Response queryFormat(String rsURL, QueryRequest queryRequest) {
logger.debug("Calling ResourceWebClient queryFormat()");
HttpResponse resourcesResponse = null;
try {
if (queryRequest == null) {
throw new ProtocolException(ProtocolException.MISSING_DATA);
Expand All @@ -326,20 +315,22 @@ public Response queryFormat(String rsURL, QueryRequest queryRequest) {
}
String pathName = "/query/format";
String body = json.writeValueAsString(queryRequest);
HttpResponse resourcesResponse = httpClientUtil.retrievePostResponse(
resourcesResponse = httpClientUtil.retrievePostResponse(
httpClientUtil.composeURL(rsURL, pathName), createHeaders(queryRequest.getResourceCredentials()), body
);

String content = httpClientUtil.readObjectFromResponse(resourcesResponse);
int status = resourcesResponse.getStatusLine().getStatusCode();
if (status != 200) {
logger.error("Query format request did not return a 200: " + resourcesResponse.getStatusLine().getStatusCode());
return Response.status(status).entity(resourcesResponse.getEntity().getContent()).build();
logger.error("Query format request did not return a 200: {}", resourcesResponse.getStatusLine().getStatusCode());
return Response.status(status).entity(content).build();
}
return Response.ok(resourcesResponse.getEntity().getContent()).build();
return Response.ok(content).build();
} catch (JsonProcessingException e) {
logger.error("Unable to encode resource credentials");
throw new NotAuthorizedException("Unable to encode resource credentials", e);
} catch (IOException e) {
throw new ResourceInterfaceException("Error getting results", e);
} finally {
closeHttpResponse(resourcesResponse);
}
}

Expand Down Expand Up @@ -377,25 +368,17 @@ public Response querySync(String rsURL, QueryRequest queryRequest, String reques
throwError(resourcesResponse, rsURL);
}

String content = httpClientUtil.readObjectFromResponse(resourcesResponse);
if (resourcesResponse.containsHeader(QUERY_METADATA_FIELD)) {
Header metadataHeader = ((Header[]) resourcesResponse.getHeaders(QUERY_METADATA_FIELD))[0];
return Response.ok(resourcesResponse.getEntity().getContent()).header(QUERY_METADATA_FIELD, metadataHeader.getValue())
.build();
return Response.ok(content).header(QUERY_METADATA_FIELD, metadataHeader.getValue()).build();
}
return Response.ok(resourcesResponse.getEntity().getContent()).build();
return Response.ok(content).build();
} catch (JsonProcessingException e) {
logger.error("Unable to encode resource credentials");
throw new NotAuthorizedException("Unable to encode resource credentials", e);
} catch (IOException e) {
throw new ResourceInterfaceException("Error getting results", e);
} finally {
if (resourcesResponse != null) {
try {
EntityUtils.consume(resourcesResponse.getEntity());
} catch (IOException e) {
logger.error("Failed to close HttpResponse entity", e);
}
}
closeHttpResponse(resourcesResponse);
}
}

Expand Down Expand Up @@ -437,24 +420,17 @@ public Response queryContinuous(String rsURL, QueryRequest queryRequest, String
}

logger.debug("Calling ResourceWebClient queryContinuous() with body: " + body + " and headers: " + queryRequest);
resourcesResponse = httpClientUtil.retrievePostResponse(httpClientUtil.composeURL(rsURL, pathName), headers, body);
resourcesResponse = httpClientUtil.retrievePostResponse(HttpClientUtil.composeURL(rsURL, pathName), headers, body);
if (resourcesResponse.getStatusLine().getStatusCode() != 200) {
throwError(resourcesResponse, rsURL);
}

return Response.ok(resourcesResponse.getEntity().getContent()).build();
String content = httpClientUtil.readObjectFromResponse(resourcesResponse);
return Response.ok(content).build();
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new ResourceInterfaceException("Error getting results", e);
} finally {
if (resourcesResponse != null) {
try {
EntityUtils.consume(resourcesResponse.getEntity());
} catch (IOException e) {
logger.error("Failed to close HttpResponse entity", e);
}
}
closeHttpResponse(resourcesResponse);
}
}

Expand All @@ -467,6 +443,7 @@ private void throwError(HttpResponse response, String baseURL) {
errorMessage += "/n" + responseNode.get("message").asText();
}
} catch (IOException e) {
logger.error(e.getMessage());
}
if (response.getStatusLine().getStatusCode() == 401) {
throw new NotAuthorizedException(errorMessage);
Expand Down
Loading

0 comments on commit 04400ba

Please sign in to comment.