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