|
| 1 | +package com.github.JanLoebel.jsonschemavalidation.advice; |
| 2 | + |
| 3 | + |
| 4 | +import com.fasterxml.jackson.databind.JsonNode; |
| 5 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 6 | +import com.github.JanLoebel.jsonschemavalidation.JsonSchemaValidation; |
| 7 | +import com.github.JanLoebel.jsonschemavalidation.provider.JsonSchemaProvider; |
| 8 | +import com.networknt.schema.ValidationMessage; |
| 9 | +import org.springframework.core.MethodParameter; |
| 10 | +import org.springframework.http.HttpHeaders; |
| 11 | +import org.springframework.http.HttpInputMessage; |
| 12 | +import org.springframework.http.converter.HttpMessageConverter; |
| 13 | +import org.springframework.util.StreamUtils; |
| 14 | +import org.springframework.web.bind.annotation.ControllerAdvice; |
| 15 | +import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice; |
| 16 | + |
| 17 | +import java.io.ByteArrayInputStream; |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.InputStream; |
| 20 | +import java.lang.reflect.Type; |
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | +import java.util.Set; |
| 23 | + |
| 24 | +@ControllerAdvice |
| 25 | +public class JsonValidationRequestBodyControllerAdvice implements RequestBodyAdvice { |
| 26 | + |
| 27 | + private ObjectMapper objectMapper; |
| 28 | + private JsonSchemaProvider jsonSchemaProvider; |
| 29 | + |
| 30 | + public JsonValidationRequestBodyControllerAdvice(ObjectMapper objectMapper, JsonSchemaProvider jsonSchemaProvider) { |
| 31 | + this.objectMapper = objectMapper; |
| 32 | + this.jsonSchemaProvider = jsonSchemaProvider; |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public boolean supports(MethodParameter parameter, |
| 37 | + Type targetType, Class<? extends HttpMessageConverter<?>> converterType) { |
| 38 | + return parameter.getNestedParameterType().isAnnotationPresent(JsonSchemaValidation.class); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, |
| 43 | + Type targetType, Class<? extends HttpMessageConverter<?>> converterType) |
| 44 | + throws IOException { |
| 45 | + final String body = toString(inputMessage); |
| 46 | + |
| 47 | + final String uri = extractSchemaUri(parameter); |
| 48 | + final JsonNode jsonNode = this.objectMapper.readTree(body); |
| 49 | + final Set<ValidationMessage> validationMessages = this.jsonSchemaProvider.loadSchema(uri).validate(jsonNode); |
| 50 | + this.jsonSchemaProvider.handleValidationMessages(validationMessages); |
| 51 | + |
| 52 | + return buildHttpInputMessage(body, inputMessage.getHeaders()); |
| 53 | + } |
| 54 | + |
| 55 | + private String extractSchemaUri(MethodParameter parameter) { |
| 56 | + final JsonSchemaValidation annotation = |
| 57 | + parameter.getNestedParameterType().getAnnotation(JsonSchemaValidation.class); |
| 58 | + return annotation.value(); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, |
| 63 | + Type targetType, Class<? extends HttpMessageConverter<?>> converterType) { |
| 64 | + // Do not touch |
| 65 | + return body; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter, |
| 70 | + Type targetType, Class<? extends HttpMessageConverter<?>> converterType) { |
| 71 | + // Do not touch |
| 72 | + return body; |
| 73 | + } |
| 74 | + |
| 75 | + private String toString(HttpInputMessage inputMessage) throws IOException { |
| 76 | + final InputStream inputStream = inputMessage.getBody(); |
| 77 | + return StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8); |
| 78 | + } |
| 79 | + |
| 80 | + private InputStream fromString(String body) { |
| 81 | + return new ByteArrayInputStream(body.getBytes()); |
| 82 | + } |
| 83 | + |
| 84 | + private HttpInputMessage buildHttpInputMessage(String body, HttpHeaders httpHeaders) { |
| 85 | + return new HttpInputMessage() { |
| 86 | + @Override |
| 87 | + public InputStream getBody() { |
| 88 | + return fromString(body); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public HttpHeaders getHeaders() { |
| 93 | + return httpHeaders; |
| 94 | + } |
| 95 | + }; |
| 96 | + } |
| 97 | +} |
0 commit comments