1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.example.proxy.telnet;
21
22 import java.net.InetSocketAddress;
23 import java.nio.charset.Charset;
24 import java.util.HashMap;
25
26 import org.apache.mina.core.RuntimeIoException;
27 import org.apache.mina.core.future.ConnectFuture;
28 import org.apache.mina.core.session.IoSession;
29 import org.apache.mina.filter.codec.ProtocolCodecFilter;
30 import org.apache.mina.filter.codec.textline.LineDelimiter;
31 import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
32 import org.apache.mina.proxy.ProxyConnector;
33 import org.apache.mina.proxy.handlers.http.HttpProxyConstants;
34 import org.apache.mina.proxy.handlers.http.HttpProxyRequest;
35 import org.apache.mina.proxy.session.ProxyIoSession;
36 import org.apache.mina.transport.socket.nio.NioSocketConnector;
37
38
39
40
41
42
43
44
45
46
47 public class ProxyTelnetTestClient {
48
49
50
51
52 public final static String USER = "TED_KODS";
53
54
55
56
57 public final static String PWD = "EDOUARD";
58
59
60
61
62 public final static InetSocketAddress serverAddress = new InetSocketAddress(
63 "localhost", 25);
64
65
66
67
68 public final static InetSocketAddress proxyAddress = new InetSocketAddress(
69 "localhost", 8080);
70
71
72
73
74
75
76
77
78 public ProxyTelnetTestClient() throws Exception {
79
80 NioSocketConnector targetConnector = new NioSocketConnector(Runtime
81 .getRuntime().availableProcessors() + 1);
82 ProxyConnector connector = new ProxyConnector(targetConnector);
83
84
85
86
87
88
89
90
91
92 HttpProxyRequest req = new HttpProxyRequest(serverAddress);
93 HashMap<String, String> props = new HashMap<String, String>();
94 props.put(HttpProxyConstants.USER_PROPERTY, USER);
95 props.put(HttpProxyConstants.PWD_PROPERTY, PWD);
96 req.setProperties(props);
97
98 ProxyIoSession proxyIoSession = new ProxyIoSession(proxyAddress, req);
99 connector.setProxyIoSession(proxyIoSession);
100
101 LineDelimiter delim = new LineDelimiter("\r\n");
102 targetConnector.getFilterChain().addLast(
103 "codec",
104 new ProtocolCodecFilter(new TextLineCodecFactory(Charset
105 .forName("UTF-8"), delim, delim)));
106
107 connector.setHandler(new TelnetSessionHandler());
108
109 IoSession session;
110 for (;;) {
111 try {
112 ConnectFuture future = connector.connect();
113 future.awaitUninterruptibly();
114 session = future.getSession();
115 break;
116 } catch (RuntimeIoException e) {
117 System.err.println("Failed to connect. Retrying in 5 secs ...");
118 Thread.sleep(5000);
119 }
120 }
121
122
123 if (session != null) {
124 session.getCloseFuture().awaitUninterruptibly();
125 }
126 connector.dispose();
127 System.exit(0);
128 }
129
130
131
132
133 public static void main(String[] args) throws Exception {
134 new ProxyTelnetTestClient();
135 }
136 }