Skip to content

Commit abd7052

Browse files
committed
ContentCachingRequestWrapper converts IOException to IllegalStateException
Issue: SPR-12810 (cherry picked from commit ce84faf)
1 parent 6c169bd commit abd7052

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

spring-web/src/main/java/org/springframework/web/util/ContentCachingRequestWrapper.java

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.util.Iterator;
2727
import java.util.List;
2828
import java.util.Map;
29-
3029
import javax.servlet.ServletInputStream;
3130
import javax.servlet.http.HttpServletRequest;
3231
import javax.servlet.http.HttpServletRequestWrapper;
@@ -39,6 +38,7 @@
3938
* <p>Used e.g. by {@link org.springframework.web.filter.AbstractRequestLoggingFilter}.
4039
*
4140
* @author Juergen Hoeller
41+
* @author Brian Clozel
4242
* @since 4.1.3
4343
*/
4444
public class ContentCachingRequestWrapper extends HttpServletRequestWrapper {
@@ -47,6 +47,7 @@ public class ContentCachingRequestWrapper extends HttpServletRequestWrapper {
4747

4848
private static final String METHOD_POST = "POST";
4949

50+
5051
private final ByteArrayOutputStream cachedContent;
5152

5253
private ServletInputStream inputStream;
@@ -89,42 +90,44 @@ public BufferedReader getReader() throws IOException {
8990

9091
@Override
9192
public String getParameter(String name) {
92-
if(this.cachedContent.size() == 0 && isFormPost()) {
93-
writeRequestParamsToContent();
93+
if (this.cachedContent.size() == 0 && isFormPost()) {
94+
writeRequestParametersToCachedContent();
9495
}
9596
return super.getParameter(name);
9697
}
9798

9899
@Override
99100
public Map<String, String[]> getParameterMap() {
100-
if(this.cachedContent.size() == 0 && isFormPost()) {
101-
writeRequestParamsToContent();
101+
if (this.cachedContent.size() == 0 && isFormPost()) {
102+
writeRequestParametersToCachedContent();
102103
}
103104
return super.getParameterMap();
104105
}
105106

106107
@Override
107108
public Enumeration<String> getParameterNames() {
108-
if(this.cachedContent.size() == 0 && isFormPost()) {
109-
writeRequestParamsToContent();
109+
if (this.cachedContent.size() == 0 && isFormPost()) {
110+
writeRequestParametersToCachedContent();
110111
}
111112
return super.getParameterNames();
112113
}
113114

114115
@Override
115116
public String[] getParameterValues(String name) {
116-
if(this.cachedContent.size() == 0 && isFormPost()) {
117-
writeRequestParamsToContent();
117+
if (this.cachedContent.size() == 0 && isFormPost()) {
118+
writeRequestParametersToCachedContent();
118119
}
119120
return super.getParameterValues(name);
120121
}
121122

123+
122124
private boolean isFormPost() {
123-
return (getContentType() != null && getContentType().contains(FORM_CONTENT_TYPE) &&
125+
String contentType = getContentType();
126+
return (contentType != null && contentType.contains(FORM_CONTENT_TYPE) &&
124127
METHOD_POST.equalsIgnoreCase(getMethod()));
125128
}
126129

127-
private void writeRequestParamsToContent() {
130+
private void writeRequestParametersToCachedContent() {
128131
try {
129132
if (this.cachedContent.size() == 0) {
130133
String requestEncoding = getCharacterEncoding();
@@ -134,23 +137,23 @@ private void writeRequestParamsToContent() {
134137
List<String> values = Arrays.asList(form.get(name));
135138
for (Iterator<String> valueIterator = values.iterator(); valueIterator.hasNext(); ) {
136139
String value = valueIterator.next();
137-
cachedContent.write(URLEncoder.encode(name, requestEncoding).getBytes());
140+
this.cachedContent.write(URLEncoder.encode(name, requestEncoding).getBytes());
138141
if (value != null) {
139-
cachedContent.write('=');
140-
cachedContent.write(URLEncoder.encode(value, requestEncoding).getBytes());
142+
this.cachedContent.write('=');
143+
this.cachedContent.write(URLEncoder.encode(value, requestEncoding).getBytes());
141144
if (valueIterator.hasNext()) {
142-
cachedContent.write('&');
145+
this.cachedContent.write('&');
143146
}
144147
}
145148
}
146149
if (nameIterator.hasNext()) {
147-
cachedContent.write('&');
150+
this.cachedContent.write('&');
148151
}
149152
}
150153
}
151154
}
152-
catch (IOException e) {
153-
throw new RuntimeException(e);
155+
catch (IOException ex) {
156+
throw new IllegalStateException("Failed to write request parameters to cached content", ex);
154157
}
155158
}
156159

@@ -161,6 +164,7 @@ public byte[] getContentAsByteArray() {
161164
return this.cachedContent.toByteArray();
162165
}
163166

167+
164168
private class ContentCachingInputStream extends ServletInputStream {
165169

166170
private final ServletInputStream is;
@@ -178,4 +182,5 @@ public int read() throws IOException {
178182
return ch;
179183
}
180184
}
185+
181186
}

0 commit comments

Comments
 (0)