result = execVal.validateParameters( invocation.getThis(), methodToValidate, invocation.getArguments(), groups); result = execVal.validateParameters( invocation.getThis(), methodToValidate, invocation.getArguments(), groups);
/** * * @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);
@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); } }
@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 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); } }
@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 <T> void beforeMethodInvoke(SwaggerInvocation invocation, SwaggerProducerOperation producerOperation, Object[] args) throws ConstraintViolationException { if (null == executableValidator) { ValidatorFactory factory = Validation.byDefaultProvider() .configure() .parameterNameProvider(new DefaultParameterNameProvider()) .buildValidatorFactory(); executableValidator = factory.getValidator().forExecutables(); } Set<ConstraintViolation<Object>> violations = executableValidator.validateParameters(producerOperation.getProducerInstance(), producerOperation.getProducerMethod(), args, Default.class); if (violations.size() > 0) { LOGGER.warn("Parameter validation failed : " + violations.toString()); throw new ConstraintViolationException(violations); } }
public Object validateMethodInvocation(InvocationContext ctx) throws Exception { ExecutableValidator executableValidator = validator.forExecutables(); Set<ConstraintViolation<Object>> violations = executableValidator.validateParameters( ctx.getTarget(), ctx.getMethod(),
@Override public void validateAllParameters(HttpRequest request, Object object, Method method, Object[] parameterValues, Class<?>... groups) { Validator validator = getValidator(request); SimpleViolationsContainer violationsContainer = getViolationsContainer(request, object); if (method.getParameterTypes().length == 0) { checkViolations(request); return; } Set<ConstraintViolation<Object>> cvs = null; try { cvs = validator.forExecutables().validateParameters(object, method, parameterValues, groups); } catch (Exception e) { violationsContainer.setException(e); throw toValidationException(e, violationsContainer); } violationsContainer.addViolations(cvs); if ((violationsContainer.isFieldsValidated() || !GetRestful.isRootResource(object.getClass()) || hasApplicationScope(object)) && violationsContainer.size() > 0) { throw new ResteasyViolationExceptionImpl(violationsContainer, request.getHttpHeaders().getAcceptableMediaTypes()); } }
private void validateParameters(final Object obj, final Method method, final Object[] args, final Class<?>[] groups) { final Set<ConstraintViolation<Object>> violations = methodValidator.validateParameters(obj, method, args, groups); if (!violations.isEmpty()) { final String message = "Invalid arguments calling '" + method + "' with " + Arrays.deepToString(args); throw new ConstraintViolationException(message, violations); } }
public< T > void validateParameters(final T instance, final Method method, final Object[] arguments) { final ExecutableValidator methodValidator = getExecutableValidator(); final Set< ConstraintViolation< T > > violations = methodValidator.validateParameters(instance, method, arguments); if (!violations.isEmpty()) { throw new ConstraintViolationException(violations); } }
public< T > void validateParameters(final T instance, final Method method, final Object[] arguments) { final ExecutableValidator methodValidator = getExecutableValidator(); final Set< ConstraintViolation< T > > violations = methodValidator.validateParameters(instance, method, arguments); if (!violations.isEmpty()) { throw new ConstraintViolationException(violations); } }
@Test(expectedExceptions = ConstraintDefinitionException.class) @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_VALIDATIONROUTINE_TYPEVALIDATORRESOLUTION, id = "a") public void testCrossParameterConstraintWithoutValidatorCausesConstraintDefinitionException() throws Exception { Object object = new OnlineCalendarService(); Method method = OnlineCalendarService.class.getMethod( "createEvent", Date.class, Date.class ); Object[] parameterValues = new Object[2]; getExecutableValidator().validateParameters( object, method, parameterValues ); }
@Test(expectedExceptions = IllegalArgumentException.class) @SpecAssertion(section = Sections.VALIDATIONAPI_VALIDATORAPI_METHODLEVELVALIDATIONMETHODS, id = "c") public void testNullPassedForMethodCausesException() throws Exception { Object object = new User(); Method method = null; Object[] parameterValues = new Object[] { null }; getExecutableValidator().validateParameters( object, method, parameterValues ); }
@Test(expectedExceptions = ConstraintDeclarationException.class) @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_GROUPSEQUENCE_GROUPCONVERSION, id = "i") @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_GROUPSEQUENCE_GROUPCONVERSION, id = "k") public void testGroupConversionGivenOnParameterInSubClass() throws Exception { Object object = new SubClassWithGroupConversionOnParameter(); Method method = SubClassWithGroupConversionOnParameter.class.getMethod( "addUser", User.class ); Object[] parameters = new Object[] { null }; getExecutableValidator().validateParameters( object, method, parameters ); }
@Test(expectedExceptions = ConstraintDeclarationException.class) @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_GROUPSEQUENCE_GROUPCONVERSION, id = "i") @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_GROUPSEQUENCE_GROUPCONVERSION, id = "k") public void testGroupConversionGivenOnParameterImplementedInterfaceWithSuperClass() throws Exception { Object object = new ImplementationOfInterfaceWithGroupConversionOnParameterAndSuperClass(); Method method = ImplementationOfInterfaceWithGroupConversionOnParameterAndSuperClass.class .getMethod( "addUsers", List.class ); Object[] parameters = new Object[] { null }; getExecutableValidator().validateParameters( object, method, parameters ); }
@Test(expectedExceptions = ConstraintDeclarationException.class) @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_METHODLEVELCONSTRAINTS_INHERITANCE, id = "a") @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_METHODLEVELCONSTRAINTS_INHERITANCE, id = "e") public void testParameterContainerElementConstraintsAddedInInterfaceImplementationCausesException() throws Exception { Object object = new ImplementationAddingParameterConstraints(); Method method = getAddParticipantsMethod( object ); Object[] parameterValues = new Object[3]; getExecutableValidator().validateParameters( object, method, parameterValues ); fail( "Implementing method must add no parameter constraints. Expected exception wasn't thrown." ); }
@Test(expectedExceptions = ConstraintDeclarationException.class) @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_METHODLEVELCONSTRAINTS_INHERITANCE, id = "b") @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_METHODLEVELCONSTRAINTS_INHERITANCE, id = "e") public void testContainerElementOfParameterIsConstrainedInInterfaceMethodAndSuperClassMethodCausesException() throws Exception { Object object = new ImplementationOfConstrainedInterfaceExtendingUnconstrainedSuperClass(); Method method = getAddParticipantsMethod( object ); Object[] parameterValues = new Object[3]; getExecutableValidator().validateParameters( object, method, parameterValues ); fail( "A method defined in an interface and a superclass not implementing this interface must have no parameter constraints. Expected exception wasn't thrown." ); }
@Test(expectedExceptions = ConstraintDeclarationException.class) @SpecAssertion(section = Sections.CONSTRAINTSDEFINITIONIMPLEMENTATION_CONSTRAINTDEFINITION_PROPERTIES_VALIDATIONAPPLIESTO, id = "c") @SpecAssertion(section = Sections.CONSTRAINTSDEFINITIONIMPLEMENTATION_CONSTRAINTDEFINITION_PROPERTIES_VALIDATIONAPPLIESTO, id = "d") @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_METHODLEVELCONSTRAINTS_PARAMETERCONSTRAINTS_CROSSPARAMETERCONSTRAINTS, id = "c") @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_METHODLEVELCONSTRAINTS_RETURNVALUECONSTRAINTS, id = "b") public void testConstraintTargetImplicitOnMethodWithParametersAndReturnValueCausesException() throws Exception { Object object = new Foo(); Method method = Foo.class.getMethod( "createEvent", Date.class, Date.class ); Object[] parameterValues = new Object[2]; getExecutableValidator().validateParameters( object, method, parameterValues ); fail( "Usage of ConstraintTarget.IMPLICIT not allowed for methods with parameters and return value. Expected exception wasn't thrown." ); }