X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Flibrary%2FRemoteLibraryServer.java;h=4f89a1fa19263c9d4bedf513274442660d3a023f;hb=d66deb8d8b30cff6b54db352eef34a3508939f84;hp=c3933ebb276c65e171546109c59d6daa96df8501;hpb=5db598bc6386aaabe69f2eed55b03625c72c236b;p=fanfix.git diff --git a/src/be/nikiroo/fanfix/library/RemoteLibraryServer.java b/src/be/nikiroo/fanfix/library/RemoteLibraryServer.java index c3933eb..4f89a1f 100644 --- a/src/be/nikiroo/fanfix/library/RemoteLibraryServer.java +++ b/src/be/nikiroo/fanfix/library/RemoteLibraryServer.java @@ -2,7 +2,6 @@ package be.nikiroo.fanfix.library; import java.io.IOException; import java.net.URL; -import java.nio.file.AccessDeniedException; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; @@ -81,7 +80,7 @@ public class RemoteLibraryServer extends ServerObject { */ public RemoteLibraryServer(String key, int port) throws IOException { super("Fanfix remote library", port, key); - setTraceHandler(Instance.getTraceHandler()); + setTraceHandler(Instance.getInstance().getTraceHandler()); } @Override @@ -111,8 +110,7 @@ public class RemoteLibraryServer extends ServerObject { } } - List whitelist = Instance.getConfig().getList( - Config.SERVER_WHITELIST); + List whitelist = Instance.getInstance().getConfig().getList(Config.SERVER_WHITELIST); if (whitelist == null) { whitelist = new ArrayList(); } @@ -121,10 +119,9 @@ public class RemoteLibraryServer extends ServerObject { wl = false; } - rw = Instance.getConfig().getBoolean(Config.SERVER_RW, rw); + rw = Instance.getInstance().getConfig().getBoolean(Config.SERVER_RW, rw); if (!subkey.isEmpty()) { - List allowed = Instance.getConfig().getList( - Config.SERVER_ALLOWED_SUBKEYS); + List allowed = Instance.getInstance().getConfig().getList(Config.SERVER_ALLOWED_SUBKEYS); if (allowed.contains(subkey)) { if ((subkey + "|").contains("|rw|")) { rw = true; @@ -142,13 +139,14 @@ public class RemoteLibraryServer extends ServerObject { for (Object arg : args) { trace += arg + " "; } - System.out.println(trace); + long now = System.currentTimeMillis(); + System.out.println(StringUtils.fromTime(now) + ": " + trace); Object rep = null; try { rep = doRequest(action, command, args, rw, whitelist); } catch (IOException e) { - rep = new RemoteLibraryException(e); + rep = new RemoteLibraryException(e, true); } commands.put(id, command); @@ -180,9 +178,12 @@ public class RemoteLibraryServer extends ServerObject { String rec = StringUtils.formatNumber(bytesReceived) + "b"; String sent = StringUtils.formatNumber(bytesSent) + "b"; - System.out.println(String.format("%s[>%s]: (%s sent, %s rec) in %d ms", - display(whitelist, rw), commands.get(id), sent, rec, - times.get(id))); + long now = System.currentTimeMillis(); + System.out.println(StringUtils.fromTime(now) + + ": " + + String.format("%s[>%s]: (%s sent, %s rec) in %d ms", + display(whitelist, rw), commands.get(id), sent, rec, + times.get(id))); commands.remove(id); times.remove(id); @@ -200,7 +201,7 @@ public class RemoteLibraryServer extends ServerObject { if ("*".equals(args[0])) { Progress pg = createPgForwarder(action); - for (MetaData meta : Instance.getLibrary().getMetas(pg)) { + for (MetaData meta : Instance.getInstance().getLibrary().getMetas(pg)) { MetaData light; if (meta.getCover() == null) { light = meta; @@ -214,7 +215,7 @@ public class RemoteLibraryServer extends ServerObject { forcePgDoneSent(pg); } else { - MetaData meta = Instance.getLibrary().getInfo((String) args[0]); + MetaData meta = Instance.getInstance().getLibrary().getInfo((String) args[0]); MetaData light; if (meta.getCover() == null) { light = meta; @@ -237,7 +238,7 @@ public class RemoteLibraryServer extends ServerObject { return metas.toArray(new MetaData[0]); } else if ("GET_STORY".equals(command)) { - MetaData meta = Instance.getLibrary().getInfo((String) args[0]); + MetaData meta = Instance.getInstance().getLibrary().getInfo((String) args[0]); if (meta == null) { return null; } @@ -254,16 +255,15 @@ public class RemoteLibraryServer extends ServerObject { action.send(meta); action.rec(); - Story story = Instance.getLibrary() - .getStory((String) args[0], null); + Story story = Instance.getInstance().getLibrary().getStory((String) args[0], null); for (Object obj : breakStory(story)) { action.send(obj); action.rec(); } } else if ("SAVE_STORY".equals(command)) { if (!rw) { - throw new AccessDeniedException("" + args[0], null, - "Read-Only remote library"); + throw new RemoteLibraryException("Read-Only remote library: " + + args[0], false); } List list = new ArrayList(); @@ -277,68 +277,62 @@ public class RemoteLibraryServer extends ServerObject { } Story story = rebuildStory(list); - Instance.getLibrary().save(story, (String) args[0], null); + Instance.getInstance().getLibrary().save(story, (String) args[0], null); return story.getMeta().getLuid(); } else if ("IMPORT".equals(command)) { if (!rw) { - throw new AccessDeniedException("" + args[0], null, - "Read-Only remote library"); + throw new RemoteLibraryException("Read-Only remote library: " + + args[0], false); } Progress pg = createPgForwarder(action); - Story story = Instance.getLibrary().imprt( - new URL((String) args[0]), pg); + MetaData meta = Instance.getInstance().getLibrary().imprt(new URL((String) args[0]), pg); forcePgDoneSent(pg); - return story.getMeta().getLuid(); + return meta.getLuid(); } else if ("DELETE_STORY".equals(command)) { if (!rw) { - throw new AccessDeniedException("" + args[0], null, - "Read-Only remote library"); + throw new RemoteLibraryException("Read-Only remote library: " + + args[0], false); } - Instance.getLibrary().delete((String) args[0]); + Instance.getInstance().getLibrary().delete((String) args[0]); } else if ("GET_COVER".equals(command)) { - return Instance.getLibrary().getCover((String) args[0]); + return Instance.getInstance().getLibrary().getCover((String) args[0]); } else if ("GET_CUSTOM_COVER".equals(command)) { if ("SOURCE".equals(args[0])) { - return Instance.getLibrary().getCustomSourceCover( - (String) args[1]); + return Instance.getInstance().getLibrary().getCustomSourceCover((String) args[1]); } else if ("AUTHOR".equals(args[0])) { - return Instance.getLibrary().getCustomAuthorCover( - (String) args[1]); + return Instance.getInstance().getLibrary().getCustomAuthorCover((String) args[1]); } else { return null; } } else if ("SET_COVER".equals(command)) { if (!rw) { - throw new AccessDeniedException("" + args[0], "" + args[1], - "Read-Only remote library"); + throw new RemoteLibraryException("Read-Only remote library: " + + args[0] + ", " + args[1], false); } if ("SOURCE".equals(args[0])) { - Instance.getLibrary().setSourceCover((String) args[1], - (String) args[2]); + Instance.getInstance().getLibrary().setSourceCover((String) args[1], (String) args[2]); } else if ("AUTHOR".equals(args[0])) { - Instance.getLibrary().setAuthorCover((String) args[1], - (String) args[2]); + Instance.getInstance().getLibrary().setAuthorCover((String) args[1], (String) args[2]); } } else if ("CHANGE_STA".equals(command)) { if (!rw) { - throw new AccessDeniedException("" + args[0], "" + args[1], - "Read-Only remote library"); + throw new RemoteLibraryException("Read-Only remote library: " + args[0] + ", " + args[1], false); } Progress pg = createPgForwarder(action); - Instance.getLibrary().changeSTA((String) args[0], (String) args[1], - (String) args[2], (String) args[3], pg); + Instance.getInstance().getLibrary().changeSTA((String) args[0], (String) args[1], (String) args[2], + (String) args[3], pg); forcePgDoneSent(pg); } else if ("EXIT".equals(command)) { if (!rw) { - throw new AccessDeniedException("EXIT", "", - "Read-Only remote library, cannot close it"); + throw new RemoteLibraryException( + "Read-Only remote library: EXIT", false); } - stop(0, false); + stop(10000, false); } return null; @@ -347,7 +341,9 @@ public class RemoteLibraryServer extends ServerObject { @Override protected void onError(Exception e) { if (e instanceof SSLException) { - System.out.println("[Client connection refused (bad key)]"); + long now = System.currentTimeMillis(); + System.out.println(StringUtils.fromTime(now) + ": " + + "[Client connection refused (bad key)]"); } else { getTraceHandler().error(e); }