Search in sources :

Example 6 with NotFoundException

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

the class WebhooksIdRequestHandler method delete.

@Override
public ApiRequestHandlerResponse delete(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsServices) throws Exception {
    String siteId = authorizer.getSiteId();
    String id = getPathParameter(event, "webhookId");
    if (awsServices.webhookService().findWebhook(siteId, id) == null) {
        throw new NotFoundException("Webhook 'id' not found");
    }
    awsServices.webhookService().deleteWebhook(siteId, id);
    return new ApiRequestHandlerResponse(SC_OK, new ApiMessageResponse("'" + id + "' object deleted"));
}
Also used : ApiMessageResponse(com.formkiq.lambda.apigateway.ApiMessageResponse) NotFoundException(com.formkiq.lambda.apigateway.exception.NotFoundException) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)

Example 7 with NotFoundException

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

the class WebhooksIdRequestHandler 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 id = getPathParameter(event, "webhookId");
    DynamicObject m = awsServices.webhookService().findWebhook(siteId, id);
    if (m == null) {
        throw new NotFoundException("Webhook 'id' not found");
    }
    String url = awsServices.ssmService().getParameterValue("/formkiq/" + awsServices.appEnvironment() + "/api/DocumentsPublicHttpUrl");
    String path = "private".equals(m.getString("enabled")) ? "/private" : "/public";
    String u = url + path + "/webhooks/" + m.getString("documentId");
    if (siteId != null && !DEFAULT_SITE_ID.equals(siteId)) {
        u += "?siteId=" + siteId;
    }
    Map<String, Object> map = new HashMap<>();
    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 new ApiRequestHandlerResponse(SC_OK, new ApiMapResponse(map));
}
Also used : DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) HashMap(java.util.HashMap) NotFoundException(com.formkiq.lambda.apigateway.exception.NotFoundException) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) ApiMapResponse(com.formkiq.lambda.apigateway.ApiMapResponse)

Example 8 with NotFoundException

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

the class WebhooksTagsRequestHandler method post.

@Override
public ApiRequestHandlerResponse post(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsServices) throws Exception {
    DocumentTag tag = fromBodyToObject(logger, event, DocumentTag.class);
    if (tag.getKey() == null || tag.getKey().length() == 0) {
        throw new BadException("invalid json body");
    }
    String siteId = authorizer.getSiteId();
    String id = getPathParameter(event, "webhookId");
    tag.setType(DocumentTagType.USERDEFINED);
    tag.setInsertedDate(new Date());
    tag.setUserId(getCallingCognitoUsername(event));
    DynamicObject webhook = awsServices.webhookService().findWebhook(siteId, id);
    if (webhook == null) {
        throw new NotFoundException("Webhook 'id' not found");
    }
    Date ttl = null;
    String ttlString = webhook.getString("TimeToLive");
    if (ttlString != null) {
        long epoch = Long.parseLong(ttlString);
        ttl = new Date(epoch * TO_MILLIS);
    }
    awsServices.webhookService().addTags(siteId, id, Arrays.asList(tag), ttl);
    ApiResponse resp = new ApiMessageResponse("Created Tag '" + tag.getKey() + "'.");
    return new ApiRequestHandlerResponse(SC_CREATED, resp);
}
Also used : DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) ApiMessageResponse(com.formkiq.lambda.apigateway.ApiMessageResponse) DynamicObject(com.formkiq.stacks.common.objects.DynamicObject) NotFoundException(com.formkiq.lambda.apigateway.exception.NotFoundException) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse) BadException(com.formkiq.lambda.apigateway.exception.BadException) Date(java.util.Date) ApiResponse(com.formkiq.lambda.apigateway.ApiResponse)

Example 9 with NotFoundException

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

the class CoreRequestHandler method findRequestHandler.

@Override
@SuppressWarnings("returncount")
public ApiGatewayRequestHandler findRequestHandler(final String method, final String resource) throws NotFoundException {
    String s = "options".equals(method) ? method : resource;
    if (isEnablePublicUrls && "/public/documents".equals(s)) {
        return new PublicDocumentsRequestHandler();
    }
    if (s.startsWith("/public/webhooks")) {
        return new PublicWebhooksRequestHandler();
    }
    if (s.startsWith("/private/webhooks")) {
        return new PrivateWebhooksRequestHandler();
    }
    ApiGatewayRequestHandler hander = URL_MAP.get(s);
    if (hander != null) {
        return hander;
    }
    throw new NotFoundException(resource + " not found");
}
Also used : ApiGatewayRequestHandler(com.formkiq.lambda.apigateway.ApiGatewayRequestHandler) PrivateWebhooksRequestHandler(com.formkiq.stacks.api.handler.PrivateWebhooksRequestHandler) NotFoundException(com.formkiq.lambda.apigateway.exception.NotFoundException) PublicWebhooksRequestHandler(com.formkiq.stacks.api.handler.PublicWebhooksRequestHandler) PublicDocumentsRequestHandler(com.formkiq.stacks.api.handler.PublicDocumentsRequestHandler)

Example 10 with NotFoundException

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

Aggregations

NotFoundException (com.formkiq.lambda.apigateway.exception.NotFoundException)16 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)14 ApiMessageResponse (com.formkiq.lambda.apigateway.ApiMessageResponse)7 ApiResponse (com.formkiq.lambda.apigateway.ApiResponse)6 BadException (com.formkiq.lambda.apigateway.exception.BadException)5 DocumentTag (com.formkiq.stacks.dynamodb.DocumentTag)5 ApiMapResponse (com.formkiq.lambda.apigateway.ApiMapResponse)4 DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)4 DocumentItem (com.formkiq.stacks.dynamodb.DocumentItem)4 DocumentService (com.formkiq.stacks.dynamodb.DocumentService)4 Date (java.util.Date)4 ApiUrlResponse (com.formkiq.stacks.api.ApiUrlResponse)2 URL (java.net.URL)2 HashMap (java.util.HashMap)2 S3Client (software.amazon.awssdk.services.s3.S3Client)2 LambdaLogger (com.amazonaws.services.lambda.runtime.LambdaLogger)1 S3ObjectMetadata (com.formkiq.aws.s3.S3ObjectMetadata)1 S3Service (com.formkiq.aws.s3.S3Service)1 ApiGatewayRequestHandler (com.formkiq.lambda.apigateway.ApiGatewayRequestHandler)1 ApiPagination (com.formkiq.lambda.apigateway.ApiPagination)1