Search in sources :

Example 1 with PatchVisitor

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

the class Elide method patch.

/**
 * Handle PATCH.
 *
 * @param baseUrlEndPoint base URL with prefix endpoint
 * @param contentType the content type
 * @param accept the accept
 * @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 patch(String baseUrlEndPoint, String contentType, String accept, String path, String jsonApiDocument, MultivaluedMap<String, String> queryParams, Map<String, List<String>> requestHeaders, User opaqueUser, String apiVersion, UUID requestId) {
    Handler<DataStoreTransaction, User, HandlerResult> handler;
    if (JsonApiPatch.isPatchExtension(contentType) && JsonApiPatch.isPatchExtension(accept)) {
        handler = (tx, user) -> {
            PatchRequestScope requestScope = new PatchRequestScope(baseUrlEndPoint, path, apiVersion, tx, user, requestId, queryParams, requestHeaders, elideSettings);
            try {
                Supplier<Pair<Integer, JsonNode>> responder = JsonApiPatch.processJsonPatch(dataStore, path, jsonApiDocument, requestScope);
                return new HandlerResult(requestScope, responder);
            } catch (RuntimeException e) {
                return new HandlerResult(requestScope, e);
            }
        };
    } else {
        handler = (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 PatchVisitor(requestScope);
            return visit(path, requestScope, visitor);
        };
    }
    return handleRequest(false, opaqueUser, dataStore::beginTransaction, requestId, handler);
}
Also used : PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) User(com.yahoo.elide.core.security.User) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) PatchVisitor(com.yahoo.elide.jsonapi.parser.PatchVisitor) EntityProjectionMaker(com.yahoo.elide.jsonapi.EntityProjectionMaker) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) RequestScope(com.yahoo.elide.core.RequestScope) BaseVisitor(com.yahoo.elide.jsonapi.parser.BaseVisitor) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) Pair(org.apache.commons.lang3.tuple.Pair)

Example 2 with PatchVisitor

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

the class JsonApiPatch method handleReplaceOp.

/**
 * Replace data via patch extension.
 */
private Supplier<Pair<Integer, JsonNode>> handleReplaceOp(String path, JsonNode patchVal, PatchRequestScope requestScope, PatchAction action) {
    try {
        JsonApiDocument value = requestScope.getMapper().readJsonApiPatchExtValue(patchVal);
        if (!path.contains("relationships")) {
            // Reserved
            Data<Resource> data = value.getData();
            Collection<Resource> resources = data.get();
            // Defer relationship updating until the end
            getSingleResource(resources).setRelationships(null);
            // Reparse since we mangle it first
            action.doc = requestScope.getMapper().readJsonApiPatchExtValue(patchVal);
            action.path = path;
            action.isPostProcessing = true;
        }
        // Defer relationship updating until the end
        PatchVisitor visitor = new PatchVisitor(new PatchRequestScope(path, value, requestScope));
        return visitor.visit(JsonApiParser.parse(path));
    } catch (IOException e) {
        throw new InvalidEntityBodyException("Could not parse patch extension value: " + patchVal);
    }
}
Also used : InvalidEntityBodyException(com.yahoo.elide.core.exceptions.InvalidEntityBodyException) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Resource(com.yahoo.elide.jsonapi.models.Resource) PatchVisitor(com.yahoo.elide.jsonapi.parser.PatchVisitor) IOException(java.io.IOException)

Aggregations

JsonApiDocument (com.yahoo.elide.jsonapi.models.JsonApiDocument)2 PatchVisitor (com.yahoo.elide.jsonapi.parser.PatchVisitor)2 RequestScope (com.yahoo.elide.core.RequestScope)1 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)1 InvalidEntityBodyException (com.yahoo.elide.core.exceptions.InvalidEntityBodyException)1 User (com.yahoo.elide.core.security.User)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 Pair (org.apache.commons.lang3.tuple.Pair)1