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 28339d375e9..e93367b6515 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 @@ -44,13 +44,11 @@ import org.springframework.util.Assert; */ public class ServletServerHttpRequest implements ServerHttpRequest { - private static final String FORM_CONTENT_TYPE = "application/x-www-form-urlencoded"; + protected static final String FORM_CONTENT_TYPE = "application/x-www-form-urlencoded"; - private static final String POST_METHOD = "POST"; + protected static final String FORM_CHARSET = "UTF-8"; - private static final String PUT_METHOD = "PUT"; - - private static final String FORM_CHARSET = "UTF-8"; + private static final String METHOD_POST = "POST"; private final HttpServletRequest servletRequest; @@ -105,20 +103,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) { - return FORM_CONTENT_TYPE.equals(request.getContentType()) && - (POST_METHOD.equalsIgnoreCase(request.getMethod()) || PUT_METHOD.equalsIgnoreCase(request.getMethod())); + private boolean isFormPost(HttpServletRequest request) { + return (request.getContentType() != null && request.getContentType().contains(FORM_CONTENT_TYPE) && + METHOD_POST.equalsIgnoreCase(request.getMethod())); } - private InputStream getFormBody(HttpServletRequest request) throws IOException { + /** + * Use {@link javax.servlet.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);