ContentCachingRequestWrapper converts IOException to IllegalStateException

Issue: SPR-12810
This commit is contained in:
Juergen Hoeller 2015-03-13 20:19:31 +01:00
parent b352dbfdeb
commit ce84fafde4
1 changed files with 23 additions and 18 deletions

View File

@ -26,7 +26,6 @@ import java.util.Enumeration;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.servlet.ServletInputStream; import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper; import javax.servlet.http.HttpServletRequestWrapper;
@ -39,6 +38,7 @@ import javax.servlet.http.HttpServletRequestWrapper;
* <p>Used e.g. by {@link org.springframework.web.filter.AbstractRequestLoggingFilter}. * <p>Used e.g. by {@link org.springframework.web.filter.AbstractRequestLoggingFilter}.
* *
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Brian Clozel
* @since 4.1.3 * @since 4.1.3
*/ */
public class ContentCachingRequestWrapper extends HttpServletRequestWrapper { public class ContentCachingRequestWrapper extends HttpServletRequestWrapper {
@ -47,6 +47,7 @@ public class ContentCachingRequestWrapper extends HttpServletRequestWrapper {
private static final String METHOD_POST = "POST"; private static final String METHOD_POST = "POST";
private final ByteArrayOutputStream cachedContent; private final ByteArrayOutputStream cachedContent;
private ServletInputStream inputStream; private ServletInputStream inputStream;
@ -89,42 +90,44 @@ public class ContentCachingRequestWrapper extends HttpServletRequestWrapper {
@Override @Override
public String getParameter(String name) { public String getParameter(String name) {
if(this.cachedContent.size() == 0 && isFormPost()) { if (this.cachedContent.size() == 0 && isFormPost()) {
writeRequestParamsToContent(); writeRequestParametersToCachedContent();
} }
return super.getParameter(name); return super.getParameter(name);
} }
@Override @Override
public Map<String, String[]> getParameterMap() { public Map<String, String[]> getParameterMap() {
if(this.cachedContent.size() == 0 && isFormPost()) { if (this.cachedContent.size() == 0 && isFormPost()) {
writeRequestParamsToContent(); writeRequestParametersToCachedContent();
} }
return super.getParameterMap(); return super.getParameterMap();
} }
@Override @Override
public Enumeration<String> getParameterNames() { public Enumeration<String> getParameterNames() {
if(this.cachedContent.size() == 0 && isFormPost()) { if (this.cachedContent.size() == 0 && isFormPost()) {
writeRequestParamsToContent(); writeRequestParametersToCachedContent();
} }
return super.getParameterNames(); return super.getParameterNames();
} }
@Override @Override
public String[] getParameterValues(String name) { public String[] getParameterValues(String name) {
if(this.cachedContent.size() == 0 && isFormPost()) { if (this.cachedContent.size() == 0 && isFormPost()) {
writeRequestParamsToContent(); writeRequestParametersToCachedContent();
} }
return super.getParameterValues(name); return super.getParameterValues(name);
} }
private boolean isFormPost() { private boolean isFormPost() {
return (getContentType() != null && getContentType().contains(FORM_CONTENT_TYPE) && String contentType = getContentType();
return (contentType != null && contentType.contains(FORM_CONTENT_TYPE) &&
METHOD_POST.equalsIgnoreCase(getMethod())); METHOD_POST.equalsIgnoreCase(getMethod()));
} }
private void writeRequestParamsToContent() { private void writeRequestParametersToCachedContent() {
try { try {
if (this.cachedContent.size() == 0) { if (this.cachedContent.size() == 0) {
String requestEncoding = getCharacterEncoding(); String requestEncoding = getCharacterEncoding();
@ -134,23 +137,23 @@ public class ContentCachingRequestWrapper extends HttpServletRequestWrapper {
List<String> values = Arrays.asList(form.get(name)); List<String> values = Arrays.asList(form.get(name));
for (Iterator<String> valueIterator = values.iterator(); valueIterator.hasNext(); ) { for (Iterator<String> valueIterator = values.iterator(); valueIterator.hasNext(); ) {
String value = valueIterator.next(); String value = valueIterator.next();
cachedContent.write(URLEncoder.encode(name, requestEncoding).getBytes()); this.cachedContent.write(URLEncoder.encode(name, requestEncoding).getBytes());
if (value != null) { if (value != null) {
cachedContent.write('='); this.cachedContent.write('=');
cachedContent.write(URLEncoder.encode(value, requestEncoding).getBytes()); this.cachedContent.write(URLEncoder.encode(value, requestEncoding).getBytes());
if (valueIterator.hasNext()) { if (valueIterator.hasNext()) {
cachedContent.write('&'); this.cachedContent.write('&');
} }
} }
} }
if (nameIterator.hasNext()) { if (nameIterator.hasNext()) {
cachedContent.write('&'); this.cachedContent.write('&');
} }
} }
} }
} }
catch (IOException e) { catch (IOException ex) {
throw new RuntimeException(e); throw new IllegalStateException("Failed to write request parameters to cached content", ex);
} }
} }
@ -161,6 +164,7 @@ public class ContentCachingRequestWrapper extends HttpServletRequestWrapper {
return this.cachedContent.toByteArray(); return this.cachedContent.toByteArray();
} }
private class ContentCachingInputStream extends ServletInputStream { private class ContentCachingInputStream extends ServletInputStream {
private final ServletInputStream is; private final ServletInputStream is;
@ -178,4 +182,5 @@ public class ContentCachingRequestWrapper extends HttpServletRequestWrapper {
return ch; return ch;
} }
} }
} }