Search in sources :

Example 1 with InvalidEntityBodyException

use of com.yahoo.elide.core.exceptions.InvalidEntityBodyException 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);
}
Also used : InvalidEntityBodyException(com.yahoo.elide.core.exceptions.InvalidEntityBodyException) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource)

Example 2 with InvalidEntityBodyException

use of com.yahoo.elide.core.exceptions.InvalidEntityBodyException in project elide by yahoo.

the class JsonApiPatch method processJsonPatch.

/**
 * Process json patch.
 *
 * @param dataStore the dataStore
 * @param uri the uri
 * @param patchDoc the patch doc
 * @param requestScope request scope
 * @return pair
 */
public static Supplier<Pair<Integer, JsonNode>> processJsonPatch(DataStore dataStore, String uri, String patchDoc, PatchRequestScope requestScope) {
    List<Patch> actions;
    try {
        actions = requestScope.getMapper().readJsonApiPatchExtDoc(patchDoc);
    } catch (IOException e) {
        throw new InvalidEntityBodyException(patchDoc);
    }
    JsonApiPatch processor = new JsonApiPatch(dataStore, actions, uri, requestScope);
    return processor.processActions(requestScope);
}
Also used : InvalidEntityBodyException(com.yahoo.elide.core.exceptions.InvalidEntityBodyException) IOException(java.io.IOException) Patch(com.yahoo.elide.jsonapi.models.Patch)

Example 3 with InvalidEntityBodyException

use of com.yahoo.elide.core.exceptions.InvalidEntityBodyException 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);
    }
}
Also used : InvalidEntityBodyException(com.yahoo.elide.core.exceptions.InvalidEntityBodyException) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Resource(com.yahoo.elide.jsonapi.models.Resource) IOException(java.io.IOException) DeleteVisitor(com.yahoo.elide.jsonapi.parser.DeleteVisitor)

Example 4 with InvalidEntityBodyException

use of com.yahoo.elide.core.exceptions.InvalidEntityBodyException in project elide by yahoo.

the class PersistentResource method updateToManyRelation.

/**
 * Updates a to-many relationship.
 *
 * @param fieldName           the field name
 * @param resourceIdentifiers the resource identifiers
 * @param mine                Existing, filtered relationships for field name
 * @return true if updated. false otherwise
 */
protected boolean updateToManyRelation(String fieldName, Set<PersistentResource> resourceIdentifiers, Set<PersistentResource> mine) {
    Set<PersistentResource> requested;
    Set<PersistentResource> updated;
    Set<PersistentResource> deleted;
    Set<PersistentResource> added;
    if (resourceIdentifiers == null) {
        throw new InvalidEntityBodyException("Bad relation data");
    }
    if (resourceIdentifiers.isEmpty()) {
        requested = new LinkedHashSet<>();
    } else {
        // TODO - this resource does not include a lineage. This could cause issues for audit.
        requested = resourceIdentifiers;
    }
    // deleted = mine - requested
    deleted = Sets.difference(mine, requested);
    // updated = (requested UNION mine) - (requested INTERSECT mine)
    updated = Sets.difference(Sets.union(mine, requested), Sets.intersection(mine, requested));
    added = Sets.difference(updated, deleted);
    checkTransferablePermission(added);
    Set<Object> newRelationships = new LinkedHashSet<>();
    Set<Object> deletedRelationships = new LinkedHashSet<>();
    deleted.stream().forEach(toDelete -> {
        deletedRelationships.add(toDelete.getObject());
    });
    added.stream().forEach(toAdd -> {
        newRelationships.add(toAdd.getObject());
    });
    Collection collection = (Collection) this.getValueUnchecked(fieldName);
    modifyCollection(collection, fieldName, newRelationships, deletedRelationships, true);
    if (!updated.isEmpty()) {
        this.markDirty();
    }
    // hook for updateRelation
    transaction.updateToManyRelation(transaction, obj, fieldName, newRelationships, deletedRelationships, requestScope);
    return !updated.isEmpty();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) InvalidEntityBodyException(com.yahoo.elide.core.exceptions.InvalidEntityBodyException) Collection(java.util.Collection)

Example 5 with InvalidEntityBodyException

use of com.yahoo.elide.core.exceptions.InvalidEntityBodyException in project elide by yahoo.

the class SubscriptionDataFetcher method get.

@Override
public Object get(DataFetchingEnvironment environment) throws Exception {
    OperationDefinition.Operation op = environment.getOperationDefinition().getOperation();
    if (op != OperationDefinition.Operation.SUBSCRIPTION) {
        throw new InvalidEntityBodyException(String.format("%s not supported for subscription models.", op));
    }
    /* build environment object, extracts required fields */
    Environment context = new Environment(environment, nonEntityDictionary);
    /* safe enable debugging */
    if (log.isDebugEnabled()) {
        logContext(log, RelationshipOp.FETCH, context);
    }
    if (context.isRoot()) {
        String entityName = context.field.getName();
        String aliasName = context.field.getAlias();
        EntityProjection projection = context.requestScope.getProjectionInfo().getProjection(aliasName, entityName);
        Flowable<PersistentResource> recordPublisher = PersistentResource.loadRecords(projection, new ArrayList<>(), context.requestScope).toFlowable(BackpressureStrategy.BUFFER).onBackpressureBuffer(bufferSize, true, false);
        return recordPublisher.map(SubscriptionNodeContainer::new);
    }
    // as the PersistentResourceFetcher.
    return context.container.processFetch(context);
}
Also used : EntityProjection(com.yahoo.elide.core.request.EntityProjection) PersistentResource(com.yahoo.elide.core.PersistentResource) InvalidEntityBodyException(com.yahoo.elide.core.exceptions.InvalidEntityBodyException) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) Environment(com.yahoo.elide.graphql.Environment) SubscriptionNodeContainer(com.yahoo.elide.graphql.subscriptions.containers.SubscriptionNodeContainer) OperationDefinition(graphql.language.OperationDefinition)

Aggregations

InvalidEntityBodyException (com.yahoo.elide.core.exceptions.InvalidEntityBodyException)14 Resource (com.yahoo.elide.jsonapi.models.Resource)6 IOException (java.io.IOException)6 JsonApiDocument (com.yahoo.elide.jsonapi.models.JsonApiDocument)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 PersistentResource (com.yahoo.elide.core.PersistentResource)3 HashMap (java.util.HashMap)3 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)2 HttpStatusException (com.yahoo.elide.core.exceptions.HttpStatusException)2 InvalidValueException (com.yahoo.elide.core.exceptions.InvalidValueException)2 TransactionException (com.yahoo.elide.core.exceptions.TransactionException)2 GraphQLEntityProjectionMaker (com.yahoo.elide.graphql.parser.GraphQLEntityProjectionMaker)2 GraphQLProjectionInfo (com.yahoo.elide.graphql.parser.GraphQLProjectionInfo)2 ExecutionInput (graphql.ExecutionInput)2 ExecutionResult (graphql.ExecutionResult)2 Map (java.util.Map)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 Version (com.fasterxml.jackson.core.Version)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1