use of com.haulmont.cuba.core.global.validation.MethodResultValidationException in project cuba by cuba-platform.
the class ServiceInterceptor method validateMethodResult.
protected void validateMethodResult(ProceedingJoinPoint ctx, ValidateServiceMethodContext validatedContext, Object methodResult) {
if (validatedContext != null) {
ExecutableValidator validator = validatedContext.getValidator();
log.trace("Validating service call result: {}", ctx.getSignature());
Class[] constraintGroups = validatedContext.getGroups();
if (constraintGroups.length == 0) {
constraintGroups = new Class[] { Default.class, ServiceResultChecks.class };
}
Set<ConstraintViolation<Object>> violations = validator.validateReturnValue(validatedContext.getTarget(), validatedContext.getMethod(), methodResult, constraintGroups);
if (!violations.isEmpty()) {
Class serviceInterface = ctx.getSignature().getDeclaringType();
Set<ConstraintViolation<Object>> paramsViolations = violations.stream().map(violation -> new ServiceMethodConstraintViolation(serviceInterface, violation)).collect(Collectors.toSet());
throw new MethodResultValidationException("Service method result validation failed", paramsViolations);
}
}
}
use of com.haulmont.cuba.core.global.validation.MethodResultValidationException 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;
}
Aggregations