1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.servlets.gzip;
20
21 import java.io.IOException;
22 import java.io.OutputStream;
23 import java.io.OutputStreamWriter;
24 import java.io.PrintWriter;
25 import java.io.UnsupportedEncodingException;
26 import java.util.HashSet;
27 import java.util.Set;
28 import java.util.StringTokenizer;
29 import java.util.zip.DeflaterOutputStream;
30 import java.util.zip.GZIPOutputStream;
31
32 import javax.servlet.ServletException;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
35
36 import org.eclipse.jetty.continuation.Continuation;
37 import org.eclipse.jetty.continuation.ContinuationListener;
38 import org.eclipse.jetty.continuation.ContinuationSupport;
39 import org.eclipse.jetty.http.HttpMethod;
40 import org.eclipse.jetty.server.Request;
41 import org.eclipse.jetty.server.handler.HandlerWrapper;
42 import org.eclipse.jetty.util.log.Log;
43 import org.eclipse.jetty.util.log.Logger;
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 public class GzipHandler extends HandlerWrapper
63 {
64 private static final Logger LOG = Log.getLogger(GzipHandler.class);
65
66 protected Set<String> _mimeTypes;
67 protected Set<String> _excluded;
68 protected int _bufferSize = 8192;
69 protected int _minGzipSize = 256;
70 protected String _vary = "Accept-Encoding, User-Agent";
71
72
73
74
75
76 public GzipHandler()
77 {
78 }
79
80
81
82
83
84
85
86 public Set<String> getMimeTypes()
87 {
88 return _mimeTypes;
89 }
90
91
92
93
94
95
96
97
98 public void setMimeTypes(Set<String> mimeTypes)
99 {
100 _mimeTypes = mimeTypes;
101 }
102
103
104
105
106
107
108
109
110 public void setMimeTypes(String mimeTypes)
111 {
112 if (mimeTypes != null)
113 {
114 _mimeTypes = new HashSet<String>();
115 StringTokenizer tok = new StringTokenizer(mimeTypes,",",false);
116 while (tok.hasMoreTokens())
117 {
118 _mimeTypes.add(tok.nextToken());
119 }
120 }
121 }
122
123
124
125
126
127
128
129 public Set<String> getExcluded()
130 {
131 return _excluded;
132 }
133
134
135
136
137
138
139
140
141 public void setExcluded(Set<String> excluded)
142 {
143 _excluded = excluded;
144 }
145
146
147
148
149
150
151
152
153 public void setExcluded(String excluded)
154 {
155 if (excluded != null)
156 {
157 _excluded = new HashSet<String>();
158 StringTokenizer tok = new StringTokenizer(excluded,",",false);
159 while (tok.hasMoreTokens())
160 _excluded.add(tok.nextToken());
161 }
162 }
163
164
165
166
167
168 public String getVary()
169 {
170 return _vary;
171 }
172
173
174
175
176
177
178
179
180
181
182
183
184 public void setVary(String vary)
185 {
186 _vary = vary;
187 }
188
189
190
191
192
193
194
195 public int getBufferSize()
196 {
197 return _bufferSize;
198 }
199
200
201
202
203
204
205
206
207 public void setBufferSize(int bufferSize)
208 {
209 _bufferSize = bufferSize;
210 }
211
212
213
214
215
216
217
218 public int getMinGzipSize()
219 {
220 return _minGzipSize;
221 }
222
223
224
225
226
227
228
229
230 public void setMinGzipSize(int minGzipSize)
231 {
232 _minGzipSize = minGzipSize;
233 }
234
235
236
237
238
239 @Override
240 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
241 {
242 if (_handler!=null && isStarted())
243 {
244 String ae = request.getHeader("accept-encoding");
245 if (ae != null && ae.indexOf("gzip")>=0 && !response.containsHeader("Content-Encoding")
246 && !HttpMethod.HEAD.is(request.getMethod()))
247 {
248 if (_excluded!=null)
249 {
250 String ua = request.getHeader("User-Agent");
251 if (_excluded.contains(ua))
252 {
253 _handler.handle(target,baseRequest, request, response);
254 return;
255 }
256 }
257
258 final CompressedResponseWrapper wrappedResponse = newGzipResponseWrapper(request,response);
259
260 boolean exceptional=true;
261 try
262 {
263 _handler.handle(target, baseRequest, request, wrappedResponse);
264 exceptional=false;
265 }
266 finally
267 {
268 Continuation continuation = ContinuationSupport.getContinuation(request);
269 if (continuation.isSuspended() && continuation.isResponseWrapped())
270 {
271 continuation.addContinuationListener(new ContinuationListener()
272 {
273 public void onComplete(Continuation continuation)
274 {
275 try
276 {
277 wrappedResponse.finish();
278 }
279 catch(IOException e)
280 {
281 LOG.warn(e);
282 }
283 }
284
285 public void onTimeout(Continuation continuation)
286 {}
287 });
288 }
289 else if (exceptional && !response.isCommitted())
290 {
291 wrappedResponse.resetBuffer();
292 wrappedResponse.noCompression();
293 }
294 else
295 wrappedResponse.finish();
296 }
297 }
298 else
299 {
300 _handler.handle(target,baseRequest, request, response);
301 }
302 }
303 }
304
305
306
307
308
309
310
311
312 protected CompressedResponseWrapper newGzipResponseWrapper(HttpServletRequest request, HttpServletResponse response)
313 {
314 return new CompressedResponseWrapper(request,response)
315 {
316 {
317 super.setMimeTypes(GzipHandler.this._mimeTypes);
318 super.setBufferSize(GzipHandler.this._bufferSize);
319 super.setMinCompressSize(GzipHandler.this._minGzipSize);
320 }
321
322 @Override
323 protected AbstractCompressedStream newCompressedStream(HttpServletRequest request,HttpServletResponse response) throws IOException
324 {
325 return new AbstractCompressedStream("gzip",request,this,_vary)
326 {
327 @Override
328 protected DeflaterOutputStream createStream() throws IOException
329 {
330 return new GZIPOutputStream(_response.getOutputStream(),_bufferSize);
331 }
332 };
333 }
334
335 @Override
336 protected PrintWriter newWriter(OutputStream out,String encoding) throws UnsupportedEncodingException
337 {
338 return GzipHandler.this.newWriter(out,encoding);
339 }
340 };
341 }
342
343
344
345
346
347
348
349
350
351 protected PrintWriter newWriter(OutputStream out,String encoding) throws UnsupportedEncodingException
352 {
353 return encoding==null?new PrintWriter(out):new PrintWriter(new OutputStreamWriter(out,encoding));
354 }
355 }