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