1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| package compressionFilters; |
18 |
| |
19 |
| import java.io.IOException; |
20 |
| import java.io.OutputStream; |
21 |
| import java.io.OutputStreamWriter; |
22 |
| import java.io.PrintWriter; |
23 |
| import java.util.Locale; |
24 |
| import javax.servlet.ServletRequest; |
25 |
| import javax.servlet.ServletResponse; |
26 |
| import javax.servlet.ServletException; |
27 |
| import javax.servlet.ServletOutputStream; |
28 |
| import javax.servlet.ServletResponse; |
29 |
| import javax.servlet.ServletResponseWrapper; |
30 |
| import javax.servlet.http.HttpServletResponse; |
31 |
| import javax.servlet.http.HttpServletResponseWrapper; |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| public class CompressionServletResponseWrapper extends HttpServletResponseWrapper { |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
0
| public CompressionServletResponseWrapper(HttpServletResponse response) {
|
52 |
0
| super(response);
|
53 |
0
| origResponse = response;
|
54 |
0
| if (debug > 1) {
|
55 |
0
| System.out.println("CompressionServletResponseWrapper constructor gets called");
|
56 |
| } |
57 |
| } |
58 |
| |
59 |
| |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
| protected HttpServletResponse origResponse = null; |
67 |
| |
68 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
| protected static final String info = "CompressionServletResponseWrapper"; |
73 |
| |
74 |
| |
75 |
| |
76 |
| |
77 |
| |
78 |
| |
79 |
| protected ServletOutputStream stream = null; |
80 |
| |
81 |
| |
82 |
| |
83 |
| |
84 |
| |
85 |
| |
86 |
| |
87 |
| protected PrintWriter writer = null; |
88 |
| |
89 |
| |
90 |
| |
91 |
| |
92 |
| protected int threshold = 0; |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
| private int debug = 0; |
98 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
| protected String contentType = null; |
103 |
| |
104 |
| |
105 |
| |
106 |
| |
107 |
| |
108 |
| |
109 |
| |
110 |
0
| public void setContentType(String contentType) {
|
111 |
0
| if (debug > 1) {
|
112 |
0
| System.out.println("setContentType to "+contentType);
|
113 |
| } |
114 |
0
| this.contentType = contentType;
|
115 |
0
| origResponse.setContentType(contentType);
|
116 |
| } |
117 |
| |
118 |
| |
119 |
| |
120 |
| |
121 |
| |
122 |
0
| public void setCompressionThreshold(int threshold) {
|
123 |
0
| if (debug > 1) {
|
124 |
0
| System.out.println("setCompressionThreshold to " + threshold);
|
125 |
| } |
126 |
0
| this.threshold = threshold;
|
127 |
| } |
128 |
| |
129 |
| |
130 |
| |
131 |
| |
132 |
| |
133 |
0
| public void setDebugLevel(int debug) {
|
134 |
0
| this.debug = debug;
|
135 |
| } |
136 |
| |
137 |
| |
138 |
| |
139 |
| |
140 |
| |
141 |
| |
142 |
| |
143 |
| |
144 |
0
| public ServletOutputStream createOutputStream() throws IOException {
|
145 |
0
| if (debug > 1) {
|
146 |
0
| System.out.println("createOutputStream gets called");
|
147 |
| } |
148 |
| |
149 |
0
| CompressionResponseStream stream = new CompressionResponseStream(origResponse);
|
150 |
0
| stream.setDebugLevel(debug);
|
151 |
0
| stream.setBuffer(threshold);
|
152 |
| |
153 |
0
| return stream;
|
154 |
| |
155 |
| } |
156 |
| |
157 |
| |
158 |
| |
159 |
| |
160 |
| |
161 |
0
| public void finishResponse() {
|
162 |
0
| try {
|
163 |
0
| if (writer != null) {
|
164 |
0
| writer.close();
|
165 |
| } else { |
166 |
0
| if (stream != null)
|
167 |
0
| stream.close();
|
168 |
| } |
169 |
| } catch (IOException e) { |
170 |
| } |
171 |
| } |
172 |
| |
173 |
| |
174 |
| |
175 |
| |
176 |
| |
177 |
| |
178 |
| |
179 |
| |
180 |
| |
181 |
| |
182 |
0
| public void flushBuffer() throws IOException {
|
183 |
0
| if (debug > 1) {
|
184 |
0
| System.out.println("flush buffer @ CompressionServletResponseWrapper");
|
185 |
| } |
186 |
0
| ((CompressionResponseStream)stream).flush();
|
187 |
| |
188 |
| } |
189 |
| |
190 |
| |
191 |
| |
192 |
| |
193 |
| |
194 |
| |
195 |
| |
196 |
| |
197 |
0
| public ServletOutputStream getOutputStream() throws IOException {
|
198 |
| |
199 |
0
| if (writer != null)
|
200 |
0
| throw new IllegalStateException("getWriter() has already been called for this response");
|
201 |
| |
202 |
0
| if (stream == null)
|
203 |
0
| stream = createOutputStream();
|
204 |
0
| if (debug > 1) {
|
205 |
0
| System.out.println("stream is set to "+stream+" in getOutputStream");
|
206 |
| } |
207 |
| |
208 |
0
| return (stream);
|
209 |
| |
210 |
| } |
211 |
| |
212 |
| |
213 |
| |
214 |
| |
215 |
| |
216 |
| |
217 |
| |
218 |
| |
219 |
0
| public PrintWriter getWriter() throws IOException {
|
220 |
| |
221 |
0
| if (writer != null)
|
222 |
0
| return (writer);
|
223 |
| |
224 |
0
| if (stream != null)
|
225 |
0
| throw new IllegalStateException("getOutputStream() has already been called for this response");
|
226 |
| |
227 |
0
| stream = createOutputStream();
|
228 |
0
| if (debug > 1) {
|
229 |
0
| System.out.println("stream is set to "+stream+" in getWriter");
|
230 |
| } |
231 |
| |
232 |
0
| String charEnc = origResponse.getCharacterEncoding();
|
233 |
0
| if (debug > 1) {
|
234 |
0
| System.out.println("character encoding is " + charEnc);
|
235 |
| } |
236 |
| |
237 |
| |
238 |
0
| if (charEnc != null) {
|
239 |
0
| writer = new PrintWriter(new OutputStreamWriter(stream, charEnc));
|
240 |
| } else { |
241 |
0
| writer = new PrintWriter(stream);
|
242 |
| } |
243 |
| |
244 |
0
| return (writer);
|
245 |
| |
246 |
| } |
247 |
| |
248 |
| |
249 |
0
| public void setContentLength(int length) {
|
250 |
| } |
251 |
| |
252 |
| |
253 |
| |
254 |
| |
255 |
| |
256 |
| |
257 |
0
| private static String getCharsetFromContentType(String type) {
|
258 |
| |
259 |
0
| if (type == null) {
|
260 |
0
| return null;
|
261 |
| } |
262 |
0
| int semi = type.indexOf(";");
|
263 |
0
| if (semi == -1) {
|
264 |
0
| return null;
|
265 |
| } |
266 |
0
| String afterSemi = type.substring(semi + 1);
|
267 |
0
| int charsetLocation = afterSemi.indexOf("charset=");
|
268 |
0
| if(charsetLocation == -1) {
|
269 |
0
| return null;
|
270 |
| } else { |
271 |
0
| String afterCharset = afterSemi.substring(charsetLocation + 8);
|
272 |
0
| String encoding = afterCharset.trim();
|
273 |
0
| return encoding;
|
274 |
| } |
275 |
| } |
276 |
| |
277 |
| } |