Skip to content

Commit

Permalink
Close resources for aggregate resource
Browse files Browse the repository at this point in the history
  • Loading branch information
ramari16 committed Nov 4, 2024
1 parent 149be33 commit e2afc16
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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) {
Expand All @@ -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()
Expand All @@ -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);
}
}

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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();
Expand All @@ -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");
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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: {} {} ",
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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. <p> The PicsureInfoService,
Expand Down Expand Up @@ -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()");

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

0 comments on commit e2afc16

Please sign in to comment.