/** * * @param invocation * @return * @throws ConstraintViolationException incase of any constraints * defined on method parameters are violated. */ @Override public Object invoke(MethodInvocation invocation) throws Throwable { if (skipMethod(invocation)) { return invocation.proceed(); } ExecutableValidator executableValidator = validatorProvider.get().forExecutables(); Set<ConstraintViolation<Object>> result = executableValidator.validateParameters( invocation.getThis(), invocation.getMethod(), invocation.getArguments()); if (!result.isEmpty()) { throw new ConstraintViolationException(result); } return invocation.proceed(); }
result = execVal.validateParameters( invocation.getThis(), methodToValidate, invocation.getArguments(), groups); result = execVal.validateParameters( invocation.getThis(), methodToValidate, invocation.getArguments(), groups); result = execVal.validateReturnValue(invocation.getThis(), methodToValidate, returnValue, groups); if (!result.isEmpty()) { throw new ConstraintViolationException(result);
@Override public void validateResult(Object resource, Invocable invocable, Object returnValue) throws ConstraintViolationException { // If the Validated annotation is on a method, then validate the response with // the specified constraint group. final Class<?>[] groups; if (invocable.getHandlingMethod().isAnnotationPresent(Validated.class)) { groups = invocable.getHandlingMethod().getAnnotation(Validated.class).value(); } else { groups = new Class<?>[]{Default.class}; } final Set<ConstraintViolation<Object>> violations = forExecutables().validateReturnValue(resource, invocable.getHandlingMethod(), returnValue, groups); if (!violations.isEmpty()) { LOGGER.trace("Response validation failed: {}", ConstraintViolations.copyOf(violations)); throw new JerseyViolationException(violations, invocable); } }
@Override public <T> Set<ConstraintViolation<T>> validateConstructorParameters(Constructor<? extends T> constructor, Object[] objects, Class<?>... classes) { return delegate.validateConstructorParameters(constructor, objects, classes); }
@Override public <T> Set<ConstraintViolation<T>> validateConstructorReturnValue(Constructor<? extends T> constructor, T t, Class<?>... classes) { return delegate.validateConstructorReturnValue(constructor, t, classes); } }
public void validateConstructorInvocation(InvocationContext ctx) throws Exception { ExecutableValidator executableValidator = validator.forExecutables(); Set<? extends ConstraintViolation<?>> violations = executableValidator.validateConstructorParameters( ctx.getConstructor(), ctx.getParameters() Object createdObject = ctx.getTarget(); violations = validator.forExecutables().validateConstructorReturnValue( ctx.getConstructor(), createdObject
@Override public void validateResult(final Object resource, final Invocable resourceMethod, final Object result) { if (configuration.getBootstrapConfiguration().isExecutableValidationEnabled()) { final Set<ConstraintViolation<Object>> constraintViolations = new HashSet<>(); final Method handlingMethod = resourceMethod.getHandlingMethod(); final BeanDescriptor beanDescriptor = getConstraintsForClass(resource.getClass()); final MethodDescriptor methodDescriptor = beanDescriptor.getConstraintsForMethod(handlingMethod.getName(), handlingMethod.getParameterTypes()); final Method definitionMethod = resourceMethod.getDefinitionMethod(); if (methodDescriptor != null && methodDescriptor.hasConstrainedReturnValue() && validateOnExecutionHandler.validateMethod(resource.getClass(), definitionMethod, handlingMethod)) { constraintViolations.addAll(forExecutables().validateReturnValue(resource, handlingMethod, result)); if (result instanceof Response) { constraintViolations.addAll(forExecutables().validateReturnValue(resource, handlingMethod, ((Response) result).getEntity())); } } if (!constraintViolations.isEmpty()) { throw new ConstraintViolationException(constraintViolations); } } } }
@Test(expectedExceptions = ConstraintDeclarationException.class) @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_GROUPSEQUENCE_GROUPCONVERSION, id = "a") public void testGroupConversionWithoutValidAnnotationOnConstructorParameter() throws Exception { Constructor<UserWithGroupConversionButWithoutValidAnnotationConstructorParameter> constructor = UserWithGroupConversionButWithoutValidAnnotationConstructorParameter.class .getConstructor( List.class ); Object[] parameters = new Object[] { null }; getExecutableValidator().validateConstructorParameters( constructor, parameters ); }
@Test(expectedExceptions = IllegalArgumentException.class) @SpecAssertion(section = Sections.VALIDATIONAPI_VALIDATORAPI_METHODLEVELVALIDATIONMETHODS, id = "l") public void testNullPassedForReturnValueCausesException() throws Exception { Constructor<Customer> constructor = Customer.class.getConstructor(); Customer returnValue = null; getExecutableValidator().validateConstructorReturnValue( constructor, returnValue ); }
@Override public void validateResourceAndInputParams(Object resource, final Invocable invocable, Object[] objects) throws ConstraintViolationException { final Class<?>[] groups = getGroup(invocable); final Set<ConstraintViolation<Object>> violations = new HashSet<>(); final BeanDescriptor beanDescriptor = getConstraintsForClass(resource.getClass()); if (beanDescriptor.isBeanConstrained()) { violations.addAll(validate(resource, groups)); } violations.addAll(forExecutables().validateParameters(resource, invocable.getHandlingMethod(), objects, groups)); if (!violations.isEmpty()) { throw new JerseyViolationException(violations, invocable); } }
result = execVal.validateParameters( invocation.getThis(), methodToValidate, invocation.getArguments(), groups); result = execVal.validateParameters( invocation.getThis(), methodToValidate, invocation.getArguments(), groups); result = execVal.validateReturnValue(invocation.getThis(), methodToValidate, returnValue, groups); if (!result.isEmpty()) { throw new ConstraintViolationException(result);
@Override public <T> Set<ConstraintViolation<T>> validateReturnValue(T t, Method method, Object o, Class<?>... classes) { return delegate.validateReturnValue(t, method, o, classes); }
@Test(expectedExceptions = IllegalArgumentException.class) @SpecAssertion(section = Sections.VALIDATIONAPI_VALIDATORAPI_METHODLEVELVALIDATIONMETHODS, id = "i") public void testNullPassedForParameterValuesCausesException() throws Exception { Constructor<User> constructor = User.class.getConstructor( String.class ); Object[] parameterValues = null; getExecutableValidator().validateConstructorParameters( constructor, parameterValues ); }
@Test(expectedExceptions = IllegalArgumentException.class) @SpecAssertion(section = Sections.VALIDATIONAPI_VALIDATORAPI_METHODLEVELVALIDATIONMETHODS, id = "l") public void testNullPassedAsSingleGroupCausesException() throws Exception { Constructor<Customer> constructor = Customer.class.getConstructor(); Customer returnValue = new Customer(); getExecutableValidator().validateConstructorReturnValue( constructor, returnValue, (Class<?>) null ); }
@Test public void testWorkflowTaskTestSetType() throws NoSuchMethodException { WorkflowTask workflowTask = createSampleWorkflowTask(); Method method = WorkflowTask.class.getMethod("setType", String.class); Object[] parameterValues = {""}; ExecutableValidator executableValidator = validator.forExecutables(); Set<ConstraintViolation<Object>> result = executableValidator.validateParameters(workflowTask, method, parameterValues); assertEquals(1, result.size()); assertEquals(result.iterator().next().getMessage(), "WorkTask type cannot be null or empty"); }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Set<ConstraintViolation<Object>> constraintViolations = validator.forExecutables().validateParameters( wrapped, method, args == null ? EMPTY_ARGS : args, groups ); if ( !constraintViolations.isEmpty() ) { throw new ConstraintViolationException( new HashSet<ConstraintViolation<?>>( constraintViolations ) ); } Object result = method.invoke( wrapped, args ); constraintViolations = validator.forExecutables().validateReturnValue( wrapped, method, result, groups ); if ( !constraintViolations.isEmpty() ) { throw new ConstraintViolationException( new HashSet<ConstraintViolation<?>>( constraintViolations ) ); } return result; } }
@Override public void validateReturnValue(HttpRequest request, Object object, Method method, Object returnValue, Class<?>... groups) { Validator validator = getValidator(request); SimpleViolationsContainer violationsContainer = getViolationsContainer(request, object); Set<ConstraintViolation<Object>> cvs = null; try { cvs = validator.forExecutables().validateReturnValue(object, method, returnValue, groups); } catch (Exception e) { violationsContainer.setException(e); throw toValidationException(e, violationsContainer); } violationsContainer.addViolations(cvs); if (violationsContainer.size() > 0) { throw new ResteasyViolationExceptionImpl(violationsContainer, request.getHttpHeaders().getAcceptableMediaTypes()); } }
@Test(expectedExceptions = ConstraintDeclarationException.class) @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_GROUPSEQUENCE_GROUPCONVERSION, id = "a") public void testGroupConversionWithoutValidAnnotationOnConstructorParameter() throws Exception { Constructor<UserWithContainerElementGroupConversionButWithoutValidAnnotationConstructorParameter> constructor = UserWithContainerElementGroupConversionButWithoutValidAnnotationConstructorParameter.class .getConstructor( List.class ); Object[] parameters = new Object[] { null }; getExecutableValidator().validateConstructorParameters( constructor, parameters ); }
@Test(expectedExceptions = ConstraintDeclarationException.class) @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_GROUPSEQUENCE_GROUPCONVERSION, id = "a") public void testGroupConversionWithoutValidAnnotationOnConstructorReturnValue() throws Exception { UserWithGroupConversionButWithoutValidAnnotationOnConstructorReturnValue object = new UserWithGroupConversionButWithoutValidAnnotationOnConstructorReturnValue(); Constructor<UserWithGroupConversionButWithoutValidAnnotationOnConstructorReturnValue> constructor = UserWithGroupConversionButWithoutValidAnnotationOnConstructorReturnValue.class .getConstructor(); getExecutableValidator().validateConstructorReturnValue( constructor, object ); }
@Override public void onValidate(final ValidationInterceptorContext ctx) { final Object resource = ctx.getResource(); final Invocable resourceMethod = ctx.getInvocable(); final Object[] args = ctx.getArgs(); final Set<ConstraintViolation<Object>> constraintViolations = new HashSet<>(); final BeanDescriptor beanDescriptor = getConstraintsForClass(resource.getClass()); // Resource validation. if (beanDescriptor.isBeanConstrained()) { constraintViolations.addAll(validate(resource)); } if (resourceMethod != null && configuration.getBootstrapConfiguration().isExecutableValidationEnabled()) { final Method handlingMethod = resourceMethod.getHandlingMethod(); // Resource method validation - input parameters. final MethodDescriptor methodDescriptor = beanDescriptor.getConstraintsForMethod(handlingMethod.getName(), handlingMethod.getParameterTypes()); if (methodDescriptor != null && methodDescriptor.hasConstrainedParameters() && validateOnExecutionHandler.validateMethod(resource.getClass(), resourceMethod.getDefinitionMethod(), resourceMethod.getHandlingMethod())) { constraintViolations.addAll(forExecutables().validateParameters(resource, handlingMethod, args)); } } if (!constraintViolations.isEmpty()) { throw new ConstraintViolationException(constraintViolations); } }