1 package be
.nikiroo
.utils
.serial
.server
;
3 import java
.io
.IOException
;
4 import java
.lang
.reflect
.Array
;
5 import java
.net
.Socket
;
6 import java
.net
.UnknownHostException
;
8 import be
.nikiroo
.utils
.StringUtils
;
9 import be
.nikiroo
.utils
.TraceHandler
;
10 import be
.nikiroo
.utils
.Version
;
11 import be
.nikiroo
.utils
.serial
.Importer
;
14 * This class implements a simple server that can bridge two other
17 * It can, of course, inspect the data that goes through it (by default, it
18 * prints traces of the data).
20 * Note: this {@link ServerBridge} has to be discarded after use (cannot be
25 public class ServerBridge
extends Server
{
26 private final String forwardToHost
;
27 private final int forwardToPort
;
28 private final boolean forwardToSsl
;
31 * Create a new server that will start listening on the network when
32 * {@link ServerBridge#start()} is called.
35 * the port to listen on, or 0 to assign any unallocated port
36 * found (which can later on be queried via
37 * {@link ServerBridge#getPort()}
39 * use an SSL connection (or not)
40 * @param forwardToHost
41 * the host server to forward the calls to
42 * @param forwardToPort
43 * the host port to forward the calls to
45 * use an SSL connection for the forward server or not
48 * in case of I/O error
49 * @throws UnknownHostException
50 * if the IP address of the host could not be determined
51 * @throws IllegalArgumentException
52 * if the port parameter is outside the specified range of valid
53 * port values, which is between 0 and 65535, inclusive
55 public ServerBridge(int port
, boolean ssl
, String forwardToHost
,
56 int forwardToPort
, boolean forwardToSsl
) throws IOException
{
58 this.forwardToHost
= forwardToHost
;
59 this.forwardToPort
= forwardToPort
;
60 this.forwardToSsl
= forwardToSsl
;
64 * Create a new server that will start listening on the network when
65 * {@link ServerBridge#start()} is called.
68 * the server name (only used for debug info and traces)
70 * the port to listen on
72 * use an SSL connection (or not)
73 * @param forwardToHost
74 * the host server to forward the calls to
75 * @param forwardToPort
76 * the host port to forward the calls to
78 * use an SSL connection for the forward server or not
81 * in case of I/O error
82 * @throws UnknownHostException
83 * if the IP address of the host could not be determined
84 * @throws IllegalArgumentException
85 * if the port parameter is outside the specified range of valid
86 * port values, which is between 0 and 65535, inclusive
88 public ServerBridge(String name
, int port
, boolean ssl
,
89 String forwardToHost
, int forwardToPort
, boolean forwardToSsl
)
91 super(name
, port
, ssl
);
92 this.forwardToHost
= forwardToHost
;
93 this.forwardToPort
= forwardToPort
;
94 this.forwardToSsl
= forwardToSsl
;
98 * The traces handler for this {@link Server}.
100 * The trace levels are handled as follow:
102 * <li>1: it will only print basic IN/OUT messages with length</li>
103 * <li>2: it will try to interpret it as an object (SLOW) and print the
104 * object class if possible</li>
105 * <li>3: it will try to print the {@link Object#toString()} value, or the
106 * data if it is not an object</li>
107 * <li>4: it will also print the unzipped serialised value if it is an
112 * the new traces handler
115 public void setTraceHandler(TraceHandler tracer
) {
116 super.setTraceHandler(tracer
);
120 protected ConnectActionServer
createConnectActionServer(Socket s
) {
121 return new ConnectActionServerString(s
) {
123 public void action(final Version clientVersion
) throws Exception
{
124 onClientContact(clientVersion
);
125 final ConnectActionServerString bridge
= this;
128 new ConnectActionClientString(forwardToHost
, forwardToPort
,
129 forwardToSsl
, clientVersion
) {
131 public void action(final Version serverVersion
)
133 onServerContact(serverVersion
);
135 for (String fromClient
= bridge
.rec(); fromClient
!= null; fromClient
= bridge
137 onRec(clientVersion
, fromClient
);
138 String fromServer
= send(fromClient
);
139 onSend(serverVersion
, fromServer
);
140 bridge
.send(fromServer
);
143 getTraceHandler().trace("=== DONE", 1);
144 getTraceHandler().trace("", 1);
148 protected void onError(Exception e
) {
149 ServerBridge
.this.onError(e
);
152 } catch (Exception e
) {
153 ServerBridge
.this.onError(e
);
160 * This is the method that is called each time a client contact us.
162 * @param clientVersion
165 protected void onClientContact(Version clientVersion
) {
166 getTraceHandler().trace(">>> CLIENT " + clientVersion
);
170 * This is the method that is called each time a client contact us.
172 * @param serverVersion
175 protected void onServerContact(Version serverVersion
) {
176 getTraceHandler().trace("<<< SERVER " + serverVersion
);
177 getTraceHandler().trace("");
181 * This is the method that is called each time a client contact us.
183 * @param clientVersion
186 * the data sent by the client
188 protected void onRec(Version clientVersion
, String data
) {
189 trace(">>> CLIENT (" + clientVersion
+ ")", data
);
193 * This is the method that is called each time the forwarded server contact
196 * @param serverVersion
199 * the data sent by the client
201 protected void onSend(Version serverVersion
, String data
) {
202 trace("<<< SERVER (" + serverVersion
+ ")", data
);
207 getTraceHandler().trace(
208 getName() + ": will forward to " + forwardToHost
+ ":"
209 + forwardToPort
+ " ("
210 + (forwardToSsl ?
"SSL" : "plain text") + ")");
215 * Trace the data with the given prefix.
218 * the prefix (client, server, version...)
222 private void trace(String prefix
, String data
) {
223 int size
= data
== null ?
0 : data
.length();
224 String ssize
= size
+ " byte";
226 ssize
= size
+ " bytes";
229 ssize
= size
+ " kb";
232 ssize
= size
+ " MB";
237 getTraceHandler().trace(prefix
+ ": " + ssize
, 1);
239 if (getTraceHandler().getTraceLevel() >= 2) {
241 while (data
.startsWith("ZIP:") || data
.startsWith("B64:")) {
242 if (data
.startsWith("ZIP:")) {
243 data
= StringUtils
.unbase64s(data
.substring(4), true);
244 } else if (data
.startsWith("B64:")) {
245 data
= StringUtils
.unbase64s(data
.substring(4), false);
249 Object obj
= new Importer().read(data
).getValue();
251 getTraceHandler().trace("NULL", 2);
252 getTraceHandler().trace("NULL", 3);
253 getTraceHandler().trace("NULL", 4);
255 if (obj
.getClass().isArray()) {
256 getTraceHandler().trace(
257 "(" + obj
.getClass() + ") with "
258 + Array
.getLength(obj
) + "element(s)",
261 getTraceHandler().trace("(" + obj
.getClass() + ")", 2);
263 getTraceHandler().trace("" + obj
.toString(), 3);
264 getTraceHandler().trace(data
, 4);
266 } catch (NoSuchMethodException e
) {
267 getTraceHandler().trace("(not an object)", 2);
268 getTraceHandler().trace(data
, 3);
269 getTraceHandler().trace("", 4);
270 } catch (NoSuchFieldException e
) {
271 getTraceHandler().trace(
272 "(incompatible: " + e
.getMessage() + ")", 2);
273 getTraceHandler().trace(data
, 3);
274 getTraceHandler().trace("", 4);
275 } catch (ClassNotFoundException e
) {
276 getTraceHandler().trace(
277 "(unknown object: " + e
.getMessage() + ")", 2);
278 getTraceHandler().trace(data
, 3);
279 getTraceHandler().trace("", 4);
280 } catch (Exception e
) {
281 getTraceHandler().trace(
282 "(decode error: " + e
.getMessage() + ")", 2);
283 getTraceHandler().trace(data
, 3);
284 getTraceHandler().trace("", 4);
287 getTraceHandler().trace("", 2);