From 4cd82c95b92445e63315fb95148a68c2e1d4bef4 Mon Sep 17 00:00:00 2001 From: GeorgeC Date: Mon, 4 Nov 2024 11:47:26 -0500 Subject: [PATCH] Initialize and consume resourcesResponse in querySync() 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. --- .../dbmi/avillach/service/ResourceWebClient.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/pic-sure-resources/pic-sure-resource-api/src/main/java/edu/harvard/dbmi/avillach/service/ResourceWebClient.java b/pic-sure-resources/pic-sure-resource-api/src/main/java/edu/harvard/dbmi/avillach/service/ResourceWebClient.java index 4ef7aaac..2bc6a81c 100644 --- a/pic-sure-resources/pic-sure-resource-api/src/main/java/edu/harvard/dbmi/avillach/service/ResourceWebClient.java +++ b/pic-sure-resources/pic-sure-resource-api/src/main/java/edu/harvard/dbmi/avillach/service/ResourceWebClient.java @@ -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"); @@ -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); } @@ -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); + } + } } }