Search in sources :

Example 1 with MethodResultValidationException

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);
        }
    }
}
Also used : ServiceParametersChecks(com.haulmont.cuba.core.global.validation.groups.ServiceParametersChecks) Persistence(com.haulmont.cuba.core.Persistence) ServiceResultChecks(com.haulmont.cuba.core.global.validation.groups.ServiceResultChecks) LoggerFactory(org.slf4j.LoggerFactory) MethodResultValidationException(com.haulmont.cuba.core.global.validation.MethodResultValidationException) ServerConfig(com.haulmont.cuba.core.app.ServerConfig) ExecutableValidator(javax.validation.executable.ExecutableValidator) com.haulmont.cuba.core.global(com.haulmont.cuba.core.global) UserSession(com.haulmont.cuba.security.global.UserSession) ServiceMethodConstraintViolation(com.haulmont.cuba.core.global.validation.ServiceMethodConstraintViolation) MiddlewareStatisticsAccumulator(com.haulmont.cuba.core.app.MiddlewareStatisticsAccumulator) ConstraintViolation(javax.validation.ConstraintViolation) Method(java.lang.reflect.Method) Nullable(javax.annotation.Nullable) Default(javax.validation.groups.Default) MethodInvocationProceedingJoinPoint(org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint) Validated(org.springframework.validation.annotation.Validated) Logger(org.slf4j.Logger) AnnotationUtils(org.springframework.core.annotation.AnnotationUtils) Set(java.util.Set) MethodParametersValidationException(com.haulmont.cuba.core.global.validation.MethodParametersValidationException) Collectors(java.util.stream.Collectors) NoUserSessionException(com.haulmont.cuba.security.global.NoUserSessionException) UserSessionsAPI(com.haulmont.cuba.security.app.UserSessionsAPI) MethodSignature(org.aspectj.lang.reflect.MethodSignature) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) ServiceMethodConstraintViolation(com.haulmont.cuba.core.global.validation.ServiceMethodConstraintViolation) ExecutableValidator(javax.validation.executable.ExecutableValidator) MethodResultValidationException(com.haulmont.cuba.core.global.validation.MethodResultValidationException) ServiceMethodConstraintViolation(com.haulmont.cuba.core.global.validation.ServiceMethodConstraintViolation) ConstraintViolation(javax.validation.ConstraintViolation)

Example 2 with MethodResultValidationException

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;
}
Also used : Default(javax.validation.groups.Default) ServiceParametersChecks(com.haulmont.cuba.core.global.validation.groups.ServiceParametersChecks) Validated(org.springframework.validation.annotation.Validated) Logger(org.slf4j.Logger) ClassUtils(org.springframework.util.ClassUtils) AnnotationUtils(org.springframework.core.annotation.AnnotationUtils) LoggerFactory(org.slf4j.LoggerFactory) MethodResultValidationException(com.haulmont.cuba.core.global.validation.MethodResultValidationException) Set(java.util.Set) MethodParametersValidationException(com.haulmont.cuba.core.global.validation.MethodParametersValidationException) BridgeMethodResolver(org.springframework.core.BridgeMethodResolver) ExecutableValidator(javax.validation.executable.ExecutableValidator) Collectors(java.util.stream.Collectors) BeanValidation(com.haulmont.cuba.core.global.BeanValidation) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) MethodInvocation(org.aopalliance.intercept.MethodInvocation) ServiceMethodConstraintViolation(com.haulmont.cuba.core.global.validation.ServiceMethodConstraintViolation) FactoryBean(org.springframework.beans.factory.FactoryBean) SmartFactoryBean(org.springframework.beans.factory.SmartFactoryBean) ConstraintViolation(javax.validation.ConstraintViolation) Method(java.lang.reflect.Method) ServiceMethodConstraintViolation(com.haulmont.cuba.core.global.validation.ServiceMethodConstraintViolation) ExecutableValidator(javax.validation.executable.ExecutableValidator) MethodResultValidationException(com.haulmont.cuba.core.global.validation.MethodResultValidationException) ServiceMethodConstraintViolation(com.haulmont.cuba.core.global.validation.ServiceMethodConstraintViolation) ConstraintViolation(javax.validation.ConstraintViolation) Method(java.lang.reflect.Method) MethodParametersValidationException(com.haulmont.cuba.core.global.validation.MethodParametersValidationException)

Aggregations

MethodParametersValidationException (com.haulmont.cuba.core.global.validation.MethodParametersValidationException)2 MethodResultValidationException (com.haulmont.cuba.core.global.validation.MethodResultValidationException)2 ServiceMethodConstraintViolation (com.haulmont.cuba.core.global.validation.ServiceMethodConstraintViolation)2 ServiceParametersChecks (com.haulmont.cuba.core.global.validation.groups.ServiceParametersChecks)2 Method (java.lang.reflect.Method)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 ConstraintViolation (javax.validation.ConstraintViolation)2 ExecutableValidator (javax.validation.executable.ExecutableValidator)2 Default (javax.validation.groups.Default)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 AnnotationUtils (org.springframework.core.annotation.AnnotationUtils)2 Validated (org.springframework.validation.annotation.Validated)2 Persistence (com.haulmont.cuba.core.Persistence)1 MiddlewareStatisticsAccumulator (com.haulmont.cuba.core.app.MiddlewareStatisticsAccumulator)1 ServerConfig (com.haulmont.cuba.core.app.ServerConfig)1 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)1 BeanValidation (com.haulmont.cuba.core.global.BeanValidation)1 ServiceResultChecks (com.haulmont.cuba.core.global.validation.groups.ServiceResultChecks)1