View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the 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 org.jboss.netty.bootstrap.ClientBootstrap;
19  import org.jboss.netty.channel.ChannelFuture;
20  import org.jboss.netty.channel.ChannelPipeline;
21  import org.jboss.netty.channel.ChannelPipelineFactory;
22  import org.jboss.netty.channel.Channels;
23  import org.jboss.netty.channel.socket.http.HttpTunnelingClientSocketChannelFactory;
24  import org.jboss.netty.channel.socket.oio.OioClientSocketChannelFactory;
25  import org.jboss.netty.handler.codec.string.StringDecoder;
26  import org.jboss.netty.handler.codec.string.StringEncoder;
27  import org.jboss.netty.handler.logging.LoggingHandler;
28  import org.jboss.netty.handler.ssl.JdkSslClientContext;
29  import org.jboss.netty.handler.ssl.util.InsecureTrustManagerFactory;
30  import org.jboss.netty.logging.InternalLogLevel;
31  
32  import java.io.BufferedReader;
33  import java.io.InputStreamReader;
34  import java.net.InetSocketAddress;
35  import java.net.URI;
36  import java.util.concurrent.Executors;
37  
38  /**
39   * An HTTP tunneled version of the telnet client example.  Please refer to the
40   * API documentation of the <tt>org.jboss.netty.channel.socket.http</tt> package
41   * for the detailed instruction on how to deploy the server-side HTTP tunnel in
42   * your Servlet container.
43   */
44  public final class HttpTunnelingClientExample {
45  
46      static final String URL = System.getProperty("url", "http://localhost:8080/netty-tunnel");
47  
48      public static void main(String[] args) throws Exception {
49          URI uri = new URI(URL);
50          String scheme = uri.getScheme() == null? "http" : uri.getScheme();
51          String host = uri.getHost() == null? "127.0.0.1" : uri.getHost();
52          int port = uri.getPort();
53          if (port == -1) {
54              if ("http".equalsIgnoreCase(scheme)) {
55                  port = 80;
56              } else if ("https".equalsIgnoreCase(scheme)) {
57                  port = 443;
58              }
59          }
60  
61          if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) {
62              System.err.println("Only HTTP(S) is supported.");
63              return;
64          }
65  
66          // Configure the client.
67          ClientBootstrap b = new ClientBootstrap(
68                  new HttpTunnelingClientSocketChannelFactory(
69                          new OioClientSocketChannelFactory(Executors.newCachedThreadPool())));
70  
71          try {
72              b.setPipelineFactory(new ChannelPipelineFactory() {
73                  public ChannelPipeline getPipeline() {
74                      return Channels.pipeline(
75                              new StringDecoder(),
76                              new StringEncoder(),
77                              new LoggingHandler(InternalLogLevel.INFO));
78                  }
79              });
80  
81              // Set additional options required by the HTTP tunneling transport.
82              b.setOption("serverName", uri.getHost());
83              b.setOption("serverPath", uri.getRawPath());
84  
85              // Configure SSL if necessary
86              if ("https".equals(scheme)) {
87                  b.setOption("sslContext", new JdkSslClientContext(InsecureTrustManagerFactory.INSTANCE).context());
88              }
89  
90              // Make the connection attempt.
91              ChannelFuture channelFuture = b.connect(new InetSocketAddress(host, port));
92              channelFuture.sync();
93  
94              // Read commands from the stdin.
95              System.err.println("Enter text ('quit' to exit)");
96  
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.sync();
112             }
113 
114             // Close the connection.
115             channelFuture.getChannel().close().sync();
116         } finally {
117             // Shut down all threads.
118             b.releaseExternalResources();
119         }
120     }
121 }