@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); } }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_EXECUTABLEDESCRIPTOR, id = "e") public void testAreParametersConstrainedForCrossParameterConstrainedMethod() { MethodDescriptor descriptor = Executables.crossParameterConstrainedMethod(); assertTrue( descriptor.hasConstrainedParameters(), "Should be constrained on parameters" ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_EXECUTABLEDESCRIPTOR, id = "e") public void testAreParametersConstrainedForCascadedMethod() { MethodDescriptor descriptor = Executables.cascadedParameterMethod(); assertTrue( descriptor.hasConstrainedParameters(), "Should be constrained on parameters" ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_EXECUTABLEDESCRIPTOR, id = "e") public void testAreParametersConstrainedForConstrainedMethod() { MethodDescriptor descriptor = Executables.parameterConstrainedMethod(); assertTrue( descriptor.hasConstrainedParameters(), "Should be constrained on parameters" ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_EXECUTABLEDESCRIPTOR, id = "e") public void testAreParametersConstrainedForParameterlessMethod() { MethodDescriptor descriptor = Executables.returnValueConstrainedMethod(); assertFalse( descriptor.hasConstrainedParameters(), "Should not be constrained on parameters" ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTMETADATA_EXECUTABLEDESCRIPTOR, id = "e") public void testAreParametersConstrainedForNotConstrainedMethod() { MethodDescriptor descriptor = Executables.cascadedReturnValueMethod(); assertFalse( descriptor.hasConstrainedParameters(), "Should not be constrained on parameters" ); }
@Test @SpecAssertions({ @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_METHODLEVELOVERRIDING, id = "a"), @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_METHODLEVELOVERRIDING, id = "c"), @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_METHODLEVELOVERRIDING, id = "f") }) public void testXmlMethodConfigurationApplied() throws Exception { MethodDescriptor descriptor = TestUtil.getMethodDescriptor( CustomerRepository.class, "listCustomers" ); assertNotNull( descriptor, "the specified method should be configured in xml" ); assertTrue( descriptor.hasConstrainedReturnValue() ); assertFalse( descriptor.hasConstrainedParameters() ); descriptor = TestUtil.getMethodDescriptor( CustomerRepository.class, "findCustomer", String.class ); assertNotNull( descriptor, "the specified method should be configured in xml" ); assertTrue( descriptor.hasConstrainedReturnValue() ); assertTrue( descriptor.hasConstrainedParameters() ); descriptor = TestUtil.getMethodDescriptor( CustomerRepository.class, "isCustomer", String.class ); assertNotNull( descriptor, "the specified method should be configured in xml" ); assertFalse( descriptor.hasConstrainedReturnValue() ); assertTrue( descriptor.hasConstrainedParameters() ); }
/** * Only accepts if method isn't parameterless and have at least one constraint. */ private boolean hasConstraints(ControllerMethod controllerMethod) { Method method = controllerMethod.getMethod(); if (method.getParameterTypes().length == 0) { logger.debug("method {} has no parameters, skipping", controllerMethod); return false; } BeanDescriptor bean = bvalidator.getConstraintsForClass(controllerMethod.getController().getType()); if(bean == null) { return false; } MethodDescriptor descriptor = bean.getConstraintsForMethod(method.getName(), method.getParameterTypes()); return descriptor != null && descriptor.hasConstrainedParameters(); }
@Test @SpecAssertions({ @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_METHODLEVELOVERRIDING, id = "a"), @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_METHODLEVELOVERRIDING, id = "b"), @SpecAssertion(section = Sections.XML_MAPPING_CONSTRAINTDECLARATIONINXML_METHODLEVELOVERRIDING, id = "c") }) public void testVarargsMethodParameter() throws Exception { MethodDescriptor descriptor = TestUtil.getMethodDescriptor( CustomerRepository.class, "addCustomers", Customer[].class ); assertNotNull( descriptor, "the specified method should be configured in xml" ); assertTrue( descriptor.hasConstrainedParameters() ); }
public boolean accepts(ResourceMethod method) { BeanDescriptor bean = methodValidator.getConstraintsForClass(method.getResource().getType()); MethodDescriptor descriptor = bean.getConstraintsForMethod(method.getMethod().getName(), method.getMethod().getParameterTypes()); return descriptor != null && descriptor.hasConstrainedParameters(); }
@AroundInvoke public Object invoke(final InvocationContext context) throws Exception { final Method method = context.getMethod(); final Class<?> targetClass = Proxies.classFor(context.getTarget().getClass()); if (!isExecutableValidated(targetClass, method, this::computeIsMethodValidated)) { return context.proceed(); } final MethodDescriptor constraintsForMethod = validator.getConstraintsForClass(targetClass) .getConstraintsForMethod(method.getName(), method.getParameterTypes()); if (!DescriptorManager.isConstrained(constraintsForMethod)) { return context.proceed(); } initExecutableValidator(); if (constraintsForMethod.hasConstrainedParameters()) { final Set<ConstraintViolation<Object>> violations = executableValidator.validateParameters(context.getTarget(), method, context.getParameters()); if (!violations.isEmpty()) { throw new ConstraintViolationException(violations); } } final Object result = context.proceed(); if (constraintsForMethod.hasConstrainedReturnValue()) { final Set<ConstraintViolation<Object>> violations = executableValidator.validateReturnValue(context.getTarget(), method, result); if (!violations.isEmpty()) { throw new ConstraintViolationException(violations); } } return result; }
@AroundInvoke public Object invoke(final InvocationContext context) throws Exception { final Method method = context.getMethod(); final Class<?> targetClass = Proxies.classFor(context.getTarget().getClass()); if (!isExecutableValidated(targetClass, method, this::computeIsMethodValidated)) { return context.proceed(); } final MethodDescriptor constraintsForMethod = validator.getConstraintsForClass(targetClass) .getConstraintsForMethod(method.getName(), method.getParameterTypes()); if (!DescriptorManager.isConstrained(constraintsForMethod)) { return context.proceed(); } initExecutableValidator(); if (constraintsForMethod.hasConstrainedParameters()) { final Set<ConstraintViolation<Object>> violations = executableValidator.validateParameters(context.getTarget(), method, context.getParameters()); if (!violations.isEmpty()) { throw new ConstraintViolationException(violations); } } final Object result = context.proceed(); if (constraintsForMethod.hasConstrainedReturnValue()) { final Set<ConstraintViolation<Object>> violations = executableValidator.validateReturnValue(context.getTarget(), method, result); if (!violations.isEmpty()) { throw new ConstraintViolationException(violations); } } return result; }
@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); } }