2023-12-18 17:23:37 +01:00
|
|
|
package eu.eudat.controllers.controllerhandler;
|
|
|
|
|
2023-12-19 13:54:17 +01:00
|
|
|
import eu.eudat.commons.validation.Validator;
|
|
|
|
import eu.eudat.commons.validation.ValidatorFactory;
|
2023-12-18 17:23:37 +01:00
|
|
|
import eu.eudat.commons.validation.ValidationFilterAnnotation;
|
|
|
|
import gr.cite.tools.exception.MyApplicationException;
|
|
|
|
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 {
|
2023-12-19 13:54:17 +01:00
|
|
|
private final ValidatorFactory validatorFactory;
|
|
|
|
|
|
|
|
public ValidatorRequestBodyAdvice(ValidatorFactory validatorFactory) {
|
|
|
|
this.validatorFactory = validatorFactory;
|
|
|
|
}
|
2023-12-18 17:23:37 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType,
|
|
|
|
Class<? extends HttpMessageConverter<?>> converterType) {
|
|
|
|
if (parameter.getMethod() != null) {
|
2023-12-19 13:54:17 +01:00
|
|
|
ValidationFilterAnnotation[] annotations = parameter.getMethod().getAnnotationsByType((ValidationFilterAnnotation.class));
|
|
|
|
|
|
|
|
if (annotations == null) return body;
|
|
|
|
for (ValidationFilterAnnotation annotation : annotations){
|
|
|
|
if (!Objects.equals(parameter.getParameterName(), annotation.argumentName())) continue;
|
2023-12-18 17:23:37 +01:00
|
|
|
|
2023-12-19 13:54:17 +01:00
|
|
|
Validator validator = validatorFactory.validator(annotation.validator());
|
|
|
|
if (validator == null) throw new MyApplicationException("validator not provided");
|
2023-12-18 17:23:37 +01:00
|
|
|
|
2023-12-19 13:54:17 +01:00
|
|
|
validator.validateForce(body);
|
|
|
|
}
|
2023-12-18 17:23:37 +01:00
|
|
|
}
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-12-19 13:54:17 +01:00
|
|
|
public boolean supports(MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
|
|
|
|
return parameter.getMethod() != null && parameter.getMethod().isAnnotationPresent((ValidationFilterAnnotation.class));
|
2023-12-18 17:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
|
|
|
|
return inputMessage;
|
|
|
|
}
|
|
|
|
}
|