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  
18  import javax.servlet.http.HttpServletRequest;
19  import javax.servlet.http.HttpServletResponse;
20  
21  
22  
23  /* ------------------------------------------------------------ */
24  /** Includable GZip Filter.
25   * This extension to the {@link GzipFilter} that uses Jetty features to allow
26   * headers to be set during calls to 
27   * {@link javax.servlet.RequestDispatcher#include(javax.servlet.ServletRequest, javax.servlet.ServletResponse)}.
28   * This allows the gzip filter to function correct during includes and to make a decision to gzip or not
29   * at the time the buffer fills and on the basis of all response headers.
30   * 
31   * 
32   *
33   */
34  public class IncludableGzipFilter extends GzipFilter
35  {
36  
37      protected GZIPResponseWrapper newGZIPResponseWrapper(HttpServletRequest request, HttpServletResponse response)
38      {
39          return new IncludableResponseWrapper(request,response);
40      }
41  
42      public class IncludableResponseWrapper extends GzipFilter.GZIPResponseWrapper
43      {
44          public IncludableResponseWrapper(HttpServletRequest request, HttpServletResponse response)
45          {
46              super(request,response);
47          }
48          
49          protected GzipStream newGzipStream(HttpServletRequest request,HttpServletResponse response,long contentLength,int bufferSize, int minGzipSize) throws IOException
50          {
51              return new IncludableGzipStream(request,response,contentLength,bufferSize,minGzipSize);
52          }
53      }
54      
55      public class IncludableGzipStream extends GzipFilter.GzipStream
56      {
57          public IncludableGzipStream(HttpServletRequest request, HttpServletResponse response, long contentLength, int bufferSize, int minGzipSize)
58                  throws IOException
59          {
60              super(request,response,contentLength,bufferSize,minGzipSize);
61          }
62  
63          protected boolean setContentEncodingGzip()
64          {
65              if (_request.getAttribute("javax.servlet.include.request_uri")!=null)
66                  _response.setHeader("org.eclipse.jetty.server.include.Content-Encoding", "gzip");
67              else
68                  _response.setHeader("Content-Encoding", "gzip");
69                  
70              return _response.containsHeader("Content-Encoding");
71          }
72          
73      }
74      
75  }