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