Search in sources :

Example 21 with DocumentItem

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

the class ApiRequestHandlerTest method testHandleGetRequest05.

/**
 * Get Document Request, Document found.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetRequest05() throws Exception {
    // given
    Date date = new Date();
    String documentId = "1a1d1938-451e-4e20-bf95-e0e7a749505a";
    String userId = "jsmith";
    DocumentItem item = new DocumentItemDynamoDb(documentId, date, userId);
    getDocumentService().saveDocument(null, item, new ArrayList<>());
    ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-documentid02.json");
    // when
    String response = handleRequest(event);
    // then
    Map<String, String> m = fromJson(response, Map.class);
    final int mapsize = 3;
    assertEquals(mapsize, m.size());
    assertEquals("200.0", String.valueOf(m.get("statusCode")));
    assertEquals(getHeaders(), "\"headers\":" + GsonUtil.getInstance().toJson(m.get("headers")));
    DynamicObject resp = new DynamicObject(fromJson(m.get("body"), Map.class));
    assertEquals(documentId, resp.get("documentId"));
    assertEquals(userId, resp.get("userId"));
    assertNotNull(documentId, resp.get("insertedDate"));
    assertEquals(DEFAULT_SITE_ID, resp.get("siteId"));
    assertNull(resp.get("next"));
    assertNull(resp.get("previous"));
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) Map(java.util.Map) Date(java.util.Date) DocumentItemDynamoDb(com.formkiq.stacks.dynamodb.DocumentItemDynamoDb) Test(org.junit.jupiter.api.Test)

Example 22 with DocumentItem

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

the class ApiRequestHandlerTest method testHandleGetRequest06.

/**
 * Get Document Request, Document with sub docs found.
 *
 * @throws Exception an error has occurred
 */
@SuppressWarnings("unchecked")
@Test
public void testHandleGetRequest06() throws Exception {
    // given
    Date date = new Date();
    String documentId0 = "1a1d1938-451e-4e20-bf95-e0e7a749505a";
    String documentId1 = UUID.randomUUID().toString();
    String userId = "jsmith";
    DocumentItem item = new DocumentItemDynamoDb(documentId0, date, userId);
    DocumentItem citem = new DocumentItemDynamoDb(documentId1, date, userId);
    DynamicDocumentItem doc = new DocumentItemToDynamicDocumentItem().apply(item);
    doc.put("documents", Arrays.asList(new DocumentItemToDynamicDocumentItem().apply(citem)));
    getDocumentService().saveDocumentItemWithTag(null, doc);
    ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-documentid02.json");
    // when
    String response = handleRequest(event);
    // then
    Map<String, String> m = fromJson(response, Map.class);
    final int mapsize = 3;
    assertEquals(mapsize, m.size());
    assertEquals("200.0", String.valueOf(m.get("statusCode")));
    assertEquals(getHeaders(), "\"headers\":" + GsonUtil.getInstance().toJson(m.get("headers")));
    DynamicObject resp = new DynamicObject(fromJson(m.get("body"), Map.class));
    assertEquals(documentId0, resp.get("documentId"));
    assertEquals(userId, resp.get("userId"));
    assertNotNull(resp.get("insertedDate"));
    assertEquals(DEFAULT_SITE_ID, resp.get("siteId"));
    assertNull(resp.get("next"));
    assertNull(resp.get("previous"));
    List<Map<String, Object>> children = (List<Map<String, Object>>) resp.get("documents");
    assertEquals(1, children.size());
    assertEquals(documentId1, children.get(0).get("documentId"));
    assertNotNull(userId, children.get(0).get("userId"));
    assertNotNull(children.get(0).get("belongsToDocumentId"));
    assertNotNull(children.get(0).get("insertedDate"));
    assertNull(children.get(0).get("siteId"));
}
Also used : DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) Date(java.util.Date) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) ArrayList(java.util.ArrayList) List(java.util.List) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) Map(java.util.Map) DocumentItemDynamoDb(com.formkiq.stacks.dynamodb.DocumentItemDynamoDb) Test(org.junit.jupiter.api.Test)

Example 23 with DocumentItem

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

the class DocumentIdContentRequestHandler method get.

@Override
public ApiRequestHandlerResponse get(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
    String siteId = authorizer.getSiteId();
    String documentId = event.getPathParameters().get("documentId");
    String versionId = getParameter(event, "versionId");
    DocumentItem item = awsservice.documentService().findDocument(siteId, documentId);
    if (item == null) {
        throw new NotFoundException("Document " + documentId + " not found.");
    }
    ApiResponse response = null;
    String s3key = createS3Key(siteId, documentId);
    if (MimeType.isPlainText(item.getContentType())) {
        try (S3Client s3 = awsservice.s3Service().buildClient()) {
            String content = awsservice.s3Service().getContentAsString(s3, awsservice.documents3bucket(), s3key, versionId);
            response = new ApiMapResponse(Map.of("content", content, "contentType", item.getContentType(), "isBase64", Boolean.FALSE));
        }
    } else {
        Duration duration = Duration.ofHours(1);
        URL url = awsservice.s3Service().presignGetUrl(awsservice.documents3bucket(), s3key, duration, versionId);
        response = new ApiMapResponse(Map.of("contentUrl", url.toString(), "contentType", item.getContentType() != null ? item.getContentType() : "application/octet-stream"));
    }
    return new ApiRequestHandlerResponse(SC_OK, response);
}
Also used : DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) NotFoundException(com.formkiq.lambda.apigateway.exception.NotFoundException) Duration(java.time.Duration) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) S3Client(software.amazon.awssdk.services.s3.S3Client) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse) ApiResponse(com.formkiq.lambda.apigateway.ApiResponse) URL(java.net.URL)

Example 24 with DocumentItem

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

the class DocumentIdUrlRequestHandler method get.

@Override
public ApiRequestHandlerResponse get(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
    String documentId = event.getPathParameters().get("documentId");
    String versionId = getParameter(event, "versionId");
    String siteId = authorizer.getSiteId();
    DocumentItem item = awsservice.documentService().findDocument(siteId, documentId);
    if (item == null) {
        throw new NotFoundException("Document " + documentId + " not found.");
    }
    URL url = getS3Url(logger, authorizer, awsservice, event, item, documentId, versionId);
    return url != null ? new ApiRequestHandlerResponse(SC_OK, new ApiUrlResponse(url.toString(), documentId)) : new ApiRequestHandlerResponse(SC_NOT_FOUND, new ApiEmptyResponse());
}
Also used : ApiUrlResponse(com.formkiq.stacks.api.ApiUrlResponse) ApiEmptyResponse(com.formkiq.stacks.api.ApiEmptyResponse) DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) NotFoundException(com.formkiq.lambda.apigateway.exception.NotFoundException) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) URL(java.net.URL)

Example 25 with DocumentItem

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

the class DocumentsIdUploadRequestHandler 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> map = event.getPathParameters();
    Map<String, String> query = event.getQueryStringParameters();
    String siteId = authorizer.getSiteId();
    DocumentService service = awsservice.documentService();
    if (map != null && map.containsKey("documentId")) {
        documentId = map.get("documentId");
        item = service.findDocument(siteId, documentId);
        documentExists = item != null;
        if (!documentExists) {
            throw new NotFoundException("Document " + documentId + " not found.");
        }
    } else 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, siteId, documentId, query);
    logger.log("generated presign url: " + urlstring + " for document " + documentId);
    if (!documentExists && item != null) {
        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) NotFoundException(com.formkiq.lambda.apigateway.exception.NotFoundException) 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)

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