Skip to content

Commit

Permalink
Initialize and consume resourcesResponse in querySync()
Browse files Browse the repository at this point in the history
Initialize 'resourcesResponse' before the try block and ensure its entity is consumed in the finally block. This change ensures that the HttpResponse resource is properly released, preventing potential resource leaks.
  • Loading branch information
Gcolon021 committed Nov 4, 2024
1 parent e75b924 commit 4cd82c9
Showing 1 changed file with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ public Response queryFormat(String rsURL, QueryRequest queryRequest) {

public Response querySync(String rsURL, QueryRequest queryRequest, String requestSource) {
logger.debug("Calling ResourceWebClient querySync()");
HttpResponse resourcesResponse = null;
try {
if (queryRequest == null) {
throw new ProtocolException("Missing query data");
Expand All @@ -337,7 +338,7 @@ public Response querySync(String rsURL, QueryRequest queryRequest, String reques
headers = newHeaders;
}

HttpResponse 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);
}
Expand All @@ -353,6 +354,14 @@ public Response querySync(String rsURL, QueryRequest queryRequest, String reques
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);
}
}
}
}

Expand Down

0 comments on commit 4cd82c9

Please sign in to comment.