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