use of com.formkiq.lambda.apigateway.exception.NotFoundException in project formkiq-core by formkiq.
the class DocumentIdUrlRequestHandler 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 versionId = getParameter(event, "versionId");
String siteId = authorizer.getSiteId();
DocumentItem item = awsservice.documentService().findDocument(siteId, documentId);
if (item == null) {
throw new NotFoundException("Document " + documentId + " not found.");
}
URL url = getS3Url(logger, authorizer, awsservice, event, item, documentId, versionId);
return url != null ? new ApiRequestHandlerResponse(SC_OK, new ApiUrlResponse(url.toString(), documentId)) : new ApiRequestHandlerResponse(SC_NOT_FOUND, new ApiEmptyResponse());
}
use of com.formkiq.lambda.apigateway.exception.NotFoundException in project formkiq-core by formkiq.
the class DocumentTagRequestHandler method put.
@SuppressWarnings("unchecked")
@Override
public ApiRequestHandlerResponse put(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
Map<String, String> map = event.getPathParameters();
final String documentId = map.get("documentId");
final String tagKey = map.get("tagKey");
Map<String, Object> body = fromBodyToObject(logger, event, Map.class);
String value = body != null ? (String) body.getOrDefault("value", null) : null;
List<String> values = body != null ? (List<String>) body.getOrDefault("values", null) : null;
if (value == null && values == null) {
throw new BadException("request body is invalid");
}
String siteId = authorizer.getSiteId();
DocumentService documentService = awsservice.documentService();
if (event.getHttpMethod().equalsIgnoreCase("put")) {
if (documentService.findDocument(siteId, documentId) == null) {
throw new NotFoundException("Document " + documentId + " not found.");
}
}
Date now = new Date();
String userId = getCallingCognitoUsername(event);
DocumentTag tag = documentService.findDocumentTag(siteId, documentId, tagKey);
if (tag == null) {
throw new NotFoundException("Tag " + tagKey + " not found.");
}
// if trying to change from tag VALUE to VALUES or VALUES to VALUE
if (isTagValueTypeChanged(tag, value, values)) {
documentService.removeTags(siteId, documentId, Arrays.asList(tagKey));
}
tag = new DocumentTag(null, tagKey, value, now, userId);
if (values != null) {
tag.setValue(null);
tag.setValues(values);
}
documentService.addTags(siteId, documentId, Arrays.asList(tag), null);
ApiResponse resp = new ApiMessageResponse("Updated tag '" + tagKey + "' on document '" + documentId + "'.");
return new ApiRequestHandlerResponse(SC_OK, resp);
}
use of com.formkiq.lambda.apigateway.exception.NotFoundException in project formkiq-core by formkiq.
the class DocumentTagRequestHandler method delete.
@Override
public ApiRequestHandlerResponse delete(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
String siteId = authorizer.getSiteId();
Map<String, String> map = event.getPathParameters();
String documentId = map.get("documentId");
String tagKey = map.get("tagKey");
DocumentService documentService = awsservice.documentService();
DocumentTag docTag = documentService.findDocumentTag(siteId, documentId, tagKey);
if (docTag == null) {
throw new NotFoundException("Tag '" + tagKey + "' not found.");
}
documentService.removeTags(siteId, documentId, Arrays.asList(tagKey));
ApiResponse resp = new ApiMessageResponse("Removed '" + tagKey + "' from document '" + documentId + "'.");
return new ApiRequestHandlerResponse(SC_OK, resp);
}
use of com.formkiq.lambda.apigateway.exception.NotFoundException in project formkiq-core by formkiq.
the class DocumentTagValueRequestHandler method delete.
@Override
public ApiRequestHandlerResponse delete(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
String siteId = authorizer.getSiteId();
Map<String, String> map = event.getPathParameters();
String documentId = map.get("documentId");
String tagKey = map.get("tagKey");
String tagValue = map.get("tagValue");
DocumentService documentService = awsservice.documentService();
boolean removed = documentService.removeTag(siteId, documentId, tagKey, tagValue);
if (!removed) {
throw new NotFoundException("Tag/Value combination not found.");
}
ApiResponse resp = new ApiMessageResponse("Removed Tag from document '" + documentId + "'.");
return new ApiRequestHandlerResponse(SC_OK, resp);
}
use of com.formkiq.lambda.apigateway.exception.NotFoundException in project formkiq-core by formkiq.
the class DocumentsIdUploadRequestHandler 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> map = event.getPathParameters();
Map<String, String> query = event.getQueryStringParameters();
String siteId = authorizer.getSiteId();
DocumentService service = awsservice.documentService();
if (map != null && map.containsKey("documentId")) {
documentId = map.get("documentId");
item = service.findDocument(siteId, documentId);
documentExists = item != null;
if (!documentExists) {
throw new NotFoundException("Document " + documentId + " not found.");
}
} else 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, siteId, documentId, query);
logger.log("generated presign url: " + urlstring + " for document " + documentId);
if (!documentExists && item != null) {
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));
}
Aggregations