/** * Gets the method return value name, if the violation is raised in it */ private static Optional<String> getMethodReturnValueName(ConstraintViolation<?> violation) { int returnValueNames = -1; final StringBuilder result = new StringBuilder("server response"); for (Path.Node node : violation.getPropertyPath()) { if (node.getKind().equals(ElementKind.RETURN_VALUE)) { returnValueNames = 0; } else if (returnValueNames >= 0) { result.append(returnValueNames++ == 0 ? " " : ".").append(node); } } return returnValueNames >= 0 ? Optional.of(result.toString()) : Optional.empty(); }
if (name != null && node.getKind() == ElementKind.PROPERTY && !name.startsWith("<")) { if (!first) { sb.append('.');
/** * Determines if constraint violation occurred in the request entity. If it did, return a client * friendly string representation of where the error occurred (eg. "patient.name") */ public static Optional<String> isRequestEntity(ConstraintViolation<?> violation, Invocable invocable) { final Collection<Path.Node> propertyPath = Lists.of(violation.getPropertyPath()); final Path.Node parent = propertyPath.stream() .skip(1L) .findFirst() .orElse(null); if (parent == null) { return Optional.empty(); } final List<Parameter> parameters = invocable.getParameters(); if (parent.getKind() == ElementKind.PARAMETER) { final Parameter param = parameters.get(parent.as(Path.ParameterNode.class).getParameterIndex()); if (param.getSource().equals(Parameter.Source.UNKNOWN)) { final String path = propertyPath.stream() .skip(2L) .map(Path.Node::toString) .collect(Collectors.joining(".")); return Optional.of(path); } } return Optional.empty(); }
private Response.Status getResponseStatus(final ConstraintViolation<?> constraintViolation) { for (final Path.Node node : constraintViolation.getPropertyPath()) { final ElementKind kind = node.getKind(); if (ElementKind.RETURN_VALUE.equals(kind)) { return Response.Status.INTERNAL_SERVER_ERROR; } } return Response.Status.BAD_REQUEST; }
@Override public String toString() { final StringBuilder b = new StringBuilder(); for (final Node node : nodes) { if (b.length() > 0 && node.getKind() == ElementKind.PROPERTY) { b.append('.'); } b.append(node.toString()); } return b.toString(); }
private PropertyMetaData getBeanPropertyMetaData(BeanMetaData<?> beanMetaData, Path.Node propertyNode ) { if ( !ElementKind.PROPERTY.equals( propertyNode.getKind() ) ) { throw LOG.getInvalidPropertyPathException( beanMetaData.getBeanClass(), propertyNode.getName() ); } return beanMetaData.getMetaDataFor( propertyNode.getName() ); }
private PropertyMetaData getBeanPropertyMetaData( Class<?> beanClass, Path.Node propertyNode ) { if ( !ElementKind.PROPERTY.equals( propertyNode.getKind() ) ) { throw log.getInvalidPropertyPathException( beanClass, propertyNode.getName() ); } BeanMetaData<?> beanMetaData = beanMetaDataManager.getBeanMetaData( beanClass ); PropertyMetaData propertyMetaData = beanMetaData.getMetaDataFor( propertyNode.getName() ); if ( propertyMetaData == null ) { throw log.getInvalidPropertyPathException( beanClass, propertyNode.getName() ); } return propertyMetaData; }
private Status getResponseStatus(final ConstraintViolation<?> violation) { for (Path.Node node : violation.getPropertyPath()) { ElementKind kind = node.getKind(); if (ElementKind.RETURN_VALUE.equals(kind)) { return Status.INTERNAL_SERVER_ERROR; } } return Status.BAD_REQUEST; }
private void assertNode(Path.Node actualNode, String expectedName, ElementKind expectedKind, boolean expectedInIterable, Integer expectedIndex, Object expectedKey) { assertEquals( actualNode.getName(), expectedName ); assertEquals( actualNode.getKind(), expectedKind ); assertEquals( actualNode.isInIterable(), expectedInIterable ); assertEquals( actualNode.getIndex(), expectedIndex ); assertEquals( actualNode.getKey(), expectedKey ); }
if (firstNode.getKind() == ElementKind.METHOD) if (secondNode.getKind() == ElementKind.PARAMETER || secondNode.getKind() == ElementKind.CROSS_PARAMETER) else if (secondNode.getKind() == ElementKind.RETURN_VALUE) throw new RuntimeException(Messages.MESSAGES.unexpectedPathNodeViolation(secondNode.getKind())); if (firstNode.getKind() == ElementKind.BEAN) if (firstNode.getKind() == ElementKind.PROPERTY) throw new RuntimeException(Messages.MESSAGES.unexpectedPathNode(firstNode.getKind()));
private PathExpectation(Path propertyPath) { for ( Path.Node node : propertyPath ) { Integer parameterIndex = null; if ( node.getKind() == ElementKind.PARAMETER ) { parameterIndex = node.as( Path.ParameterNode.class ).getParameterIndex(); } Class<?> containerClass = getContainerClass( node ); Integer typeArgumentIndex = getTypeArgumentIndex( node ); nodes.add( new NodeExpectation( node.getName(), node.getKind(), node.isInIterable(), node.getKey(), node.getIndex(), parameterIndex, containerClass, typeArgumentIndex ) ); } }
private static Integer getTypeArgumentIndex(Path.Node node) { Integer typeArgumentIndex = null; if ( node.getKind() == ElementKind.PROPERTY ) { typeArgumentIndex = node.as( Path.PropertyNode.class ).getTypeArgumentIndex(); } if ( node.getKind() == ElementKind.BEAN ) { typeArgumentIndex = node.as( Path.BeanNode.class ).getTypeArgumentIndex(); } if ( node.getKind() == ElementKind.CONTAINER_ELEMENT ) { typeArgumentIndex = node.as( Path.ContainerElementNode.class ).getTypeArgumentIndex(); } return typeArgumentIndex; } }
private static Class<?> getContainerClass(Path.Node node) { Class<?> containerClass = null; if ( node.getKind() == ElementKind.PROPERTY ) { containerClass = node.as( Path.PropertyNode.class ).getContainerClass(); } if ( node.getKind() == ElementKind.BEAN ) { containerClass = node.as( Path.BeanNode.class ).getContainerClass(); } if ( node.getKind() == ElementKind.CONTAINER_ELEMENT ) { containerClass = node.as( Path.ContainerElementNode.class ).getContainerClass(); } return containerClass; }
while (iter.hasNext()) { Path.Node next = iter.next(); if (next.getKind() == ElementKind.PROPERTY) { final String fieldName = next.getName(); final Field theField = beanClazz.getDeclaredField(fieldName);
if (name != null && node.getKind() == ElementKind.PROPERTY && !name.startsWith("<")) { if (!first) { sb.append('.');
/** * Determine the response status (400 or 500) from the given BV exception. * * @param violation BV exception. * @return response status (400 or 500). */ public static Response.Status getResponseStatus(final ConstraintViolationException violation) { final Iterator<ConstraintViolation<?>> iterator = violation.getConstraintViolations().iterator(); if (iterator.hasNext()) { for (final Path.Node node : iterator.next().getPropertyPath()) { final ElementKind kind = node.getKind(); if (ElementKind.RETURN_VALUE.equals(kind)) { return Response.Status.INTERNAL_SERVER_ERROR; } } } return Response.Status.BAD_REQUEST; }
/** * Gets a method parameter (or a parameter field) name, if the violation raised in it. */ private static Optional<String> getMemberName(ConstraintViolation<?> violation, Invocable invocable) { final List<Path.Node> propertyPath = Lists.of(violation.getPropertyPath()); final int size = propertyPath.size(); if (size < 2) { return Optional.empty(); } final Path.Node parent = propertyPath.get(size - 2); final Path.Node member = propertyPath.get(size - 1); switch (parent.getKind()) { case PARAMETER: // Constraint violation most likely failed with a BeanParam final List<Parameter> parameters = invocable.getParameters(); final Parameter param = parameters.get(parent.as(Path.ParameterNode.class).getParameterIndex()); // Extract the failing *Param annotation inside the Bean Param if (param.getSource().equals(Parameter.Source.BEAN_PARAM)) { final Field field = FieldUtils.getField(param.getRawType(), member.getName(), true); return JerseyParameterNameProvider.getParameterNameFromAnnotations(field.getDeclaredAnnotations()); } break; case METHOD: return Optional.of(member.getName()); default: break; } return Optional.empty(); }
/** * Given a set of constraint violations and a Jersey {@link Invocable} where the constraint * occurred, determine the HTTP Status code for the response. A return value violation is an * internal server error, an invalid request body is unprocessable entity, and any params that * are invalid means a bad request */ public static <T extends ConstraintViolation<?>> int determineStatus(Set<T> violations, Invocable invocable) { if (violations.size() > 0) { final ConstraintViolation<?> violation = violations.iterator().next(); for (Path.Node node : violation.getPropertyPath()) { switch (node.getKind()) { case RETURN_VALUE: return 500; case PARAMETER: // Now determine if the parameter is the request entity final int index = node.as(Path.ParameterNode.class).getParameterIndex(); final Parameter parameter = invocable.getParameters().get(index); return parameter.getSource().equals(Parameter.Source.UNKNOWN) ? 422 : 400; default: continue; } } } // This shouldn't hit, but if it does, we'll return a unprocessable entity return 422; } }