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 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
40
41
42
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
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
82 b.setOption("serverName", uri.getHost());
83 b.setOption("serverPath", uri.getRawPath());
84
85
86 if ("https".equals(scheme)) {
87 b.setOption("sslContext", new JdkSslClientContext(InsecureTrustManagerFactory.INSTANCE).context());
88 }
89
90
91 ChannelFuture channelFuture = b.connect(new InetSocketAddress(host, port));
92 channelFuture.sync();
93
94
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
106 lastWriteFuture = channelFuture.getChannel().write(line);
107 }
108
109
110 if (lastWriteFuture != null) {
111 lastWriteFuture.sync();
112 }
113
114
115 channelFuture.getChannel().close().sync();
116 } finally {
117
118 b.releaseExternalResources();
119 }
120 }
121 }