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.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
39
40
41
42
43
44
45
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
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
78 b.setOption("serverName", uri.getHost());
79 b.setOption("serverPath", uri.getRawPath());
80
81
82 if (scheme.equals("https")) {
83 b.setOption("sslContext", SecureChatSslContextFactory.getClientContext());
84 } else if (!scheme.equals("http")) {
85
86 System.err.println("Only HTTP(S) is supported.");
87 return;
88 }
89
90
91 ChannelFuture channelFuture = b.connect(
92 new InetSocketAddress(uri.getHost(), uri.getPort()));
93 channelFuture.awaitUninterruptibly();
94
95
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
106 lastWriteFuture = channelFuture.getChannel().write(line);
107 }
108
109
110 if (lastWriteFuture != null) {
111 lastWriteFuture.awaitUninterruptibly();
112 }
113
114 channelFuture.getChannel().close();
115
116 channelFuture.getChannel().getCloseFuture().awaitUninterruptibly();
117
118
119 b.releaseExternalResources();
120 }
121 }