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);
}
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);
}
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);
}
}
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();
}
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);
}
Aggregations