Skip to content

Commit

Permalink
Add HttpHeaders parameter to proxy methods
Browse files Browse the repository at this point in the history
Updated service interface and tests to reflect this change, allowing for better request source tracking.
  • Loading branch information
Gcolon021 committed Oct 2, 2024
1 parent 4220651 commit 431ff87
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -34,12 +36,17 @@ public ProxyWebClient() {
client = HttpClientUtil.getConfiguredHttpClient();
}

public Response postProxy(String containerId, String path, String body, MultivaluedMap<String, String> queryParams) {
public Response postProxy(
String containerId, String path, String body, MultivaluedMap<String, String> queryParams, HttpHeaders headers
) {
if (containerIsNOTAResource(containerId)) {
return Response.status(400, "container name not trustworthy").build();
}

LOG.info("path={}, containerId={}, body={}, queryParams={}", path, containerId, body, queryParams);
String requestSource = Utilities.getRequestSourceFromHeader(headers);
LOG.info(
"path={}, requestSource={}, containerId={}, body={}, queryParams={}, ", path, requestSource, containerId, body, queryParams
);

try {
URI uri =
Expand All @@ -56,12 +63,13 @@ public Response postProxy(String containerId, String path, String body, Multival
}
}

public Response getProxy(String containerId, String path, MultivaluedMap<String, String> queryParams) {
public Response getProxy(String containerId, String path, MultivaluedMap<String, String> queryParams, HttpHeaders headers) {
if (containerIsNOTAResource(containerId)) {
return Response.status(400, "container name not trustworthy").build();
}

LOG.info("path={}, containerId={}, queryParams={}", path, containerId, queryParams);
String requestSource = Utilities.getRequestSourceFromHeader(headers);
LOG.info("path={}, requestSource={}, containerId={}, queryParams={}", path, requestSource, containerId, queryParams);

try {
URI uri =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

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

Expand All @@ -89,7 +88,7 @@ public void shouldPostWithParams() throws IOException {

MultivaluedHashMap<String, String> 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());
}
Expand Down

0 comments on commit 431ff87

Please sign in to comment.