From 7a3f02bce94b472915be2af0d65cd33b2d370aa2 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Tue, 20 Sep 2011 09:18:47 +0000 Subject: [PATCH] SPR-8688 Don't use Servlet request params for form 'PUT' in ServletServerHttpRequest. --- .../http/server/ServletServerHttpRequest.java | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/org.springframework.web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java b/org.springframework.web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java index 4303503858..9ca65e7867 100644 --- a/org.springframework.web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java +++ b/org.springframework.web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java @@ -31,6 +31,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import javax.servlet.ServletRequest; import javax.servlet.http.HttpServletRequest; import org.springframework.http.HttpHeaders; @@ -51,9 +52,6 @@ public class ServletServerHttpRequest implements ServerHttpRequest { private static final String METHOD_POST = "POST"; - private static final String METHOD_PUT = "PUT"; - - private final HttpServletRequest servletRequest; private HttpHeaders headers; @@ -107,20 +105,26 @@ public class ServletServerHttpRequest implements ServerHttpRequest { } public InputStream getBody() throws IOException { - if (isFormSubmittal(this.servletRequest)) { - return getFormBody(this.servletRequest); + if (isFormPost(this.servletRequest)) { + return getBodyFromServletRequestParameters(this.servletRequest); } else { return this.servletRequest.getInputStream(); } } - private boolean isFormSubmittal(HttpServletRequest request) { + private boolean isFormPost(HttpServletRequest request) { return request.getContentType() != null && request.getContentType().contains(FORM_CONTENT_TYPE) && - (METHOD_POST.equalsIgnoreCase(request.getMethod()) || METHOD_PUT.equalsIgnoreCase(request.getMethod())); + (METHOD_POST.equalsIgnoreCase(request.getMethod())); } - private InputStream getFormBody(HttpServletRequest request) throws IOException { + /** + * Use {@link ServletRequest#getParameterMap()} to reconstruct the body of + * a form 'POST' providing a predictable outcome as opposed to reading + * from the body, which can fail if any other code has used ServletRequest + * to access a parameter thus causing the input stream to be "consumed". + */ + private InputStream getBodyFromServletRequestParameters(HttpServletRequest request) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); Writer writer = new OutputStreamWriter(bos, FORM_CHARSET);