Search in sources :

Example 1 with ApiResponseStatus

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

Aggregations

ApiMapResponse (com.formkiq.lambda.apigateway.ApiMapResponse)1 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)1 ApiResponseStatus (com.formkiq.lambda.apigateway.ApiResponseStatus)1 BadException (com.formkiq.lambda.apigateway.exception.BadException)1 NotFoundException (com.formkiq.lambda.apigateway.exception.NotFoundException)1 DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)1