use of com.formkiq.stacks.dynamodb.DynamicDocumentItem in project formkiq-core by formkiq.
the class StagingS3Create method configfile.
/**
* Determines whether {@link String} is a JSON config file.
*
* @param s3Client {@link S3Client}
* @param logger {@link LambdaLogger}
* @param bucket {@link String}
* @param documentId {@link String}
* @return {@link DynamicDocumentItem}
*/
@SuppressWarnings("unchecked")
private DynamicDocumentItem configfile(final S3Client s3Client, final LambdaLogger logger, final String bucket, final String documentId) {
DynamicDocumentItem obj = null;
if (documentId.endsWith(FORMKIQ_B64_EXT)) {
String s = this.s3.getContentAsString(s3Client, bucket, documentId, null);
if ("true".equals(System.getenv("DEBUG"))) {
logger.log(s);
}
Map<String, Object> map = this.gson.fromJson(s, Map.class);
obj = new DynamicDocumentItem(map);
if (obj.containsKey("insertedDate")) {
SimpleDateFormat f = new SimpleDateFormat(DATE_FORMAT);
try {
obj.setInsertedDate(f.parse(obj.getString("insertedDate")));
} catch (ParseException e) {
obj.setInsertedDate(new Date());
}
} else {
obj.setInsertedDate(new Date());
}
}
return obj;
}
use of com.formkiq.stacks.dynamodb.DynamicDocumentItem in project formkiq-core by formkiq.
the class StagingS3Create method copyFile.
/**
* Copies Documentid to a new file that is a {@link UUID}.
*
* @param s3Client {@link S3Client}
* @param logger {@link LambdaLogger}
* @param bucket {@link String}
* @param originalkey {@link String}
* @param date {@link Date}
*/
private void copyFile(final S3Client s3Client, final LambdaLogger logger, final String bucket, final String originalkey, final Date date) {
String siteId = getSiteId(originalkey);
String key = resetDatabaseKey(siteId, originalkey);
boolean uuid = isUuid(key);
String documentId = uuid ? key : UUID.randomUUID().toString();
String destKey = createDatabaseKey(siteId, documentId);
logger.log(String.format("Copying %s from bucket %s to %s in bucket %s.", originalkey, bucket, destKey, this.documentsBucket));
S3ObjectMetadata metadata = this.s3.getObjectMetadata(s3Client, bucket, originalkey);
String username = metadata.getMetadata().entrySet().stream().filter(s -> s.getKey().equalsIgnoreCase("userid")).findFirst().map(s -> s.getValue()).orElse("System");
DynamicDocumentItem doc = new DynamicDocumentItem(Collections.emptyMap());
doc.setDocumentId(documentId);
doc.setContentLength(metadata.getContentLength());
doc.setContentType(metadata.getContentType());
doc.setUserId(username);
doc.setChecksum(metadata.getEtag());
doc.setInsertedDate(date);
if (!uuid) {
doc.setPath(key);
}
this.service.saveDocumentItemWithTag(siteId, doc);
this.s3.copyObject(s3Client, bucket, originalkey, this.documentsBucket, destKey, metadata.getContentType());
}
use of com.formkiq.stacks.dynamodb.DynamicDocumentItem 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.DynamicDocumentItem 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.DynamicDocumentItem 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