Search in sources :

Example 1 with BadException

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

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

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

the class DocumentsUploadRequestHandler method get.

@Override
public ApiRequestHandlerResponse get(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
    boolean documentExists = false;
    Date date = new Date();
    String documentId = UUID.randomUUID().toString();
    String username = getCallingCognitoUsername(event);
    DocumentItem item = new DocumentItemDynamoDb(documentId, date, username);
    List<DocumentTag> tags = new ArrayList<>();
    Map<String, String> query = event.getQueryStringParameters();
    String siteId = authorizer.getSiteId();
    DocumentService service = awsservice.documentService();
    if (query != null && query.containsKey("path")) {
        String path = query.get("path");
        path = URLDecoder.decode(path, StandardCharsets.UTF_8.toString());
        item.setPath(path);
        tags.add(new DocumentTag(documentId, "path", path, date, username, DocumentTagType.SYSTEMDEFINED));
    }
    String urlstring = generatePresignedUrl(awsservice, logger, siteId, documentId, query);
    logger.log("generated presign url: " + urlstring + " for document " + documentId);
    if (!documentExists) {
        tags.add(new DocumentTag(documentId, "untagged", "true", date, username, DocumentTagType.SYSTEMDEFINED));
        String value = this.restrictionMaxDocuments.getValue(awsservice, siteId);
        if (!this.restrictionMaxDocuments.enforced(awsservice, siteId, value)) {
            logger.log("saving document: " + item.getDocumentId() + " on path " + item.getPath());
            service.saveDocument(siteId, item, tags);
            if (value != null) {
                awsservice.documentCountService().incrementDocumentCount(siteId);
            }
        } else {
            throw new BadException("Max Number of Documents reached");
        }
    }
    return new ApiRequestHandlerResponse(SC_OK, new ApiUrlResponse(urlstring, documentId));
}
Also used : DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) ApiUrlResponse(com.formkiq.stacks.api.ApiUrlResponse) ArrayList(java.util.ArrayList) BadException(com.formkiq.lambda.apigateway.exception.BadException) Date(java.util.Date) DocumentService(com.formkiq.stacks.dynamodb.DocumentService) DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) DocumentItemDynamoDb(com.formkiq.stacks.dynamodb.DocumentItemDynamoDb)

Example 4 with BadException

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

the class PublicWebhooksRequestHandler method post.

@Override
public ApiRequestHandlerResponse post(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
    String siteId = getParameter(event, "siteId");
    String webhookId = getPathParameter(event, "webhooks");
    DynamicObject hook = awsservice.webhookService().findWebhook(siteId, webhookId);
    checkIsWebhookValid(hook);
    String body = ApiGatewayRequestEventUtil.getBodyAsString(event);
    String contentType = getContentType(event);
    if (isContentTypeJson(contentType) && !isJsonValid(body)) {
        throw new BadException("body isn't valid JSON");
    }
    DynamicObject item = buildDynamicObject(awsservice, siteId, webhookId, hook, body, contentType);
    if (!isIdempotencyCached(awsservice, event, siteId, item)) {
        putObjectToStaging(logger, awsservice, item, siteId);
    }
    return buildResponse(event, item);
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) BadException(com.formkiq.lambda.apigateway.exception.BadException)

Example 5 with BadException

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

the class SearchRequestHandler method post.

@Override
public ApiRequestHandlerResponse post(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
    DynamoDbCacheService cacheService = awsservice.documentCacheService();
    ApiPagination pagination = getPagination(cacheService, event);
    int limit = pagination != null ? pagination.getLimit() : getLimit(logger, event);
    PaginationMapToken ptoken = pagination != null ? pagination.getStartkey() : null;
    QueryRequest q = fromBodyToObject(logger, event, QueryRequest.class);
    if (q == null || q.query() == null || q.query().tag() == null) {
        throw new BadException("Invalid JSON body.");
    }
    Collection<String> documentIds = q.query().documentIds();
    if (documentIds != null) {
        if (documentIds.size() > MAX_DOCUMENT_IDS) {
            throw new BadException("Maximum number of DocumentIds is " + MAX_DOCUMENT_IDS);
        }
        if (!getQueryParameterMap(event).containsKey("limit")) {
            limit = documentIds.size();
        }
    }
    String siteId = authorizer.getSiteId();
    PaginationResults<DynamicDocumentItem> results = awsservice.documentSearchService().search(siteId, q.query(), ptoken, limit);
    ApiPagination current = createPagination(cacheService, event, pagination, results.getToken(), limit);
    List<DynamicDocumentItem> documents = subList(results.getResults(), limit);
    Map<String, Object> map = new HashMap<>();
    map.put("documents", documents);
    map.put("previous", current.getPrevious());
    map.put("next", current.hasNext() ? current.getNext() : null);
    ApiMapResponse resp = new ApiMapResponse(map);
    return new ApiRequestHandlerResponse(SC_OK, resp);
}
Also used : ApiPagination(com.formkiq.lambda.apigateway.ApiPagination) QueryRequest(com.formkiq.stacks.api.QueryRequest) HashMap(java.util.HashMap) BadException(com.formkiq.lambda.apigateway.exception.BadException) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DynamoDbCacheService(com.formkiq.stacks.dynamodb.DynamoDbCacheService) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse) PaginationMapToken(com.formkiq.stacks.dynamodb.PaginationMapToken)

Aggregations

BadException (com.formkiq.lambda.apigateway.exception.BadException)9 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)7 NotFoundException (com.formkiq.lambda.apigateway.exception.NotFoundException)5 DocumentTag (com.formkiq.stacks.dynamodb.DocumentTag)5 Date (java.util.Date)5 ApiMessageResponse (com.formkiq.lambda.apigateway.ApiMessageResponse)3 ApiResponse (com.formkiq.lambda.apigateway.ApiResponse)3 DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)3 DocumentService (com.formkiq.stacks.dynamodb.DocumentService)3 ApiMapResponse (com.formkiq.lambda.apigateway.ApiMapResponse)2 ApiUrlResponse (com.formkiq.stacks.api.ApiUrlResponse)2 DocumentItem (com.formkiq.stacks.dynamodb.DocumentItem)2 DocumentItemDynamoDb (com.formkiq.stacks.dynamodb.DocumentItemDynamoDb)2 ArrayList (java.util.ArrayList)2 LambdaLogger (com.amazonaws.services.lambda.runtime.LambdaLogger)1 ApiPagination (com.formkiq.lambda.apigateway.ApiPagination)1 ApiResponseStatus (com.formkiq.lambda.apigateway.ApiResponseStatus)1 ForbiddenException (com.formkiq.lambda.apigateway.exception.ForbiddenException)1 NotImplementedException (com.formkiq.lambda.apigateway.exception.NotImplementedException)1 TooManyRequestsException (com.formkiq.lambda.apigateway.exception.TooManyRequestsException)1