Search in sources :

Example 11 with DocumentItem

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));
}
Also used : DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) ApiUrlResponse(com.formkiq.stacks.api.ApiUrlResponse) ArrayList(java.util.ArrayList) BadException(com.formkiq.lambda.apigateway.exception.BadException) Date(java.util.Date) DocumentService(com.formkiq.stacks.dynamodb.DocumentService) DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) DocumentItemDynamoDb(com.formkiq.stacks.dynamodb.DocumentItemDynamoDb)

Example 12 with DocumentItem

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;
}
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 13 with DocumentItem

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);
    }
}
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 DocumentItem

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);
    }
}
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 DocumentItem

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);
    }
}
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

DocumentItem (com.formkiq.stacks.dynamodb.DocumentItem)25 DynamicDocumentItem (com.formkiq.stacks.dynamodb.DynamicDocumentItem)20 Date (java.util.Date)17 Test (org.junit.jupiter.api.Test)14 DocumentTag (com.formkiq.stacks.dynamodb.DocumentTag)10 Timeout (org.junit.jupiter.api.Timeout)9 DocumentItemToDynamicDocumentItem (com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem)7 DynamicDocumentTag (com.formkiq.stacks.dynamodb.DynamicDocumentTag)7 S3Client (software.amazon.awssdk.services.s3.S3Client)7 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)6 DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)6 DocumentItemDynamoDb (com.formkiq.stacks.dynamodb.DocumentItemDynamoDb)6 ApiGatewayRequestEvent (com.formkiq.lambda.apigateway.ApiGatewayRequestEvent)5 Map (java.util.Map)5 NotFoundException (com.formkiq.lambda.apigateway.exception.NotFoundException)4 ArrayList (java.util.ArrayList)4 ApiMapResponse (com.formkiq.lambda.apigateway.ApiMapResponse)3 BadException (com.formkiq.lambda.apigateway.exception.BadException)3 ApiUrlResponse (com.formkiq.stacks.api.ApiUrlResponse)3 DocumentService (com.formkiq.stacks.dynamodb.DocumentService)3