Java Code Examples for javax.validation.ElementKind#PARAMETER

The following examples show how to use javax.validation.ElementKind#PARAMETER . 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: AbstractMethodValidationInterceptor.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private String getMessage(Member member, Object[] args, Set<? extends ConstraintViolation<?>> violations) {

        StringBuilder message = new StringBuilder();
        message.append(violations.size());
        message.append(" constraint violation(s) occurred during method validation.");
        message.append("\nConstructor or Method: ");
        message.append(member);
        message.append("\nArgument values: ");
        message.append(Arrays.toString(args));
        message.append("\nConstraint violations: ");

        int i = 1;
        for (ConstraintViolation<?> constraintViolation : violations) {
            Path.Node leafNode = getLeafNode(constraintViolation);

            message.append("\n (");
            message.append(i);
            message.append(")");
            message.append(" Kind: ");
            message.append(leafNode.getKind());
            if (leafNode.getKind() == ElementKind.PARAMETER) {
                message.append("\n parameter index: ");
                message.append(leafNode.as(Path.ParameterNode.class).getParameterIndex());
            }
            message.append("\n message: ");
            message.append(constraintViolation.getMessage());
            message.append("\n root bean: ");
            message.append(constraintViolation.getRootBean());
            message.append("\n property path: ");
            message.append(constraintViolation.getPropertyPath());
            message.append("\n constraint: ");
            message.append(constraintViolation.getConstraintDescriptor().getAnnotation());

            i++;
        }

        return message.toString();
    }
 
Example 2
Source File: ConstraintTypeUtil20.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public ConstraintType.Type getConstraintType(Object o) {
    if (!(o instanceof ConstraintViolation)) {
        throw new RuntimeException(Messages.MESSAGES.unknownObjectPassedAsConstraintViolation(o));
    }
    ConstraintViolation<?> v = ConstraintViolation.class.cast(o);

    Iterator<Node> nodes = v.getPropertyPath().iterator();
    Node firstNode = nodes.next();

    switch (firstNode.getKind()) {
        case BEAN:
            return ConstraintType.Type.CLASS;
        case CONSTRUCTOR:
        case METHOD:
            Node secondNode = nodes.next();

            if (secondNode.getKind() == ElementKind.PARAMETER || secondNode.getKind() == ElementKind.CROSS_PARAMETER) {
                return ConstraintType.Type.PARAMETER;
            } else if (secondNode.getKind() == ElementKind.RETURN_VALUE) {
                return ConstraintType.Type.RETURN_VALUE;
            } else {
                throw new RuntimeException(Messages.MESSAGES.unexpectedPathNodeViolation(secondNode.getKind()));
            }
        case PROPERTY:
            return ConstraintType.Type.PROPERTY;
        case CROSS_PARAMETER:
        case PARAMETER:
        case RETURN_VALUE:
        case CONTAINER_ELEMENT: // we shouldn't encounter these element types at the root
        default:
            throw new RuntimeException(Messages.MESSAGES.unexpectedPathNode(firstNode.getKind()));
    }
}
 
Example 3
Source File: ConstraintViolationMapper.java    From act-platform with ISC License 5 votes vote down vote up
private String printPropertyPath(Path path) {
  if (path == null) return "UNKNOWN";

  String propertyPath = "";
  Path.Node parameterNode = null;
  // Construct string representation of property path.
  // This will strip away any other nodes added by RESTEasy (method, parameter, ...).
  for (Path.Node node : path) {
    if (node.getKind() == ElementKind.PARAMETER) {
      parameterNode = node;
    }

    if (node.getKind() == ElementKind.PROPERTY) {
      if (!propertyPath.isEmpty()) {
        propertyPath += ".";
      }

      propertyPath += node;
    }
  }

  if (propertyPath.isEmpty() && parameterNode != null) {
    // No property path constructed, assume this is a validation failure on a request parameter.
    propertyPath = parameterNode.toString();
  }

  return propertyPath;
}
 
Example 4
Source File: ConstraintViolationExceptionMapper.java    From crnk-framework with Apache License 2.0 5 votes vote down vote up
/**
 * Translate validated bean and root path into validated resource and
 * resource path. For example, embeddables belonging to an entity document
 * are mapped back to an entity violation and a proper path to the
 * embeddable attribute.
 *
 * @param violation to compute the reference
 * @return computaed reference
 */
private ResourceRef resolvePath(ConstraintViolation<?> violation) {
	Object resource = violation.getRootBean();

	Object nodeObject = resource;
	ResourceRef ref = new ResourceRef(resource);

	Iterator<Node> iterator = violation.getPropertyPath().iterator();
	while (iterator.hasNext()) {
		Node node = iterator.next();

		// ignore methods/parameters
		if (node.getKind() == ElementKind.METHOD) {
			continue;
		}
		if (node.getKind() == ElementKind.PARAMETER) {
			resource = getParameterValue(node);
			nodeObject = resource;
			ref = new ResourceRef(resource);
			assertResource(resource);
			continue;
		}

		// visit list, set, map references
		nodeObject = ref.getNodeReference(nodeObject, node);
		ref.visitNode(nodeObject);

		// visit property
		nodeObject = ref.visitProperty(nodeObject, node);
	}

	return ref;
}
 
Example 5
Source File: ValidationResponse.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
public ValidationResponse(final ConstraintViolationException cause) {
  super(false, new ArrayList<>());
  //noinspection ThrowableResultOfMethodCallIgnored
  checkNotNull(cause);
  Set<ConstraintViolation<?>> violations = cause.getConstraintViolations();
  if (violations != null && !violations.isEmpty()) {
    for (ConstraintViolation<?> violation : violations) {
      List<String> entries = new ArrayList<>();
      // iterate path to get the full path
      Iterator<Node> it = violation.getPropertyPath().iterator();
      while (it.hasNext()) {
        Node node = it.next();
        if (ElementKind.PROPERTY == node.getKind() || (ElementKind.PARAMETER == node.getKind() && !it.hasNext())) {
          if (node.getKey() != null) {
            entries.add(node.getKey().toString());
          }
          entries.add(node.getName());
        }
      }
      if (entries.isEmpty()) {
        if (messages == null) {
          messages = new ArrayList<>();
        }
        messages.add(violation.getMessage());
      }
      else {
        if (errors == null) {
          errors = new HashMap<>();
        }
        errors.put(Joiner.on('.').join(entries), violation.getMessage());
      }
    }
  }
  else if (cause.getMessage() != null) {
    messages = new ArrayList<>();
    messages.add(cause.getMessage());
  }
}
 
Example 6
Source File: ValidationMethodInterceptor.java    From guice-validator with MIT License 5 votes vote down vote up
private String getMessage(final Member member, final Object[] args,
                          final Set<? extends ConstraintViolation<?>> violations) {
    final StringBuilder message = new StringBuilder(200)
            .append(violations.size())
            .append(" constraint violation(s) occurred during method validation.")
            .append("\nConstructor or Method: ").append(member)
            .append("\nArgument values: ").append(Arrays.toString(args))
            .append("\nConstraint violations: ");
    int i = 1;
    for (ConstraintViolation<?> constraintViolation : violations) {
        final Path.Node leafNode = getLeafNode(constraintViolation);
        message.append("\n (")
                .append(i++)
                .append(") Kind: ")
                .append(leafNode.getKind());
        if (leafNode.getKind() == ElementKind.PARAMETER) {
            message.append("\n parameter index: ")
                    .append(leafNode.as(Path.ParameterNode.class).getParameterIndex());
        }
        message.append("\n message: ").append(constraintViolation.getMessage())
                .append("\n root bean: ").append(constraintViolation.getRootBean())
                .append("\n property path: ").append(constraintViolation.getPropertyPath())
                .append("\n constraint: ").append(constraintViolation.getConstraintDescriptor().getAnnotation());
    }

    return message.toString();
}
 
Example 7
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 8
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];

    }