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.tunnel;
    17
    18  import java.io.BufferedReader;
    19  import java.io.InputStreamReader;
    20  import java.net.InetSocketAddress;
    21  import java.net.URI;
    22  import java.util.concurrent.Executors;
    23
    24  import org.jboss.netty.bootstrap.ClientBootstrap;
    25  import org.jboss.netty.channel.ChannelFuture;
    26  import org.jboss.netty.channel.ChannelPipeline;
    27  import org.jboss.netty.channel.ChannelPipelineFactory;
    28  import org.jboss.netty.channel.Channels;
    29  import org.jboss.netty.channel.socket.http.HttpTunnelingClientSocketChannelFactory;
    30  import org.jboss.netty.channel.socket.oio.OioClientSocketChannelFactory;
    31  import org.jboss.netty.example.securechat.SecureChatSslContextFactory;
    32  import org.jboss.netty.handler.codec.string.StringDecoder;
    33  import org.jboss.netty.handler.codec.string.StringEncoder;
    34  import org.jboss.netty.handler.logging.LoggingHandler;
    35  import org.jboss.netty.logging.InternalLogLevel;
    36
    37  /**
    38   * An HTTP tunneled version of the telnet client example.  Please refer to the
    39   * API documentation of the <tt>org.jboss.netty.channel.socket.http</tt> package
    40   * for the detailed instruction on how to deploy the server-side HTTP tunnel in
    41   * your Servlet container.
    42   *
    43   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
    44   * @author Andy Taylor (andy.taylor@jboss.org)
    45   * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
    46   */
    47  public class HttpTunnelingClientExample {
    48
    49      public static void main(String[] args) throws Exception {
    50          if (args.length != 1) {
    51              System.err.println(
    52                      "Usage: " + HttpTunnelingClientExample.class.getSimpleName() +
    53                      " <URL>");
    54              System.err.println(
    55                      "Example: " + HttpTunnelingClientExample.class.getSimpleName() +
    56                      " http://localhost:8080/netty-tunnel");
    57              return;
    58          }
    59
    60          URI uri = new URI(args[0]);
    61          String scheme = uri.getScheme() == null? "http" : uri.getScheme();
    62
    63          // Configure the client.
    64          ClientBootstrap b = new ClientBootstrap(
    65                  new HttpTunnelingClientSocketChannelFactory(
    66                          new OioClientSocketChannelFactory(Executors.newCachedThreadPool())));
    67
    68          b.setPipelineFactory(new ChannelPipelineFactory() {
    69              public ChannelPipeline getPipeline() throws Exception {
    70                  return Channels.pipeline(
    71                          new StringDecoder(),
    72                          new StringEncoder(),
    73                          new LoggingHandler(InternalLogLevel.INFO));
    74              }
    75          });
    76
    77          // Set additional options required by the HTTP tunneling transport.
    78          b.setOption("serverName", uri.getHost());
    79          b.setOption("serverPath", uri.getRawPath());
    80
    81          // Configure SSL if necessary
    82          if (scheme.equals("https")) {
    83              b.setOption("sslContext", SecureChatSslContextFactory.getClientContext());
    84          } else if (!scheme.equals("http")) {
    85              // Only HTTP and HTTPS are supported.
    86              System.err.println("Only HTTP(S) is supported.");
    87              return;
    88          }
    89
    90          // Make the connection attempt.
    91          ChannelFuture channelFuture = b.connect(
    92                  new InetSocketAddress(uri.getHost(), uri.getPort()));
    93          channelFuture.awaitUninterruptibly();
    94
    95          // Read commands from the stdin.
    96          System.out.println("Enter text ('quit' to exit)");
    97          ChannelFuture lastWriteFuture = null;
    98          BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    99          for (; ;) {
    100             String line = in.readLine();
    101             if (line == null || "quit".equalsIgnoreCase(line)) {
    102                 break;
    103             }
    104
    105             // Sends the received line to the server.
    106             lastWriteFuture = channelFuture.getChannel().write(line);
    107         }
    108
    109         // Wait until all messages are flushed before closing the channel.
    110         if (lastWriteFuture != null) {
    111             lastWriteFuture.awaitUninterruptibly();
    112         }
    113
    114         channelFuture.getChannel().close();
    115         // Wait until the connection is closed or the connection attempt fails.
    116         channelFuture.getChannel().getCloseFuture().awaitUninterruptibly();
    117
    118         // Shut down all threads.
    119         b.releaseExternalResources();
    120     }
    121 }