1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
40
41
42
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
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
70 b.setOption("serverName", uri.getHost());
71 b.setOption("serverPath", uri.getRawPath());
72
73
74 if (scheme.equals("https")) {
75 b.setOption("sslContext", SecureChatSslContextFactory.getClientContext());
76 } else if (!scheme.equals("http")) {
77
78 System.err.println("Only HTTP(S) is supported.");
79 return;
80 }
81
82
83 ChannelFuture channelFuture = b.connect(
84 new InetSocketAddress(uri.getHost(), uri.getPort()));
85 channelFuture.awaitUninterruptibly();
86
87
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
98 lastWriteFuture = channelFuture.getChannel().write(line);
99 }
100
101
102 if (lastWriteFuture != null) {
103 lastWriteFuture.awaitUninterruptibly();
104 }
105
106 channelFuture.getChannel().close();
107
108 channelFuture.getChannel().getCloseFuture().awaitUninterruptibly();
109
110
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 }