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);
}
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);
}
}
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));
}
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));
}
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;
}
Aggregations