Search in sources :

Example 6 with ApiRequestHandlerResponse

use of com.formkiq.lambda.apigateway.ApiRequestHandlerResponse in project formkiq-core by formkiq.

the class DocumentTagsRequestHandler method get.

@Override
public ApiRequestHandlerResponse get(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
    CacheService cacheService = awsservice.documentCacheService();
    ApiPagination pagination = getPagination(cacheService, event);
    int limit = pagination != null ? pagination.getLimit() : getLimit(logger, event);
    PaginationMapToken ptoken = pagination != null ? pagination.getStartkey() : null;
    String siteId = authorizer.getSiteId();
    String documentId = event.getPathParameters().get("documentId");
    PaginationResults<DocumentTag> results = awsservice.documentService().findDocumentTags(siteId, documentId, ptoken, limit);
    results.getResults().forEach(r -> r.setDocumentId(null));
    ApiPagination current = createPagination(cacheService, event, pagination, results.getToken(), limit);
    List<DocumentTag> tags = subList(results.getResults(), limit);
    List<ApiDocumentTagItemResponse> list = tags.stream().map(t -> {
        ApiDocumentTagItemResponse r = new ApiDocumentTagItemResponse();
        r.setDocumentId(t.getDocumentId());
        r.setInsertedDate(t.getInsertedDate());
        r.setKey(t.getKey());
        r.setValue(t.getValue());
        r.setValues(t.getValues());
        r.setUserId(t.getUserId());
        r.setType(t.getType() != null ? t.getType().name().toLowerCase() : null);
        return r;
    }).collect(Collectors.toList());
    ApiDocumentTagsItemResponse resp = new ApiDocumentTagsItemResponse();
    resp.setTags(list);
    resp.setPrevious(current.getPrevious());
    resp.setNext(current.hasNext() ? current.getNext() : null);
    return new ApiRequestHandlerResponse(SC_OK, resp);
}
Also used : SC_CREATED(com.formkiq.lambda.apigateway.ApiResponseStatus.SC_CREATED) Arrays(java.util.Arrays) DocumentTags(com.formkiq.stacks.dynamodb.DocumentTags) Date(java.util.Date) AwsServiceCache(com.formkiq.lambda.apigateway.AwsServiceCache) ApiAuthorizer(com.formkiq.lambda.apigateway.ApiAuthorizer) ApiResponse(com.formkiq.lambda.apigateway.ApiResponse) ApiGatewayRequestEventUtil(com.formkiq.lambda.apigateway.ApiGatewayRequestEventUtil) LambdaLogger(com.amazonaws.services.lambda.runtime.LambdaLogger) DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) SC_OK(com.formkiq.lambda.apigateway.ApiResponseStatus.SC_OK) PaginationMapToken(com.formkiq.stacks.dynamodb.PaginationMapToken) ApiDocumentTagsItemResponse(com.formkiq.stacks.api.ApiDocumentTagsItemResponse) ApiGatewayRequestHandler(com.formkiq.lambda.apigateway.ApiGatewayRequestHandler) ApiPagination(com.formkiq.lambda.apigateway.ApiPagination) ApiDocumentTagItemResponse(com.formkiq.stacks.api.ApiDocumentTagItemResponse) Collectors(java.util.stream.Collectors) BadException(com.formkiq.lambda.apigateway.exception.BadException) List(java.util.List) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) CacheService(com.formkiq.stacks.dynamodb.CacheService) ApiMessageResponse(com.formkiq.lambda.apigateway.ApiMessageResponse) Collections(java.util.Collections) DocumentTagType(com.formkiq.stacks.dynamodb.DocumentTagType) PaginationResults(com.formkiq.stacks.dynamodb.PaginationResults) ApiPagination(com.formkiq.lambda.apigateway.ApiPagination) DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) ApiDocumentTagItemResponse(com.formkiq.stacks.api.ApiDocumentTagItemResponse) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) PaginationMapToken(com.formkiq.stacks.dynamodb.PaginationMapToken) ApiDocumentTagsItemResponse(com.formkiq.stacks.api.ApiDocumentTagsItemResponse) CacheService(com.formkiq.stacks.dynamodb.CacheService)

Example 7 with ApiRequestHandlerResponse

use of com.formkiq.lambda.apigateway.ApiRequestHandlerResponse in project formkiq-core by formkiq.

the class DocumentVersionsRequestHandler method get.

@Override
public ApiRequestHandlerResponse get(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
    SimpleDateFormat df = new SimpleDateFormat(GsonUtil.DATE_FORMAT);
    String siteId = authorizer.getSiteId();
    String documentId = event.getPathParameters().get("documentId");
    String next = getParameter(event, "next");
    String tz = getParameter(event, "tz");
    ZoneOffset offset = DateUtil.getZoneOffset(tz);
    df.setTimeZone(TimeZone.getTimeZone(offset));
    String s3key = createDatabaseKey(siteId, documentId);
    S3Service s3service = awsservice.s3Service();
    try (S3Client s3 = s3service.buildClient()) {
        ListObjectVersionsResponse response = s3service.getObjectVersions(s3, awsservice.documents3bucket(), s3key, next);
        List<ApiDocumentVersion> list = response.versions().stream().map(v -> {
            ApiDocumentVersion dv = new ApiDocumentVersion();
            dv.setVersionId(v.versionId());
            Date date = Date.from(v.lastModified());
            dv.setLastModifiedDate(df.format(date));
            return dv;
        }).collect(Collectors.toList());
        ApiDocumentVersionsResponse resp = new ApiDocumentVersionsResponse();
        resp.setNext(response.nextKeyMarker());
        resp.setVersions(list);
        return new ApiRequestHandlerResponse(SC_OK, resp);
    }
}
Also used : Date(java.util.Date) TimeZone(java.util.TimeZone) S3Client(software.amazon.awssdk.services.s3.S3Client) ApiGatewayRequestHandler(com.formkiq.lambda.apigateway.ApiGatewayRequestHandler) SimpleDateFormat(java.text.SimpleDateFormat) AwsServiceCache(com.formkiq.lambda.apigateway.AwsServiceCache) ApiAuthorizer(com.formkiq.lambda.apigateway.ApiAuthorizer) Collectors(java.util.stream.Collectors) SiteIdKeyGenerator.createDatabaseKey(com.formkiq.stacks.dynamodb.SiteIdKeyGenerator.createDatabaseKey) List(java.util.List) ApiGatewayRequestEventUtil(com.formkiq.lambda.apigateway.ApiGatewayRequestEventUtil) GsonUtil(com.formkiq.lambda.apigateway.util.GsonUtil) LambdaLogger(com.amazonaws.services.lambda.runtime.LambdaLogger) DateUtil(com.formkiq.stacks.dynamodb.DateUtil) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) ZoneOffset(java.time.ZoneOffset) SC_OK(com.formkiq.lambda.apigateway.ApiResponseStatus.SC_OK) ApiDocumentVersionsResponse(com.formkiq.stacks.api.ApiDocumentVersionsResponse) S3Service(com.formkiq.aws.s3.S3Service) ApiDocumentVersion(com.formkiq.stacks.api.ApiDocumentVersion) ListObjectVersionsResponse(software.amazon.awssdk.services.s3.model.ListObjectVersionsResponse) ApiDocumentVersionsResponse(com.formkiq.stacks.api.ApiDocumentVersionsResponse) ApiDocumentVersion(com.formkiq.stacks.api.ApiDocumentVersion) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) S3Client(software.amazon.awssdk.services.s3.S3Client) SimpleDateFormat(java.text.SimpleDateFormat) S3Service(com.formkiq.aws.s3.S3Service) ListObjectVersionsResponse(software.amazon.awssdk.services.s3.model.ListObjectVersionsResponse) Date(java.util.Date) ZoneOffset(java.time.ZoneOffset)

Example 8 with ApiRequestHandlerResponse

use of com.formkiq.lambda.apigateway.ApiRequestHandlerResponse in project formkiq-core by formkiq.

the class DocumentsRequestHandler method get.

@Override
public ApiRequestHandlerResponse get(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
    ApiPagination pagination = getPagination(awsservice.documentCacheService(), event);
    final int limit = pagination != null ? pagination.getLimit() : getLimit(logger, event);
    final PaginationMapToken ptoken = pagination != null ? pagination.getStartkey() : null;
    String tz = getParameter(event, "tz");
    String dateString = getParameter(event, "date");
    if (StringUtils.isBlank(dateString)) {
        if (StringUtils.isNotBlank(tz)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            ZoneOffset offset = DateUtil.getZoneOffset(tz);
            sdf.setTimeZone(TimeZone.getTimeZone(offset));
            dateString = sdf.format(new Date());
        } else {
            dateString = this.df.format(new Date());
        }
    }
    ZonedDateTime date = transformToDate(logger, awsservice, dateString, tz);
    String siteId = authorizer.getSiteId();
    final PaginationResults<DocumentItem> results = awsservice.documentService().findDocumentsByDate(siteId, date, ptoken, limit);
    ApiPagination current = createPagination(awsservice.documentCacheService(), event, pagination, results.getToken(), limit);
    List<DocumentItem> documents = subList(results.getResults(), limit);
    List<DynamicDocumentItem> items = documents.stream().map(m -> new DocumentItemToDynamicDocumentItem().apply(m)).collect(Collectors.toList());
    items.forEach(i -> i.put("siteId", siteId != null ? siteId : DEFAULT_SITE_ID));
    Map<String, Object> map = new HashMap<>();
    map.put("documents", items);
    map.put("previous", current.getPrevious());
    map.put("next", current.hasNext() ? current.getNext() : null);
    return new ApiRequestHandlerResponse(SC_OK, new ApiMapResponse(map));
}
Also used : StringUtils(software.amazon.awssdk.utils.StringUtils) Date(java.util.Date) ZonedDateTime(java.time.ZonedDateTime) DEFAULT_SITE_ID(com.formkiq.stacks.dynamodb.SiteIdKeyGenerator.DEFAULT_SITE_ID) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) AwsServiceCache(com.formkiq.lambda.apigateway.AwsServiceCache) ApiAuthorizer(com.formkiq.lambda.apigateway.ApiAuthorizer) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) ApiGatewayRequestEventUtil(com.formkiq.lambda.apigateway.ApiGatewayRequestEventUtil) LambdaLogger(com.amazonaws.services.lambda.runtime.LambdaLogger) Map(java.util.Map) ZoneOffset(java.time.ZoneOffset) SC_OK(com.formkiq.lambda.apigateway.ApiResponseStatus.SC_OK) PaginationMapToken(com.formkiq.stacks.dynamodb.PaginationMapToken) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) TimeZone(java.util.TimeZone) ApiGatewayRequestHandler(com.formkiq.lambda.apigateway.ApiGatewayRequestHandler) ApiPagination(com.formkiq.lambda.apigateway.ApiPagination) ZoneRulesException(java.time.zone.ZoneRulesException) Collectors(java.util.stream.Collectors) BadException(com.formkiq.lambda.apigateway.exception.BadException) List(java.util.List) DateUtil(com.formkiq.stacks.dynamodb.DateUtil) ApiGatewayRequestEvent(com.formkiq.lambda.apigateway.ApiGatewayRequestEvent) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) PaginationResults(com.formkiq.stacks.dynamodb.PaginationResults) DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) ApiPagination(com.formkiq.lambda.apigateway.ApiPagination) HashMap(java.util.HashMap) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) Date(java.util.Date) ZoneOffset(java.time.ZoneOffset) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) ZonedDateTime(java.time.ZonedDateTime) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse) PaginationMapToken(com.formkiq.stacks.dynamodb.PaginationMapToken) SimpleDateFormat(java.text.SimpleDateFormat)

Example 9 with ApiRequestHandlerResponse

use of com.formkiq.lambda.apigateway.ApiRequestHandlerResponse 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 10 with ApiRequestHandlerResponse

use of com.formkiq.lambda.apigateway.ApiRequestHandlerResponse in project formkiq-core by formkiq.

the class PublicWebhooksRequestHandler method buildRedirect.

private ApiRequestHandlerResponse buildRedirect(final ApiGatewayRequestEvent event, final String redirectUri, final String body) {
    ApiRequestHandlerResponse response;
    StringBuilder sb = new StringBuilder();
    sb.append(redirectUri);
    Map<String, String> queryMap = decodeQueryString(body);
    String responseFields = getParameter(event, "responseFields");
    if (StringUtils.isNotBlank(responseFields)) {
        String[] fields = responseFields.split(",");
        for (int i = 0; i < fields.length; i++) {
            String value = queryMap.get(fields[i]);
            sb.append(i == 0 && redirectUri.indexOf("?") == -1 ? "?" : "&");
            sb.append(fields[i] + "=" + value);
        }
    }
    response = new ApiRequestHandlerResponse(MOVED_PERMANENTLY, new ApiRedirectResponse(sb.toString()));
    return response;
}
Also used : ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) ApiRedirectResponse(com.formkiq.lambda.apigateway.ApiRedirectResponse)

Aggregations

ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)27 NotFoundException (com.formkiq.lambda.apigateway.exception.NotFoundException)15 ApiMapResponse (com.formkiq.lambda.apigateway.ApiMapResponse)11 BadException (com.formkiq.lambda.apigateway.exception.BadException)11 Date (java.util.Date)11 ApiMessageResponse (com.formkiq.lambda.apigateway.ApiMessageResponse)10 DocumentTag (com.formkiq.stacks.dynamodb.DocumentTag)10 ApiResponse (com.formkiq.lambda.apigateway.ApiResponse)9 DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)7 LambdaLogger (com.amazonaws.services.lambda.runtime.LambdaLogger)6 ApiAuthorizer (com.formkiq.lambda.apigateway.ApiAuthorizer)6 ApiGatewayRequestEvent (com.formkiq.lambda.apigateway.ApiGatewayRequestEvent)6 ApiGatewayRequestEventUtil (com.formkiq.lambda.apigateway.ApiGatewayRequestEventUtil)6 ApiGatewayRequestHandler (com.formkiq.lambda.apigateway.ApiGatewayRequestHandler)6 SC_OK (com.formkiq.lambda.apigateway.ApiResponseStatus.SC_OK)6 AwsServiceCache (com.formkiq.lambda.apigateway.AwsServiceCache)6 DocumentItem (com.formkiq.stacks.dynamodb.DocumentItem)6 HashMap (java.util.HashMap)6 List (java.util.List)6 Collectors (java.util.stream.Collectors)6