Java Code Examples for javax.validation.Validator#forExecutables()

The following examples show how to use javax.validation.Validator#forExecutables() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ValidationInterceptor.java    From krazo with Apache License 2.0 5 votes vote down vote up
@AroundInvoke
public Object validateMethodInvocation(InvocationContext ctx) throws Exception {

    Object resource = ctx.getTarget();
    Method method = ctx.getMethod();

    log.log(Level.FINE, "Starting validation for controller method: {0}#{1}", new Object[]{
            resource.getClass().getName(), method.getName()
    });

    Validator validator = validatorFactory.getValidator();
    ExecutableValidator executableValidator = validator.forExecutables();

    // validate controller property parameters
    processViolations(ctx,
            validator.validate(resource)
    );

    // validate controller method parameters
    processViolations(ctx,
            executableValidator.validateParameters(resource, method, ctx.getParameters())
    );

    // execute method
    Object result = ctx.proceed();

    // TODO: Does this make sense? Nobody will be able to handle these. Remove?
    processViolations(ctx,
            executableValidator.validateReturnValue(resource, method, result)
    );

    return result;

}
 
Example 2
Source File: RestRouter.java    From rest.vertx with Apache License 2.0 5 votes vote down vote up
private static void validate(Method method, RouteDefinition definition, Validator validator, Object toInvoke, Object[] args) {

		// check method params first (if any)
		if (validator != null && args != null) {
			ExecutableValidator executableValidator = validator.forExecutables();
			Set<ConstraintViolation<Object>> result = executableValidator.validateParameters(toInvoke, method, args);
			if (result != null && result.size() > 0) {
				throw new ConstraintException(definition, result);
			}
		}
	}
 
Example 3
Source File: RestRouter.java    From rest.vertx with Apache License 2.0 5 votes vote down vote up
private static void validateResult(Object result, Method method, RouteDefinition definition, Validator validator, Object toInvoke) {

		if (validator != null) {
			ExecutableValidator executableValidator = validator.forExecutables();
			Set<ConstraintViolation<Object>> validationResult = executableValidator.validateReturnValue(toInvoke, method, result);
			if (validationResult != null && validationResult.size() > 0) {
				throw new ConstraintException(definition, validationResult);
			}
		}
	}
 
Example 4
Source File: ConstraintViolationsTest.java    From krazo with Apache License 2.0 4 votes vote down vote up
private List<ConstraintViolation<?>> simulateValidation(Object controller, String methodName, Object... args) {

        ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();

        Validator validator = validatorFactory.getValidator();
        ExecutableValidator executableValidator = validator.forExecutables();

        List<ConstraintViolation<?>> result = new ArrayList<>();

        // validate controller fields
        result.addAll(validator.validate(controller));

        // controller method parameters
        Method method = getFirstMethod(controller.getClass(), methodName);
        result.addAll(executableValidator.validateParameters(controller, method, args));

        return result;

    }
 
Example 5
Source File: ValidationInterceptor.java    From ozark with Apache License 2.0 4 votes vote down vote up
@AroundInvoke
public Object validateMethodInvocation(InvocationContext ctx) throws Exception {

    Object resource = ctx.getTarget();
    Method method = ctx.getMethod();

    log.log(Level.FINE, "Starting validation for controller method: {0}#{1}", new Object[]{
            resource.getClass().getName(), method.getName()
    });

    Validator validator = validatorFactory.getValidator();
    ExecutableValidator executableValidator = validator.forExecutables();

    // validate controller property parameters
    processViolations(ctx,
            validator.validate(resource)
    );

    // validate controller method parameters
    processViolations(ctx,
            executableValidator.validateParameters(resource, method, ctx.getParameters())
    );

    // execute method
    Object result = ctx.proceed();

    // TODO: Does this make sense? Nobody will be able to handle these. Remove?
    processViolations(ctx,
            executableValidator.validateReturnValue(resource, method, result)
    );

    return result;

}
 
Example 6
Source File: ConstraintViolationsTest.java    From ozark with Apache License 2.0 4 votes vote down vote up
private List<ConstraintViolation<?>> simulateValidation(Object controller, String methodName, Object... args) {

        ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();

        Validator validator = validatorFactory.getValidator();
        ExecutableValidator executableValidator = validator.forExecutables();

        List<ConstraintViolation<?>> result = new ArrayList<>();

        // validate controller fields
        result.addAll(validator.validate(controller));

        // controller method parameters
        Method method = getFirstMethod(controller.getClass(), methodName);
        result.addAll(executableValidator.validateParameters(controller, method, args));

        return result;

    }
 
Example 7
Source File: ValidationModule.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@Provides
@Singleton
ExecutableValidator executableValidator(final Validator validator) {
  return validator.forExecutables();
}