Search in sources :

Example 1 with ApiResponse

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

the class DocumentIdRequestHandler method delete.

@Override
public ApiRequestHandlerResponse delete(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
    String documentBucket = awsservice.documents3bucket();
    String documentId = event.getPathParameters().get("documentId");
    logger.log("deleting object " + documentId + " from bucket '" + documentBucket + "'");
    try {
        S3Service s3Service = awsservice.s3Service();
        try (S3Client s3 = s3Service.buildClient()) {
            S3ObjectMetadata md = s3Service.getObjectMetadata(s3, documentBucket, documentId);
            if (md.isObjectExists()) {
                s3Service.deleteObject(s3, documentBucket, documentId);
                ApiResponse resp = new ApiMessageResponse("'" + documentId + "' object deleted");
                return new ApiRequestHandlerResponse(SC_OK, resp);
            }
        }
        throw new NotFoundException("Document " + documentId + " not found.");
    } catch (S3Exception e) {
        if (e.statusCode() == SC_NOT_FOUND.getStatusCode()) {
            throw new NotFoundException("Document " + documentId + " not found.");
        }
        throw e;
    }
}
Also used : ApiMessageResponse(com.formkiq.lambda.apigateway.ApiMessageResponse) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) NotFoundException(com.formkiq.lambda.apigateway.exception.NotFoundException) S3ObjectMetadata(com.formkiq.aws.s3.S3ObjectMetadata) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) S3Client(software.amazon.awssdk.services.s3.S3Client) S3Service(com.formkiq.aws.s3.S3Service) ApiResponse(com.formkiq.lambda.apigateway.ApiResponse)

Example 2 with ApiResponse

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

the class DocumentTagsRequestHandler method post.

@Override
public ApiRequestHandlerResponse post(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
    DocumentTag tag = fromBodyToObject(logger, event, DocumentTag.class);
    DocumentTags tags = fromBodyToObject(logger, event, DocumentTags.class);
    boolean tagValid = isValid(tag);
    boolean tagsValid = isValid(tags);
    if (!tagValid && !tagsValid) {
        throw new BadException("invalid json body");
    }
    if (!tagsValid) {
        tags = new DocumentTags();
        tags.setTags(Arrays.asList(tag));
    }
    tags.getTags().forEach(t -> {
        t.setType(DocumentTagType.USERDEFINED);
        t.setInsertedDate(new Date());
        t.setUserId(getCallingCognitoUsername(event));
    });
    String documentId = event.getPathParameters().get("documentId");
    String siteId = authorizer.getSiteId();
    awsservice.documentService().deleteDocumentTag(siteId, documentId, "untagged");
    awsservice.documentService().addTags(siteId, documentId, tags.getTags(), null);
    ApiResponse resp = tagsValid ? new ApiMessageResponse("Created Tags.") : new ApiMessageResponse("Created Tag '" + tag.getKey() + "'.");
    return new ApiRequestHandlerResponse(SC_CREATED, resp);
}
Also used : DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) ApiMessageResponse(com.formkiq.lambda.apigateway.ApiMessageResponse) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) BadException(com.formkiq.lambda.apigateway.exception.BadException) Date(java.util.Date) ApiResponse(com.formkiq.lambda.apigateway.ApiResponse) DocumentTags(com.formkiq.stacks.dynamodb.DocumentTags)

Example 3 with ApiResponse

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

the class WebhooksTagsRequestHandler method post.

@Override
public ApiRequestHandlerResponse post(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsServices) throws Exception {
    DocumentTag tag = fromBodyToObject(logger, event, DocumentTag.class);
    if (tag.getKey() == null || tag.getKey().length() == 0) {
        throw new BadException("invalid json body");
    }
    String siteId = authorizer.getSiteId();
    String id = getPathParameter(event, "webhookId");
    tag.setType(DocumentTagType.USERDEFINED);
    tag.setInsertedDate(new Date());
    tag.setUserId(getCallingCognitoUsername(event));
    DynamicObject webhook = awsServices.webhookService().findWebhook(siteId, id);
    if (webhook == null) {
        throw new NotFoundException("Webhook 'id' not found");
    }
    Date ttl = null;
    String ttlString = webhook.getString("TimeToLive");
    if (ttlString != null) {
        long epoch = Long.parseLong(ttlString);
        ttl = new Date(epoch * TO_MILLIS);
    }
    awsServices.webhookService().addTags(siteId, id, Arrays.asList(tag), ttl);
    ApiResponse resp = new ApiMessageResponse("Created Tag '" + tag.getKey() + "'.");
    return new ApiRequestHandlerResponse(SC_CREATED, resp);
}
Also used : DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) ApiMessageResponse(com.formkiq.lambda.apigateway.ApiMessageResponse) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) NotFoundException(com.formkiq.lambda.apigateway.exception.NotFoundException) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) BadException(com.formkiq.lambda.apigateway.exception.BadException) Date(java.util.Date) ApiResponse(com.formkiq.lambda.apigateway.ApiResponse)

Example 4 with ApiResponse

use of com.formkiq.lambda.apigateway.ApiResponse 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)

Example 5 with ApiResponse

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

the class DocumentTagRequestHandler method put.

@SuppressWarnings("unchecked")
@Override
public ApiRequestHandlerResponse put(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
    Map<String, String> map = event.getPathParameters();
    final String documentId = map.get("documentId");
    final String tagKey = map.get("tagKey");
    Map<String, Object> body = fromBodyToObject(logger, event, Map.class);
    String value = body != null ? (String) body.getOrDefault("value", null) : null;
    List<String> values = body != null ? (List<String>) body.getOrDefault("values", null) : null;
    if (value == null && values == null) {
        throw new BadException("request body is invalid");
    }
    String siteId = authorizer.getSiteId();
    DocumentService documentService = awsservice.documentService();
    if (event.getHttpMethod().equalsIgnoreCase("put")) {
        if (documentService.findDocument(siteId, documentId) == null) {
            throw new NotFoundException("Document " + documentId + " not found.");
        }
    }
    Date now = new Date();
    String userId = getCallingCognitoUsername(event);
    DocumentTag tag = documentService.findDocumentTag(siteId, documentId, tagKey);
    if (tag == null) {
        throw new NotFoundException("Tag " + tagKey + " not found.");
    }
    // if trying to change from tag VALUE to VALUES or VALUES to VALUE
    if (isTagValueTypeChanged(tag, value, values)) {
        documentService.removeTags(siteId, documentId, Arrays.asList(tagKey));
    }
    tag = new DocumentTag(null, tagKey, value, now, userId);
    if (values != null) {
        tag.setValue(null);
        tag.setValues(values);
    }
    documentService.addTags(siteId, documentId, Arrays.asList(tag), null);
    ApiResponse resp = new ApiMessageResponse("Updated tag '" + tagKey + "' on document '" + documentId + "'.");
    return new ApiRequestHandlerResponse(SC_OK, resp);
}
Also used : DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) NotFoundException(com.formkiq.lambda.apigateway.exception.NotFoundException) BadException(com.formkiq.lambda.apigateway.exception.BadException) DocumentService(com.formkiq.stacks.dynamodb.DocumentService) Date(java.util.Date) ApiResponse(com.formkiq.lambda.apigateway.ApiResponse) ApiMessageResponse(com.formkiq.lambda.apigateway.ApiMessageResponse) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)

Aggregations

ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)7 ApiResponse (com.formkiq.lambda.apigateway.ApiResponse)7 ApiMessageResponse (com.formkiq.lambda.apigateway.ApiMessageResponse)6 NotFoundException (com.formkiq.lambda.apigateway.exception.NotFoundException)6 DocumentTag (com.formkiq.stacks.dynamodb.DocumentTag)4 BadException (com.formkiq.lambda.apigateway.exception.BadException)3 DocumentService (com.formkiq.stacks.dynamodb.DocumentService)3 Date (java.util.Date)3 S3Client (software.amazon.awssdk.services.s3.S3Client)2 S3ObjectMetadata (com.formkiq.aws.s3.S3ObjectMetadata)1 S3Service (com.formkiq.aws.s3.S3Service)1 ApiMapResponse (com.formkiq.lambda.apigateway.ApiMapResponse)1 DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)1 DocumentItem (com.formkiq.stacks.dynamodb.DocumentItem)1 DocumentTags (com.formkiq.stacks.dynamodb.DocumentTags)1 URL (java.net.URL)1 Duration (java.time.Duration)1 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)1