X-Git-Url: http://git.nikiroo.be/?p=fanfix.git;a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Flibrary%2FWebLibraryServerHtml.java;h=d5ba533f27b5ac746c631ccb2457ea3c3aa3649f;hp=ac0631dd60ba32abc8f34b7297510bbc78039c95;hb=1f1318b8c0ba0bc472c263e54079cbaa6e303fe0;hpb=33c6a3f700ed10ce63a5601237c9072f25b825ef diff --git a/src/be/nikiroo/fanfix/library/WebLibraryServerHtml.java b/src/be/nikiroo/fanfix/library/WebLibraryServerHtml.java index ac0631d..d5ba533 100644 --- a/src/be/nikiroo/fanfix/library/WebLibraryServerHtml.java +++ b/src/be/nikiroo/fanfix/library/WebLibraryServerHtml.java @@ -5,6 +5,8 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.security.KeyStore; +import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -14,7 +16,6 @@ import javax.net.ssl.SSLServerSocketFactory; import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.bundles.Config; -import be.nikiroo.fanfix.bundles.UiConfig; import be.nikiroo.fanfix.data.Chapter; import be.nikiroo.fanfix.data.MetaData; import be.nikiroo.fanfix.data.Paragraph; @@ -22,6 +23,7 @@ import be.nikiroo.fanfix.data.Paragraph.ParagraphType; import be.nikiroo.fanfix.data.Story; import be.nikiroo.fanfix.library.WebLibraryServer.WLoginResult; import be.nikiroo.fanfix.library.web.WebLibraryServerIndex; +import be.nikiroo.fanfix.library.web.templates.WebLibraryServerTemplates; import be.nikiroo.fanfix.reader.TextOutput; import be.nikiroo.utils.IOUtils; import be.nikiroo.utils.NanoHTTPD; @@ -35,6 +37,9 @@ abstract class WebLibraryServerHtml implements Runnable { private NanoHTTPD server; protected TraceHandler tracer = new TraceHandler(); + WebLibraryServerTemplates templates = WebLibraryServerTemplates + .getInstance(); + abstract protected WLoginResult login(String who, String cookie); abstract protected WLoginResult login(String who, String key, @@ -70,6 +75,13 @@ abstract class WebLibraryServerHtml implements Runnable { protected abstract Response delete(String uri, WLoginResult login) throws IOException; + /** + * Wait until all operations are done and stop the server. + *

+ * All the new R/W operations will be refused after a call to stop. + */ + protected abstract Response stop(WLoginResult login); + public WebLibraryServerHtml(boolean secure) throws IOException { Integer port = Instance.getInstance().getConfig() .getInteger(Config.SERVER_PORT); @@ -166,13 +178,14 @@ abstract class WebLibraryServerHtml implements Runnable { } Response rep = null; - if (!login.isSuccess() && WebLibraryUrls.isSupportedUrl(uri)) { - rep = loginPage(login, uri); - } + try { + if (!login.isSuccess() + && WebLibraryUrls.isSupportedUrl(uri, true)) { + rep = loginPage(login, uri); + } - if (rep == null) { - try { - if (WebLibraryUrls.isSupportedUrl(uri)) { + if (rep == null) { + if (WebLibraryUrls.isSupportedUrl(uri, false)) { if (WebLibraryUrls.INDEX_URL.equals(uri)) { rep = root(session, cookies, login); } else if (WebLibraryUrls.VERSION_URL.equals(uri)) { @@ -210,6 +223,8 @@ abstract class WebLibraryServerHtml implements Runnable { } } else if (WebLibraryUrls.isDeleteUrl(uri)) { rep = delete(uri, login); + } else if (WebLibraryUrls.EXIT_URL.equals(uri)) { + rep = WebLibraryServerHtml.this.stop(login); } else { getTraceHandler().error( "Supported URL was not processed: " @@ -243,13 +258,12 @@ abstract class WebLibraryServerHtml implements Runnable { NanoHTTPD.MIME_PLAINTEXT, "Not Found"); } } - } catch (Exception e) { - Instance.getInstance().getTraceHandler().error( - new IOException("Cannot process web request", - e)); - rep = newFixedLengthResponse(Status.INTERNAL_ERROR, - NanoHTTPD.MIME_PLAINTEXT, "An error occured"); } + } catch (Exception e) { + Instance.getInstance().getTraceHandler().error( + new IOException("Cannot process web request", e)); + rep = newFixedLengthResponse(Status.INTERNAL_ERROR, + NanoHTTPD.MIME_PLAINTEXT, "An error occured"); } return rep; @@ -272,6 +286,10 @@ abstract class WebLibraryServerHtml implements Runnable { } } + protected void doStop() { + server.stop(); + } + /** * The traces handler for this {@link WebLibraryServerHtml}. * @@ -295,43 +313,26 @@ abstract class WebLibraryServerHtml implements Runnable { this.tracer = tracer; } - private Response loginPage(WLoginResult login, String uri) { - StringBuilder builder = new StringBuilder(); - - appendPreHtml(builder, true); + private Response loginPage(WLoginResult login, String uri) + throws IOException { + List