~ubuntu-branches/ubuntu/utopic/jetty/utopic-proposed

« back to all changes in this revision

Viewing changes to modules/util/src/main/java/org/mortbay/servlet/GzipFilter.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2010-04-29 07:36:43 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20100429073643-eo2x6y2fit4m6o66
Tags: 6.1.24-2
* Set JAVA_HOME in d/rules to /usr/lib/jvm/default-java. (Closes: #578618,
  #579469)
* Fix the installation of jetty-util5.jar. (Closes: #569328)

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.
6
 
//You may obtain a copy of the License at 
 
6
//You may obtain a copy of the License at
7
7
//http://www.apache.org/licenses/LICENSE-2.0
8
8
//Unless required by applicable law or agreed to in writing, software
9
9
//distributed under the License is distributed on an "AS IS" BASIS,
22
22
import java.util.Set;
23
23
import java.util.StringTokenizer;
24
24
import java.util.zip.GZIPOutputStream;
25
 
 
26
25
import javax.servlet.FilterChain;
27
26
import javax.servlet.FilterConfig;
28
27
import javax.servlet.ServletException;
46
45
 * if no mimeTypes are defined the content-type is not "application/gzip"</li>
47
46
 * <li>No content-encoding is specified by the resource</li>
48
47
 * </ul>
49
 
 * 
 
48
 *
50
49
 * <p>
51
50
 * Compressing the content can greatly improve the network bandwidth usage, but at a cost of memory and
52
 
 * CPU cycles.   If this filter is mapped for static content, then use of efficient direct NIO may be 
53
 
 * prevented, thus use of the gzip mechanism of the {@link org.mortbay.jetty.servlet.DefaultServlet} is 
 
51
 * CPU cycles.   If this filter is mapped for static content, then use of efficient direct NIO may be
 
52
 * prevented, thus use of the gzip mechanism of the {@link org.mortbay.jetty.servlet.DefaultServlet} is
54
53
 * advised instead.
55
54
 * </p>
56
55
 * <p>
57
 
 * This filter extends {@link UserAgentFilter} and if the the initParameter <code>excludedAgents</code> 
 
56
 * This filter extends {@link UserAgentFilter} and if the the initParameter <code>excludedAgents</code>
58
57
 * is set to a comma separated list of user agents, then these agents will be excluded from gzip content.
59
58
 * </p>
60
 
 *  
 
59
 *
61
60
 * @author gregw
62
61
 *
63
62
 */
67
66
    protected int _bufferSize=8192;
68
67
    protected int _minGzipSize=0;
69
68
    protected Set _excluded;
70
 
    
 
69
 
71
70
    public void init(FilterConfig filterConfig) throws ServletException
72
71
    {
73
72
        super.init(filterConfig);
74
 
        
 
73
 
75
74
        String tmp=filterConfig.getInitParameter("bufferSize");
76
75
        if (tmp!=null)
77
76
            _bufferSize=Integer.parseInt(tmp);
79
78
        tmp=filterConfig.getInitParameter("minGzipSize");
80
79
        if (tmp!=null)
81
80
            _minGzipSize=Integer.parseInt(tmp);
82
 
        
 
81
 
83
82
        tmp=filterConfig.getInitParameter("mimeTypes");
84
83
        if (tmp!=null)
85
84
        {
88
87
            while (tok.hasMoreTokens())
89
88
                _mimeTypes.add(tok.nextToken());
90
89
        }
91
 
        
 
90
 
92
91
        tmp=filterConfig.getInitParameter("excludedAgents");
93
92
        if (tmp!=null)
94
93
        {
103
102
    {
104
103
    }
105
104
 
106
 
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) 
 
105
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
107
106
        throws IOException, ServletException
108
107
    {
109
108
        HttpServletRequest request=(HttpServletRequest)req;
125
124
            }
126
125
 
127
126
            GZIPResponseWrapper wrappedResponse=newGZIPResponseWrapper(request,response);
128
 
            
 
127
 
129
128
            boolean exceptional=true;
130
129
            try
131
130
            {
155
154
            super.doFilter(request,response,chain);
156
155
        }
157
156
    }
158
 
    
 
157
 
159
158
    protected GZIPResponseWrapper newGZIPResponseWrapper(HttpServletRequest request, HttpServletResponse response)
160
159
    {
161
160
        return new GZIPResponseWrapper(request,response);
194
193
                    ct=ct.substring(0,colon);
195
194
            }
196
195
 
197
 
            if ((_gzStream==null || _gzStream._out==null) && 
 
196
            if ((_gzStream==null || _gzStream._out==null) &&
198
197
                (_mimeTypes==null && "application/gzip".equalsIgnoreCase(ct) ||
199
198
                 _mimeTypes!=null && (ct==null||!_mimeTypes.contains(StringUtil.asciiToLowerCase(ct)))))
200
199
            {
202
201
            }
203
202
        }
204
203
 
205
 
        
 
204
 
206
205
        public void setStatus(int sc, String sm)
207
206
        {
208
207
            super.setStatus(sc,sm);
223
222
            if (_gzStream!=null)
224
223
                _gzStream.setContentLength(length);
225
224
        }
226
 
        
 
225
 
227
226
        public void addHeader(String name, String value)
228
227
        {
229
228
            if ("content-length".equalsIgnoreCase(name))
233
232
                    _gzStream.setContentLength(_contentLength);
234
233
            }
235
234
            else if ("content-type".equalsIgnoreCase(name))
236
 
            {   
 
235
            {
237
236
                setContentType(value);
238
237
            }
239
238
            else if ("content-encoding".equalsIgnoreCase(name))
240
 
            {   
 
239
            {
241
240
                super.addHeader(name,value);
242
241
                if (!isCommitted())
243
242
                {
247
246
            else
248
247
                super.addHeader(name,value);
249
248
        }
250
 
        
 
249
 
251
250
        public void setHeader(String name, String value)
252
251
        {
253
252
            if ("content-length".equalsIgnoreCase(name))
257
256
                    _gzStream.setContentLength(_contentLength);
258
257
            }
259
258
            else if ("content-type".equalsIgnoreCase(name))
260
 
            {   
 
259
            {
261
260
                setContentType(value);
262
261
            }
263
262
            else if ("content-encoding".equalsIgnoreCase(name))
264
 
            {   
 
263
            {
265
264
                super.setHeader(name,value);
266
265
                if (!isCommitted())
267
266
                {
304
303
            _noGzip=false;
305
304
            _contentLength=-1;
306
305
        }
307
 
        
 
306
 
308
307
        public void resetBuffer()
309
308
        {
310
309
            super.resetBuffer();
313
312
            _writer=null;
314
313
            _gzStream=null;
315
314
        }
316
 
        
 
315
 
317
316
        public void sendError(int sc, String msg) throws IOException
318
317
        {
319
318
            resetBuffer();
320
319
            super.sendError(sc,msg);
321
320
        }
322
 
        
 
321
 
323
322
        public void sendError(int sc) throws IOException
324
323
        {
325
324
            resetBuffer();
326
325
            super.sendError(sc);
327
326
        }
328
 
        
 
327
 
329
328
        public void sendRedirect(String location) throws IOException
330
329
        {
331
330
            resetBuffer();
338
337
            {
339
338
                if (getResponse().isCommitted() || _noGzip)
340
339
                    return getResponse().getOutputStream();
341
 
                
 
340
 
342
341
                _gzStream=newGzipStream(_request,(HttpServletResponse)getResponse(),_contentLength,_bufferSize,_minGzipSize);
343
342
            }
344
343
            else if (_writer!=null)
345
344
                throw new IllegalStateException("getWriter() called");
346
 
            
347
 
            return _gzStream;   
 
345
 
 
346
            return _gzStream;
348
347
        }
349
348
 
350
349
        public PrintWriter getWriter() throws IOException
351
350
        {
352
351
            if (_writer==null)
353
 
            { 
 
352
            {
354
353
                if (_gzStream!=null)
355
354
                    throw new IllegalStateException("getOutputStream() called");
356
 
                
 
355
 
357
356
                if (getResponse().isCommitted() || _noGzip)
358
357
                    return getResponse().getWriter();
359
 
                
 
358
 
360
359
                _gzStream=newGzipStream(_request,(HttpServletResponse)getResponse(),_contentLength,_bufferSize,_minGzipSize);
361
360
                _writer=newWriter(_gzStream,getCharacterEncoding());
362
361
            }
363
 
            return _writer;   
 
362
            return _writer;
364
363
        }
365
364
 
366
365
        void noGzip()
378
377
                }
379
378
            }
380
379
        }
381
 
        
 
380
 
382
381
        void finish() throws IOException
383
382
        {
384
383
            if (_writer!=null && !_gzStream._closed)
386
385
            if (_gzStream!=null)
387
386
                _gzStream.finish();
388
387
        }
389
 
     
 
388
 
390
389
        protected GzipStream newGzipStream(HttpServletRequest request,HttpServletResponse response,long contentLength,int bufferSize, int minGzipSize) throws IOException
391
390
        {
392
391
            return new GzipStream(request,response,contentLength,bufferSize,minGzipSize);
393
392
        }
394
393
    }
395
394
 
396
 
    
 
395
 
397
396
    public static class GzipStream extends ServletOutputStream
398
397
    {
399
398
        protected HttpServletRequest _request;
431
430
        {
432
431
            _contentLength=length;
433
432
        }
434
 
        
 
433
 
435
434
        public void flush() throws IOException
436
435
        {
437
436
            if (_out==null || _bOut!=null)
441
440
                else
442
441
                    doGzip();
443
442
            }
444
 
            
 
443
 
445
444
            _out.flush();
446
445
        }
447
446
 
448
447
        public void close() throws IOException
449
448
        {
450
 
            if (_request.getAttribute("javax.servlet.include.request_uri")!=null)            
 
449
            if (_request.getAttribute("javax.servlet.include.request_uri")!=null)
451
450
                flush();
452
451
            else
453
452
            {
466
465
                }
467
466
 
468
467
                if (_gzOut!=null)
469
 
                    _gzOut.finish();
470
 
                _out.close();
 
468
                    _gzOut.close();
 
469
                else
 
470
                    _out.close();
471
471
                _closed=true;
472
472
            }
473
 
        }  
 
473
        }
474
474
 
475
475
        public void finish() throws IOException
476
476
        {
483
483
                    else
484
484
                        doGzip();
485
485
                }
486
 
                
487
 
                if (_gzOut!=null)
488
 
                    _gzOut.finish();
 
486
 
 
487
                if (_gzOut!=null && !_closed)
 
488
                {
 
489
                    _closed=true;
 
490
                    _gzOut.close();
 
491
                }
489
492
            }
490
 
        }  
 
493
        }
491
494
 
492
495
        public void write(int b) throws IOException
493
 
        {    
 
496
        {
494
497
            checkOut(1);
495
498
            _out.write(b);
496
499
        }
506
509
            checkOut(len);
507
510
            _out.write(b,off,len);
508
511
        }
509
 
        
 
512
 
510
513
        protected boolean setContentEncodingGzip()
511
514
        {
512
515
            _response.setHeader("Content-Encoding", "gzip");
513
516
            return _response.containsHeader("Content-Encoding");
514
517
        }
515
 
        
 
518
 
516
519
        public void doGzip() throws IOException
517
520
        {
518
 
            if (_gzOut==null) 
 
521
            if (_gzOut==null)
519
522
            {
520
523
                if (_response.isCommitted())
521
524
                    throw new IllegalStateException();
522
 
                
 
525
 
523
526
                if (setContentEncodingGzip())
524
527
                {
525
528
                    _out=_gzOut=new GZIPOutputStream(_response.getOutputStream(),_bufferSize);
530
533
                        _bOut=null;
531
534
                    }
532
535
                }
533
 
                else 
 
536
                else
534
537
                    doNotGzip();
535
538
            }
536
539
        }
537
 
        
 
540
 
538
541
        public void doNotGzip() throws IOException
539
542
        {
540
 
            if (_gzOut!=null) 
 
543
            if (_gzOut!=null)
541
544
                throw new IllegalStateException();
542
545
            if (_out==null || _bOut!=null )
543
546
            {
553
556
                if (_bOut!=null)
554
557
                    _out.write(_bOut.getBuf(),0,_bOut.getCount());
555
558
                _bOut=null;
556
 
            }   
 
559
            }
557
560
        }
558
 
        
559
 
        private void checkOut(int length) throws IOException 
 
561
 
 
562
        private void checkOut(int length) throws IOException
560
563
        {
561
 
            if (_closed) 
562
 
            {
563
 
                new Throwable().printStackTrace();
 
564
            if (_closed)
564
565
                throw new IOException("CLOSED");
565
 
            }
566
 
            
 
566
 
567
567
            if (_out==null)
568
568
            {
569
569
                if (_response.isCommitted() || (_contentLength>=0 && _contentLength<_minGzipSize))