Search in sources :

Example 11 with ApiMapResponse

use of com.formkiq.lambda.apigateway.ApiMapResponse in project formkiq-core by formkiq.

the class DocumentIdContentRequestHandler method get.

@Override
public ApiRequestHandlerResponse get(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
    String siteId = authorizer.getSiteId();
    String documentId = event.getPathParameters().get("documentId");
    String versionId = getParameter(event, "versionId");
    DocumentItem item = awsservice.documentService().findDocument(siteId, documentId);
    if (item == null) {
        throw new NotFoundException("Document " + documentId + " not found.");
    }
    ApiResponse response = null;
    String s3key = createS3Key(siteId, documentId);
    if (MimeType.isPlainText(item.getContentType())) {
        try (S3Client s3 = awsservice.s3Service().buildClient()) {
            String content = awsservice.s3Service().getContentAsString(s3, awsservice.documents3bucket(), s3key, versionId);
            response = new ApiMapResponse(Map.of("content", content, "contentType", item.getContentType(), "isBase64", Boolean.FALSE));
        }
    } else {
        Duration duration = Duration.ofHours(1);
        URL url = awsservice.s3Service().presignGetUrl(awsservice.documents3bucket(), s3key, duration, versionId);
        response = new ApiMapResponse(Map.of("contentUrl", url.toString(), "contentType", item.getContentType() != null ? item.getContentType() : "application/octet-stream"));
    }
    return new ApiRequestHandlerResponse(SC_OK, response);
}
Also used : DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) NotFoundException(com.formkiq.lambda.apigateway.exception.NotFoundException) Duration(java.time.Duration) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) S3Client(software.amazon.awssdk.services.s3.S3Client) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse) ApiResponse(com.formkiq.lambda.apigateway.ApiResponse) URL(java.net.URL)

Aggregations

ApiMapResponse (com.formkiq.lambda.apigateway.ApiMapResponse)11 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)11 BadException (com.formkiq.lambda.apigateway.exception.BadException)5 NotFoundException (com.formkiq.lambda.apigateway.exception.NotFoundException)5 DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)5 HashMap (java.util.HashMap)5 LambdaLogger (com.amazonaws.services.lambda.runtime.LambdaLogger)4 ApiAuthorizer (com.formkiq.lambda.apigateway.ApiAuthorizer)4 ApiGatewayRequestEvent (com.formkiq.lambda.apigateway.ApiGatewayRequestEvent)4 ApiGatewayRequestEventUtil (com.formkiq.lambda.apigateway.ApiGatewayRequestEventUtil)4 ApiGatewayRequestHandler (com.formkiq.lambda.apigateway.ApiGatewayRequestHandler)4 SC_OK (com.formkiq.lambda.apigateway.ApiResponseStatus.SC_OK)4 AwsServiceCache (com.formkiq.lambda.apigateway.AwsServiceCache)4 List (java.util.List)4 Map (java.util.Map)4 Collectors (java.util.stream.Collectors)4 ApiPagination (com.formkiq.lambda.apigateway.ApiPagination)3 DocumentItem (com.formkiq.stacks.dynamodb.DocumentItem)3 DynamicDocumentItem (com.formkiq.stacks.dynamodb.DynamicDocumentItem)3 DEFAULT_SITE_ID (com.formkiq.stacks.dynamodb.SiteIdKeyGenerator.DEFAULT_SITE_ID)3