55 lines
2.3 KiB
Java
55 lines
2.3 KiB
Java
|
package eu.eudat.controllers.controllerhandler;
|
||
|
|
||
|
import eu.eudat.commons.validation.BaseValidator;
|
||
|
import eu.eudat.commons.validation.ValidationFilterAnnotation;
|
||
|
import eu.eudat.logic.services.ApiContext;
|
||
|
import gr.cite.tools.exception.MyApplicationException;
|
||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||
|
import org.springframework.core.MethodParameter;
|
||
|
import org.springframework.http.HttpInputMessage;
|
||
|
import org.springframework.http.converter.HttpMessageConverter;
|
||
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||
|
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;
|
||
|
|
||
|
import java.io.IOException;
|
||
|
import java.lang.reflect.Type;
|
||
|
import java.util.Objects;
|
||
|
|
||
|
@ControllerAdvice
|
||
|
@RestControllerAdvice
|
||
|
public class ValidatorRequestBodyAdvice implements RequestBodyAdvice {
|
||
|
@Autowired
|
||
|
private ApiContext apiContext;
|
||
|
|
||
|
@Override
|
||
|
public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType,
|
||
|
Class<? extends HttpMessageConverter<?>> converterType) {
|
||
|
if (parameter.getMethod() != null) {
|
||
|
ValidationFilterAnnotation annotation = parameter.getMethod().getAnnotation((ValidationFilterAnnotation.class));
|
||
|
|
||
|
if (annotation == null || !Objects.equals(parameter.getParameterName(), annotation.argumentName())) return body;
|
||
|
|
||
|
BaseValidator validator = this.apiContext.getOperationsContext().getApplicationContext().getBean(annotation.validator(), BaseValidator.class);
|
||
|
if (validator == null) throw new MyApplicationException("validator not provided");
|
||
|
|
||
|
validator.validateForce(body);
|
||
|
}
|
||
|
return body;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
|
||
|
return body;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
|
||
|
return inputMessage;
|
||
|
}
|
||
|
}
|