View Javadoc

1   //========================================================================
2   //Copyright 2004-2008 Mort Bay Consulting Pty. Ltd.
3   //------------------------------------------------------------------------
4   //Licensed under the Apache License, Version 2.0 (the "License");
5   //you may not use this file except in compliance with the License.
6   //You may obtain a copy of the License at 
7   //http://www.apache.org/licenses/LICENSE-2.0
8   //Unless required by applicable law or agreed to in writing, software
9   //distributed under the License is distributed on an "AS IS" BASIS,
10  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  //See the License for the specific language governing permissions and
12  //limitations under the License.
13  //========================================================================
14  
15  package org.mortbay.servlet.jetty;
16  
17  import java.io.IOException;
18  
19  import javax.servlet.http.HttpServletRequest;
20  import javax.servlet.http.HttpServletResponse;
21  
22  import org.mortbay.jetty.HttpConnection;
23  import org.mortbay.servlet.GzipFilter;
24  
25  /* ------------------------------------------------------------ */
26  /** Includable GZip Filter.
27   * This extension to the {@link GzipFilter} that uses Jetty APIs to allow
28   * headers to be set during calls to 
29   * {@link javax.servlet.RequestDispatcher#include(javax.servlet.ServletRequest, javax.servlet.ServletResponse)}.
30   * This allows the gzip filter to function correct during includes and to make a decision to gzip or not
31   * at the time the buffer fills and on the basis of all response headers.
32   * 
33   * @author gregw
34   *
35   */
36  public class IncludableGzipFilter extends GzipFilter
37  {
38  
39      protected GZIPResponseWrapper newGZIPResponseWrapper(HttpServletRequest request, HttpServletResponse response)
40      {
41          return new IncludableResponseWrapper(request,response);
42      }
43  
44      public class IncludableResponseWrapper extends GzipFilter.GZIPResponseWrapper
45      {
46          public IncludableResponseWrapper(HttpServletRequest request, HttpServletResponse response)
47          {
48              super(request,response);
49          }
50          
51          protected GzipStream newGzipStream(HttpServletRequest request,HttpServletResponse response,long contentLength,int bufferSize, int minGzipSize) throws IOException
52          {
53              return new IncludableGzipStream(request,response,contentLength,bufferSize,minGzipSize);
54          }
55      }
56      
57      public class IncludableGzipStream extends GzipFilter.GzipStream
58      {
59          public IncludableGzipStream(HttpServletRequest request, HttpServletResponse response, long contentLength, int bufferSize, int minGzipSize)
60                  throws IOException
61          {
62              super(request,response,contentLength,bufferSize,minGzipSize);
63          }
64  
65          protected boolean setContentEncodingGzip()
66          {
67              HttpConnection connection = HttpConnection.getCurrentConnection();
68              connection.getResponseFields().put("Content-Encoding", "gzip");
69              return true;
70          }
71          
72      }
73      
74  }