/** * Return a set of executable types contained in {@link ValidateOnExecution} annotation belonging to the {@code element}. * * @param element element to be examined for {@link ValidateOnExecution}. * @return set of executable types or an empty set if the element is not annotated with {@link ValidateOnExecution}. */ private Set<ExecutableType> getExecutableTypes(final AnnotatedElement element) { final ValidateOnExecution validateExecutable = element.getAnnotation(ValidateOnExecution.class); return validateExecutable != null ? Arrays.stream(validateExecutable.type()).collect(Collectors.toSet()) : Collections.emptySet(); }
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);
/** * * @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(); }
@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); } }
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 <T> Set<ConstraintViolation<T>> validateConstructorReturnValue(Constructor<? extends T> constructor, T t, Class<?>... classes) { return delegate.validateConstructorReturnValue(constructor, t, classes); } }
/** * @author Gunnar Morling */ @ValidateOnExecution(type = ExecutableType.ALL) public interface DeliveryService { public void createDelivery(@NotNull String name); }
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 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); } }
@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); } } } }
protected static ExecutableType[] getExecutableTypesOnMethod(Method method) { ValidateOnExecution voe = method.getAnnotation(ValidateOnExecution.class); if (voe == null || voe.type().length == 0) { return null; } ExecutableType[] types = voe.type(); if (types == null || types.length == 0) { return null; } return types; }
@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; } }
@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 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()); } }
private EnumSet<ExecutableType> commonExecutableTypeChecks(ValidateOnExecution validateOnExecutionAnnotation) { if ( validateOnExecutionAnnotation == null ) { return EnumSet.noneOf( ExecutableType.class ); } EnumSet<ExecutableType> executableTypes = EnumSet.noneOf( ExecutableType.class ); if ( validateOnExecutionAnnotation.type().length == 0 ) { // HV-757 executableTypes.add( ExecutableType.NONE ); } else { Collections.addAll( executableTypes, validateOnExecutionAnnotation.type() ); } // IMPLICIT cannot be mixed 10.1.2 of spec - Mixing IMPLICIT and other executable types is illegal if ( executableTypes.contains( ExecutableType.IMPLICIT ) && executableTypes.size() > 1 ) { throw log.getMixingImplicitWithOtherExecutableTypesException(); } // NONE can be removed 10.1.2 of spec - A list containing NONE and other types of executables is equivalent to a // list containing the types of executables without NONE. if ( executableTypes.contains( ExecutableType.NONE ) && executableTypes.size() > 1 ) { executableTypes.remove( ExecutableType.NONE ); } // 10.1.2 of spec - A list containing ALL and other types of executables is equivalent to a list containing only ALL if ( executableTypes.contains( ExecutableType.ALL ) ) { executableTypes = ALL_EXECUTABLE_TYPES; } return executableTypes; }
public Object validateMethodInvocation(InvocationContext ctx) throws Exception { ExecutableValidator executableValidator = validator.forExecutables(); Set<ConstraintViolation<Object>> violations = executableValidator.validateParameters( ctx.getTarget(), ctx.getMethod(), violations = executableValidator.validateReturnValue( ctx.getTarget(), ctx.getMethod(),
@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 <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); } }
@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()); } }