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));
}
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;
}
}
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));
}
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);
}
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);
}
Aggregations