use of com.haulmont.cuba.core.global.validation.MethodParametersValidationException in project cuba by cuba-platform.
the class CubaMethodValidationInterceptor method invoke.
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
// Avoid Validator invocation on FactoryBean.getObjectType/isSingleton
if (isFactoryBeanMetadataMethod(invocation.getMethod())) {
return invocation.proceed();
}
log.trace("Validating service call params: {}", invocation.getStaticPart());
Class<?>[] groups = determineValidationGroups(invocation);
if (groups.length == 0) {
groups = new Class[] { Default.class, ServiceParametersChecks.class };
}
ExecutableValidator execVal = this.beanValidation.getValidator().forExecutables();
Method methodToValidate = invocation.getMethod();
Set<ConstraintViolation<Object>> result;
try {
result = execVal.validateParameters(invocation.getThis(), methodToValidate, invocation.getArguments(), groups);
} catch (IllegalArgumentException ex) {
// Probably a generic type mismatch between interface and impl as reported in SPR-12237 / HV-1011
// Let's try to find the bridged method on the implementation class...
methodToValidate = BridgeMethodResolver.findBridgedMethod(ClassUtils.getMostSpecificMethod(invocation.getMethod(), invocation.getThis().getClass()));
result = execVal.validateParameters(invocation.getThis(), methodToValidate, invocation.getArguments(), groups);
}
if (!result.isEmpty()) {
Class serviceInterface = invocation.getMethod().getDeclaringClass();
Set<ConstraintViolation<Object>> resultViolations = result.stream().map(violation -> new ServiceMethodConstraintViolation(serviceInterface, violation)).collect(Collectors.toSet());
throw new MethodParametersValidationException("Service method parameters validation failed", resultViolations);
}
Object returnValue = invocation.proceed();
log.trace("Validating service call result: {}", invocation.getStaticPart());
result = execVal.validateReturnValue(invocation.getThis(), methodToValidate, returnValue, groups);
if (!result.isEmpty()) {
Class serviceInterface = invocation.getMethod().getDeclaringClass();
Set<ConstraintViolation<Object>> paramsViolations = result.stream().map(violation -> new ServiceMethodConstraintViolation(serviceInterface, violation)).collect(Collectors.toSet());
throw new MethodResultValidationException("Service method result validation failed", paramsViolations);
}
return returnValue;
}
use of com.haulmont.cuba.core.global.validation.MethodParametersValidationException in project cuba by cuba-platform.
the class ServiceInterceptor method validateMethodParameters.
protected void validateMethodParameters(ProceedingJoinPoint ctx, @Nullable ValidateServiceMethodContext validatedContext) {
if (validatedContext != null) {
log.trace("Validating service call params: {}", ctx.getSignature());
ExecutableValidator validator = validatedContext.getValidator();
Class[] constraintGroups = validatedContext.getGroups();
if (constraintGroups.length == 0) {
constraintGroups = new Class[] { Default.class, ServiceParametersChecks.class };
}
Set<ConstraintViolation<Object>> violations = validator.validateParameters(validatedContext.getTarget(), validatedContext.getMethod(), validatedContext.getArgs(), constraintGroups);
if (!violations.isEmpty()) {
Class serviceInterface = ctx.getSignature().getDeclaringType();
Set<ConstraintViolation<Object>> resultViolations = violations.stream().map(violation -> new ServiceMethodConstraintViolation(serviceInterface, violation)).collect(Collectors.toSet());
throw new MethodParametersValidationException("Service method parameters validation failed", resultViolations);
}
}
}
Aggregations