From 4cce432bb87f6ba70b112e9b721c1ba12c9b73ec Mon Sep 17 00:00:00 2001 From: Paulo Gustavo Veiga Date: Fri, 7 Sep 2012 19:40:08 -0300 Subject: [PATCH] Add debug information on broken input streams. --- ...bugMappingJacksonHttpMessageConverter.java | 54 +++++++++++++++++++ .../rest/RestMvcConfiguration.java | 25 +++++++++ .../main/webapp/WEB-INF/wisemapping-rest.xml | 1 - 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 wise-webapp/src/main/java/com/wisemapping/rest/DebugMappingJacksonHttpMessageConverter.java create mode 100644 wise-webapp/src/main/java/com/wisemapping/rest/RestMvcConfiguration.java diff --git a/wise-webapp/src/main/java/com/wisemapping/rest/DebugMappingJacksonHttpMessageConverter.java b/wise-webapp/src/main/java/com/wisemapping/rest/DebugMappingJacksonHttpMessageConverter.java new file mode 100644 index 00000000..9337d42b --- /dev/null +++ b/wise-webapp/src/main/java/com/wisemapping/rest/DebugMappingJacksonHttpMessageConverter.java @@ -0,0 +1,54 @@ +package com.wisemapping.rest; + +import org.apache.commons.io.IOUtils; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpInputMessage; +import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; + + +public class DebugMappingJacksonHttpMessageConverter extends MappingJacksonHttpMessageConverter { + @Override + protected Object readInternal(Class clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { + final byte[] bytes = IOUtils.toByteArray(inputMessage.getBody()); + final ByteArrayInputStream bais = new ByteArrayInputStream(bytes); + final WrapHttpInputMessage wrap = new WrapHttpInputMessage(bais, inputMessage.getHeaders()); + + try { + return super.readInternal(clazz, wrap); + + } catch (org.springframework.http.converter.HttpMessageNotReadableException e) { + throw new HttpMessageNotReadableException("Request Body:\n" + new String(bytes, "UTF-8"), e); + } + } +} + +class HttpMessageNotReadableException extends org.springframework.http.converter.HttpMessageNotReadableException { + + public HttpMessageNotReadableException(String msg, org.springframework.http.converter.HttpMessageNotReadableException cause) { + super(msg, cause); + } +} + +class WrapHttpInputMessage implements HttpInputMessage { + private InputStream body; + private HttpHeaders headers; + + WrapHttpInputMessage(InputStream is, HttpHeaders headers) { + this.body = is; + this.headers = headers; + } + + @Override + public InputStream getBody() throws IOException { + return body; + } + + @Override + public HttpHeaders getHeaders() { + return headers; + } +} diff --git a/wise-webapp/src/main/java/com/wisemapping/rest/RestMvcConfiguration.java b/wise-webapp/src/main/java/com/wisemapping/rest/RestMvcConfiguration.java new file mode 100644 index 00000000..9b713bba --- /dev/null +++ b/wise-webapp/src/main/java/com/wisemapping/rest/RestMvcConfiguration.java @@ -0,0 +1,25 @@ +package com.wisemapping.rest; + +import org.jetbrains.annotations.NotNull; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; + +import java.util.List; + +@Configuration +public class RestMvcConfiguration extends WebMvcConfigurerAdapter { + + @Override + public void configureMessageConverters(@NotNull final List> converters) { + converters.add(converter()); + } + + @Bean + MappingJacksonHttpMessageConverter converter() { + return new DebugMappingJacksonHttpMessageConverter(); + } +} \ No newline at end of file diff --git a/wise-webapp/src/main/webapp/WEB-INF/wisemapping-rest.xml b/wise-webapp/src/main/webapp/WEB-INF/wisemapping-rest.xml index a9de0c13..d5ca79e6 100644 --- a/wise-webapp/src/main/webapp/WEB-INF/wisemapping-rest.xml +++ b/wise-webapp/src/main/webapp/WEB-INF/wisemapping-rest.xml @@ -14,7 +14,6 @@ -