View Javadoc

    1   /*
    2    * Copyright 2009 Red Hat, Inc.
    3    *
    4    * Red Hat licenses this file to you under the Apache License, version 2.0
    5    * (the "License"); you may not use this file except in compliance with the
    6    * License.  You may obtain a copy of the License at:
    7    *
    8    *    http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
    13   * License for the specific language governing permissions and limitations
    14   * under the License.
    15   */
    16  package org.jboss.netty.handler.codec.http;
    17
    18  import org.jboss.netty.buffer.ChannelBuffer;
    19  import org.jboss.netty.handler.codec.compression.ZlibEncoder;
    20  import org.jboss.netty.handler.codec.compression.ZlibWrapper;
    21  import org.jboss.netty.handler.codec.embedder.EncoderEmbedder;
    22
    23  /**
    24   * Compresses an {@link HttpMessage} and an {@link HttpChunk} in {@code gzip} or
    25   * {@code deflate} encoding while respecting the {@code "Accept-Encoding"} header.
    26   * If there is no matching encoding, no compression is done.  For more
    27   * information on how this handler modifies the message, please refer to
    28   * {@link HttpContentEncoder}.
    29   *
    30   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
    31   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
    32   * @version $Rev: 2121 $, $Date: 2010-02-02 09:38:07 +0900 (Tue, 02 Feb 2010) $
    33   */
    34  public class HttpContentCompressor extends HttpContentEncoder {
    35
    36      private final int compressionLevel;
    37
    38      /**
    39       * Creates a new handler with the default compression level (<tt>6</tt>).
    40       */
    41      public HttpContentCompressor() {
    42          this(6);
    43      }
    44
    45      /**
    46       * Creates a new handler with the specified compression level.
    47       *
    48       * @param compressionLevel
    49       *        {@code 1} yields the fastest compression and {@code 9} yields the
    50       *        best compression.  {@code 0} means no compression.  The default
    51       *        compression level is {@code 6}.
    52       */
    53      public HttpContentCompressor(int compressionLevel) {
    54          if (compressionLevel < 0 || compressionLevel > 9) {
    55              throw new IllegalArgumentException(
    56                      "compressionLevel: " + compressionLevel +
    57                      " (expected: 0-9)");
    58          }
    59          this.compressionLevel = compressionLevel;
    60      }
    61
    62      @Override
    63      protected EncoderEmbedder<ChannelBuffer> newContentEncoder(String acceptEncoding) throws Exception {
    64          ZlibWrapper wrapper = determineWrapper(acceptEncoding);
    65          if (wrapper == null) {
    66              return null;
    67          }
    68
    69          return new EncoderEmbedder<ChannelBuffer>(new ZlibEncoder(wrapper, compressionLevel));
    70      }
    71
    72      @Override
    73      protected String getTargetContentEncoding(String acceptEncoding) throws Exception {
    74          ZlibWrapper wrapper = determineWrapper(acceptEncoding);
    75          if (wrapper == null) {
    76              return null;
    77          }
    78
    79          switch (wrapper) {
    80          case GZIP:
    81              return "gzip";
    82          case ZLIB:
    83              return "deflate";
    84          default:
    85              throw new Error();
    86          }
    87      }
    88
    89      private ZlibWrapper determineWrapper(String acceptEncoding) {
    90          // FIXME: Use the Q value.
    91          if (acceptEncoding.indexOf("gzip") >= 0) {
    92              return ZlibWrapper.GZIP;
    93          }
    94          if (acceptEncoding.indexOf("deflate") >= 0) {
    95              return ZlibWrapper.ZLIB;
    96          }
    97          return null;
    98      }
    99  }