Search in sources :

Example 1 with PostVisitor

use of com.yahoo.elide.jsonapi.parser.PostVisitor in project elide by yahoo.

the class Elide method post.

/**
 * Handle POST.
 *
 * @param baseUrlEndPoint base URL with prefix endpoint
 * @param path the path
 * @param jsonApiDocument the json api document
 * @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 post(String baseUrlEndPoint, String path, String jsonApiDocument, MultivaluedMap<String, String> queryParams, Map<String, List<String>> requestHeaders, User opaqueUser, String apiVersion, UUID requestId) {
    return handleRequest(false, opaqueUser, dataStore::beginTransaction, requestId, (tx, user) -> {
        JsonApiDocument jsonApiDoc = mapper.readJsonApiDocument(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 PostVisitor(requestScope);
        return visit(path, requestScope, visitor);
    });
}
Also used : JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) BaseVisitor(com.yahoo.elide.jsonapi.parser.BaseVisitor) EntityProjectionMaker(com.yahoo.elide.jsonapi.EntityProjectionMaker) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) RequestScope(com.yahoo.elide.core.RequestScope) PostVisitor(com.yahoo.elide.jsonapi.parser.PostVisitor)

Example 2 with PostVisitor

use of com.yahoo.elide.jsonapi.parser.PostVisitor in project elide by yahoo.

the class JsonApiPatch method handleAddOp.

/**
 * Add a document via patch extension.
 */
private Supplier<Pair<Integer, JsonNode>> handleAddOp(String path, JsonNode patchValue, PatchRequestScope requestScope, PatchAction action) {
    try {
        JsonApiDocument value = requestScope.getMapper().readJsonApiPatchExtValue(patchValue);
        Data<Resource> data = value.getData();
        if (data == null || data.get() == null) {
            throw new InvalidEntityBodyException("Expected an entity body but received none.");
        }
        Collection<Resource> resources = data.get();
        if (!path.contains("relationships")) {
            // Reserved key for relationships
            String id = getSingleResource(resources).getId();
            if (StringUtils.isEmpty(id)) {
                throw new InvalidEntityBodyException("Patch extension requires all objects to have an assigned " + "ID (temporary or permanent) when assigning relationships.");
            }
            String fullPath = path + "/" + id;
            // Defer relationship updating until the end
            getSingleResource(resources).setRelationships(null);
            // Reparse since we mangle it first
            action.doc = requestScope.getMapper().readJsonApiPatchExtValue(patchValue);
            action.path = fullPath;
            action.isPostProcessing = true;
        }
        PostVisitor visitor = new PostVisitor(new PatchRequestScope(path, value, requestScope));
        return visitor.visit(JsonApiParser.parse(path));
    } catch (HttpStatusException e) {
        action.cause = e;
        throw e;
    } catch (IOException e) {
        throw new InvalidEntityBodyException("Could not parse patch extension value: " + patchValue);
    }
}
Also used : InvalidEntityBodyException(com.yahoo.elide.core.exceptions.InvalidEntityBodyException) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Resource(com.yahoo.elide.jsonapi.models.Resource) HttpStatusException(com.yahoo.elide.core.exceptions.HttpStatusException) IOException(java.io.IOException) PostVisitor(com.yahoo.elide.jsonapi.parser.PostVisitor)

Aggregations

JsonApiDocument (com.yahoo.elide.jsonapi.models.JsonApiDocument)2 PostVisitor (com.yahoo.elide.jsonapi.parser.PostVisitor)2 RequestScope (com.yahoo.elide.core.RequestScope)1 HttpStatusException (com.yahoo.elide.core.exceptions.HttpStatusException)1 InvalidEntityBodyException (com.yahoo.elide.core.exceptions.InvalidEntityBodyException)1 EntityProjectionMaker (com.yahoo.elide.jsonapi.EntityProjectionMaker)1 PatchRequestScope (com.yahoo.elide.jsonapi.extensions.PatchRequestScope)1 Resource (com.yahoo.elide.jsonapi.models.Resource)1 BaseVisitor (com.yahoo.elide.jsonapi.parser.BaseVisitor)1 IOException (java.io.IOException)1