diff --git a/spring-web/src/main/java/org/springframework/web/util/ContentCachingRequestWrapper.java b/spring-web/src/main/java/org/springframework/web/util/ContentCachingRequestWrapper.java index 27a1c441a9c5..57fefe535fdf 100644 --- a/spring-web/src/main/java/org/springframework/web/util/ContentCachingRequestWrapper.java +++ b/spring-web/src/main/java/org/springframework/web/util/ContentCachingRequestWrapper.java @@ -20,7 +20,6 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStreamReader; -import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.Arrays; import java.util.Enumeration; @@ -88,14 +87,36 @@ public BufferedReader getReader() throws IOException { return this.reader; } - /** - * Return the cached request content as a byte array. - */ - public byte[] getContentAsByteArray() { + @Override + public String getParameter(String name) { if(this.cachedContent.size() == 0 && isFormPost()) { writeRequestParamsToContent(); } - return this.cachedContent.toByteArray(); + return super.getParameter(name); + } + + @Override + public Map getParameterMap() { + if(this.cachedContent.size() == 0 && isFormPost()) { + writeRequestParamsToContent(); + } + return super.getParameterMap(); + } + + @Override + public Enumeration getParameterNames() { + if(this.cachedContent.size() == 0 && isFormPost()) { + writeRequestParamsToContent(); + } + return super.getParameterNames(); + } + + @Override + public String[] getParameterValues(String name) { + if(this.cachedContent.size() == 0 && isFormPost()) { + writeRequestParamsToContent(); + } + return super.getParameterValues(name); } private boolean isFormPost() { @@ -107,7 +128,7 @@ private void writeRequestParamsToContent() { try { if (this.cachedContent.size() == 0) { String requestEncoding = getCharacterEncoding(); - Map form = getParameterMap(); + Map form = super.getParameterMap(); for (Iterator nameIterator = form.keySet().iterator(); nameIterator.hasNext(); ) { String name = nameIterator.next(); List values = Arrays.asList(form.get(name)); @@ -133,6 +154,13 @@ private void writeRequestParamsToContent() { } } + /** + * Return the cached request content as a byte array. + */ + public byte[] getContentAsByteArray() { + return this.cachedContent.toByteArray(); + } + private class ContentCachingInputStream extends ServletInputStream { private final ServletInputStream is; @@ -150,5 +178,4 @@ public int read() throws IOException { return ch; } } - } diff --git a/spring-web/src/test/java/org/springframework/web/util/ContentCachingRequestWrapperTests.java b/spring-web/src/test/java/org/springframework/web/util/ContentCachingRequestWrapperTests.java index 45e383efbddd..69661536f95a 100644 --- a/spring-web/src/test/java/org/springframework/web/util/ContentCachingRequestWrapperTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/ContentCachingRequestWrapperTests.java @@ -61,6 +61,23 @@ public void requestParams() throws Exception { // getting request parameters will consume the request body Assert.assertFalse(wrapper.getParameterMap().isEmpty()); Assert.assertEquals("first=value&second=foo&second=bar", new String(wrapper.getContentAsByteArray())); + // SPR-12810 : inputstream body should be consumed + Assert.assertEquals("", new String(FileCopyUtils.copyToByteArray(wrapper.getInputStream()))); + } + + // SPR-12810 + @Test + public void inputStreamFormPostRequest() throws Exception { + this.request.setMethod("POST"); + this.request.setContentType(FORM_CONTENT_TYPE); + this.request.setCharacterEncoding(CHARSET); + this.request.setParameter("first", "value"); + this.request.setParameter("second", new String[] {"foo", "bar"}); + + ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(this.request); + + byte[] response = FileCopyUtils.copyToByteArray(wrapper.getInputStream()); + Assert.assertArrayEquals(response, wrapper.getContentAsByteArray()); } }