update validator package

This commit is contained in:
Efstratios Giannopoulos 2024-01-17 12:04:33 +02:00
parent b76a158824
commit 70c94c5bdc
3 changed files with 2 additions and 63 deletions

View File

@ -32,7 +32,7 @@
<dependency> <dependency>
<groupId>gr.cite</groupId> <groupId>gr.cite</groupId>
<artifactId>validation</artifactId> <artifactId>validation</artifactId>
<version>3.0.0</version> <version>3.0.1</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>gr.cite</groupId> <groupId>gr.cite</groupId>

View File

@ -330,7 +330,7 @@
<dependency> <dependency>
<groupId>gr.cite</groupId> <groupId>gr.cite</groupId>
<artifactId>validation</artifactId> <artifactId>validation</artifactId>
<version>3.0.0</version> <version>3.0.1</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>gr.cite</groupId> <groupId>gr.cite</groupId>

View File

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