Search in sources :

Example 26 with DocumentTag

use of com.formkiq.stacks.dynamodb.DocumentTag 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 27 with DocumentTag

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

the class WebhooksRequestHandler method saveWebhook.

private String saveWebhook(final ApiGatewayRequestEvent event, final AwsServiceCache awsservice, final String siteId, final DynamicObject o) {
    Date ttlDate = getTtlDate(awsservice, siteId, o);
    String name = o.getString("name");
    String userId = getCallingCognitoUsername(event);
    String enabled = o.containsKey("enabled") ? o.getString("enabled") : "true";
    String id = awsservice.webhookService().saveWebhook(siteId, name, userId, ttlDate, enabled);
    if (o.containsKey("tags")) {
        List<DynamicObject> dtags = o.getList("tags");
        Date date = new Date();
        Collection<DocumentTag> tags = dtags.stream().map(d -> new DocumentTag(null, d.getString("key"), d.getString("value"), date, userId)).collect(Collectors.toList());
        awsservice.webhookService().addTags(siteId, id, tags, ttlDate);
    }
    return id;
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) WEBHOOK_TIME_TO_LIVE(com.formkiq.stacks.dynamodb.ConfigService.WEBHOOK_TIME_TO_LIVE) Date(java.util.Date) ZonedDateTime(java.time.ZonedDateTime) Collection(java.util.Collection) ApiGatewayRequestHandler(com.formkiq.lambda.apigateway.ApiGatewayRequestHandler) DEFAULT_SITE_ID(com.formkiq.stacks.dynamodb.SiteIdKeyGenerator.DEFAULT_SITE_ID) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse) HashMap(java.util.HashMap) AwsServiceCache(com.formkiq.lambda.apigateway.AwsServiceCache) ApiAuthorizer(com.formkiq.lambda.apigateway.ApiAuthorizer) Collectors(java.util.stream.Collectors) BadException(com.formkiq.lambda.apigateway.exception.BadException) TooManyRequestsException(com.formkiq.lambda.apigateway.exception.TooManyRequestsException) MAX_WEBHOOKS(com.formkiq.stacks.dynamodb.ConfigService.MAX_WEBHOOKS) List(java.util.List) ApiGatewayRequestEventUtil(com.formkiq.lambda.apigateway.ApiGatewayRequestEventUtil) LambdaLogger(com.amazonaws.services.lambda.runtime.LambdaLogger) Map(java.util.Map) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) ZoneOffset(java.time.ZoneOffset) SC_OK(com.formkiq.lambda.apigateway.ApiResponseStatus.SC_OK) DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) Date(java.util.Date)

Example 28 with DocumentTag

use of com.formkiq.stacks.dynamodb.DocumentTag 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 29 with DocumentTag

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

the class DocumentsS3UpdateTest method testHandleRequest05.

/**
 * Test Processing Document with sub documents.
 *
 * @throws Exception Exception
 */
@Test
@Timeout(unit = TimeUnit.MILLISECONDS, value = TEST_TIMEOUT)
public void testHandleRequest05() throws Exception {
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        this.logger.reset();
        Date now = new Date();
        DynamicDocumentItem doc = createSubDocuments(now);
        service.saveDocumentItemWithTag(siteId, doc);
        String key = createDatabaseKey(siteId, doc.getDocumentId());
        final Map<String, Object> map = loadFileAsMap(this, "/objectcreate-event1.json", BUCKET_KEY, key);
        addS3File(key, "pdf", false, "testdata");
        // when
        DocumentItem item = handleRequest(siteId, doc.getDocumentId(), map);
        DocumentItem itemchild = handleRequest(siteId, doc.getDocuments().get(0).getDocumentId(), map);
        // then
        try (DynamoDbClient client = dbBuilder.build()) {
            Map<String, AttributeValue> m = client.getItem(GetItemRequest.builder().tableName("Documents").key(keysDocument(siteId, doc.getDocumentId())).build()).item();
            assertNotNull(m.get(GSI1_PK));
            Map<String, AttributeValue> mchild = client.getItem(GetItemRequest.builder().tableName("Documents").key(keysDocument(siteId, itemchild.getDocumentId())).build()).item();
            assertNull(mchild.get(GSI1_PK));
        }
        assertEquals(doc.getDocumentId(), item.getDocumentId());
        PaginationResults<DocumentTag> tags = service.findDocumentTags(siteId, doc.getDocumentId(), null, MAX_RESULTS);
        assertEquals(2, tags.getResults().size());
        assertDocumentTagEquals(new DocumentTag().setKey("category").setValue("none").setType(DocumentTagType.USERDEFINED).setDocumentId(doc.getDocumentId()), tags.getResults().get(0));
        assertDocumentTagEquals(new DocumentTag().setKey("userId").setValue(doc.getUserId()).setType(DocumentTagType.SYSTEMDEFINED).setDocumentId(doc.getDocumentId()), tags.getResults().get(1));
        tags = service.findDocumentTags(siteId, itemchild.getDocumentId(), null, MAX_RESULTS);
        assertEquals(1, tags.getResults().size());
        assertDocumentTagEquals(new DocumentTag().setKey("category1").setValue("").setType(DocumentTagType.USERDEFINED).setDocumentId(itemchild.getDocumentId()), tags.getResults().get(0));
    }
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) DynamicDocumentTag(com.formkiq.stacks.dynamodb.DynamicDocumentTag) DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) 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 30 with DocumentTag

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

the class DocumentsS3UpdateTest method testHandleRequest04.

/**
 * Create Document Request on child document.
 *
 * @throws Exception Exception
 */
@Test
@Timeout(unit = TimeUnit.MILLISECONDS, value = TEST_TIMEOUT)
public void testHandleRequest04() throws Exception {
    String documentId = UUID.randomUUID().toString();
    for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
        // given
        this.logger.reset();
        String key = createDatabaseKey(siteId, documentId);
        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.setPath("test.txt");
        doc.setUserId("joe");
        DynamicDocumentItem child = new DynamicDocumentItem(Map.of());
        child.setInsertedDate(new Date());
        child.setDocumentId(documentId);
        child.setUserId("joe");
        doc.put("documents", Arrays.asList(child));
        service.saveDocumentItemWithTag(siteId, doc);
        addS3File(key, "pdf", false, "testdata");
        // when
        final DocumentItem item = handleRequest(siteId, child.getDocumentId(), map);
        // then
        assertNotNull(item.getBelongsToDocumentId());
        PaginationResults<DocumentTag> tags = service.findDocumentTags(siteId, BUCKET_KEY, null, MAX_RESULTS);
        try (DynamoDbClient client = dbBuilder.build()) {
            Map<String, AttributeValue> m = client.getItem(GetItemRequest.builder().tableName("Documents").key(keysDocument(siteId, child.getDocumentId())).build()).item();
            assertNull(m.get(GSI1_PK));
        }
        final int count = 3;
        int i = 0;
        assertEquals(count, tags.getResults().size());
        assertDocumentTagEquals(new DocumentTag().setKey("path").setValue("test.txt").setDocumentId(BUCKET_KEY).setType(DocumentTagType.SYSTEMDEFINED).setUserId("joe"), tags.getResults().get(i++));
        assertDocumentTagEquals(new DocumentTag().setKey("untagged").setValue("true").setDocumentId(BUCKET_KEY).setType(DocumentTagType.SYSTEMDEFINED).setUserId("joe"), tags.getResults().get(i++));
        assertDocumentTagEquals(new DocumentTag().setKey("userId").setValue("joe").setDocumentId(BUCKET_KEY).setType(DocumentTagType.SYSTEMDEFINED).setUserId("joe"), tags.getResults().get(i++));
        assertEquals(0, service.findDocumentFormats(siteId, BUCKET_KEY, null, MAX_RESULTS).getResults().size());
        verifyDocumentSaved(siteId, item, "pdf", "8");
        assertPublishSnsMessage(siteId, sqsDocumentEventUrl, "create", false, true);
        tags = service.findDocumentTags(siteId, documentId, null, MAX_RESULTS);
        assertEquals(0, tags.getResults().size());
    }
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DynamicDocumentTag(com.formkiq.stacks.dynamodb.DynamicDocumentTag) DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) 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

DocumentTag (com.formkiq.stacks.dynamodb.DocumentTag)46 Date (java.util.Date)34 Test (org.junit.jupiter.api.Test)34 ApiGatewayRequestEvent (com.formkiq.lambda.apigateway.ApiGatewayRequestEvent)31 DocumentItemDynamoDb (com.formkiq.stacks.dynamodb.DocumentItemDynamoDb)13 ApiMessageResponse (com.formkiq.lambda.apigateway.ApiMessageResponse)11 DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)10 DocumentItem (com.formkiq.stacks.dynamodb.DocumentItem)10 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)9 DynamicDocumentItem (com.formkiq.stacks.dynamodb.DynamicDocumentItem)9 BadException (com.formkiq.lambda.apigateway.exception.BadException)7 Map (java.util.Map)7 ApiResponse (com.formkiq.lambda.apigateway.ApiResponse)5 NotFoundException (com.formkiq.lambda.apigateway.exception.NotFoundException)5 DynamicDocumentTag (com.formkiq.stacks.dynamodb.DynamicDocumentTag)5 Timeout (org.junit.jupiter.api.Timeout)5 DocumentService (com.formkiq.stacks.dynamodb.DocumentService)4 S3Client (software.amazon.awssdk.services.s3.S3Client)4 LambdaLogger (com.amazonaws.services.lambda.runtime.LambdaLogger)3 DocumentSearchQuery (com.formkiq.stacks.client.models.DocumentSearchQuery)3