|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2014 the original author or authors. |
| 2 | + * Copyright 2002-2015 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
20 | 20 | import java.io.ByteArrayOutputStream;
|
21 | 21 | import java.io.IOException;
|
22 | 22 | import java.io.InputStreamReader;
|
| 23 | +import java.io.UnsupportedEncodingException; |
| 24 | +import java.net.URLEncoder; |
| 25 | +import java.util.Arrays; |
| 26 | +import java.util.Enumeration; |
| 27 | +import java.util.Iterator; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | + |
23 | 31 | import javax.servlet.ServletInputStream;
|
24 | 32 | import javax.servlet.http.HttpServletRequest;
|
25 | 33 | import javax.servlet.http.HttpServletRequestWrapper;
|
|
36 | 44 | */
|
37 | 45 | public class ContentCachingRequestWrapper extends HttpServletRequestWrapper {
|
38 | 46 |
|
| 47 | + private static final String FORM_CONTENT_TYPE = "application/x-www-form-urlencoded"; |
| 48 | + |
| 49 | + private static final String METHOD_POST = "POST"; |
| 50 | + |
39 | 51 | private final ByteArrayOutputStream cachedContent;
|
40 | 52 |
|
41 | 53 | private ServletInputStream inputStream;
|
@@ -80,9 +92,46 @@ public BufferedReader getReader() throws IOException {
|
80 | 92 | * Return the cached request content as a byte array.
|
81 | 93 | */
|
82 | 94 | public byte[] getContentAsByteArray() {
|
| 95 | + if(this.cachedContent.size() == 0 && isFormPost()) { |
| 96 | + writeRequestParamsToContent(); |
| 97 | + } |
83 | 98 | return this.cachedContent.toByteArray();
|
84 | 99 | }
|
85 | 100 |
|
| 101 | + private boolean isFormPost() { |
| 102 | + return (getContentType() != null && getContentType().contains(FORM_CONTENT_TYPE) && |
| 103 | + METHOD_POST.equalsIgnoreCase(getMethod())); |
| 104 | + } |
| 105 | + |
| 106 | + private void writeRequestParamsToContent() { |
| 107 | + try { |
| 108 | + if (this.cachedContent.size() == 0) { |
| 109 | + String requestEncoding = getCharacterEncoding(); |
| 110 | + Map<String, String[]> form = getParameterMap(); |
| 111 | + for (Iterator<String> nameIterator = form.keySet().iterator(); nameIterator.hasNext(); ) { |
| 112 | + String name = nameIterator.next(); |
| 113 | + List<String> values = Arrays.asList(form.get(name)); |
| 114 | + for (Iterator<String> valueIterator = values.iterator(); valueIterator.hasNext(); ) { |
| 115 | + String value = valueIterator.next(); |
| 116 | + cachedContent.write(URLEncoder.encode(name, requestEncoding).getBytes()); |
| 117 | + if (value != null) { |
| 118 | + cachedContent.write('='); |
| 119 | + cachedContent.write(URLEncoder.encode(value, requestEncoding).getBytes()); |
| 120 | + if (valueIterator.hasNext()) { |
| 121 | + cachedContent.write('&'); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + if (nameIterator.hasNext()) { |
| 126 | + cachedContent.write('&'); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + catch (IOException e) { |
| 132 | + throw new RuntimeException(e); |
| 133 | + } |
| 134 | + } |
86 | 135 |
|
87 | 136 | private class ContentCachingInputStream extends ServletInputStream {
|
88 | 137 |
|
|
0 commit comments