Search in sources :

Example 11 with DynamicDocumentItem

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;
}
Also used : DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 12 with DynamicDocumentItem

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());
}
Also used : SdkHttpUtils(software.amazon.awssdk.utils.http.SdkHttpUtils) DocumentService(com.formkiq.stacks.dynamodb.DocumentService) Date(java.util.Date) SimpleDateFormat(java.text.SimpleDateFormat) Context(com.amazonaws.services.lambda.runtime.Context) HashMap(java.util.HashMap) RequestHandler(com.amazonaws.services.lambda.runtime.RequestHandler) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) SiteIdKeyGenerator.createDatabaseKey(com.formkiq.stacks.dynamodb.SiteIdKeyGenerator.createDatabaseKey) GsonBuilder(com.google.gson.GsonBuilder) DocumentServiceImpl(com.formkiq.stacks.dynamodb.DocumentServiceImpl) SiteIdKeyGenerator.resetDatabaseKey(com.formkiq.stacks.dynamodb.SiteIdKeyGenerator.resetDatabaseKey) LambdaLogger(com.amazonaws.services.lambda.runtime.LambdaLogger) Gson(com.google.gson.Gson) Map(java.util.Map) DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) S3ConnectionBuilder(com.formkiq.aws.s3.S3ConnectionBuilder) ParseException(java.text.ParseException) S3ObjectMetadata(com.formkiq.aws.s3.S3ObjectMetadata) Region(software.amazon.awssdk.regions.Region) S3Service(com.formkiq.aws.s3.S3Service) PutObjectResponse(software.amazon.awssdk.services.s3.model.PutObjectResponse) SqsConnectionBuilder(com.formkiq.aws.sqs.SqsConnectionBuilder) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) DATE_FORMAT(com.formkiq.stacks.dynamodb.DocumentService.DATE_FORMAT) S3Client(software.amazon.awssdk.services.s3.S3Client) UUID(java.util.UUID) StandardCharsets(java.nio.charset.StandardCharsets) SqsService(com.formkiq.aws.sqs.SqsService) Reflectable(com.formkiq.graalvm.annotations.Reflectable) ReflectableImport(com.formkiq.graalvm.annotations.ReflectableImport) Base64(java.util.Base64) List(java.util.List) DocumentItemDynamoDb(com.formkiq.stacks.dynamodb.DocumentItemDynamoDb) SiteIdKeyGenerator.getSiteId(com.formkiq.stacks.dynamodb.SiteIdKeyGenerator.getSiteId) DynamoDbConnectionBuilder(com.formkiq.stacks.dynamodb.DynamoDbConnectionBuilder) Collections(java.util.Collections) DocumentTagType(com.formkiq.stacks.dynamodb.DocumentTagType) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) S3ObjectMetadata(com.formkiq.aws.s3.S3ObjectMetadata)

Example 13 with DynamicDocumentItem

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);
    }
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) Date(java.util.Date) GetItemRequest(software.amazon.awssdk.services.dynamodb.model.GetItemRequest) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 14 with DynamicDocumentItem

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);
    }
}
Also used : DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DynamicDocumentTag(com.formkiq.stacks.dynamodb.DynamicDocumentTag) DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) Date(java.util.Date) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 15 with DynamicDocumentItem

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);
    }
}
Also used : DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) Date(java.util.Date) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Aggregations

DynamicDocumentItem (com.formkiq.stacks.dynamodb.DynamicDocumentItem)27 Date (java.util.Date)18 Test (org.junit.jupiter.api.Test)15 DocumentItem (com.formkiq.stacks.dynamodb.DocumentItem)14 Timeout (org.junit.jupiter.api.Timeout)9 DocumentTag (com.formkiq.stacks.dynamodb.DocumentTag)7 DynamicDocumentTag (com.formkiq.stacks.dynamodb.DynamicDocumentTag)6 S3Client (software.amazon.awssdk.services.s3.S3Client)6 DocumentItemToDynamicDocumentItem (com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem)5 DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)4 DynamoDbClient (software.amazon.awssdk.services.dynamodb.DynamoDbClient)4 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)4 ApiGatewayRequestEvent (com.formkiq.lambda.apigateway.ApiGatewayRequestEvent)3 ApiMapResponse (com.formkiq.lambda.apigateway.ApiMapResponse)3 ApiPagination (com.formkiq.lambda.apigateway.ApiPagination)3 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)3 SimpleDateFormat (java.text.SimpleDateFormat)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3