use of com.yahoo.elide.jsonapi.models.JsonApiDocument in project elide by yahoo.
the class CollectionTerminalState method handlePost.
@Override
public Supplier<Pair<Integer, JsonNode>> handlePost(StateContext state) {
RequestScope requestScope = state.getRequestScope();
JsonApiMapper mapper = requestScope.getMapper();
newObject = createObject(requestScope);
parent.ifPresent(persistentResource -> persistentResource.addRelation(relationName.get(), newObject));
return () -> {
JsonApiDocument returnDoc = new JsonApiDocument();
returnDoc.setData(new Data<>(newObject.toResource()));
JsonNode responseBody = mapper.getObjectMapper().convertValue(returnDoc, JsonNode.class);
return Pair.of(HttpStatus.SC_CREATED, responseBody);
};
}
use of com.yahoo.elide.jsonapi.models.JsonApiDocument in project elide by yahoo.
the class CollectionTerminalState method handleGet.
@Override
public Supplier<Pair<Integer, JsonNode>> handleGet(StateContext state) {
JsonApiDocument jsonApiDocument = new JsonApiDocument();
RequestScope requestScope = state.getRequestScope();
MultivaluedMap<String, String> queryParams = requestScope.getQueryParams();
Set<PersistentResource> collection = getResourceCollection(requestScope).toList(LinkedHashSet::new).blockingGet();
// Set data
jsonApiDocument.setData(getData(collection, requestScope.getDictionary()));
// Run include processor
DocumentProcessor includedProcessor = new IncludedProcessor();
includedProcessor.execute(jsonApiDocument, collection, queryParams);
Pagination pagination = parentProjection.getPagination();
if (parent.isPresent()) {
pagination = parentProjection.getRelationship(relationName.orElseThrow(IllegalStateException::new)).get().getProjection().getPagination();
}
// Add pagination meta data
if (!pagination.isDefaultInstance()) {
Map<String, Number> pageMetaData = new HashMap<>();
pageMetaData.put("number", (pagination.getOffset() / pagination.getLimit()) + 1);
pageMetaData.put("limit", pagination.getLimit());
// Get total records if it has been requested and add to the page meta data
if (pagination.returnPageTotals()) {
Long totalRecords = pagination.getPageTotals();
pageMetaData.put("totalPages", totalRecords / pagination.getLimit() + ((totalRecords % pagination.getLimit()) > 0 ? 1 : 0));
pageMetaData.put("totalRecords", totalRecords);
}
Map<String, Object> allMetaData = new HashMap<>();
allMetaData.put("page", pageMetaData);
Meta meta = new Meta(allMetaData);
jsonApiDocument.setMeta(meta);
}
JsonNode responseBody = requestScope.getMapper().toJsonObject(jsonApiDocument);
return () -> Pair.of(HttpStatus.SC_OK, responseBody);
}
use of com.yahoo.elide.jsonapi.models.JsonApiDocument in project elide by yahoo.
the class RecordTerminalState method handlePatch.
@Override
public Supplier<Pair<Integer, JsonNode>> handlePatch(StateContext state) {
JsonApiDocument jsonApiDocument = state.getJsonApiDocument();
Data<Resource> data = jsonApiDocument.getData();
if (data == null) {
throw new InvalidEntityBodyException("Expected data but found null");
}
if (!data.isToOne()) {
throw new InvalidEntityBodyException("Expected single element but found list");
}
Resource resource = data.getSingleValue();
if (!record.matchesId(resource.getId())) {
throw new InvalidEntityBodyException("Id in response body does not match requested id to update from path");
}
patch(resource, state.getRequestScope());
return constructPatchResponse(record, state);
}
use of com.yahoo.elide.jsonapi.models.JsonApiDocument in project elide by yahoo.
the class JsonApiPatch method handleRemoveOp.
/**
* Remove data via patch extension.
*/
private Supplier<Pair<Integer, JsonNode>> handleRemoveOp(String path, JsonNode patchValue, PatchRequestScope requestScope) {
try {
JsonApiDocument value = requestScope.getMapper().readJsonApiPatchExtValue(patchValue);
String fullPath;
if (path.contains("relationships")) {
// Reserved keyword for relationships
fullPath = path;
} else {
Data<Resource> data = value.getData();
if (data == null || data.get() == null) {
fullPath = path;
} else {
Collection<Resource> resources = data.get();
String id = getSingleResource(resources).getId();
fullPath = path + "/" + id;
}
}
DeleteVisitor visitor = new DeleteVisitor(new PatchRequestScope(path, value, requestScope));
return visitor.visit(JsonApiParser.parse(fullPath));
} catch (IOException e) {
throw new InvalidEntityBodyException("Could not parse patch extension value: " + patchValue);
}
}
use of com.yahoo.elide.jsonapi.models.JsonApiDocument in project elide by yahoo.
the class Elide method get.
/**
* Handle GET.
*
* @param baseUrlEndPoint base URL with prefix endpoint
* @param path the path
* @param queryParams the query params
* @param requestHeaders the request headers
* @param opaqueUser the opaque user
* @param apiVersion the API version
* @param requestId the request ID
* @return Elide response object
*/
public ElideResponse get(String baseUrlEndPoint, String path, MultivaluedMap<String, String> queryParams, Map<String, List<String>> requestHeaders, User opaqueUser, String apiVersion, UUID requestId) {
if (elideSettings.isStrictQueryParams()) {
try {
verifyQueryParams(queryParams);
} catch (BadRequestException e) {
return buildErrorResponse(e, false);
}
}
return handleRequest(true, opaqueUser, dataStore::beginReadTransaction, requestId, (tx, user) -> {
JsonApiDocument jsonApiDoc = new JsonApiDocument();
RequestScope requestScope = new RequestScope(baseUrlEndPoint, path, apiVersion, jsonApiDoc, tx, user, queryParams, requestHeaders, requestId, elideSettings);
requestScope.setEntityProjection(new EntityProjectionMaker(elideSettings.getDictionary(), requestScope).parsePath(path));
BaseVisitor visitor = new GetVisitor(requestScope);
return visit(path, requestScope, visitor);
});
}
Aggregations