X-Git-Url: https://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Flibrary%2FWebLibraryServer.java;h=1fce3911eb67f5b47cb95198a9a8c542da9ebf2d;hb=878ec53820a09ab01397bfe74b3e0f3884e6f9cd;hp=5cda3b5d712e6aa062ca87e9b16b4d157a40752f;hpb=f70bcacf8bfccfff405d4fdaaa38b0e322945125;p=fanfix.git diff --git a/src/be/nikiroo/fanfix/library/WebLibraryServer.java b/src/be/nikiroo/fanfix/library/WebLibraryServer.java index 5cda3b5..1fce391 100644 --- a/src/be/nikiroo/fanfix/library/WebLibraryServer.java +++ b/src/be/nikiroo/fanfix/library/WebLibraryServer.java @@ -4,6 +4,7 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; +import java.net.URLDecoder; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -31,10 +32,6 @@ import be.nikiroo.utils.Progress; public class WebLibraryServer extends WebLibraryServerHtml { class WLoginResult extends LoginResult { - private boolean rw; - private boolean wl; - private boolean bl; - public WLoginResult(boolean badLogin, boolean badCookie) { super(badLogin, badCookie); } @@ -43,9 +40,6 @@ public class WebLibraryServer extends WebLibraryServerHtml { boolean wl, boolean bl) { super(who, key, subkey, (rw ? "|rw" : "") + (wl ? "|wl" : "") + (bl ? "|bl" : "") + "|"); - this.rw = rw; - this.wl = wl; - this.bl = bl; } public WLoginResult(String cookie, String who, String key, @@ -77,6 +71,8 @@ public class WebLibraryServer extends WebLibraryServerHtml { private Map imprts = new HashMap(); + private boolean exiting; + public WebLibraryServer(boolean secure) throws IOException { super(secure); @@ -104,6 +100,56 @@ public class WebLibraryServer extends WebLibraryServerHtml { new Thread(this).start(); } + @Override + protected Response stop(WLoginResult login) { + if (!login.isRw()) { + return NanoHTTPD.newFixedLengthResponse(Status.FORBIDDEN, + NanoHTTPD.MIME_PLAINTEXT, "Exit not allowed"); + } + + if (exiting) { + return NanoHTTPD.newFixedLengthResponse(Status.SERVICE_UNAVAILABLE, + NanoHTTPD.MIME_PLAINTEXT, "Server is already exiting..."); + } + + exiting = true; + Instance.getInstance().getTraceHandler().trace("Exiting"); + + boolean ok; + do { + synchronized (imprts) { + ok = imprts.isEmpty(); + } + if (!ok) { + try { + Thread.sleep(2000); + } catch (InterruptedException e) { + Instance.getInstance().getTraceHandler() + .trace("Waiting to exit..."); + } + } + } while (!ok); + + doStop(); + + new Thread(new Runnable() { + @Override + public void run() { + try { + Thread.sleep(1500); + } catch (InterruptedException e) { + } + + Instance.getInstance().getTraceHandler() + .trace("Exit timeout: force-quit"); + System.exit(0); + } + }, "Exit program after timeout of 1500 ms").start(); + + return NanoHTTPD.newFixedLengthResponse(Status.OK, + NanoHTTPD.MIME_PLAINTEXT, "Exited"); + } + @Override protected WLoginResult login(boolean badLogin, boolean badCookie) { return new WLoginResult(false, false); @@ -172,9 +218,8 @@ public class WebLibraryServer extends WebLibraryServerHtml { jsons.add(JsonIO.toJson(meta)); } - return newInputStreamResponse("application/json", - new ByteArrayInputStream( - new JSONArray(jsons).toString().getBytes())); + return NanoHTTPD.newFixedLengthResponse(Status.OK, + "application/json", new JSONArray(jsons).toString()); } return NanoHTTPD.newFixedLengthResponse(Status.BAD_REQUEST, @@ -306,6 +351,16 @@ public class WebLibraryServer extends WebLibraryServerHtml { NanoHTTPD.MIME_PLAINTEXT, "Invalid story part request"); } + if (!login.isRw()) { + return NanoHTTPD.newFixedLengthResponse(Status.FORBIDDEN, + NanoHTTPD.MIME_PLAINTEXT, "SET story part not allowed"); + } + + if (exiting) { + return NanoHTTPD.newFixedLengthResponse(Status.SERVICE_UNAVAILABLE, + NanoHTTPD.MIME_PLAINTEXT, "Server is exiting..."); + } + String luid = uriParts[off + 0]; String type = uriParts[off + 1]; @@ -341,7 +396,7 @@ public class WebLibraryServer extends WebLibraryServerHtml { } String type = uriParts[off + 0]; - String id = uriParts[off + 1]; + String id = URLDecoder.decode(uriParts[off + 1], "UTF-8"); InputStream in = null; @@ -381,8 +436,18 @@ public class WebLibraryServer extends WebLibraryServerHtml { NanoHTTPD.MIME_PLAINTEXT, "Invalid cover request"); } + if (!login.isRw()) { + return NanoHTTPD.newFixedLengthResponse(Status.FORBIDDEN, + NanoHTTPD.MIME_PLAINTEXT, "Cover request not allowed"); + } + + if (exiting) { + return NanoHTTPD.newFixedLengthResponse(Status.SERVICE_UNAVAILABLE, + NanoHTTPD.MIME_PLAINTEXT, "Server is exiting..."); + } + String type = uriParts[off + 0]; - String id = uriParts[off + 1]; + String id = URLDecoder.decode(uriParts[off + 1], "UTF-8"); if ("source".equals(type)) { sourceCover(id, login, luid); @@ -402,6 +467,16 @@ public class WebLibraryServer extends WebLibraryServerHtml { throws IOException { final BasicLibrary lib = Instance.getInstance().getLibrary(); + if (!login.isRw()) { + return NanoHTTPD.newFixedLengthResponse(Status.FORBIDDEN, + NanoHTTPD.MIME_PLAINTEXT, "Import not allowed"); + } + + if (exiting) { + return NanoHTTPD.newFixedLengthResponse(Status.SERVICE_UNAVAILABLE, + NanoHTTPD.MIME_PLAINTEXT, "Server is exiting..."); + } + final URL url = new URL(urlStr); final Progress pg = new Progress(); final String luid = lib.getNextId(); @@ -414,7 +489,7 @@ public class WebLibraryServer extends WebLibraryServerHtml { @Override public void run() { try { - lib.imprt(url, pg); + lib.imprt(url, luid, pg); } catch (IOException e) { Instance.getInstance().getTraceHandler().error(e); } finally { @@ -425,8 +500,6 @@ public class WebLibraryServer extends WebLibraryServerHtml { } }, "Import story: " + urlStr).start(); - lib.imprt(url, pg); - return NanoHTTPD.newFixedLengthResponse(Status.OK, NanoHTTPD.MIME_PLAINTEXT, luid); } @@ -455,6 +528,35 @@ public class WebLibraryServer extends WebLibraryServerHtml { return newInputStreamResponse(NanoHTTPD.MIME_PLAINTEXT, null); } + @Override + protected Response delete(String uri, WLoginResult login) + throws IOException { + String[] uriParts = uri.split("/"); + int off = 2; // "" and "delete" + + if (uriParts.length < off + 1) { + return NanoHTTPD.newFixedLengthResponse(Status.BAD_REQUEST, + NanoHTTPD.MIME_PLAINTEXT, "Invalid delete request"); + } + + if (!login.isRw()) { + return NanoHTTPD.newFixedLengthResponse(Status.FORBIDDEN, + NanoHTTPD.MIME_PLAINTEXT, "Delete not allowed"); + } + + if (exiting) { + return NanoHTTPD.newFixedLengthResponse(Status.SERVICE_UNAVAILABLE, + NanoHTTPD.MIME_PLAINTEXT, "Server is exiting..."); + } + + String luid = uriParts[off + 0]; + + BasicLibrary lib = Instance.getInstance().getLibrary(); + lib.delete(luid); + + return newInputStreamResponse(NanoHTTPD.MIME_PLAINTEXT, null); + } + @Override protected List metas(WLoginResult login) throws IOException { BasicLibrary lib = Instance.getInstance().getLibrary();