Search in sources :

Example 1 with ParametersDeserializationException

use of io.crnk.core.exception.ParametersDeserializationException in project crnk-framework by crnk-project.

the class DefaultQuerySpecDeserializer method deserializeFilter.

protected void deserializeFilter(QuerySpec querySpec, Parameter parameter) {
    Class<?> attributeType = getAttributeType(querySpec, parameter.attributePath);
    Set<Object> typedValues = new HashSet<>();
    for (String stringValue : parameter.values) {
        try {
            @SuppressWarnings({ "unchecked", "rawtypes" }) Object value = typeParser.parse(stringValue, (Class) attributeType);
            typedValues.add(value);
        } catch (ParserException e) {
            if (ignoreParseExceptions) {
                typedValues.add(stringValue);
                LOGGER.debug("failed to parse {}", parameter);
            } else {
                throw new ParametersDeserializationException(parameter.toString(), e);
            }
        }
    }
    Object value = typedValues.size() == 1 ? typedValues.iterator().next() : typedValues;
    querySpec.addFilter(new FilterSpec(parameter.attributePath, parameter.operator, value));
}
Also used : ParserException(io.crnk.core.engine.parser.ParserException) ParametersDeserializationException(io.crnk.core.exception.ParametersDeserializationException) HashSet(java.util.HashSet)

Example 2 with ParametersDeserializationException

use of io.crnk.core.exception.ParametersDeserializationException in project crnk-framework by crnk-project.

the class DefaultQueryParamsParser method buildPropertyListFromEntry.

protected static List<String> buildPropertyListFromEntry(Map.Entry<String, Set<String>> entry, String prefix) {
    String entryKey = entry.getKey().substring(prefix.length());
    String pattern = "[^\\]\\[]+(?<!\\[)(?=\\])";
    Pattern regexp = Pattern.compile(pattern);
    Matcher matcher = regexp.matcher(entryKey);
    List<String> matchList = new LinkedList<>();
    while (matcher.find()) {
        matchList.add(matcher.group());
    }
    if (matchList.isEmpty()) {
        throw new ParametersDeserializationException("Malformed filter legacy: " + entryKey);
    }
    return matchList;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ParametersDeserializationException(io.crnk.core.exception.ParametersDeserializationException)

Example 3 with ParametersDeserializationException

use of io.crnk.core.exception.ParametersDeserializationException in project crnk-framework by crnk-project.

the class DefaultQueryParamsParser method parseIncludedRelationsParameters.

/**
 * <strong>Important!</strong> Crnk implementation differs form JSON API
 * <a href="http://jsonapi.org/format/#fetching-includes">definition of includes</a>
 * in order to fit standard query legacy serializing strategy and maximize effective processing of data.
 * <p>
 * Included field set params can be send with following format: <br>
 * <strong>include[ResourceType] = "property(.property)*"</strong><br>
 * <p>
 * Examples of accepted sparse field sets of resources:
 * <ul>
 * <li>{@code GET /tasks/?include[tasks]=author}</li>
 * <li>{@code GET /tasks/?include[tasks][]=author&include[tasks][]=comments}</li>
 * <li>{@code GET /projects/?include[projects]=task&include[tasks]=comments}</li>
 * </ul>
 *
 * @param context Don't know, didn't write the code
 * @return {@link TypedParams} Map of sparse field set params passed to a request grouped by type of document
 */
protected TypedParams<IncludedRelationsParams> parseIncludedRelationsParameters(QueryParamsParserContext context) {
    String includeKey = RestrictedQueryParamsMembers.include.name();
    Map<String, Set<String>> inclusions = filterQueryParamsByKey(context, includeKey);
    Map<String, Set<Inclusion>> temporaryInclusionsMap = new LinkedHashMap<>();
    for (Map.Entry<String, Set<String>> entry : inclusions.entrySet()) {
        List<String> propertyList = buildPropertyListFromEntry(entry, includeKey);
        if (propertyList.size() > 1) {
            throw new ParametersDeserializationException("Exceeded maximum level of nesting of 'include' " + "legacy (1)");
        }
        String resourceType = propertyList.get(0);
        Set<Inclusion> resourceParams;
        if (temporaryInclusionsMap.containsKey(resourceType)) {
            resourceParams = temporaryInclusionsMap.get(resourceType);
        } else {
            resourceParams = new LinkedHashSet<>();
        }
        for (String path : entry.getValue()) {
            resourceParams.add(new Inclusion(path));
        }
        temporaryInclusionsMap.put(resourceType, resourceParams);
    }
    Map<String, IncludedRelationsParams> decodedInclusions = new LinkedHashMap<>();
    for (Map.Entry<String, Set<Inclusion>> resourceTypesMap : temporaryInclusionsMap.entrySet()) {
        Set<Inclusion> inclusionSet = Collections.unmodifiableSet(resourceTypesMap.getValue());
        decodedInclusions.put(resourceTypesMap.getKey(), new IncludedRelationsParams(inclusionSet));
    }
    return new TypedParams<>(Collections.unmodifiableMap(decodedInclusions));
}
Also used : Inclusion(io.crnk.legacy.queryParams.include.Inclusion) ParametersDeserializationException(io.crnk.core.exception.ParametersDeserializationException)

Aggregations

ParametersDeserializationException (io.crnk.core.exception.ParametersDeserializationException)3 ParserException (io.crnk.core.engine.parser.ParserException)1 Inclusion (io.crnk.legacy.queryParams.include.Inclusion)1 HashSet (java.util.HashSet)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1