use of com.formkiq.stacks.dynamodb.DocumentItem 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.stacks.dynamodb.DocumentItem in project formkiq-core by formkiq.
the class DocumentsS3Update method getObjectTags.
/**
* Get Object Tags from S3.
*
* @param s3 {@link S3Client}
* @param item {@link DocumentItem}
* @param bucket {@link String}
* @param documentId {@link String}
* @return {@link List} {@link DynamicDocumentTag}
*/
private List<DynamicDocumentTag> getObjectTags(final S3Client s3, final DocumentItem item, final String bucket, final String documentId) {
GetObjectTaggingResponse objectTags = this.s3service.getObjectTags(s3, bucket, documentId);
List<DocumentTag> tags = objectTags.tagSet().stream().map(t -> new DocumentTag(documentId, t.key(), t.value(), item.getInsertedDate(), item.getUserId())).collect(Collectors.toList());
// Any System Defined Tags in the S3 Metadata, set them to SystemDefined.
tags.stream().filter(t -> SYSTEM_DEFINED_TAGS.contains(t.getKey())).forEach(t -> t.setType(DocumentTagType.SYSTEMDEFINED));
List<DynamicDocumentTag> dtags = tags.stream().map(t -> new DocumentTagToDynamicDocumentTag().apply(t)).collect(Collectors.toList());
return dtags;
}
use of com.formkiq.stacks.dynamodb.DocumentItem in project formkiq-core by formkiq.
the class DocumentsS3UpdateTest method testHandleRequest09.
/**
* Create Document Request without existing Tags/Formats and TTL.
*
* @throws Exception Exception
*/
@Test
@Timeout(unit = TimeUnit.MILLISECONDS, value = TEST_TIMEOUT)
public void testHandleRequest09() throws Exception {
String ttl = "1612061365";
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
this.logger.reset();
String key = createDatabaseKey(siteId, BUCKET_KEY);
final Map<String, Object> map = loadFileAsMap(this, "/objectcreate-event1.json", BUCKET_KEY, key);
DynamicDocumentItem doc = new DynamicDocumentItem(Map.of());
doc.setInsertedDate(new Date());
doc.setDocumentId(BUCKET_KEY);
doc.setUserId("joe");
doc.setPath("test.txt");
doc.put("TimeToLive", ttl);
service.saveDocumentItemWithTag(siteId, doc);
addS3File(key, "pdf", false, "testdata");
// when
final DocumentItem item = handleRequest(siteId, BUCKET_KEY, map);
// then
GetItemRequest r = GetItemRequest.builder().key(keysDocument(siteId, item.getDocumentId())).tableName(dbHelper.getDocumentTable()).build();
try (DynamoDbClient db = dbBuilder.build()) {
Map<String, AttributeValue> result = db.getItem(r).item();
assertEquals(ttl, result.get("TimeToLive").n());
for (String tagKey : Arrays.asList("untagged", "userId")) {
r = GetItemRequest.builder().key(keysDocumentTag(siteId, item.getDocumentId(), tagKey)).tableName(dbHelper.getDocumentTable()).build();
result = db.getItem(r).item();
assertEquals(ttl, result.get("TimeToLive").n());
}
}
verifyDocumentSaved(siteId, item, "pdf", "8");
assertPublishSnsMessage(siteId, sqsDocumentEventUrl, "create", false, false);
}
}
use of com.formkiq.stacks.dynamodb.DocumentItem in project formkiq-core by formkiq.
the class DocumentsS3UpdateTest method testHandleRequest03.
/**
* Delete Document Request.
*
* @throws Exception Exception
*/
@Test
@Timeout(unit = TimeUnit.MILLISECONDS, value = TEST_TIMEOUT)
public void testHandleRequest03() throws Exception {
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
this.logger.reset();
String key = createDatabaseKey(siteId, BUCKET_KEY);
final Map<String, Object> map = loadFileAsMap(this, "/objectremove-event1.json", BUCKET_KEY, key);
DynamicDocumentItem doc = new DynamicDocumentItem(Map.of());
doc.setInsertedDate(new Date());
doc.setDocumentId(BUCKET_KEY);
doc.setPath("test.txt");
DynamicDocumentTag tag = new DynamicDocumentTag(Map.of("documentId", BUCKET_KEY, "key", "person", "value", "category", "insertedDate", new Date(), "userId", "asd"));
doc.put("tags", Arrays.asList(tag));
service.saveDocumentItemWithTag(siteId, doc);
addS3File(key, "pdf", false, "testdata");
// when
DocumentItem item = handleRequest(siteId, BUCKET_KEY, map);
// then
assertNull(item);
assertPublishSnsMessage(siteId, sqsDocumentEventUrl, "delete", false, true);
}
}
use of com.formkiq.stacks.dynamodb.DocumentItem in project formkiq-core by formkiq.
the class DocumentsS3UpdateTest method testHandleRequest07.
/**
* Create Document Request with Text Content TOO LARGE.
*
* @throws Exception Exception
*/
@Test
@Timeout(unit = TimeUnit.MILLISECONDS, value = TEST_TIMEOUT)
public void testHandleRequest07() throws Exception {
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
this.logger.reset();
String key = createDatabaseKey(siteId, BUCKET_KEY);
final Map<String, Object> map = loadFileAsMap(this, "/objectcreate-event1.json", BUCKET_KEY, key);
DynamicDocumentItem doc = new DynamicDocumentItem(Map.of());
doc.setInsertedDate(new Date());
doc.setDocumentId(BUCKET_KEY);
doc.setUserId("joe");
doc.setPath("test.txt");
service.saveDocumentItemWithTag(siteId, doc);
String content = loadFile(this, "/256kb-text.txt");
addS3File(key, "text/plain", false, content);
// when
final DocumentItem item = handleRequest(siteId, BUCKET_KEY, map);
// then
verifyDocumentSaved(siteId, item, "text/plain", "" + content.length());
assertPublishSnsMessage(siteId, sqsDocumentEventUrl, "create", false, false);
}
}
Aggregations