View Javadoc

1   // ========================================================================
2   // Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.servlets;
15  
16  import java.io.IOException;
17  import java.io.OutputStream;
18  import java.io.OutputStreamWriter;
19  import java.io.PrintWriter;
20  import java.io.UnsupportedEncodingException;
21  
22  import javax.servlet.FilterConfig;
23  import javax.servlet.ServletException;
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.eclipse.jetty.http.gzip.GzipResponseWrapper;
28  import org.eclipse.jetty.http.gzip.GzipStream;
29  import org.eclipse.jetty.io.UncheckedPrintWriter;
30  
31  
32  
33  /* ------------------------------------------------------------ */
34  /** Includable GZip Filter.
35   * This extension to the {@link GzipFilter} that uses Jetty features to allow
36   * headers to be set during calls to 
37   * {@link javax.servlet.RequestDispatcher#include(javax.servlet.ServletRequest, javax.servlet.ServletResponse)}.
38   * This allows the gzip filter to function correct during includes and to make a decision to gzip or not
39   * at the time the buffer fills and on the basis of all response headers.
40   * 
41   * If the init parameter "uncheckedPrintWriter" is set to "true", then the PrintWriter used by
42   * the wrapped getWriter will be {@link UncheckedPrintWriter}.
43   *
44   */
45  public class IncludableGzipFilter extends GzipFilter
46  {
47      boolean _uncheckedPrintWriter=false;
48  
49      @Override
50      public void init(FilterConfig filterConfig) throws ServletException
51      {
52          super.init(filterConfig);
53          
54          String tmp=filterConfig.getInitParameter("uncheckedPrintWriter");
55          if (tmp!=null)
56              _uncheckedPrintWriter=Boolean.valueOf(tmp).booleanValue();
57      }
58  
59      @Override
60      protected GzipResponseWrapper newGzipResponseWrapper(HttpServletRequest request, HttpServletResponse response)
61      {
62          return new IncludableResponseWrapper(request,response);
63      }
64  
65      public class IncludableResponseWrapper extends GzipResponseWrapper
66      {
67          public IncludableResponseWrapper(HttpServletRequest request, HttpServletResponse response)
68          {
69              super(request,response);
70  
71              _mimeTypes = IncludableGzipFilter.this._mimeTypes;
72              _bufferSize = IncludableGzipFilter.this._bufferSize;
73              _minGzipSize = IncludableGzipFilter.this._minGzipSize;
74          }
75  
76          @Override
77          protected GzipStream newGzipStream(HttpServletRequest request,HttpServletResponse response,long contentLength,int bufferSize, int minGzipSize) throws IOException
78          {
79              return new IncludableGzipStream(request,response,contentLength,bufferSize,minGzipSize);
80          }
81  
82          @Override
83          protected PrintWriter newWriter(OutputStream out,String encoding) throws UnsupportedEncodingException
84          {
85              return IncludableGzipFilter.this.newWriter(out,encoding);
86          }
87      }
88      
89      public class IncludableGzipStream extends GzipStream
90      {
91          public IncludableGzipStream(HttpServletRequest request, HttpServletResponse response, long contentLength, int bufferSize, int minGzipSize)
92                  throws IOException
93          {
94              super(request,response,contentLength,bufferSize,minGzipSize);
95          }
96  
97          @Override
98          protected boolean setContentEncodingGzip()
99          {
100             if (_request.getAttribute("javax.servlet.include.request_uri")!=null)
101                 _response.setHeader("org.eclipse.jetty.server.include.Content-Encoding", "gzip");
102             else
103                 _response.setHeader("Content-Encoding", "gzip");
104                 
105             return _response.containsHeader("Content-Encoding");
106         }
107     }
108 
109     @Override
110     protected PrintWriter newWriter(OutputStream out,String encoding) throws UnsupportedEncodingException
111     {
112         if (_uncheckedPrintWriter)
113             return encoding==null?new UncheckedPrintWriter(out):new UncheckedPrintWriter(new OutputStreamWriter(out,encoding));
114         return super.newWriter(out,encoding);
115     }
116 }