Search in sources :

Example 1 with ApiRequestHandlerResponse

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

the class DocumentIdRequestHandler method get.

@Override
public ApiRequestHandlerResponse get(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
    String siteId = authorizer.getSiteId();
    int limit = getLimit(logger, event);
    ApiPagination token = getPagination(awsservice.documentCacheService(), event);
    String documentId = event.getPathParameters().get("documentId");
    ApiPagination pagination = getPagination(awsservice.documentCacheService(), event);
    PaginationResult<DocumentItem> presult = awsservice.documentService().findDocument(siteId, documentId, true, token != null ? token.getStartkey() : null, limit);
    DocumentItem result = presult.getResult();
    if (result == null) {
        throw new NotFoundException("Document " + documentId + " not found.");
    }
    ApiPagination current = createPagination(awsservice.documentCacheService(), event, pagination, presult.getToken(), limit);
    DynamicDocumentItem item = new DocumentItemToDynamicDocumentItem().apply(result);
    item.put("siteId", siteId != null ? siteId : DEFAULT_SITE_ID);
    item.put("previous", current.getPrevious());
    item.put("next", current.hasNext() ? current.getNext() : null);
    return new ApiRequestHandlerResponse(SC_OK, new ApiMapResponse(item));
}
Also used : ApiPagination(com.formkiq.lambda.apigateway.ApiPagination) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) NotFoundException(com.formkiq.lambda.apigateway.exception.NotFoundException) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse)

Example 2 with ApiRequestHandlerResponse

use of com.formkiq.lambda.apigateway.ApiRequestHandlerResponse 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 3 with ApiRequestHandlerResponse

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

the class DocumentIdRequestHandler method patch.

@Override
public ApiRequestHandlerResponse patch(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
    boolean isUpdate = event.getHttpMethod().equalsIgnoreCase("patch") && event.getPathParameters().containsKey("documentId");
    String siteId = authorizer.getSiteId();
    String documentId = UUID.randomUUID().toString();
    if (isUpdate) {
        documentId = event.getPathParameters().get("documentId");
        if (awsservice.documentService().findDocument(siteId, documentId) == null) {
            throw new NotFoundException("Document " + documentId + " not found.");
        }
    }
    String maxDocumentCount = null;
    DynamicObject item = fromBodyToDynamicObject(logger, event);
    updateContentType(event, item);
    List<DynamicObject> documents = item.getList("documents");
    if (!isUpdate) {
        if (!item.hasString("content") && item.getList("documents").isEmpty()) {
            throw new BadException("Invalid JSON body.");
        }
        maxDocumentCount = this.restrictionMaxDocuments.getValue(awsservice, siteId);
        if (maxDocumentCount != null && this.restrictionMaxDocuments.enforced(awsservice, siteId, maxDocumentCount)) {
            throw new BadException("Max Number of Documents reached");
        }
    }
    addFieldsToObject(event, awsservice, siteId, documentId, item, documents);
    item.put("documents", documents);
    logger.log("setting userId: " + item.getString("userId") + " contentType: " + item.getString("contentType"));
    putObjectToStaging(logger, awsservice, maxDocumentCount, siteId, item);
    Map<String, String> uploadUrls = generateUploadUrls(awsservice, siteId, documentId, item, documents);
    Map<String, Object> map = buildResponse(siteId, documentId, documents, uploadUrls);
    ApiResponseStatus status = isUpdate ? SC_OK : SC_CREATED;
    return new ApiRequestHandlerResponse(status, new ApiMapResponse(map));
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) NotFoundException(com.formkiq.lambda.apigateway.exception.NotFoundException) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiResponseStatus(com.formkiq.lambda.apigateway.ApiResponseStatus) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) BadException(com.formkiq.lambda.apigateway.exception.BadException) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse)

Example 4 with ApiRequestHandlerResponse

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

the class DocumentTagRequestHandler method get.

@Override
public ApiRequestHandlerResponse get(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
    String documentId = event.getPathParameters().get("documentId");
    String tagKey = event.getPathParameters().get("tagKey");
    String siteId = authorizer.getSiteId();
    DocumentTag tag = awsservice.documentService().findDocumentTag(siteId, documentId, tagKey);
    if (tag == null) {
        throw new NotFoundException("Tag " + tagKey + " not found.");
    }
    ApiDocumentTagItemResponse resp = new ApiDocumentTagItemResponse();
    resp.setKey(tagKey);
    resp.setValue(tag.getValue());
    resp.setValues(tag.getValues());
    resp.setInsertedDate(tag.getInsertedDate());
    resp.setUserId(tag.getUserId());
    resp.setType(tag.getType() != null ? tag.getType().name().toLowerCase() : null);
    resp.setDocumentId(tag.getDocumentId());
    return new ApiRequestHandlerResponse(SC_OK, resp);
}
Also used : DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) ApiDocumentTagItemResponse(com.formkiq.stacks.api.ApiDocumentTagItemResponse) NotFoundException(com.formkiq.lambda.apigateway.exception.NotFoundException) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)

Example 5 with ApiRequestHandlerResponse

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

Aggregations

ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)27 NotFoundException (com.formkiq.lambda.apigateway.exception.NotFoundException)15 ApiMapResponse (com.formkiq.lambda.apigateway.ApiMapResponse)11 BadException (com.formkiq.lambda.apigateway.exception.BadException)11 Date (java.util.Date)11 ApiMessageResponse (com.formkiq.lambda.apigateway.ApiMessageResponse)10 DocumentTag (com.formkiq.stacks.dynamodb.DocumentTag)10 ApiResponse (com.formkiq.lambda.apigateway.ApiResponse)9 DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)7 LambdaLogger (com.amazonaws.services.lambda.runtime.LambdaLogger)6 ApiAuthorizer (com.formkiq.lambda.apigateway.ApiAuthorizer)6 ApiGatewayRequestEvent (com.formkiq.lambda.apigateway.ApiGatewayRequestEvent)6 ApiGatewayRequestEventUtil (com.formkiq.lambda.apigateway.ApiGatewayRequestEventUtil)6 ApiGatewayRequestHandler (com.formkiq.lambda.apigateway.ApiGatewayRequestHandler)6 SC_OK (com.formkiq.lambda.apigateway.ApiResponseStatus.SC_OK)6 AwsServiceCache (com.formkiq.lambda.apigateway.AwsServiceCache)6 DocumentItem (com.formkiq.stacks.dynamodb.DocumentItem)6 HashMap (java.util.HashMap)6 List (java.util.List)6 Collectors (java.util.stream.Collectors)6