Add debug information on broken input streams.

This commit is contained in:
Paulo Gustavo Veiga 2012-09-07 19:40:08 -03:00
parent 337a67a8f6
commit 4cce432bb8
3 changed files with 79 additions and 1 deletions

View File

@ -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;
}
}

View File

@ -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<HttpMessageConverter<?>> converters) {
converters.add(converter());
}
@Bean
MappingJacksonHttpMessageConverter converter() {
return new DebugMappingJacksonHttpMessageConverter();
}
}

View File

@ -14,7 +14,6 @@
<context:component-scan base-package="com.wisemapping.rest"/> <context:component-scan base-package="com.wisemapping.rest"/>
<context:annotation-config/> <context:annotation-config/>
<mvc:annotation-driven/>
<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> <bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound"> <property name="classesToBeBound">