Skip to content

Commit

Permalink
Cleanup resources in passthrough resource
Browse files Browse the repository at this point in the history
  • Loading branch information
ramari16 committed Nov 4, 2024
1 parent e2afc16 commit 3e122ff
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.apache.http.HttpResponse;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -23,6 +24,7 @@
import edu.harvard.dbmi.avillach.util.exception.ApplicationException;
import edu.harvard.dbmi.avillach.util.exception.ProtocolException;
import static edu.harvard.dbmi.avillach.service.ResourceWebClient.QUERY_METADATA_FIELD;
import static edu.harvard.dbmi.avillach.util.HttpClientUtil.closeHttpResponse;

@Path("/passthru")
@Produces("application/json")
Expand Down Expand Up @@ -67,6 +69,7 @@ public PassThroughResourceRS(ApplicationProperties properties, HttpClientUtil ht
public ResourceInfo info(QueryRequest infoRequest) {
String pathName = "/info";

HttpResponse response = null;
try {
QueryRequest chainRequest = new GeneralQueryRequest();
if (infoRequest != null) {
Expand All @@ -77,7 +80,7 @@ public ResourceInfo info(QueryRequest infoRequest) {

String payload = objectMapper.writeValueAsString(chainRequest);

HttpResponse response = httpClient
response = httpClient
.retrievePostResponse(HttpClientUtil.composeURL(properties.getTargetPicsureUrl(), pathName), createAuthHeader(), payload);
if (response.getStatusLine().getStatusCode() != 200) {
logger.error(
Expand All @@ -97,7 +100,9 @@ public ResourceInfo info(QueryRequest infoRequest) {
} catch (ClassCastException | IllegalArgumentException e) {
logger.error(e.getMessage());
throw new ProtocolException(ProtocolException.INCORRECTLY_FORMATTED_REQUEST);
}
} finally {
closeHttpResponse(response);
}
}

@POST
Expand All @@ -112,13 +117,13 @@ public QueryStatus query(QueryRequest queryRequest) {
}

String pathName = "/query";

HttpResponse response = null;
try {
QueryRequest chainRequest = queryRequest.copy();
chainRequest.setResourceUUID(UUID.fromString(properties.getTargetResourceId()));

String payload = objectMapper.writeValueAsString(chainRequest);
HttpResponse response = httpClient
response = httpClient
.retrievePostResponse(httpClient.composeURL(properties.getTargetPicsureUrl(), pathName), createAuthHeader(), payload);
if (response.getStatusLine().getStatusCode() != 200) {
logger.error(
Expand All @@ -135,6 +140,8 @@ public QueryStatus query(QueryRequest queryRequest) {
} catch (ClassCastException | IllegalArgumentException e) {
logger.error(e.getMessage());
throw new ProtocolException(ProtocolException.INCORRECTLY_FORMATTED_REQUEST);
} finally {
closeHttpResponse(response);
}
}

Expand All @@ -147,30 +154,34 @@ public Response queryResult(@PathParam("resourceQueryId") String queryId, QueryR

String pathName = "/query/" + queryId + "/result";

HttpResponse response = null;
try {
QueryRequest chainRequest = new GeneralQueryRequest();
chainRequest.setQuery(resultRequest.getQuery());
chainRequest.setResourceCredentials(resultRequest.getResourceCredentials());
chainRequest.setResourceUUID(UUID.fromString(properties.getTargetResourceId()));

String payload = objectMapper.writeValueAsString(chainRequest);
HttpResponse response = httpClient
response = httpClient
.retrievePostResponse(httpClient.composeURL(properties.getTargetPicsureUrl(), pathName), createAuthHeader(), payload);
if (response.getStatusLine().getStatusCode() != 200) {
String content = httpClient.readObjectFromResponse(response);
if (response.getStatusLine().getStatusCode() != 200) {
logger.error(
"{}{} calling resource with id {} did not return a 200: {} {} ", properties.getTargetPicsureUrl(), pathName,
chainRequest.getResourceUUID(), response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()
);
httpClient.throwInternalResponseError(response, properties.getTargetPicsureUrl());
}

return Response.ok(response.getEntity().getContent()).build();
return Response.ok(content).build();
} catch (IOException e) {
throw new ApplicationException("Error encoding query for resource with id " + resultRequest.getResourceUUID());
} catch (ClassCastException | IllegalArgumentException e) {
logger.error(e.getMessage());
throw new ProtocolException(ProtocolException.INCORRECTLY_FORMATTED_REQUEST);
}
} finally {
closeHttpResponse(response);
}
}

@POST
Expand All @@ -182,15 +193,15 @@ public QueryStatus queryStatus(@PathParam("resourceQueryId") String queryId, Que
}

String pathName = "/query/" + queryId + "/status";

HttpResponse response = null;
try {
QueryRequest chainRequest = new GeneralQueryRequest();
chainRequest.setQuery(statusRequest.getQuery());
chainRequest.setResourceCredentials(statusRequest.getResourceCredentials());
chainRequest.setResourceUUID(UUID.fromString(properties.getTargetResourceId()));

String payload = objectMapper.writeValueAsString(chainRequest);
HttpResponse response = httpClient
response = httpClient
.retrievePostResponse(httpClient.composeURL(properties.getTargetPicsureUrl(), pathName), createAuthHeader(), payload);
if (response.getStatusLine().getStatusCode() != 200) {
logger.error(
Expand All @@ -207,6 +218,8 @@ public QueryStatus queryStatus(@PathParam("resourceQueryId") String queryId, Que
} catch (ClassCastException | IllegalArgumentException e) {
logger.error(e.getMessage());
throw new ProtocolException(ProtocolException.INCORRECTLY_FORMATTED_REQUEST);
} finally {
closeHttpResponse(response);
}
}

Expand All @@ -224,14 +237,15 @@ public Response querySync(QueryRequest queryRequest) {

String pathName = "/query/sync";

HttpResponse response = null;
try {
QueryRequest chainRequest = new GeneralQueryRequest();
chainRequest.setQuery(queryRequest.getQuery());
chainRequest.setResourceCredentials(queryRequest.getResourceCredentials());
chainRequest.setResourceUUID(UUID.fromString(properties.getTargetResourceId()));

String payload = objectMapper.writeValueAsString(chainRequest);
HttpResponse response = httpClient
response = httpClient
.retrievePostResponse(httpClient.composeURL(properties.getTargetPicsureUrl(), pathName), createAuthHeader(), payload);
if (response.getStatusLine().getStatusCode() != 200) {
logger.error(
Expand All @@ -253,6 +267,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 All @@ -267,6 +283,7 @@ public SearchResults search(QueryRequest searchRequest) {
throw new ProtocolException((ProtocolException.MISSING_DATA));
}

HttpResponse response = null;
String pathName = "/search/" + properties.getTargetResourceId();
try {
QueryRequest chainRequest = new GeneralQueryRequest();
Expand All @@ -275,7 +292,7 @@ public SearchResults search(QueryRequest searchRequest) {
chainRequest.setResourceUUID(UUID.fromString(properties.getTargetResourceId()));

String payload = objectMapper.writeValueAsString(chainRequest);
HttpResponse response = httpClient
response = httpClient
.retrievePostResponse(httpClient.composeURL(properties.getTargetPicsureUrl(), pathName), createAuthHeader(), payload);
if (response.getStatusLine().getStatusCode() != 200) {
logger.error(
Expand All @@ -289,6 +306,8 @@ public SearchResults search(QueryRequest searchRequest) {
// Note: this shouldn't ever happen
logger.error("Error encoding search payload", e);
throw new ApplicationException("Error encoding search for resource with id " + searchRequest.getResourceUUID());
} finally {
closeHttpResponse(response);
}
}

Expand All @@ -303,15 +322,15 @@ public Response queryFormat(QueryRequest queryRequest) {
}

String pathName = "/query/format";

HttpResponse response = null;
try {
QueryRequest chainRequest = new GeneralQueryRequest();
chainRequest.setQuery(queryRequest.getQuery());
chainRequest.setResourceCredentials(queryRequest.getResourceCredentials());
chainRequest.setResourceUUID(UUID.fromString(properties.getTargetResourceId()));

String payload = objectMapper.writeValueAsString(chainRequest);
HttpResponse response = httpClient
response = httpClient
.retrievePostResponse(httpClient.composeURL(properties.getTargetPicsureUrl(), pathName), createAuthHeader(), payload);
if (response.getStatusLine().getStatusCode() != 200) {
logger.error(
Expand All @@ -327,6 +346,8 @@ public Response queryFormat(QueryRequest queryRequest) {
} catch (ClassCastException | IllegalArgumentException e) {
logger.error(e.getMessage());
throw new ProtocolException(ProtocolException.INCORRECTLY_FORMATTED_REQUEST);
} finally {
closeHttpResponse(response);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ void testQueryResult() throws Exception {
when(httpResponse.getStatusLine()).thenReturn(statusLine);
when(httpResponse.getEntity()).thenReturn(httpResponseEntity);
when(httpClient.retrievePostResponse(anyString(), any(Header[].class), anyString())).thenReturn(httpResponse);
when(httpClient.readObjectFromResponse(any(HttpResponse.class))).thenReturn("4");

assertThrows(ProtocolException.class, () -> {
resource.queryResult("", null);
Expand Down Expand Up @@ -164,7 +165,7 @@ void testQueryResult() throws Exception {
when(httpResponse.getEntity()).thenReturn(httpResponseEntity);
GeneralQueryRequest queryRequest = newQueryRequest(null);
javax.ws.rs.core.Response returnVal = resource.queryResult(queryId.toString(), queryRequest);
assertEquals("4", IOUtils.toString((InputStream) returnVal.getEntity(), StandardCharsets.UTF_8));
assertEquals("4", returnVal.getEntity());
//assertEquals(resultId, returnVal.getHeaderString("resultId"));
}

Expand Down

0 comments on commit 3e122ff

Please sign in to comment.