use of com.yahoo.elide.core.exceptions.InvalidValueException in project elide by yahoo.
the class CollectionTerminalState method createObject.
private PersistentResource createObject(RequestScope requestScope) throws ForbiddenAccessException, InvalidObjectIdentifierException {
JsonApiDocument doc = requestScope.getJsonApiDocument();
JsonApiMapper mapper = requestScope.getMapper();
if (doc.getData() == null) {
throw new InvalidEntityBodyException("Invalid JSON-API document: " + doc);
}
Data<Resource> data = doc.getData();
Collection<Resource> resources = data.get();
Resource resource = (resources.size() == 1) ? IterableUtils.first(resources) : null;
if (resource == null) {
try {
throw new InvalidEntityBodyException(mapper.writeJsonApiDocument(doc));
} catch (JsonProcessingException e) {
throw new InternalServerErrorException(e);
}
}
String id = resource.getId();
Type<?> newObjectClass = requestScope.getDictionary().getEntityClass(resource.getType(), requestScope.getApiVersion());
if (newObjectClass == null) {
throw new UnknownEntityException("Entity " + resource.getType() + " not found");
}
if (!entityClass.isAssignableFrom(newObjectClass)) {
throw new InvalidValueException("Cannot assign value of type: " + resource.getType() + " to type: " + entityClass);
}
PersistentResource pResource = PersistentResource.createObject(parent.orElse(null), relationName.orElse(null), newObjectClass, requestScope, Optional.ofNullable(id));
Map<String, Object> attributes = resource.getAttributes();
if (attributes != null) {
for (Map.Entry<String, Object> entry : attributes.entrySet()) {
String fieldName = entry.getKey();
Object val = entry.getValue();
pResource.updateAttribute(fieldName, val);
}
}
Map<String, Relationship> relationships = resource.getRelationships();
if (relationships != null) {
for (Map.Entry<String, Relationship> entry : relationships.entrySet()) {
String fieldName = entry.getKey();
Relationship relationship = entry.getValue();
Set<PersistentResource> resourceSet = (relationship == null) ? null : relationship.toPersistentResources(requestScope);
pResource.updateRelation(fieldName, resourceSet);
}
}
return pResource;
}
use of com.yahoo.elide.core.exceptions.InvalidValueException in project elide by yahoo.
the class FilterPredicatePushdownExtractorTest method testInvalidField.
@Test
public void testInvalidField() {
InvalidValueException e = assertThrows(InvalidValueException.class, () -> new InPredicate(new Path(Book.class, dictionary, "badfield"), "Literary Fiction"));
assertEquals("Invalid value: book does not contain the field badfield", e.getMessage());
}
use of com.yahoo.elide.core.exceptions.InvalidValueException in project elide by yahoo.
the class SearchDataTransaction method canSearch.
private FilterSupport canSearch(Type<?> entityClass, FilterExpression expression) {
/* Collapse the filter expression to a list of leaf predicates */
Collection<FilterPredicate> predicates = expression.accept(new PredicateExtractionVisitor());
/* Return the least support among all the predicates */
FilterSupport support = predicates.stream().map((predicate) -> canSearch(entityClass, predicate)).max(Comparator.comparing(Enum::ordinal)).orElse(FilterSupport.NONE);
if (support == FilterSupport.NONE) {
return support;
}
/* Throw an exception if ngram size is violated */
predicates.stream().forEach((predicate) -> {
predicate.getValues().stream().map(Object::toString).forEach((value) -> {
if (value.length() < minNgram || value.length() > maxNgram) {
String message = String.format("Field values for %s on entity %s must be >= %d and <= %d", predicate.getField(), dictionary.getJsonAliasFor(entityClass), minNgram, maxNgram);
throw new InvalidValueException(predicate.getValues(), message);
}
});
});
return support;
}
use of com.yahoo.elide.core.exceptions.InvalidValueException in project elide by yahoo.
the class GraphQLEntityProjectionMaker method addSorting.
/**
* Creates a {@link Sorting} object from sorting GraphQL argument value and attaches it to the entity sorted
* according to the newly created {@link Sorting} object.
*
* @param argument An argument that contains the value of sorting spec
* @param projectionBuilder projection that is being built
*/
private void addSorting(Argument argument, EntityProjectionBuilder projectionBuilder) {
String sortRule = (String) variableResolver.resolveValue(argument.getValue());
try {
Sorting sorting = SortingImpl.parseSortRule(sortRule, projectionBuilder.getType(), projectionBuilder.getAttributes(), entityDictionary);
projectionBuilder.sorting(sorting);
} catch (InvalidValueException e) {
throw new BadRequestException("Invalid sorting clause " + sortRule + " for type " + entityDictionary.getJsonAliasFor(projectionBuilder.getType()));
}
}
Aggregations