From e2afc1652c0af191241ab9bf2f49a4da459c5a74 Mon Sep 17 00:00:00 2001 From: Ryan Amari Date: Mon, 4 Nov 2024 14:33:54 -0500 Subject: [PATCH] Close resources for aggregate resource --- .../AggregateDataSharingResourceRS.java | 55 +++++++++++++------ .../avillach/service/ResourceWebClient.java | 12 +--- .../dbmi/avillach/util/HttpClientUtil.java | 10 ++++ 3 files changed, 51 insertions(+), 26 deletions(-) diff --git a/pic-sure-resources/pic-sure-aggregate-data-sharing-resource/src/main/java/edu/harvard/hms/dbmi/avillach/AggregateDataSharingResourceRS.java b/pic-sure-resources/pic-sure-aggregate-data-sharing-resource/src/main/java/edu/harvard/hms/dbmi/avillach/AggregateDataSharingResourceRS.java index a8d1a872..e3e7d7ad 100644 --- a/pic-sure-resources/pic-sure-aggregate-data-sharing-resource/src/main/java/edu/harvard/hms/dbmi/avillach/AggregateDataSharingResourceRS.java +++ b/pic-sure-resources/pic-sure-aggregate-data-sharing-resource/src/main/java/edu/harvard/hms/dbmi/avillach/AggregateDataSharingResourceRS.java @@ -36,8 +36,7 @@ import java.util.stream.Stream; import static edu.harvard.dbmi.avillach.service.ResourceWebClient.QUERY_METADATA_FIELD; -import static edu.harvard.dbmi.avillach.util.HttpClientUtil.getConfiguredHttpClient; -import static edu.harvard.dbmi.avillach.util.HttpClientUtil.readObjectFromResponse; +import static edu.harvard.dbmi.avillach.util.HttpClientUtil.*; @Path("/aggregate-data-sharing") @Produces("application/json") @@ -115,6 +114,7 @@ public ResourceInfo info(QueryRequest infoRequest) { logger.debug("Calling Aggregate Data Sharing Resource info()"); String pathName = "/info"; + HttpResponse response = null; try { QueryRequest chainRequest = new GeneralQueryRequest(); if (infoRequest != null) { @@ -129,8 +129,8 @@ public ResourceInfo info(QueryRequest infoRequest) { String payload = objectMapper.writeValueAsString(chainRequest); String composedURL = HttpClientUtil.composeURL(properties.getTargetPicsureUrl(), pathName); - HttpResponse response = httpClientUtil.retrievePostResponse(composedURL, headers, payload); - if (!httpClientUtil.is2xx(response)) { + response = httpClientUtil.retrievePostResponse(composedURL, headers, payload); + if (!HttpClientUtil.is2xx(response)) { logger.error( "{}{} did not return a 200: {} {} ", properties.getTargetPicsureUrl(), pathName, response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase() @@ -149,6 +149,8 @@ public ResourceInfo info(QueryRequest infoRequest) { } catch (ClassCastException | IllegalArgumentException e) { logger.error(e.getMessage()); throw new ProtocolException(ProtocolException.INCORRECTLY_FORMATTED_REQUEST); + } finally { + HttpClientUtil.closeHttpResponse(response); } } @@ -158,8 +160,13 @@ public ResourceInfo info(QueryRequest infoRequest) { public SearchResults search(QueryRequest searchRequest) { logger.debug("Calling Aggregate Data Sharing Search"); checkQuery(searchRequest); - HttpResponse response = postRequest(searchRequest, "/search"); - return readObjectFromResponse(response, SearchResults.class); + HttpResponse response = null; + try { + response = postRequest(searchRequest, "/search"); + return readObjectFromResponse(response, SearchResults.class); + } finally { + HttpClientUtil.closeHttpResponse(response); + } } @POST @@ -168,9 +175,13 @@ public SearchResults search(QueryRequest searchRequest) { public QueryStatus query(QueryRequest queryRequest) { logger.debug("Calling Aggregate Data Sharing Resource query()"); checkQuery(queryRequest); - HttpResponse response = postRequest(queryRequest, "/query"); - return readObjectFromResponse(response, QueryStatus.class); - + HttpResponse response = null; + try { + response = postRequest(queryRequest, "/query"); + return readObjectFromResponse(response, QueryStatus.class); + } finally { + HttpClientUtil.closeHttpResponse(response); + } } @POST @@ -179,8 +190,13 @@ public QueryStatus query(QueryRequest queryRequest) { public QueryStatus queryStatus(@PathParam("resourceQueryId") UUID queryId, QueryRequest statusRequest) { logger.debug("Calling Aggregate Data Sharing Resource queryStatus() for query {}", queryId); checkQuery(statusRequest); - HttpResponse response = postRequest(statusRequest, "/query/" + queryId + "/status"); - return readObjectFromResponse(response, QueryStatus.class); + HttpResponse response = null; + try { + response = postRequest(statusRequest, "/query/" + queryId + "/status"); + return readObjectFromResponse(response, QueryStatus.class); + } finally { + HttpClientUtil.closeHttpResponse(response); + } } @POST @@ -191,9 +207,10 @@ public Response queryResult(@PathParam("resourceQueryId") UUID queryId, QueryReq checkQuery(resultRequest); HttpResponse response = postRequest(resultRequest, "/query/" + queryId + "/result"); try { - return Response.ok(response.getEntity().getContent()).build(); - } catch (IOException e) { - throw new ApplicationException("Error encoding query for resource with id " + resultRequest.getResourceUUID()); + String responseBody = httpClientUtil.readObjectFromResponse(response); + return Response.ok(responseBody).build(); + } finally { + HttpClientUtil.closeHttpResponse(response); } } @@ -225,6 +242,7 @@ public Response querySync(QueryRequest queryRequest) { logger.debug("Calling Aggregate Data Sharing Resource querySync()"); checkQuery(queryRequest); + HttpResponse response = null; try { Object query = queryRequest.getQuery(); UUID resourceUUID = queryRequest.getResourceUUID(); @@ -245,7 +263,7 @@ public Response querySync(QueryRequest queryRequest) { return Response.status(Response.Status.BAD_REQUEST).build(); } - HttpResponse response = getHttpResponse(queryRequest, resourceUUID, "/query/sync", properties.getTargetPicsureUrl()); + response = getHttpResponse(queryRequest, resourceUUID, "/query/sync", properties.getTargetPicsureUrl()); HttpEntity entity = response.getEntity(); String entityString = EntityUtils.toString(entity, "UTF-8"); @@ -266,6 +284,8 @@ public Response querySync(QueryRequest queryRequest) { } catch (ClassCastException | IllegalArgumentException e) { logger.error(e.getMessage()); throw new ProtocolException(ProtocolException.INCORRECTLY_FORMATTED_REQUEST); + } finally { + closeHttpResponse(response); } } @@ -417,10 +437,11 @@ public Response queryFormat(QueryRequest queryRequest) { UUID resourceUUID = queryRequest.getResourceUUID(); String pathName = "/query/format"; + HttpResponse response = null; try { String queryString = objectMapper.writeValueAsString(queryRequest); String composedURL = HttpClientUtil.composeURL(properties.getTargetPicsureUrl(), pathName); - HttpResponse response = httpClientUtil.retrievePostResponse(composedURL, headers, queryString); + response = httpClientUtil.retrievePostResponse(composedURL, headers, queryString); if (!HttpClientUtil.is2xx(response)) { logger.error( composedURL + " calling resource with id " + resourceUUID + " did not return a 200: {} {} ", @@ -435,6 +456,8 @@ public Response queryFormat(QueryRequest queryRequest) { } catch (ClassCastException | IllegalArgumentException e) { logger.error(e.getMessage()); throw new ProtocolException(ProtocolException.INCORRECTLY_FORMATTED_REQUEST); + } finally { + HttpClientUtil.closeHttpResponse(response); } } 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 1f5cb8d7..0ac54062 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 @@ -25,6 +25,8 @@ import java.net.URISyntaxException; import java.util.Map; +import static edu.harvard.dbmi.avillach.util.HttpClientUtil.closeHttpResponse; + /** * The ResourceWebClient class implements the client side logic for the endpoints specified in IResourceRS.

The PicsureInfoService, @@ -215,16 +217,6 @@ public QueryStatus queryStatus(String rsURL, String queryId, QueryRequest queryR } } - 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()"); diff --git a/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/HttpClientUtil.java b/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/HttpClientUtil.java index cd1541c7..b4f1fb2a 100644 --- a/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/HttpClientUtil.java +++ b/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/HttpClientUtil.java @@ -337,4 +337,14 @@ public static HttpClient getConfiguredHttpClient(HttpClientConnectionManager con public static HttpClientUtil getInstance(HttpClientConnectionManager connectionManager) { return new HttpClientUtil(getConfiguredHttpClient(connectionManager)); } + + public static void closeHttpResponse(HttpResponse resourcesResponse) { + if (resourcesResponse != null) { + try { + EntityUtils.consume(resourcesResponse.getEntity()); + } catch (IOException e) { + logger.error("Failed to close HttpResponse entity", e); + } + } + } }