Search in sources :

Example 1 with ApiMapResponse

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

the class DocumentIdRequestHandler method get.

@Override
public ApiRequestHandlerResponse get(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
    String siteId = authorizer.getSiteId();
    int limit = getLimit(logger, event);
    ApiPagination token = getPagination(awsservice.documentCacheService(), event);
    String documentId = event.getPathParameters().get("documentId");
    ApiPagination pagination = getPagination(awsservice.documentCacheService(), event);
    PaginationResult<DocumentItem> presult = awsservice.documentService().findDocument(siteId, documentId, true, token != null ? token.getStartkey() : null, limit);
    DocumentItem result = presult.getResult();
    if (result == null) {
        throw new NotFoundException("Document " + documentId + " not found.");
    }
    ApiPagination current = createPagination(awsservice.documentCacheService(), event, pagination, presult.getToken(), limit);
    DynamicDocumentItem item = new DocumentItemToDynamicDocumentItem().apply(result);
    item.put("siteId", siteId != null ? siteId : DEFAULT_SITE_ID);
    item.put("previous", current.getPrevious());
    item.put("next", current.hasNext() ? current.getNext() : null);
    return new ApiRequestHandlerResponse(SC_OK, new ApiMapResponse(item));
}
Also used : ApiPagination(com.formkiq.lambda.apigateway.ApiPagination) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) DynamicDocumentItem(com.formkiq.stacks.dynamodb.DynamicDocumentItem) DocumentItemToDynamicDocumentItem(com.formkiq.stacks.dynamodb.DocumentItemToDynamicDocumentItem) DocumentItem(com.formkiq.stacks.dynamodb.DocumentItem) NotFoundException(com.formkiq.lambda.apigateway.exception.NotFoundException) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse)

Example 2 with ApiMapResponse

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

the class DocumentIdRequestHandler method patch.

@Override
public ApiRequestHandlerResponse patch(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
    boolean isUpdate = event.getHttpMethod().equalsIgnoreCase("patch") && event.getPathParameters().containsKey("documentId");
    String siteId = authorizer.getSiteId();
    String documentId = UUID.randomUUID().toString();
    if (isUpdate) {
        documentId = event.getPathParameters().get("documentId");
        if (awsservice.documentService().findDocument(siteId, documentId) == null) {
            throw new NotFoundException("Document " + documentId + " not found.");
        }
    }
    String maxDocumentCount = null;
    DynamicObject item = fromBodyToDynamicObject(logger, event);
    updateContentType(event, item);
    List<DynamicObject> documents = item.getList("documents");
    if (!isUpdate) {
        if (!item.hasString("content") && item.getList("documents").isEmpty()) {
            throw new BadException("Invalid JSON body.");
        }
        maxDocumentCount = this.restrictionMaxDocuments.getValue(awsservice, siteId);
        if (maxDocumentCount != null && this.restrictionMaxDocuments.enforced(awsservice, siteId, maxDocumentCount)) {
            throw new BadException("Max Number of Documents reached");
        }
    }
    addFieldsToObject(event, awsservice, siteId, documentId, item, documents);
    item.put("documents", documents);
    logger.log("setting userId: " + item.getString("userId") + " contentType: " + item.getString("contentType"));
    putObjectToStaging(logger, awsservice, maxDocumentCount, siteId, item);
    Map<String, String> uploadUrls = generateUploadUrls(awsservice, siteId, documentId, item, documents);
    Map<String, Object> map = buildResponse(siteId, documentId, documents, uploadUrls);
    ApiResponseStatus status = isUpdate ? SC_OK : SC_CREATED;
    return new ApiRequestHandlerResponse(status, new ApiMapResponse(map));
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) NotFoundException(com.formkiq.lambda.apigateway.exception.NotFoundException) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiResponseStatus(com.formkiq.lambda.apigateway.ApiResponseStatus) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) BadException(com.formkiq.lambda.apigateway.exception.BadException) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse)

Example 3 with ApiMapResponse

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

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

the class PublicWebhooksRequestHandler method buildResponse.

private ApiRequestHandlerResponse buildResponse(final ApiGatewayRequestEvent event, final DynamicObject item) {
    String body = item.getString("content");
    String documentId = item.getString("documentId");
    String contentType = item.getString("contentType");
    ApiRequestHandlerResponse response = new ApiRequestHandlerResponse(SC_OK, new ApiMapResponse(Map.of("documentId", documentId)));
    String redirectUri = getParameter(event, "redirect_uri");
    if ("application/x-www-form-urlencoded".equals(contentType) && StringUtils.isNotBlank(redirectUri)) {
        response = buildRedirect(event, redirectUri, body);
    } else if (StringUtils.isNotBlank(redirectUri)) {
        response = new ApiRequestHandlerResponse(MOVED_PERMANENTLY, new ApiRedirectResponse(redirectUri));
    }
    return response;
}
Also used : ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) ApiRedirectResponse(com.formkiq.lambda.apigateway.ApiRedirectResponse) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse)

Example 5 with ApiMapResponse

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

the class SitesRequestHandler method get.

@Override
public ApiRequestHandlerResponse get(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
    List<DynamicObject> sites = authorizer.getSiteIds().stream().map(siteId -> {
        DynamicObject config = awsservice.config(siteId);
        config.put("siteId", siteId != null ? siteId : DEFAULT_SITE_ID);
        return config;
    }).collect(Collectors.toList());
    sites.forEach(ob -> {
        ob.remove("PK");
        ob.remove("SK");
    });
    updateUploadEmail(logger, awsservice, authorizer, sites);
    return new ApiRequestHandlerResponse(SC_OK, new ApiMapResponse(Map.of("sites", sites)));
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiGatewayRequestHandler(com.formkiq.lambda.apigateway.ApiGatewayRequestHandler) DEFAULT_SITE_ID(com.formkiq.stacks.dynamodb.SiteIdKeyGenerator.DEFAULT_SITE_ID) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse) Random(java.util.Random) AwsServiceCache(com.formkiq.lambda.apigateway.AwsServiceCache) ApiAuthorizer(com.formkiq.lambda.apigateway.ApiAuthorizer) Collectors(java.util.stream.Collectors) 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) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) SC_OK(com.formkiq.lambda.apigateway.ApiResponseStatus.SC_OK) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse)

Aggregations

ApiMapResponse (com.formkiq.lambda.apigateway.ApiMapResponse)11 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)11 BadException (com.formkiq.lambda.apigateway.exception.BadException)5 NotFoundException (com.formkiq.lambda.apigateway.exception.NotFoundException)5 DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)5 HashMap (java.util.HashMap)5 LambdaLogger (com.amazonaws.services.lambda.runtime.LambdaLogger)4 ApiAuthorizer (com.formkiq.lambda.apigateway.ApiAuthorizer)4 ApiGatewayRequestEvent (com.formkiq.lambda.apigateway.ApiGatewayRequestEvent)4 ApiGatewayRequestEventUtil (com.formkiq.lambda.apigateway.ApiGatewayRequestEventUtil)4 ApiGatewayRequestHandler (com.formkiq.lambda.apigateway.ApiGatewayRequestHandler)4 SC_OK (com.formkiq.lambda.apigateway.ApiResponseStatus.SC_OK)4 AwsServiceCache (com.formkiq.lambda.apigateway.AwsServiceCache)4 List (java.util.List)4 Map (java.util.Map)4 Collectors (java.util.stream.Collectors)4 ApiPagination (com.formkiq.lambda.apigateway.ApiPagination)3 DocumentItem (com.formkiq.stacks.dynamodb.DocumentItem)3 DynamicDocumentItem (com.formkiq.stacks.dynamodb.DynamicDocumentItem)3 DEFAULT_SITE_ID (com.formkiq.stacks.dynamodb.SiteIdKeyGenerator.DEFAULT_SITE_ID)3