X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=library%2FWebLibraryServer.java;h=56d78ed4de20b5a52583af21105043a07cc8e89f;hb=379a497e6fd2b959c57b2ff4023413e2daf36232;hp=9073b8cad58895f02ccbd88d2a8d2a9deebf6397;hpb=cd1aa392a6e17382f573ee816c26fd5f19301f02;p=fanfix.git diff --git a/library/WebLibraryServer.java b/library/WebLibraryServer.java index 9073b8c..56d78ed 100644 --- a/library/WebLibraryServer.java +++ b/library/WebLibraryServer.java @@ -70,6 +70,8 @@ public class WebLibraryServer extends WebLibraryServerHtml { private Map imprts = new HashMap(); + private boolean exiting; + public WebLibraryServer(boolean secure) throws IOException { super(secure); @@ -97,6 +99,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); @@ -165,9 +217,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, @@ -304,6 +355,11 @@ public class WebLibraryServer extends WebLibraryServerHtml { 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]; @@ -384,6 +440,11 @@ public class WebLibraryServer extends WebLibraryServerHtml { 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]; @@ -410,6 +471,11 @@ public class WebLibraryServer extends WebLibraryServerHtml { 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(); @@ -477,6 +543,11 @@ public class WebLibraryServer extends WebLibraryServerHtml { 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();