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  import java.io.OutputStream;
    19  import java.io.OutputStreamWriter;
    20  import java.io.PrintWriter;
    21  import java.io.UnsupportedEncodingException;
    22
    23  import javax.servlet.FilterConfig;
    24  import javax.servlet.ServletException;
    25  import javax.servlet.http.HttpServletRequest;
    26  import javax.servlet.http.HttpServletResponse;
    27
    28  import org.mortbay.io.UncheckedPrintWriter;
    29  import org.mortbay.jetty.HttpConnection;
    30  import org.mortbay.servlet.GzipFilter;
    31
    32  /* ------------------------------------------------------------ */
    33  /** Includable GZip Filter.
    34   * This extension to the {@link GzipFilter} that uses Jetty APIs to allow
    35   * headers to be set during calls to
    36   * {@link javax.servlet.RequestDispatcher#include(javax.servlet.ServletRequest, javax.servlet.ServletResponse)}.
    37   * This allows the gzip filter to function correct during includes and to make a decision to gzip or not
    38   * at the time the buffer fills and on the basis of all response headers.
    39   *
    40   * If the init parameter "uncheckedPrintWriter" is set to "true", then the PrintWriter used by
    41   * the wrapped getWriter will be {@link UncheckedPrintWriter}.
    42   *
    43   */
    44  public class IncludableGzipFilter extends GzipFilter
    45  {
    46      boolean _uncheckedPrintWriter=false;
    47
    48      public void init(FilterConfig filterConfig) throws ServletException
    49      {
    50          super.init(filterConfig);
    51
    52          String tmp=filterConfig.getInitParameter("uncheckedPrintWriter");
    53          if (tmp!=null)
    54              _uncheckedPrintWriter=Boolean.valueOf(tmp).booleanValue();
    55      }
    56
    57      protected GZIPResponseWrapper newGZIPResponseWrapper(HttpServletRequest request, HttpServletResponse response)
    58      {
    59          return new IncludableResponseWrapper(request,response);
    60      }
    61
    62      public class IncludableResponseWrapper extends GzipFilter.GZIPResponseWrapper
    63      {
    64          public IncludableResponseWrapper(HttpServletRequest request, HttpServletResponse response)
    65          {
    66              super(request,response);
    67          }
    68
    69          protected GzipStream newGzipStream(HttpServletRequest request,HttpServletResponse response,long contentLength,int bufferSize, int minGzipSize) throws IOException
    70          {
    71              return new IncludableGzipStream(request,response,contentLength,bufferSize,minGzipSize);
    72          }
    73      }
    74
    75      public class IncludableGzipStream extends GzipFilter.GzipStream
    76      {
    77          public IncludableGzipStream(HttpServletRequest request, HttpServletResponse response, long contentLength, int bufferSize, int minGzipSize)
    78                  throws IOException
    79          {
    80              super(request,response,contentLength,bufferSize,minGzipSize);
    81          }
    82
    83          protected boolean setContentEncodingGzip()
    84          {
    85              HttpConnection connection = HttpConnection.getCurrentConnection();
    86              connection.getResponseFields().put("Content-Encoding", "gzip");
    87              return true;
    88          }
    89      }
    90
    91      protected PrintWriter newWriter(OutputStream out,String encoding) throws UnsupportedEncodingException
    92      {
    93          if (_uncheckedPrintWriter)
    94              return encoding==null?new UncheckedPrintWriter(out):new UncheckedPrintWriter(new OutputStreamWriter(out,encoding));
    95          return super.newWriter(out,encoding);
    96      }
    97  }