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.example.http.snoop;
    17
    18  import static org.jboss.netty.channel.Channels.*;
    19
    20  import javax.net.ssl.SSLEngine;
    21
    22  import org.jboss.netty.channel.ChannelPipeline;
    23  import org.jboss.netty.channel.ChannelPipelineFactory;
    24  import org.jboss.netty.example.securechat.SecureChatSslContextFactory;
    25  import org.jboss.netty.handler.codec.http.HttpClientCodec;
    26  import org.jboss.netty.handler.codec.http.HttpContentDecompressor;
    27  import org.jboss.netty.handler.ssl.SslHandler;
    28
    29  /**
    30   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
    31   * @author Andy Taylor (andy.taylor@jboss.org)
    32   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
    33   *
    34   * @version $Rev: 2226 $, $Date: 2010-03-31 11:26:51 +0900 (Wed, 31 Mar 2010) $
    35   */
    36  public class HttpClientPipelineFactory implements ChannelPipelineFactory {
    37
    38      private final boolean ssl;
    39
    40      public HttpClientPipelineFactory(boolean ssl) {
    41          this.ssl = ssl;
    42      }
    43
    44      public ChannelPipeline getPipeline() throws Exception {
    45          // Create a default pipeline implementation.
    46          ChannelPipeline pipeline = pipeline();
    47
    48          // Enable HTTPS if necessary.
    49          if (ssl) {
    50              SSLEngine engine =
    51                  SecureChatSslContextFactory.getClientContext().createSSLEngine();
    52              engine.setUseClientMode(true);
    53
    54              pipeline.addLast("ssl", new SslHandler(engine));
    55          }
    56
    57          pipeline.addLast("codec", new HttpClientCodec());
    58
    59          // Remove the following line if you don't want automatic content decompression.
    60          pipeline.addLast("inflater", new HttpContentDecompressor());
    61
    62          // Uncomment the following line if you don't want to handle HttpChunks.
    63          //pipeline.addLast("aggregator", new HttpChunkAggregator(1048576));
    64
    65          pipeline.addLast("handler", new HttpResponseHandler());
    66          return pipeline;
    67      }
    68  }