use of com.formkiq.lambda.apigateway.ApiGatewayRequestEvent in project formkiq-core by formkiq.
the class ApiDocumentsTagsRequestTest method testHandleGetTags03.
/**
* GET /documents/{documentId}/tags/{tagKey} request. Tag and NO value found.
*
* @throws Exception an error has occurred
*/
@SuppressWarnings("unchecked")
@Test
public void testHandleGetTags03() throws Exception {
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
String documentId = UUID.randomUUID().toString();
DocumentItemDynamoDb item = new DocumentItemDynamoDb(documentId, new Date(), "jsmith");
DocumentTag tag = new DocumentTag(documentId, "category", null, new Date(), "jsmith");
getDocumentService().saveDocument(siteId, item, Arrays.asList(tag));
ApiGatewayRequestEvent event = toRequestEvent("/request-get-documents-documentid-tags01.json");
addParameter(event, "siteId", siteId);
setPathParameter(event, "documentId", documentId);
// when
String response = handleRequest(event);
// then
Map<String, String> m = GsonUtil.getInstance().fromJson(response, Map.class);
final int mapsize = 3;
assertEquals(mapsize, m.size());
assertEquals("200.0", String.valueOf(m.get("statusCode")));
assertEquals(getHeaders(), "\"headers\":" + GsonUtil.getInstance().toJson(m.get("headers")));
ApiDocumentTagItemResponse resp = GsonUtil.getInstance().fromJson(m.get("body"), ApiDocumentTagItemResponse.class);
assertEquals("category", resp.getKey());
assertEquals("", resp.getValue());
}
}
use of com.formkiq.lambda.apigateway.ApiGatewayRequestEvent 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.ApiGatewayRequestEvent 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.ApiGatewayRequestEvent 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.ApiGatewayRequestEvent 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)));
}
Aggregations