Skip to content

Commit

Permalink
Add new endpoint to IResourceRS interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Gcolon021 committed Jul 17, 2023
1 parent 022b291 commit 52b2788
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@
import javax.ws.rs.core.Response;

import edu.harvard.dbmi.avillach.domain.*;
import edu.harvard.dbmi.avillach.service.FormatService;
import edu.harvard.dbmi.avillach.service.PicsureInfoService;
import edu.harvard.dbmi.avillach.service.PicsureQueryService;
import edu.harvard.dbmi.avillach.service.PicsureSearchService;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.info.Info;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.responses.ApiResponse;

@OpenAPIDefinition(info = @Info(title = "Pic-sure API", version = "1.0.0", description = "This is the Pic-sure API."))
@Path("/")
Expand All @@ -34,17 +38,20 @@ public class PicsureRS {
@Inject
PicsureQueryService queryService;

@Inject
FormatService formatService;

@POST
@Path("/info/{resourceId}")
@Operation(
summary = "Returns information about the provided resource",
tags = { "info" },
operationId = "resourceInfo",
responses = { @io.swagger.v3.oas.annotations.responses.ApiResponse(
responses = { @ApiResponse(
responseCode = "200",
description = "Resource information",
content = @io.swagger.v3.oas.annotations.media.Content(
schema = @io.swagger.v3.oas.annotations.media.Schema(
content = @Content(
schema = @Schema(
implementation = ResourceInfo.class
)
)
Expand All @@ -62,11 +69,11 @@ public ResourceInfo resourceInfo(@Parameter(description="The UUID of the resourc
@Operation(
summary = "Returns list of resources available",
responses = {
@io.swagger.v3.oas.annotations.responses.ApiResponse(
@ApiResponse(
responseCode = "200",
description = "Resource information",
content = @io.swagger.v3.oas.annotations.media.Content(
schema = @io.swagger.v3.oas.annotations.media.Schema(
content = @Content(
schema = @Schema(
implementation = Map.class
)
)
Expand Down Expand Up @@ -97,19 +104,19 @@ public PaginatedSearchResult<?> searchGenomicConceptValues(
@Operation(
summary = "Searches for concept paths on the given resource matching the supplied search term",
responses = {
@io.swagger.v3.oas.annotations.responses.ApiResponse(
@ApiResponse(
responseCode = "200",
description = "Search results",
content = @io.swagger.v3.oas.annotations.media.Content(
schema = @io.swagger.v3.oas.annotations.media.Schema(
content = @Content(
schema = @Schema(
implementation = SearchResults.class
)
)
)},
requestBody = @RequestBody(
required = true,
content = @io.swagger.v3.oas.annotations.media.Content(
schema = @io.swagger.v3.oas.annotations.media.Schema(
content = @Content(
schema = @Schema(
example = "{ \"query\": \"searchTerm\" }"
)
)
Expand All @@ -126,11 +133,11 @@ public SearchResults search(@Parameter(description="The UUID of the resource to
@Operation(
summary = "Submits a query to the given resource",
responses = {
@io.swagger.v3.oas.annotations.responses.ApiResponse(
@ApiResponse(
responseCode = "200",
description = "Query status",
content = @io.swagger.v3.oas.annotations.media.Content(
schema = @io.swagger.v3.oas.annotations.media.Schema(
content = @Content(
schema = @Schema(
implementation = QueryStatus.class
)
)
Expand Down Expand Up @@ -158,11 +165,11 @@ public QueryStatus query(
@Operation(
summary = "Returns the status of the given query",
responses = {
@io.swagger.v3.oas.annotations.responses.ApiResponse(
@ApiResponse(
responseCode = "200",
description = "Query status",
content = @io.swagger.v3.oas.annotations.media.Content(
schema = @io.swagger.v3.oas.annotations.media.Schema(
content = @Content(
schema = @Schema(
implementation = QueryStatus.class
)
)
Expand Down Expand Up @@ -197,11 +204,11 @@ public QueryStatus queryStatus(
@Operation(
summary = "Returns result for given query",
responses = {
@io.swagger.v3.oas.annotations.responses.ApiResponse(
@ApiResponse(
responseCode = "200",
description = "Query result",
content = @io.swagger.v3.oas.annotations.media.Content(
schema = @io.swagger.v3.oas.annotations.media.Schema(
content = @Content(
schema = @Schema(
implementation = Response.class
)
)
Expand All @@ -220,11 +227,11 @@ public Response queryResult(@Parameter(description="The UUID of the query to fet
@Operation(
summary = "Returns result for given query",
responses = {
@io.swagger.v3.oas.annotations.responses.ApiResponse(
@ApiResponse(
responseCode = "200",
description = "Query result",
content = @io.swagger.v3.oas.annotations.media.Content(
schema = @io.swagger.v3.oas.annotations.media.Schema(
content = @Content(
schema = @Schema(
implementation = Response.class
)
)
Expand All @@ -244,11 +251,11 @@ public Response querySync(@Context HttpHeaders headers,
description = "Generally used to reconstruct a query that was previously submitted. The queryId is " +
"returned by the /query endpoint as the \"picsureResultId\" in the response object",
responses = {
@io.swagger.v3.oas.annotations.responses.ApiResponse(
@ApiResponse(
responseCode = "200",
description = "Query metadata",
content = @io.swagger.v3.oas.annotations.media.Content(
schema = @io.swagger.v3.oas.annotations.media.Schema(
content = @Content(
schema = @Schema(
implementation = QueryStatus.class
)
)
Expand All @@ -258,5 +265,11 @@ public Response querySync(@Context HttpHeaders headers,
public QueryStatus queryMetadata(@PathParam("queryId") UUID queryId, @Context HttpHeaders headers){
return queryService.queryMetadata(queryId, headers);
}

@POST
@Path("/bin/continuous")
public Response generateContinuousBin(QueryRequest continuousData, @Context HttpHeaders headers) {
return formatService.format(continuousData, headers);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package edu.harvard.dbmi.avillach.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import edu.harvard.dbmi.avillach.data.entity.Resource;
import edu.harvard.dbmi.avillach.data.repository.ResourceRepository;
import edu.harvard.dbmi.avillach.domain.QueryRequest;
import edu.harvard.dbmi.avillach.util.Utilities;
import edu.harvard.dbmi.avillach.util.exception.ApplicationException;
import edu.harvard.dbmi.avillach.util.exception.ProtocolException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
import java.util.HashMap;
import java.util.UUID;

public class FormatService {

private final Logger logger = LoggerFactory.getLogger(FormatService.class);

private final static ObjectMapper mapper = new ObjectMapper();

@Inject
ResourceRepository resourceRepo;

@Inject
ResourceWebClient resourceWebClient;

public Response format(QueryRequest credentialsQueryRequest, HttpHeaders headers) {
Resource resource = resourceRepo.getById(credentialsQueryRequest.getResourceUUID());
if (resource == null){
throw new ProtocolException(ProtocolException.RESOURCE_NOT_FOUND + credentialsQueryRequest.getResourceUUID().toString());
}

if (resource.getResourceRSPath() == null){
throw new ApplicationException(ApplicationException.MISSING_RESOURCE_PATH);
}
if (credentialsQueryRequest == null){
credentialsQueryRequest = new QueryRequest();
}

if (credentialsQueryRequest.getResourceCredentials() == null) {
credentialsQueryRequest.setResourceCredentials(new HashMap<String, String>());
}

String requestSourceFromHeader = Utilities.getRequestSourceFromHeader(headers);
logger.info("path=/info/{resourceId}, resourceId={}, requestSource={}, credentialsQueryRequest={}",
credentialsQueryRequest.getResourceUUID().toString(),
requestSourceFromHeader,
Utilities.convertQueryRequestToString(mapper, credentialsQueryRequest)
);

return resourceWebClient.queryContinuous(resource.getResourceRSPath(), credentialsQueryRequest, requestSourceFromHeader);
}

}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,47 @@ public Response querySync(String rsURL, QueryRequest queryRequest, String reques
}
}

// Write a query to the visualization service to the new /bin/continuous endpoint and return the response.
public Response queryContinuous(String rsURL, QueryRequest queryRequest, String requestSource) {
logger.debug("Calling ResourceWebClient queryContinuous()");
try {
if (queryRequest == null) {
throw new ProtocolException("Missing query data");
}
if (queryRequest.getResourceCredentials() == null) {
throw new NotAuthorizedException("Missing credentials");
}
if (rsURL == null) {
throw new ApplicationException("Missing resource URL");
}

String pathName = "/bin/continuous";
String body = json.writeValueAsString(queryRequest);

Header[] headers = createHeaders(queryRequest.getResourceCredentials());
if (requestSource != null) {
Header sourceHeader = new BasicHeader("request-source", requestSource);

// Add the source header to the headers array.
Header[] newHeaders = new Header[headers.length + 1];
System.arraycopy(headers, 0, newHeaders, 0, headers.length);
newHeaders[headers.length] = sourceHeader;
headers = newHeaders;
}

HttpResponse resourcesResponse = retrievePostResponse(composeURL(rsURL, pathName), headers, body);
if (resourcesResponse.getStatusLine().getStatusCode() != 200) {
throwError(resourcesResponse, rsURL);
}

return Response.ok(resourcesResponse.getEntity().getContent()).build();
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new ResourceInterfaceException("Error getting results", e);
}
}

private void throwError(HttpResponse response, String baseURL){
logger.error("ResourceRS did not return a 200");
String errorMessage = baseURL + " " + response.getStatusLine().getStatusCode() + " " + response.getStatusLine().getReasonPhrase();
Expand Down

0 comments on commit 52b2788

Please sign in to comment.