diff --git a/pic-sure-api-war/src/main/java/edu/harvard/dbmi/avillach/PicsureRS.java b/pic-sure-api-war/src/main/java/edu/harvard/dbmi/avillach/PicsureRS.java index 2f711dcf..49bdb337 100644 --- a/pic-sure-api-war/src/main/java/edu/harvard/dbmi/avillach/PicsureRS.java +++ b/pic-sure-api-war/src/main/java/edu/harvard/dbmi/avillach/PicsureRS.java @@ -7,7 +7,6 @@ import javax.ws.rs.*; import javax.ws.rs.core.*; -import edu.harvard.dbmi.avillach.data.entity.Query; import edu.harvard.dbmi.avillach.domain.*; import edu.harvard.dbmi.avillach.service.*; import io.swagger.v3.oas.annotations.OpenAPIDefinition; @@ -154,34 +153,35 @@ public QueryStatus queryStatus( @POST @Path("/query/{queryId}/result") @Operation( - summary = "Returns result for given query", - responses = {@ApiResponse( - responseCode = "200", description = "Query result", content = @Content(schema = @Schema(implementation = Response.class)) - )} + summary = "Returns result for given query", + responses = {@ApiResponse( + responseCode = "200", description = "Query result", content = @Content(schema = @Schema(implementation = Response.class)) + )} ) @Produces(MediaType.APPLICATION_OCTET_STREAM) public Response queryResult( - @Parameter( - description = "The UUID of the query to fetch the status of. The UUID is " - + "returned by the /query endpoint as the \"picsureResultId\" in the response object" - ) @PathParam("queryId") UUID queryId, @Parameter QueryRequest credentialsQueryRequest, @Context HttpHeaders headers + @Parameter( + description = "The UUID of the query to fetch the status of. The UUID is " + + "returned by the /query endpoint as the \"picsureResultId\" in the response object" + ) @PathParam("queryId") UUID queryId, @Parameter QueryRequest credentialsQueryRequest, @Context HttpHeaders headers ) { return queryService.queryResult(queryId, credentialsQueryRequest, headers); } + @POST @Path("/query/{queryId}/signed-url") @Operation( - summary = "Returns a signed url for given query", - responses = {@ApiResponse( - responseCode = "200", description = "Query result", content = @Content(schema = @Schema(implementation = Response.class)) - )} + summary = "Returns a signed url for given query", + responses = {@ApiResponse( + responseCode = "200", description = "Query result", content = @Content(schema = @Schema(implementation = Response.class)) + )} ) @Produces(MediaType.APPLICATION_JSON) public Response queryResultSignedUrl( - @Parameter( - description = "The UUID of the query to fetch the status of. The UUID is " - + "returned by the /query endpoint as the \"picsureResultId\" in the response object" - ) @PathParam("queryId") UUID queryId, @Parameter QueryRequest credentialsQueryRequest, @Context HttpHeaders headers + @Parameter( + description = "The UUID of the query to fetch the status of. The UUID is " + + "returned by the /query endpoint as the \"picsureResultId\" in the response object" + ) @PathParam("queryId") UUID queryId, @Parameter QueryRequest credentialsQueryRequest, @Context HttpHeaders headers ) { return queryService.queryResultSignedUrl(queryId, credentialsQueryRequest, headers); } @@ -229,16 +229,20 @@ public Response generateContinuousBin(QueryRequest continuousData, @Context Http @Path("/proxy/{container}/{request : .+}") @Operation(hidden = true) public Response postProxy( - @PathParam("container") String containerId, @PathParam("request") String request, @Context UriInfo uriInfo, String body + @PathParam("container") String containerId, @PathParam("request") String request, @Context UriInfo uriInfo, String body, + @Context HttpHeaders headers ) { - return proxyWebClient.postProxy(containerId, request, body, uriInfo.getQueryParameters()); + return proxyWebClient.postProxy(containerId, request, body, uriInfo.getQueryParameters(), headers); } @GET @Path("/proxy/{container}/{request : .+}") @Operation(hidden = true) - public Response getProxy(@PathParam("container") String containerId, @PathParam("request") String request, @Context UriInfo uriInfo) { - return proxyWebClient.getProxy(containerId, request, uriInfo.getQueryParameters()); + public Response getProxy( + @PathParam("container") String containerId, @PathParam("request") String request, @Context UriInfo uriInfo, + @Context HttpHeaders headers + ) { + return proxyWebClient.getProxy(containerId, request, uriInfo.getQueryParameters(), headers); } } diff --git a/pic-sure-resources/pic-sure-resource-api/src/main/java/edu/harvard/dbmi/avillach/service/ProxyWebClient.java b/pic-sure-resources/pic-sure-resource-api/src/main/java/edu/harvard/dbmi/avillach/service/ProxyWebClient.java index 6aba4ce8..f5025233 100644 --- a/pic-sure-resources/pic-sure-resource-api/src/main/java/edu/harvard/dbmi/avillach/service/ProxyWebClient.java +++ b/pic-sure-resources/pic-sure-resource-api/src/main/java/edu/harvard/dbmi/avillach/service/ProxyWebClient.java @@ -2,6 +2,7 @@ import edu.harvard.dbmi.avillach.data.repository.ResourceRepository; import edu.harvard.dbmi.avillach.util.HttpClientUtil; +import edu.harvard.dbmi.avillach.util.Utilities; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; @@ -16,6 +17,7 @@ import javax.enterprise.context.ApplicationScoped; import javax.inject.Inject; +import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; import java.io.IOException; @@ -34,10 +36,18 @@ public ProxyWebClient() { client = HttpClientUtil.getConfiguredHttpClient(); } - public Response postProxy(String containerId, String path, String body, MultivaluedMap queryParams) { + public Response postProxy( + String containerId, String path, String body, MultivaluedMap queryParams, HttpHeaders headers + ) { if (containerIsNOTAResource(containerId)) { return Response.status(400, "container name not trustworthy").build(); } + + String requestSource = Utilities.getRequestSourceFromHeader(headers); + LOG.info( + "path={}, requestSource={}, containerId={}, body={}, queryParams={}, ", path, requestSource, containerId, body, queryParams + ); + try { URI uri = new URIBuilder().setScheme("http").setHost(containerId).setPath(path).setParameters(processParams(queryParams)).build(); @@ -53,10 +63,14 @@ public Response postProxy(String containerId, String path, String body, Multival } } - public Response getProxy(String containerId, String path, MultivaluedMap queryParams) { + public Response getProxy(String containerId, String path, MultivaluedMap queryParams, HttpHeaders headers) { if (containerIsNOTAResource(containerId)) { return Response.status(400, "container name not trustworthy").build(); } + + String requestSource = Utilities.getRequestSourceFromHeader(headers); + LOG.info("path={}, requestSource={}, containerId={}, queryParams={}", path, requestSource, containerId, queryParams); + try { URI uri = new URIBuilder().setScheme("http").setHost(containerId).setPath(path).setParameters(processParams(queryParams)).build(); diff --git a/pic-sure-resources/pic-sure-resource-api/src/test/java/edu/harvard/dbmi/avillach/service/ProxyWebClientTest.java b/pic-sure-resources/pic-sure-resource-api/src/test/java/edu/harvard/dbmi/avillach/service/ProxyWebClientTest.java index 70305337..21fb61a1 100644 --- a/pic-sure-resources/pic-sure-resource-api/src/test/java/edu/harvard/dbmi/avillach/service/ProxyWebClientTest.java +++ b/pic-sure-resources/pic-sure-resource-api/src/test/java/edu/harvard/dbmi/avillach/service/ProxyWebClientTest.java @@ -20,7 +20,6 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.util.List; -import java.util.Map; import static org.junit.Assert.*; @@ -50,7 +49,7 @@ public void shouldPostToProxy() throws IOException { Mockito.when(resourceRepository.getByColumn("name", "foo")).thenReturn(List.of(new Resource())); subject.client = client; - Response actual = subject.postProxy("foo", "/my/cool/path", "{}", new MultivaluedHashMap<>()); + Response actual = subject.postProxy("foo", "/my/cool/path", "{}", new MultivaluedHashMap<>(), null); Assert.assertEquals(200, actual.getStatus()); } @@ -63,7 +62,7 @@ public void shouldGetToProxy() throws IOException { Mockito.when(resourceRepository.getByColumn("name", "bar")).thenReturn(List.of(new Resource())); subject.client = client; - Response actual = subject.getProxy("bar", "/my/cool/path", new MultivaluedHashMap<>()); + Response actual = subject.getProxy("bar", "/my/cool/path", new MultivaluedHashMap<>(), null); Assert.assertEquals(200, actual.getStatus()); } @@ -72,10 +71,10 @@ public void shouldGetToProxy() throws IOException { public void shouldRejectNastyHost() { Mockito.when(resourceRepository.getByColumn("name", "an.evil.domain")).thenReturn(List.of()); - Response actual = subject.postProxy("an.evil.domain", "hax", null, new MultivaluedHashMap<>()); + Response actual = subject.postProxy("an.evil.domain", "hax", null, new MultivaluedHashMap<>(), null); assertEquals(400, actual.getStatus()); - actual = subject.getProxy("an.evil.domain", "hax", new MultivaluedHashMap<>()); + actual = subject.getProxy("an.evil.domain", "hax", new MultivaluedHashMap<>(), null); assertEquals(400, actual.getStatus()); } @@ -89,7 +88,7 @@ public void shouldPostWithParams() throws IOException { MultivaluedHashMap params = new MultivaluedHashMap<>(); params.put("site", List.of("bch")); - Response actual = subject.postProxy("foo", "/my/cool/path", "{}", params); + Response actual = subject.postProxy("foo", "/my/cool/path", "{}", params, null); Assert.assertEquals(200, actual.getStatus()); }