Skip to content

Commit

Permalink
ALS-7014: Create endpoint for signed urls
Browse files Browse the repository at this point in the history
  • Loading branch information
ramari16 committed Aug 26, 2024
1 parent 755015a commit 2b8f0bf
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,20 +154,37 @@ 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))
)}
)
@Produces(MediaType.APPLICATION_OCTET_STREAM)
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
) {
return queryService.queryResultSignedUrl(queryId, credentialsQueryRequest, headers);
}

@POST
@Path("/query/sync")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,56 @@ public Response queryResult(UUID queryId, QueryRequest credentialsQueryRequest,
}

logger.info(
"path=/query/{queryId}/result, resourceId={}, requestSource={}, queryRequest={}", queryId,
Utilities.getRequestSourceFromHeader(headers), Utilities.convertQueryRequestToString(mapper, credentialsQueryRequest)
"path=/query/{queryId}/result, resourceId={}, requestSource={}, queryRequest={}", queryId,
Utilities.getRequestSourceFromHeader(headers), Utilities.convertQueryRequestToString(mapper, credentialsQueryRequest)
);


credentialsQueryRequest.getResourceCredentials().put(ResourceWebClient.BEARER_TOKEN_KEY, resource.getToken());
return resourceWebClient.queryResult(resource.getResourceRSPath(), query.getResourceResultId(), credentialsQueryRequest);
}

/**
* Streams the result for a given queryId by looking up the target resource from the database and calling the target resource for a
* result. The queryStatus method should be used to verify that the result is available prior to retrieving it.
*
* @param queryId - id of target resource
* @param credentialsQueryRequest - contains resource specific credentials object
* @return Response
*/
@Transactional
public Response queryResultSignedUrl(UUID queryId, QueryRequest credentialsQueryRequest, HttpHeaders headers) {
if (queryId == null) {
throw new ProtocolException(ProtocolException.MISSING_QUERY_ID);
}
Query query = queryRepo.getById(queryId);
if (query == null) {
throw new ProtocolException(ProtocolException.QUERY_NOT_FOUND + queryId.toString());
}
Resource resource = query.getResource();
if (resource == null) {
throw new ApplicationException(ApplicationException.MISSING_RESOURCE);
}
if (resource.getResourceRSPath() == null) {
throw new ApplicationException(ApplicationException.MISSING_RESOURCE_PATH);
}
if (credentialsQueryRequest == null) {
throw new ProtocolException(ProtocolException.MISSING_DATA);
}
if (credentialsQueryRequest.getResourceCredentials() == null) {
credentialsQueryRequest.setResourceCredentials(new HashMap<>());
}

logger.info(
"path=/query/{queryId}/signed-url, resourceId={}, requestSource={}, queryRequest={}", queryId,
Utilities.getRequestSourceFromHeader(headers), Utilities.convertQueryRequestToString(mapper, credentialsQueryRequest)
);


credentialsQueryRequest.getResourceCredentials().put(ResourceWebClient.BEARER_TOKEN_KEY, resource.getToken());
return resourceWebClient.queryResultSignedUrl(resource.getResourceRSPath(), query.getResourceResultId(), credentialsQueryRequest);
}

/**
* Streams the result for a query by looking up the target resource from the database and calling the target resource for a result.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,37 @@ public Response queryResult(String rsURL, String queryId, QueryRequest queryRequ
throw new ResourceInterfaceException("Error getting results", e);
}
}

public Response queryResultSignedUrl(String rsURL, String queryId, QueryRequest queryRequest){
logger.debug("Calling ResourceWebClient querySignedUrl()");
try {
if (queryRequest == null){
throw new ProtocolException(ProtocolException.MISSING_DATA);
}
if (queryRequest.getResourceCredentials() == null){
throw new NotAuthorizedException("Missing credentials");
}
if (rsURL == null){
throw new ApplicationException(ApplicationException.MISSING_RESOURCE_PATH);
}
if (queryId == null){
throw new ProtocolException(ProtocolException.MISSING_QUERY_ID);
}
String pathName = "/query/" + queryId + "/signed-url";
String body = json.writeValueAsString(queryRequest);
HttpResponse resourcesResponse = retrievePostResponse(composeURL(rsURL, pathName), createHeaders(queryRequest.getResourceCredentials()), body);
if (resourcesResponse.getStatusLine().getStatusCode() != 200) {
logger.error("ResourceRS did not return a 200");
throwResponseError(resourcesResponse, rsURL);
}
return Response.ok(resourcesResponse.getEntity().getContent()).build();
} catch (JsonProcessingException e){
logger.error("Unable to encode resource credentials");
throw new NotAuthorizedException("Unable to encode resource credentials", e);
} catch (IOException e){
throw new ResourceInterfaceException("Error getting results", e);
}
}


public Response queryFormat(String rsURL, QueryRequest queryRequest){
Expand Down

0 comments on commit 2b8f0bf

Please sign in to comment.