Java Code Examples for javax.validation.Path#MethodNode

The following examples show how to use javax.validation.Path#MethodNode . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConstraintViolationExceptionMapper.java    From microservices-springboot with MIT License 5 votes vote down vote up
/**
 * Handle an invalid parameter. Can be:
 * <p>
 * 1. Invalid request parameter (annotated method parameter)
 * 2. Invalid request entity (annotated method parameter)
 *
 * @param constraintViolation
 */
private ValidationError handleInvalidParameter(ConstraintViolation constraintViolation) {

    List<Path.Node> nodes = new ArrayList<>();
    constraintViolation.getPropertyPath().iterator().forEachRemaining(nodes::add);

    Path.Node parent = nodes.get(nodes.size() - 2);
    Path.Node child = nodes.get(nodes.size() - 1);

    if (ElementKind.METHOD == parent.getKind()) {

        Path.MethodNode methodNode = parent.as(Path.MethodNode.class);
        Path.ParameterNode parameterNode = child.as(Path.ParameterNode.class);

        try {

            // Can be an invalid request parameter (annotated method parameter)
            Class<?> beanClass = constraintViolation.getLeafBean().getClass();
            Method method = beanClass.getMethod(methodNode.getName(), methodNode.getParameterTypes().stream().toArray(Class[]::new));
            Annotation[] annotations = method.getParameterAnnotations()[parameterNode.getParameterIndex()];
            Optional<ParameterDetails> optionalParameterDetails = getParameterDetails(annotations);
            if (optionalParameterDetails.isPresent()) {
                return createErrorForParameter(optionalParameterDetails.get(), constraintViolation);
            }

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

        // Assumes that the request entity is invalid (annotated method parameter)
        ValidationError error = new ValidationError();
        error.setType(REQUEST_ENTITY);
        error.setMessage(constraintViolation.getMessage());
        return error;
    }

    return handleUnknownSource(constraintViolation);
}
 
Example 2
Source File: ValidationExceptionMapper.java    From seed with Mozilla Public License 2.0 5 votes vote down vote up
private Method resolveMethod(Class<?> resourceClass, Path.MethodNode methodNode) {
    List<Class<?>> parameterTypes = methodNode.getParameterTypes();
    try {
        return resourceClass.getMethod(
                methodNode.getName(),
                parameterTypes.toArray(new Class[parameterTypes.size()])
        );
    } catch (NoSuchMethodException e) {
        return null;
    }
}
 
Example 3
Source File: ConstraintViolations.java    From krazo with Apache License 2.0 4 votes vote down vote up
private static Annotation[] getParameterAnnotations(ConstraintViolation<?> violation, Path.MethodNode methodNode,
                                                    Path.ParameterNode parameterNode) {

    try {

        String methodName = methodNode.getName();

        int paramCount = methodNode.getParameterTypes().size();
        Class[] paramTypes = methodNode.getParameterTypes().toArray(new Class[paramCount]);

        Class<?> rootBeanClass = violation.getRootBean().getClass();
        Method method = rootBeanClass.getMethod(methodName, paramTypes);

        int parameterIndex = parameterNode.getParameterIndex();
        return method.getParameterAnnotations()[parameterIndex];

    } catch (NoSuchMethodException e) {
        throw new IllegalStateException(e);
    }

}
 
Example 4
Source File: ConstraintViolations.java    From ozark with Apache License 2.0 4 votes vote down vote up
private static Annotation[] getParameterAnnotations(ConstraintViolation<?> violation, Path.MethodNode methodNode,
                                                    Path.ParameterNode parameterNode) {

    try {

        String methodName = methodNode.getName();

        int paramCount = methodNode.getParameterTypes().size();
        Class[] paramTypes = methodNode.getParameterTypes().toArray(new Class[paramCount]);

        Class<?> rootBeanClass = violation.getRootBean().getClass();
        Method method = rootBeanClass.getMethod(methodName, paramTypes);

        int parameterIndex = parameterNode.getParameterIndex();
        return method.getParameterAnnotations()[parameterIndex];

    } catch (NoSuchMethodException e) {
        throw new IllegalStateException(e);
    }

}
 
Example 5
Source File: ConstraintViolations.java    From krazo with Apache License 2.0 3 votes vote down vote up
private static Annotation[] getAnnotations(ConstraintViolation<?> violation) {


        // create a simple list of nodes from the path
        List<Path.Node> nodes = new ArrayList<>();
        for (Path.Node node : violation.getPropertyPath()) {
            nodes.add(node);
        }
        Path.Node lastNode = nodes.get(nodes.size() - 1);

        // the path refers to some property of the leaf bean
        if (lastNode.getKind() == ElementKind.PROPERTY) {

            Path.PropertyNode propertyNode = lastNode.as(Path.PropertyNode.class);
            return getPropertyAnnotations(violation, propertyNode);

        }

        // The path refers to a method parameter
        else if (lastNode.getKind() == ElementKind.PARAMETER && nodes.size() == 2) {

            Path.MethodNode methodNode = nodes.get(0).as(Path.MethodNode.class);
            Path.ParameterNode parameterNode = nodes.get(1).as(Path.ParameterNode.class);

            return getParameterAnnotations(violation, methodNode, parameterNode);

        }


        log.warning("Could not read annotations for path: " + violation.getPropertyPath().toString());
        return new Annotation[0];

    }
 
Example 6
Source File: ConstraintViolations.java    From ozark with Apache License 2.0 3 votes vote down vote up
private static Annotation[] getAnnotations(ConstraintViolation<?> violation) {


        // create a simple list of nodes from the path
        List<Path.Node> nodes = new ArrayList<>();
        for (Path.Node node : violation.getPropertyPath()) {
            nodes.add(node);
        }
        Path.Node lastNode = nodes.get(nodes.size() - 1);

        // the path refers to some property of the leaf bean
        if (lastNode.getKind() == ElementKind.PROPERTY) {

            Path.PropertyNode propertyNode = lastNode.as(Path.PropertyNode.class);
            return getPropertyAnnotations(violation, propertyNode);

        }

        // The path refers to a method parameter
        else if (lastNode.getKind() == ElementKind.PARAMETER && nodes.size() == 2) {

            Path.MethodNode methodNode = nodes.get(0).as(Path.MethodNode.class);
            Path.ParameterNode parameterNode = nodes.get(1).as(Path.ParameterNode.class);

            return getParameterAnnotations(violation, methodNode, parameterNode);

        }


        log.warning("Could not read annotations for path: " + violation.getPropertyPath().toString());
        return new Annotation[0];

    }