remote string -> stream + encrypt
[fanfix.git] / src / be / nikiroo / fanfix / library / RemoteLibrary.java
index 63e45efc63b2977571eaacc18a133bc9cee3597d..d6bdd1568546d625477f5a0c4baaae1f5bc3708f 100644 (file)
@@ -7,12 +7,13 @@ import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.util.List;
 
+import javax.net.ssl.SSLException;
+
 import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.fanfix.data.Story;
 import be.nikiroo.utils.Image;
 import be.nikiroo.utils.Progress;
-import be.nikiroo.utils.Version;
 import be.nikiroo.utils.serial.server.ConnectActionClientObject;
 
 /**
@@ -59,23 +60,23 @@ public class RemoteLibrary extends BasicLibrary {
                        Instance.getTraceHandler().trace("Getting remote lib status...");
                        new ConnectActionClientObject(host, port, key) {
                                @Override
-                               public void action(Version serverVersion) throws Exception {
-                                       try {
-                                               Object rep = send(new Object[] { "PING" });
-
-                                               if ("PONG".equals(rep)) {
-                                                       result[0] = Status.READY;
-                                               } else {
-                                                       result[0] = Status.UNAUTORIZED;
-                                               }
-                                       } catch (IllegalArgumentException e) {
+                               public void action() throws Exception {
+                                       Object rep = send(new Object[] { "PING" });
+
+                                       if ("PONG".equals(rep)) {
+                                               result[0] = Status.READY;
+                                       } else {
                                                result[0] = Status.UNAUTORIZED;
                                        }
                                }
 
                                @Override
                                protected void onError(Exception e) {
-                                       result[0] = Status.UNAVAILABLE;
+                                       if (e instanceof SSLException) {
+                                               result[0] = Status.UNAUTORIZED;
+                                       } else {
+                                               result[0] = Status.UNAVAILABLE;
+                                       }
                                }
                        }.connect();
                } catch (UnknownHostException e) {
@@ -97,14 +98,19 @@ public class RemoteLibrary extends BasicLibrary {
                try {
                        new ConnectActionClientObject(host, port, key) {
                                @Override
-                               public void action(Version serverVersion) throws Exception {
+                               public void action() throws Exception {
                                        Object rep = send(new Object[] { "GET_COVER", luid });
                                        result[0] = (Image) rep;
                                }
 
                                @Override
                                protected void onError(Exception e) {
-                                       Instance.getTraceHandler().error(e);
+                                       if (e instanceof SSLException) {
+                                               Instance.getTraceHandler().error(
+                                                               "Connection refused (bad key)");
+                                       } else {
+                                               Instance.getTraceHandler().error(e);
+                                       }
                                }
                        }.connect();
                } catch (Exception e) {
@@ -131,7 +137,7 @@ public class RemoteLibrary extends BasicLibrary {
                try {
                        new ConnectActionClientObject(host, port, key) {
                                @Override
-                               public void action(Version serverVersion) throws Exception {
+                               public void action() throws Exception {
                                        Object rep = send(new Object[] { "GET_CUSTOM_COVER", type,
                                                        source });
                                        result[0] = (Image) rep;
@@ -139,7 +145,12 @@ public class RemoteLibrary extends BasicLibrary {
 
                                @Override
                                protected void onError(Exception e) {
-                                       Instance.getTraceHandler().error(e);
+                                       if (e instanceof SSLException) {
+                                               Instance.getTraceHandler().error(
+                                                               "Connection refused (bad key)");
+                                       } else {
+                                               Instance.getTraceHandler().error(e);
+                                       }
                                }
                        }.connect();
                } catch (Exception e) {
@@ -157,7 +168,7 @@ public class RemoteLibrary extends BasicLibrary {
                try {
                        new ConnectActionClientObject(host, port, key) {
                                @Override
-                               public void action(Version serverVersion) throws Exception {
+                               public void action() throws Exception {
                                        Progress pg = pgF;
                                        if (pg == null) {
                                                pg = new Progress();
@@ -185,7 +196,12 @@ public class RemoteLibrary extends BasicLibrary {
 
                                @Override
                                protected void onError(Exception e) {
-                                       Instance.getTraceHandler().error(e);
+                                       if (e instanceof SSLException) {
+                                               Instance.getTraceHandler().error(
+                                                               "Connection refused (bad key)");
+                                       } else {
+                                               Instance.getTraceHandler().error(e);
+                                       }
                                }
                        }.connect();
                } catch (Exception e) {
@@ -213,7 +229,7 @@ public class RemoteLibrary extends BasicLibrary {
 
                new ConnectActionClientObject(host, port, key) {
                        @Override
-                       public void action(Version serverVersion) throws Exception {
+                       public void action() throws Exception {
                                Progress pg = pgF;
                                if (story.getMeta().getWords() <= Integer.MAX_VALUE) {
                                        pg.setMinMax(0, (int) story.getMeta().getWords());
@@ -234,7 +250,12 @@ public class RemoteLibrary extends BasicLibrary {
 
                        @Override
                        protected void onError(Exception e) {
-                               Instance.getTraceHandler().error(e);
+                               if (e instanceof SSLException) {
+                                       Instance.getTraceHandler().error(
+                                                       "Connection refused (bad key)");
+                               } else {
+                                       Instance.getTraceHandler().error(e);
+                               }
                        }
                }.connect();
 
@@ -258,13 +279,18 @@ public class RemoteLibrary extends BasicLibrary {
        public synchronized void delete(final String luid) throws IOException {
                new ConnectActionClientObject(host, port, key) {
                        @Override
-                       public void action(Version serverVersion) throws Exception {
+                       public void action() throws Exception {
                                send(new Object[] { "DELETE_STORY", luid });
                        }
 
                        @Override
                        protected void onError(Exception e) {
-                               Instance.getTraceHandler().error(e);
+                               if (e instanceof SSLException) {
+                                       Instance.getTraceHandler().error(
+                                                       "Connection refused (bad key)");
+                               } else {
+                                       Instance.getTraceHandler().error(e);
+                               }
                        }
                }.connect();
        }
@@ -285,13 +311,18 @@ public class RemoteLibrary extends BasicLibrary {
                try {
                        new ConnectActionClientObject(host, port, key) {
                                @Override
-                               public void action(Version serverVersion) throws Exception {
+                               public void action() throws Exception {
                                        send(new Object[] { "SET_COVER", type, value, luid });
                                }
 
                                @Override
                                protected void onError(Exception e) {
-                                       Instance.getTraceHandler().error(e);
+                                       if (e instanceof SSLException) {
+                                               Instance.getTraceHandler().error(
+                                                               "Connection refused (bad key)");
+                                       } else {
+                                               Instance.getTraceHandler().error(e);
+                                       }
                                }
                        }.connect();
                } catch (IOException e) {
@@ -325,7 +356,7 @@ public class RemoteLibrary extends BasicLibrary {
                try {
                        new ConnectActionClientObject(host, port, key) {
                                @Override
-                               public void action(Version serverVersion) throws Exception {
+                               public void action() throws Exception {
                                        Progress pg = pgF;
 
                                        Object rep = send(new Object[] { "IMPORT", url.toString() });
@@ -344,7 +375,12 @@ public class RemoteLibrary extends BasicLibrary {
 
                                @Override
                                protected void onError(Exception e) {
-                                       Instance.getTraceHandler().error(e);
+                                       if (e instanceof SSLException) {
+                                               Instance.getTraceHandler().error(
+                                                               "Connection refused (bad key)");
+                                       } else {
+                                               Instance.getTraceHandler().error(e);
+                                       }
                                }
                        }.connect();
                } catch (IOException e) {
@@ -372,7 +408,7 @@ public class RemoteLibrary extends BasicLibrary {
                try {
                        new ConnectActionClientObject(host, port, key) {
                                @Override
-                               public void action(Version serverVersion) throws Exception {
+                               public void action() throws Exception {
                                        Progress pg = pgF;
 
                                        Object rep = send(new Object[] { "CHANGE_STA", luid,
@@ -388,7 +424,12 @@ public class RemoteLibrary extends BasicLibrary {
 
                                @Override
                                protected void onError(Exception e) {
-                                       Instance.getTraceHandler().error(e);
+                                       if (e instanceof SSLException) {
+                                               Instance.getTraceHandler().error(
+                                                               "Connection refused (bad key)");
+                                       } else {
+                                               Instance.getTraceHandler().error(e);
+                                       }
                                }
                        }.connect();
                } catch (IOException e) {
@@ -409,13 +450,18 @@ public class RemoteLibrary extends BasicLibrary {
                try {
                        new ConnectActionClientObject(host, port, key) {
                                @Override
-                               public void action(Version serverVersion) throws Exception {
+                               public void action() throws Exception {
                                        send(new Object[] { "EXIT" });
                                }
 
                                @Override
                                protected void onError(Exception e) {
-                                       Instance.getTraceHandler().error(e);
+                                       if (e instanceof SSLException) {
+                                               Instance.getTraceHandler().error(
+                                                               "Connection refused (bad key)");
+                                       } else {
+                                               Instance.getTraceHandler().error(e);
+                                       }
                                }
                        }.connect();
                } catch (IOException e) {
@@ -488,7 +534,7 @@ public class RemoteLibrary extends BasicLibrary {
                try {
                        new ConnectActionClientObject(host, port, key) {
                                @Override
-                               public void action(Version serverVersion) throws Exception {
+                               public void action() throws Exception {
                                        Progress pg = pgF;
                                        if (pg == null) {
                                                pg = new Progress();
@@ -515,7 +561,12 @@ public class RemoteLibrary extends BasicLibrary {
 
                                @Override
                                protected void onError(Exception e) {
-                                       Instance.getTraceHandler().error(e);
+                                       if (e instanceof SSLException) {
+                                               Instance.getTraceHandler().error(
+                                                               "Connection refused (bad key)");
+                                       } else {
+                                               Instance.getTraceHandler().error(e);
+                                       }
                                }
                        }.connect();
                } catch (Exception e) {