Search in sources :

Example 1 with ValidationResult

use of org.craftercms.commons.validation.ValidationResult in project commons by craftercms.

the class ValidateParamsAspect method doValidation.

@Before("@within(org.craftercms.commons.validation.annotations.param.ValidateParams) || " + "@annotation(org.craftercms.commons.validation.annotations.param.ValidateParams)")
public void doValidation(JoinPoint joinPoint) {
    Object[] args = joinPoint.getArgs();
    Method method = AopUtils.getActualMethod(joinPoint);
    Annotation[][] allParamAnnotations = method.getParameterAnnotations();
    ValidationResult result = new ValidationResult(errorMessageBundle);
    if (ArrayUtils.isNotEmpty(allParamAnnotations)) {
        for (int i = 0; i < args.length; i++) {
            Object param = args[i];
            Annotation[] paramAnnotations = allParamAnnotations[i];
            for (Annotation annotation : paramAnnotations) {
                validateParam(annotation, param, result);
            }
        }
    }
    if (result.hasErrors()) {
        String methodStr = method.toGenericString();
        result.setMessage(ValidationUtils.getErrorMessage(errorMessageBundle, INVALID_METHOD_PARAMS_ERROR_CODE, methodStr));
        throw new ValidationRuntimeException(result);
    }
}
Also used : Method(java.lang.reflect.Method) ValidationRuntimeException(org.craftercms.commons.validation.ValidationRuntimeException) ValidationResult(org.craftercms.commons.validation.ValidationResult) JoinPoint(org.aspectj.lang.JoinPoint) Annotation(java.lang.annotation.Annotation) Before(org.aspectj.lang.annotation.Before)

Example 2 with ValidationResult

use of org.craftercms.commons.validation.ValidationResult in project commons by craftercms.

the class ValidationAwareRestExceptionHandlers method handleHttpMessageNotReadable.

@Override
protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatus status, WebRequest webRequest) {
    Throwable cause = ExceptionUtils.getRootCause(ex);
    if (cause instanceof UnrecognizedPropertyException) {
        UnrecognizedPropertyException upe = (UnrecognizedPropertyException) cause;
        String field = upe.getPropertyName();
        ValidationResult result = new ValidationResult();
        result.addError(field, ValidationUtils.getErrorMessage(errorMessageBundle, FIELD_UNRECOGNIZED_ERROR_CODE));
        return handleExceptionInternal(ex, result, new HttpHeaders(), HttpStatus.BAD_REQUEST, webRequest);
    } else {
        String message = ValidationUtils.getErrorMessage(errorMessageBundle, INVALID_REQUEST_BODY_ERROR_CODE);
        return handleExceptionInternal(ex, message, new HttpHeaders(), HttpStatus.BAD_REQUEST, webRequest);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) UnrecognizedPropertyException(com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException) ValidationResult(org.craftercms.commons.validation.ValidationResult)

Aggregations

ValidationResult (org.craftercms.commons.validation.ValidationResult)2 UnrecognizedPropertyException (com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException)1 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 JoinPoint (org.aspectj.lang.JoinPoint)1 Before (org.aspectj.lang.annotation.Before)1 ValidationRuntimeException (org.craftercms.commons.validation.ValidationRuntimeException)1 HttpHeaders (org.springframework.http.HttpHeaders)1