fix Base64 but breaks compat
[nikiroo-utils.git] / src / be / nikiroo / utils / serial / server / ServerBridge.java
index db50876b32aafb144bc89047e73073c391c569f8..2c5d8bf1250fc559f507995d73d9711b56fd8640 100644 (file)
@@ -1,13 +1,14 @@
 package be.nikiroo.utils.serial.server;
 
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.lang.reflect.Array;
 import java.net.Socket;
 import java.net.UnknownHostException;
 
 import be.nikiroo.utils.StringUtils;
 import be.nikiroo.utils.TraceHandler;
-import be.nikiroo.utils.Version;
 import be.nikiroo.utils.serial.Importer;
 
 /**
@@ -25,7 +26,7 @@ import be.nikiroo.utils.serial.Importer;
 public class ServerBridge extends Server {
        private final String forwardToHost;
        private final int forwardToPort;
-       private final boolean forwardToSsl;
+       private final String forwardToKey;
 
        /**
         * Create a new server that will start listening on the network when
@@ -35,14 +36,16 @@ public class ServerBridge extends Server {
         *            the port to listen on, or 0 to assign any unallocated port
         *            found (which can later on be queried via
         *            {@link ServerBridge#getPort()}
-        * @param ssl
-        *            use an SSL connection (or not)
+        * @param key
+        *            an optional key to encrypt all the communications (if NULL,
+        *            everything will be sent in clear text)
         * @param forwardToHost
         *            the host server to forward the calls to
         * @param forwardToPort
         *            the host port to forward the calls to
-        * @param forwardToSsl
-        *            use an SSL connection for the forward server or not
+        * @param forwardToKey
+        *            an optional key to encrypt all the communications (if NULL,
+        *            everything will be sent in clear text)
         * 
         * @throws IOException
         *             in case of I/O error
@@ -52,12 +55,12 @@ public class ServerBridge extends Server {
         *             if the port parameter is outside the specified range of valid
         *             port values, which is between 0 and 65535, inclusive
         */
-       public ServerBridge(int port, boolean ssl, String forwardToHost,
-                       int forwardToPort, boolean forwardToSsl) throws IOException {
-               super(port, ssl);
+       public ServerBridge(int port, String key, String forwardToHost,
+                       int forwardToPort, String forwardToKey) throws IOException {
+               super(port, key);
                this.forwardToHost = forwardToHost;
                this.forwardToPort = forwardToPort;
-               this.forwardToSsl = forwardToSsl;
+               this.forwardToKey = forwardToKey;
        }
 
        /**
@@ -68,14 +71,17 @@ public class ServerBridge extends Server {
         *            the server name (only used for debug info and traces)
         * @param port
         *            the port to listen on
-        * @param ssl
-        *            use an SSL connection (or not)
+        * @param key
+        *            an optional key to encrypt all the communications (if NULL,
+        *            everything will be sent in clear text)
         * @param forwardToHost
         *            the host server to forward the calls to
         * @param forwardToPort
         *            the host port to forward the calls to
-        * @param forwardToSsl
-        *            use an SSL connection for the forward server or not
+        * @param forwardToKey
+        *            an optional key to encrypt all the communications (if NULL,
+        *            everything will be sent in clear text) use an SSL connection
+        *            for the forward server or not
         * 
         * @throws IOException
         *             in case of I/O error
@@ -85,13 +91,13 @@ public class ServerBridge extends Server {
         *             if the port parameter is outside the specified range of valid
         *             port values, which is between 0 and 65535, inclusive
         */
-       public ServerBridge(String name, int port, boolean ssl,
-                       String forwardToHost, int forwardToPort, boolean forwardToSsl)
+       public ServerBridge(String name, int port, String key,
+                       String forwardToHost, int forwardToPort, String forwardToKey)
                        throws IOException {
-               super(name, port, ssl);
+               super(name, port, key);
                this.forwardToHost = forwardToHost;
                this.forwardToPort = forwardToPort;
-               this.forwardToSsl = forwardToSsl;
+               this.forwardToKey = forwardToKey;
        }
 
        /**
@@ -118,25 +124,25 @@ public class ServerBridge extends Server {
 
        @Override
        protected ConnectActionServer createConnectActionServer(Socket s) {
-               return new ConnectActionServerString(s) {
+               // Bad impl, not up to date (should work, but not efficient)
+               return new ConnectActionServerString(s, key) {
                        @Override
-                       public void action(final Version clientVersion) throws Exception {
-                               onClientContact(clientVersion);
+                       public void action() throws Exception {
+                               onClientContact();
                                final ConnectActionServerString bridge = this;
 
                                try {
                                        new ConnectActionClientString(forwardToHost, forwardToPort,
-                                                       forwardToSsl, clientVersion) {
+                                                       forwardToKey) {
                                                @Override
-                                               public void action(final Version serverVersion)
-                                                               throws Exception {
-                                                       onServerContact(serverVersion);
+                                               public void action() throws Exception {
+                                                       onServerContact();
 
                                                        for (String fromClient = bridge.rec(); fromClient != null; fromClient = bridge
                                                                        .rec()) {
-                                                               onRec(clientVersion, fromClient);
+                                                               onRec(fromClient);
                                                                String fromServer = send(fromClient);
-                                                               onSend(serverVersion, fromServer);
+                                                               onSend(fromServer);
                                                                bridge.send(fromServer);
                                                        }
 
@@ -158,48 +164,38 @@ public class ServerBridge extends Server {
 
        /**
         * This is the method that is called each time a client contact us.
-        * 
-        * @param clientVersion
-        *            the client version
         */
-       protected void onClientContact(Version clientVersion) {
-               getTraceHandler().trace(">>> CLIENT " + clientVersion);
+       protected void onClientContact() {
+               getTraceHandler().trace(">>> CLIENT ");
        }
 
        /**
         * This is the method that is called each time a client contact us.
-        * 
-        * @param serverVersion
-        *            the server version
         */
-       protected void onServerContact(Version serverVersion) {
-               getTraceHandler().trace("<<< SERVER " + serverVersion);
+       protected void onServerContact() {
+               getTraceHandler().trace("<<< SERVER");
                getTraceHandler().trace("");
        }
 
        /**
         * This is the method that is called each time a client contact us.
         * 
-        * @param clientVersion
-        *            the client version
         * @param data
         *            the data sent by the client
         */
-       protected void onRec(Version clientVersion, String data) {
-               trace(">>> CLIENT (" + clientVersion + ")", data);
+       protected void onRec(String data) {
+               trace(">>> CLIENT", data);
        }
 
        /**
         * This is the method that is called each time the forwarded server contact
         * us.
         * 
-        * @param serverVersion
-        *            the client version
         * @param data
         *            the data sent by the client
         */
-       protected void onSend(Version serverVersion, String data) {
-               trace("<<< SERVER (" + serverVersion + ")", data);
+       protected void onSend(String data) {
+               trace("<<< SERVER", data);
        }
 
        @Override
@@ -207,7 +203,8 @@ public class ServerBridge extends Server {
                getTraceHandler().trace(
                                getName() + ": will forward to " + forwardToHost + ":"
                                                + forwardToPort + " ("
-                                               + (forwardToSsl ? "SSL" : "plain text") + ")");
+                                               + (forwardToKey != null ? "encrypted" : "plain text")
+                                               + ")");
                super.run();
        }
 
@@ -220,19 +217,9 @@ public class ServerBridge extends Server {
         *            the data to trace
         */
        private void trace(String prefix, String data) {
+               // TODO: we convert to string and back
                int size = data == null ? 0 : data.length();
-               String ssize = size + " byte";
-               if (size > 1) {
-                       ssize = size + " bytes";
-                       if (size >= 1000) {
-                               size = size / 1000;
-                               ssize = size + " kb";
-                               if (size > 1000) {
-                                       size = size / 1000;
-                                       ssize = size + " MB";
-                               }
-                       }
-               }
+               String ssize = StringUtils.formatNumber(size) + "bytes";
 
                getTraceHandler().trace(prefix + ": " + ssize, 1);
 
@@ -240,28 +227,35 @@ public class ServerBridge extends Server {
                        try {
                                while (data.startsWith("ZIP:") || data.startsWith("B64:")) {
                                        if (data.startsWith("ZIP:")) {
-                                               data = StringUtils.unbase64s(data.substring(4), true);
+                                               data = StringUtils.unzip64s(data.substring(4));
                                        } else if (data.startsWith("B64:")) {
-                                               data = StringUtils.unbase64s(data.substring(4), false);
+                                               data = StringUtils.unzip64s(data.substring(4));
                                        }
                                }
 
-                               Object obj = new Importer().read(data).getValue();
-                               if (obj == null) {
-                                       getTraceHandler().trace("NULL", 2);
-                                       getTraceHandler().trace("NULL", 3);
-                                       getTraceHandler().trace("NULL", 4);
-                               } else {
-                                       if (obj.getClass().isArray()) {
-                                               getTraceHandler().trace(
-                                                               "(" + obj.getClass() + ") with "
-                                                                               + Array.getLength(obj) + "element(s)",
-                                                               3);
+                               InputStream stream = new ByteArrayInputStream(
+                                               data.getBytes("UTF-8"));
+                               try {
+                                       Object obj = new Importer().read(stream).getValue();
+                                       if (obj == null) {
+                                               getTraceHandler().trace("NULL", 2);
+                                               getTraceHandler().trace("NULL", 3);
+                                               getTraceHandler().trace("NULL", 4);
                                        } else {
-                                               getTraceHandler().trace("(" + obj.getClass() + ")", 2);
+                                               if (obj.getClass().isArray()) {
+                                                       getTraceHandler().trace(
+                                                                       "(" + obj.getClass() + ") with "
+                                                                                       + Array.getLength(obj)
+                                                                                       + "element(s)", 3);
+                                               } else {
+                                                       getTraceHandler().trace("(" + obj.getClass() + ")",
+                                                                       2);
+                                               }
+                                               getTraceHandler().trace("" + obj.toString(), 3);
+                                               getTraceHandler().trace(data, 4);
                                        }
-                                       getTraceHandler().trace("" + obj.toString(), 3);
-                                       getTraceHandler().trace(data, 4);
+                               } finally {
+                                       stream.close();
                                }
                        } catch (NoSuchMethodException e) {
                                getTraceHandler().trace("(not an object)", 2);