Search in sources :

Example 1 with DynamicDocumentTag

use of com.formkiq.stacks.dynamodb.DynamicDocumentTag in project formkiq-core by formkiq.

the class DocumentsS3Update method processS3File.

/**
 * Process S3 File.
 *
 * @param logger {@link LambdaLogger}
 * @param create boolean
 * @param s3bucket {@link String}
 * @param s3key {@link String}
 * @param debug boolean
 * @throws FileNotFoundException FileNotFoundException
 */
private void processS3File(final LambdaLogger logger, final boolean create, final String s3bucket, final String s3key, final boolean debug) throws FileNotFoundException {
    String key = urlDecode(s3key);
    String siteId = getSiteId(key);
    String documentId = resetDatabaseKey(siteId, key);
    try (S3Client s3 = this.s3service.buildClient()) {
        S3ObjectMetadata resp = this.s3service.getObjectMetadata(s3, s3bucket, key);
        if (!resp.isObjectExists()) {
            throw new FileNotFoundException("Object " + documentId + " not found in bucket " + s3bucket);
        }
        String contentType = resp.getContentType();
        Long contentLength = resp.getContentLength();
        DocumentItem item = this.service.findDocument(siteId, documentId);
        if (item != null) {
            DynamicDocumentItem doc = new DocumentItemToDynamicDocumentItem().apply(item);
            if (contentType != null && contentType.length() > 0) {
                doc.setContentType(contentType);
            }
            doc.setChecksum(resp.getEtag());
            if (contentLength != null) {
                doc.setContentLength(contentLength);
            }
            logger.log("saving document " + createDatabaseKey(siteId, item.getDocumentId()));
            List<DynamicDocumentTag> tags = getObjectTags(s3, item, s3bucket, key);
            doc.put("tags", tags);
            if (debug) {
                logger.log("original " + this.gson.toJson(item));
                logger.log("new " + this.gson.toJson(doc));
            }
            this.service.saveDocumentItemWithTag(siteId, doc);
            this.service.deleteDocumentFormats(siteId, item.getDocumentId());
            String content = getContent(s3bucket, key, s3, resp, doc);
            sendSnsMessage(logger, create ? "create" : "update", siteId, doc, s3bucket, key, content);
        } else {
            logger.log("Cannot find document " + documentId + " in site " + siteId);
        }
    }
}
Also used : DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) DynamicDocumentTag(com.formkiq.stacks.dynamodb.DynamicDocumentTag) DocumentTagToDynamicDocumentTag(com.formkiq.stacks.dynamodb.DocumentTagToDynamicDocumentTag) FileNotFoundException(java.io.FileNotFoundException) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) S3ObjectMetadata(com.formkiq.aws.s3.S3ObjectMetadata) S3Client(software.amazon.awssdk.services.s3.S3Client)

Example 2 with DynamicDocumentTag

use of com.formkiq.stacks.dynamodb.DynamicDocumentTag in project formkiq-core by formkiq.

the class DocumentsS3UpdateTest method testHandleRequest02.

/**
 * Update Document Request with existing Tags.
 *
 * @throws Exception Exception
 */
@Test
@Timeout(unit = TimeUnit.MILLISECONDS, value = TEST_TIMEOUT)
public void testHandleRequest02() 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, "/objectupdate-event1.json", BUCKET_KEY, key);
        DynamicDocumentItem doc = new DynamicDocumentItem(Map.of());
        doc.setInsertedDate(new Date());
        doc.setDocumentId(BUCKET_KEY);
        doc.setUserId("asd");
        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);
        DocumentFormat format = new DocumentFormat();
        format.setContentType("application/pdf");
        format.setDocumentId(BUCKET_KEY);
        format.setInsertedDate(new Date());
        format.setUserId("asd");
        service.saveDocumentFormat(siteId, format);
        addS3File(key, "pdf", true, "testdata");
        // when
        final DocumentItem item = handleRequest(siteId, BUCKET_KEY, map);
        // then
        PaginationResults<DocumentTag> tags = service.findDocumentTags(siteId, BUCKET_KEY, null, MAX_RESULTS);
        final int size = 5;
        int i = 0;
        assertEquals(size, tags.getResults().size());
        assertEquals("CLAMAV_SCAN_STATUS", tags.getResults().get(i).getKey());
        assertEquals("GOOD", tags.getResults().get(i).getValue());
        assertEquals(DocumentTagType.SYSTEMDEFINED, tags.getResults().get(i++).getType());
        assertEquals("path", tags.getResults().get(i).getKey());
        assertEquals(DocumentTagType.SYSTEMDEFINED, tags.getResults().get(i++).getType());
        assertEquals("person", tags.getResults().get(i).getKey());
        assertEquals(DocumentTagType.USERDEFINED, tags.getResults().get(i++).getType());
        assertEquals("12345", tags.getResults().get(i).getValue());
        assertEquals(DocumentTagType.USERDEFINED, tags.getResults().get(i).getType());
        assertEquals("12345", tags.getResults().get(i++).getValue());
        assertEquals(DocumentTagType.SYSTEMDEFINED, tags.getResults().get(i).getType());
        assertEquals("asd", tags.getResults().get(i++).getValue());
        assertEquals(0, service.findDocumentFormats(siteId, BUCKET_KEY, null, MAX_RESULTS).getResults().size());
        verifyDocumentSaved(siteId, item, "pdf", "8");
        assertPublishSnsMessage(siteId, sqsDocumentEventUrl, "update", false, false);
    }
}
Also used : DocumentFormat(com.formkiq.stacks.dynamodb.DocumentFormat) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DynamicDocumentTag(com.formkiq.stacks.dynamodb.DynamicDocumentTag) DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) 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 3 with DynamicDocumentTag

use of com.formkiq.stacks.dynamodb.DynamicDocumentTag 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;
}
Also used : SdkHttpUtils(software.amazon.awssdk.utils.http.SdkHttpUtils) DocumentService(com.formkiq.stacks.dynamodb.DocumentService) 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(com.formkiq.stacks.dynamodb.SiteIdKeyGenerator) SiteIdKeyGenerator.createDatabaseKey(com.formkiq.stacks.dynamodb.SiteIdKeyGenerator.createDatabaseKey) GsonBuilder(com.google.gson.GsonBuilder) ArrayList(java.util.ArrayList) 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) S3ObjectMetadata(com.formkiq.aws.s3.S3ObjectMetadata) Region(software.amazon.awssdk.regions.Region) S3Service(com.formkiq.aws.s3.S3Service) SqsConnectionBuilder(com.formkiq.aws.sqs.SqsConnectionBuilder) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) S3Client(software.amazon.awssdk.services.s3.S3Client) SYSTEM_DEFINED_TAGS(com.formkiq.stacks.dynamodb.DocumentService.SYSTEM_DEFINED_TAGS) DynamicDocumentTag(com.formkiq.stacks.dynamodb.DynamicDocumentTag) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) SqsService(com.formkiq.aws.sqs.SqsService) SnsConnectionBuilder(com.formkiq.aws.sns.SnsConnectionBuilder) Reflectable(com.formkiq.graalvm.annotations.Reflectable) List(java.util.List) SnsService(com.formkiq.aws.sns.SnsService) SiteIdKeyGenerator.getSiteId(com.formkiq.stacks.dynamodb.SiteIdKeyGenerator.getSiteId) MimeType(com.formkiq.stacks.common.formats.MimeType) DynamoDbConnectionBuilder(com.formkiq.stacks.dynamodb.DynamoDbConnectionBuilder) MessageAttributeValue(software.amazon.awssdk.services.sns.model.MessageAttributeValue) DocumentTagToDynamicDocumentTag(com.formkiq.stacks.dynamodb.DocumentTagToDynamicDocumentTag) DocumentTagType(com.formkiq.stacks.dynamodb.DocumentTagType) GetObjectTaggingResponse(software.amazon.awssdk.services.s3.model.GetObjectTaggingResponse) DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) DynamicDocumentTag(com.formkiq.stacks.dynamodb.DynamicDocumentTag) DocumentTagToDynamicDocumentTag(com.formkiq.stacks.dynamodb.DocumentTagToDynamicDocumentTag) GetObjectTaggingResponse(software.amazon.awssdk.services.s3.model.GetObjectTaggingResponse) DynamicDocumentTag(com.formkiq.stacks.dynamodb.DynamicDocumentTag) DocumentTagToDynamicDocumentTag(com.formkiq.stacks.dynamodb.DocumentTagToDynamicDocumentTag) DocumentTagToDynamicDocumentTag(com.formkiq.stacks.dynamodb.DocumentTagToDynamicDocumentTag)

Example 4 with DynamicDocumentTag

use of com.formkiq.stacks.dynamodb.DynamicDocumentTag 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)

Aggregations

DocumentItem (com.formkiq.stacks.dynamodb.DocumentItem)4 DynamicDocumentItem (com.formkiq.stacks.dynamodb.DynamicDocumentItem)4 DynamicDocumentTag (com.formkiq.stacks.dynamodb.DynamicDocumentTag)4 S3ObjectMetadata (com.formkiq.aws.s3.S3ObjectMetadata)2 DocumentItemToDynamicDocumentItem (com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem)2 DocumentTag (com.formkiq.stacks.dynamodb.DocumentTag)2 DocumentTagToDynamicDocumentTag (com.formkiq.stacks.dynamodb.DocumentTagToDynamicDocumentTag)2 FileNotFoundException (java.io.FileNotFoundException)2 Date (java.util.Date)2 Test (org.junit.jupiter.api.Test)2 Timeout (org.junit.jupiter.api.Timeout)2 S3Client (software.amazon.awssdk.services.s3.S3Client)2 Context (com.amazonaws.services.lambda.runtime.Context)1 LambdaLogger (com.amazonaws.services.lambda.runtime.LambdaLogger)1 RequestHandler (com.amazonaws.services.lambda.runtime.RequestHandler)1 S3ConnectionBuilder (com.formkiq.aws.s3.S3ConnectionBuilder)1 S3Service (com.formkiq.aws.s3.S3Service)1 SnsConnectionBuilder (com.formkiq.aws.sns.SnsConnectionBuilder)1 SnsService (com.formkiq.aws.sns.SnsService)1 SqsConnectionBuilder (com.formkiq.aws.sqs.SqsConnectionBuilder)1