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