Search in sources :

Example 6 with BadException

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

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

the class DocumentTagRequestHandler method put.

@SuppressWarnings("unchecked")
@Override
public ApiRequestHandlerResponse put(final LambdaLogger logger, final ApiGatewayRequestEvent event, final ApiAuthorizer authorizer, final AwsServiceCache awsservice) throws Exception {
    Map<String, String> map = event.getPathParameters();
    final String documentId = map.get("documentId");
    final String tagKey = map.get("tagKey");
    Map<String, Object> body = fromBodyToObject(logger, event, Map.class);
    String value = body != null ? (String) body.getOrDefault("value", null) : null;
    List<String> values = body != null ? (List<String>) body.getOrDefault("values", null) : null;
    if (value == null && values == null) {
        throw new BadException("request body is invalid");
    }
    String siteId = authorizer.getSiteId();
    DocumentService documentService = awsservice.documentService();
    if (event.getHttpMethod().equalsIgnoreCase("put")) {
        if (documentService.findDocument(siteId, documentId) == null) {
            throw new NotFoundException("Document " + documentId + " not found.");
        }
    }
    Date now = new Date();
    String userId = getCallingCognitoUsername(event);
    DocumentTag tag = documentService.findDocumentTag(siteId, documentId, tagKey);
    if (tag == null) {
        throw new NotFoundException("Tag " + tagKey + " not found.");
    }
    // if trying to change from tag VALUE to VALUES or VALUES to VALUE
    if (isTagValueTypeChanged(tag, value, values)) {
        documentService.removeTags(siteId, documentId, Arrays.asList(tagKey));
    }
    tag = new DocumentTag(null, tagKey, value, now, userId);
    if (values != null) {
        tag.setValue(null);
        tag.setValues(values);
    }
    documentService.addTags(siteId, documentId, Arrays.asList(tag), null);
    ApiResponse resp = new ApiMessageResponse("Updated tag '" + tagKey + "' on document '" + documentId + "'.");
    return new ApiRequestHandlerResponse(SC_OK, resp);
}
Also used : DocumentTag(com.formkiq.stacks.dynamodb.DocumentTag) NotFoundException(com.formkiq.lambda.apigateway.exception.NotFoundException) BadException(com.formkiq.lambda.apigateway.exception.BadException) DocumentService(com.formkiq.stacks.dynamodb.DocumentService) Date(java.util.Date) ApiResponse(com.formkiq.lambda.apigateway.ApiResponse) ApiMessageResponse(com.formkiq.lambda.apigateway.ApiMessageResponse) ApiRequestHandlerResponse(com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)

Example 8 with BadException

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

Example 9 with BadException

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

the class AbstractApiRequestHandler method handleRequest.

@Override
public void handleRequest(final InputStream input, final OutputStream output, final Context context) throws IOException {
    LambdaLogger logger = context.getLogger();
    ApiGatewayRequestEvent event = getApiGatewayEvent(input, logger, getAwsServices());
    ApiAuthorizer authorizer = new ApiAuthorizer(event);
    try {
        ApiRequestHandlerResponse object = processRequest(logger, event, authorizer);
        processResponse(authorizer, event, object);
        buildResponse(logger, output, object.getStatus(), object.getHeaders(), object.getResponse());
    } catch (NotFoundException e) {
        buildResponse(logger, output, SC_NOT_FOUND, Collections.emptyMap(), new ApiResponseError(e.getMessage()));
    } catch (TooManyRequestsException e) {
        buildResponse(logger, output, SC_TOO_MANY_REQUESTS, Collections.emptyMap(), new ApiResponseError(e.getMessage()));
    } catch (BadException | InvalidConditionsException | DateTimeException e) {
        buildResponse(logger, output, SC_BAD_REQUEST, Collections.emptyMap(), new ApiResponseError(e.getMessage()));
    } catch (ForbiddenException e) {
        buildResponse(logger, output, SC_FORBIDDEN, Collections.emptyMap(), new ApiResponseError(e.getMessage()));
    } catch (UnauthorizedException e) {
        buildResponse(logger, output, SC_UNAUTHORIZED, Collections.emptyMap(), new ApiResponseError(e.getMessage()));
    } catch (NotImplementedException e) {
        buildResponse(logger, output, SC_NOT_IMPLEMENTED, Collections.emptyMap(), new ApiResponseError(e.getMessage()));
    } catch (Exception e) {
        logError(logger, e);
        buildResponse(logger, output, SC_ERROR, Collections.emptyMap(), new ApiResponseError("Internal Server Error"));
    }
}
Also used : ForbiddenException(com.formkiq.lambda.apigateway.exception.ForbiddenException) NotImplementedException(com.formkiq.lambda.apigateway.exception.NotImplementedException) NotFoundException(com.formkiq.lambda.apigateway.exception.NotFoundException) BadException(com.formkiq.lambda.apigateway.exception.BadException) ForbiddenException(com.formkiq.lambda.apigateway.exception.ForbiddenException) NotImplementedException(com.formkiq.lambda.apigateway.exception.NotImplementedException) DateTimeException(java.time.DateTimeException) UnauthorizedException(com.formkiq.lambda.apigateway.exception.UnauthorizedException) IOException(java.io.IOException) NotFoundException(com.formkiq.lambda.apigateway.exception.NotFoundException) BadException(com.formkiq.lambda.apigateway.exception.BadException) TooManyRequestsException(com.formkiq.lambda.apigateway.exception.TooManyRequestsException) InvalidConditionsException(com.formkiq.stacks.dynamodb.InvalidConditionsException) TooManyRequestsException(com.formkiq.lambda.apigateway.exception.TooManyRequestsException) InvalidConditionsException(com.formkiq.stacks.dynamodb.InvalidConditionsException) DateTimeException(java.time.DateTimeException) UnauthorizedException(com.formkiq.lambda.apigateway.exception.UnauthorizedException) LambdaLogger(com.amazonaws.services.lambda.runtime.LambdaLogger)

Aggregations

BadException (com.formkiq.lambda.apigateway.exception.BadException)9 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)7 NotFoundException (com.formkiq.lambda.apigateway.exception.NotFoundException)5 DocumentTag (com.formkiq.stacks.dynamodb.DocumentTag)5 Date (java.util.Date)5 ApiMessageResponse (com.formkiq.lambda.apigateway.ApiMessageResponse)3 ApiResponse (com.formkiq.lambda.apigateway.ApiResponse)3 DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)3 DocumentService (com.formkiq.stacks.dynamodb.DocumentService)3 ApiMapResponse (com.formkiq.lambda.apigateway.ApiMapResponse)2 ApiUrlResponse (com.formkiq.stacks.api.ApiUrlResponse)2 DocumentItem (com.formkiq.stacks.dynamodb.DocumentItem)2 DocumentItemDynamoDb (com.formkiq.stacks.dynamodb.DocumentItemDynamoDb)2 ArrayList (java.util.ArrayList)2 LambdaLogger (com.amazonaws.services.lambda.runtime.LambdaLogger)1 ApiPagination (com.formkiq.lambda.apigateway.ApiPagination)1 ApiResponseStatus (com.formkiq.lambda.apigateway.ApiResponseStatus)1 ForbiddenException (com.formkiq.lambda.apigateway.exception.ForbiddenException)1 NotImplementedException (com.formkiq.lambda.apigateway.exception.NotImplementedException)1 TooManyRequestsException (com.formkiq.lambda.apigateway.exception.TooManyRequestsException)1