Search in sources :

Example 1 with NotFoundException

use of com.formkiq.lambda.apigateway.exception.NotFoundException 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 NotFoundException

use of com.formkiq.lambda.apigateway.exception.NotFoundException 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 NotFoundException

use of com.formkiq.lambda.apigateway.exception.NotFoundException 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 NotFoundException

use of com.formkiq.lambda.apigateway.exception.NotFoundException 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 NotFoundException

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

the class WebhooksIdRequestHandler method patch.

@Override
public ApiRequestHandlerResponse patch(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsServices) throws Exception {
    String siteId = authorizer.getSiteId();
    String id = getPathParameter(event, "webhookId");
    WebhooksService webhookService = awsServices.webhookService();
    if (webhookService.findWebhook(siteId, id) == null) {
        throw new NotFoundException("Webhook 'id' not found");
    }
    DynamicObject obj = fromBodyToDynamicObject(logger, event);
    Map<String, Object> map = new HashMap<>();
    if (obj.containsKey("name")) {
        map.put("name", obj.getString("name"));
    }
    if (obj.containsKey("enabled")) {
        map.put("enabled", obj.getBoolean("enabled"));
    }
    Date ttlDate = null;
    if (obj.containsKey("ttl")) {
        ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC).plusSeconds(Long.parseLong(obj.getString("ttl")));
        ttlDate = Date.from(now.toInstant());
        map.put("TimeToLive", ttlDate);
    }
    webhookService.updateWebhook(siteId, id, new DynamicObject(map));
    if (ttlDate != null) {
        webhookService.updateTimeToLive(siteId, id, ttlDate);
    }
    return new ApiRequestHandlerResponse(SC_OK, new ApiMessageResponse("'" + id + "' object updated"));
}
Also used : ApiMessageResponse(com.formkiq.lambda.apigateway.ApiMessageResponse) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) HashMap(java.util.HashMap) ZonedDateTime(java.time.ZonedDateTime) NotFoundException(com.formkiq.lambda.apigateway.exception.NotFoundException) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) WebhooksService(com.formkiq.stacks.dynamodb.WebhooksService) Date(java.util.Date)

Aggregations

NotFoundException (com.formkiq.lambda.apigateway.exception.NotFoundException)16 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)14 ApiMessageResponse (com.formkiq.lambda.apigateway.ApiMessageResponse)7 ApiResponse (com.formkiq.lambda.apigateway.ApiResponse)6 BadException (com.formkiq.lambda.apigateway.exception.BadException)5 DocumentTag (com.formkiq.stacks.dynamodb.DocumentTag)5 ApiMapResponse (com.formkiq.lambda.apigateway.ApiMapResponse)4 DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)4 DocumentItem (com.formkiq.stacks.dynamodb.DocumentItem)4 DocumentService (com.formkiq.stacks.dynamodb.DocumentService)4 Date (java.util.Date)4 ApiUrlResponse (com.formkiq.stacks.api.ApiUrlResponse)2 URL (java.net.URL)2 HashMap (java.util.HashMap)2 S3Client (software.amazon.awssdk.services.s3.S3Client)2 LambdaLogger (com.amazonaws.services.lambda.runtime.LambdaLogger)1 S3ObjectMetadata (com.formkiq.aws.s3.S3ObjectMetadata)1 S3Service (com.formkiq.aws.s3.S3Service)1 ApiGatewayRequestHandler (com.formkiq.lambda.apigateway.ApiGatewayRequestHandler)1 ApiPagination (com.formkiq.lambda.apigateway.ApiPagination)1