Search in sources :

Example 11 with ApiRequestHandlerResponse

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

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

Example 13 with ApiRequestHandlerResponse

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

the class VersionRequestHandler method get.

@Override
public ApiRequestHandlerResponse get(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
    String key = "/formkiq/" + awsservice.appEnvironment() + "/version";
    String version = awsservice.ssmService().getParameterValue(key);
    return new ApiRequestHandlerResponse(SC_OK, new ApiMapResponse(Map.of("version", version, "type", awsservice.formkiqType())));
}
Also used : ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse)

Example 14 with ApiRequestHandlerResponse

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

the class WebhooksIdRequestHandler method patch.

@Override
public ApiRequestHandlerResponse patch(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsServices) throws Exception {
    String siteId = authorizer.getSiteId();
    String id = getPathParameter(event, "webhookId");
    WebhooksService webhookService = awsServices.webhookService();
    if (webhookService.findWebhook(siteId, id) == null) {
        throw new NotFoundException("Webhook 'id' not found");
    }
    DynamicObject obj = fromBodyToDynamicObject(logger, event);
    Map<String, Object> map = new HashMap<>();
    if (obj.containsKey("name")) {
        map.put("name", obj.getString("name"));
    }
    if (obj.containsKey("enabled")) {
        map.put("enabled", obj.getBoolean("enabled"));
    }
    Date ttlDate = null;
    if (obj.containsKey("ttl")) {
        ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC).plusSeconds(Long.parseLong(obj.getString("ttl")));
        ttlDate = Date.from(now.toInstant());
        map.put("TimeToLive", ttlDate);
    }
    webhookService.updateWebhook(siteId, id, new DynamicObject(map));
    if (ttlDate != null) {
        webhookService.updateTimeToLive(siteId, id, ttlDate);
    }
    return new ApiRequestHandlerResponse(SC_OK, new ApiMessageResponse("'" + id + "' object updated"));
}
Also used : ApiMessageResponse(com.formkiq.lambda.apigateway.ApiMessageResponse) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) HashMap(java.util.HashMap) ZonedDateTime(java.time.ZonedDateTime) NotFoundException(com.formkiq.lambda.apigateway.exception.NotFoundException) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) WebhooksService(com.formkiq.stacks.dynamodb.WebhooksService) Date(java.util.Date)

Example 15 with ApiRequestHandlerResponse

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

the class WebhooksRequestHandler method get.

@Override
public ApiRequestHandlerResponse get(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsServices) throws Exception {
    String siteId = authorizer.getSiteId();
    String url = awsServices.ssmService().getParameterValue("/formkiq/" + awsServices.appEnvironment() + "/api/DocumentsPublicHttpUrl");
    List<DynamicObject> list = awsServices.webhookService().findWebhooks(siteId);
    List<Map<String, Object>> webhooks = list.stream().map(m -> {
        String path = "private".equals(m.getString("enabled")) ? "/private" : "/public";
        Map<String, Object> map = new HashMap<>();
        String u = url + path + "/webhooks/" + m.getString("documentId");
        if (siteId != null && !DEFAULT_SITE_ID.equals(siteId)) {
            u += "?siteId=" + siteId;
        }
        map.put("siteId", siteId != null ? siteId : DEFAULT_SITE_ID);
        map.put("id", m.getString("documentId"));
        map.put("name", m.getString("path"));
        map.put("url", u);
        map.put("insertedDate", m.getString("inserteddate"));
        map.put("userId", m.getString("userId"));
        map.put("enabled", m.getString("enabled"));
        map.put("ttl", m.getString("ttl"));
        return map;
    }).collect(Collectors.toList());
    return new ApiRequestHandlerResponse(SC_OK, new ApiMapResponse(Map.of("webhooks", webhooks)));
}
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) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse) HashMap(java.util.HashMap) Map(java.util.Map)

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