Search in sources :

Example 6 with InvalidValueException

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;
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) UnknownEntityException(com.yahoo.elide.core.exceptions.UnknownEntityException) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) ToString(lombok.ToString) InvalidValueException(com.yahoo.elide.core.exceptions.InvalidValueException) InvalidEntityBodyException(com.yahoo.elide.core.exceptions.InvalidEntityBodyException) Relationship(com.yahoo.elide.jsonapi.models.Relationship) InternalServerErrorException(com.yahoo.elide.core.exceptions.InternalServerErrorException) JsonApiMapper(com.yahoo.elide.jsonapi.JsonApiMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) HashMap(java.util.HashMap) Map(java.util.Map) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Example 7 with InvalidValueException

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());
}
Also used : InvalidValueException(com.yahoo.elide.core.exceptions.InvalidValueException) Path(com.yahoo.elide.core.Path) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) Test(org.junit.jupiter.api.Test)

Example 8 with InvalidValueException

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;
}
Also used : InvalidValueException(com.yahoo.elide.core.exceptions.InvalidValueException) PredicateExtractionVisitor(com.yahoo.elide.core.filter.expression.PredicateExtractionVisitor) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate)

Example 9 with InvalidValueException

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()));
    }
}
Also used : InvalidValueException(com.yahoo.elide.core.exceptions.InvalidValueException) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) Sorting(com.yahoo.elide.core.request.Sorting)

Aggregations

InvalidValueException (com.yahoo.elide.core.exceptions.InvalidValueException)9 ToString (lombok.ToString)3 Path (com.yahoo.elide.core.Path)2 PersistentResource (com.yahoo.elide.core.PersistentResource)2 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)2 PredicateExtractionVisitor (com.yahoo.elide.core.filter.expression.PredicateExtractionVisitor)2 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 RequestScope (com.yahoo.elide.core.RequestScope)1 BadRequestException (com.yahoo.elide.core.exceptions.BadRequestException)1 InternalServerErrorException (com.yahoo.elide.core.exceptions.InternalServerErrorException)1 InvalidEntityBodyException (com.yahoo.elide.core.exceptions.InvalidEntityBodyException)1 InvalidObjectIdentifierException (com.yahoo.elide.core.exceptions.InvalidObjectIdentifierException)1 UnknownEntityException (com.yahoo.elide.core.exceptions.UnknownEntityException)1 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)1 InPredicate (com.yahoo.elide.core.filter.predicates.InPredicate)1 Attribute (com.yahoo.elide.core.request.Attribute)1