X-Git-Url: http://git.nikiroo.be/?p=nikiroo-utils.git;a=blobdiff_plain;f=library%2FWebLibraryServerHtml.java;h=42ebead137c05e8189fec17a9d57d4da91e4858a;hp=c28188cde5467746b21e5f5f9905b9aeede978db;hb=258e065f81071a861711ef935dca3ec5563f4360;hpb=87ffaa787388b0389f5134aa70c4b1e2e940c118 diff --git a/library/WebLibraryServerHtml.java b/library/WebLibraryServerHtml.java index c28188c..42ebead 100644 --- a/library/WebLibraryServerHtml.java +++ b/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; @@ -22,6 +24,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 +38,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, @@ -47,12 +53,36 @@ abstract class WebLibraryServerHtml implements Runnable { abstract protected Response getStoryPart(String uri, WLoginResult login); + abstract protected Response setStoryPart(String uri, String value, + WLoginResult login) throws IOException; + + abstract protected Response getCover(String uri, WLoginResult login) + throws IOException; + + abstract protected Response setCover(String uri, String luid, + WLoginResult login) throws IOException; + abstract protected List metas(WLoginResult login) throws IOException; abstract protected Story story(String luid, WLoginResult login) throws IOException; + protected abstract Response imprt(String uri, String url, + WLoginResult login) throws IOException; + + protected abstract Response imprtProgress(String uri, WLoginResult login); + + 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); @@ -149,29 +179,53 @@ 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)) { rep = newFixedLengthResponse(Status.OK, MIME_PLAINTEXT, Version.getCurrentVersion().toString()); + } else if (WebLibraryUrls.isCoverUrl(uri)) { + String luid = params.get("luid"); + if (luid != null) { + rep = setCover(uri, luid, login); + } else { + rep = getCover(uri, login); + } } else if (WebLibraryUrls.isListUrl(uri)) { rep = getList(uri, login); } else if (WebLibraryUrls.isStoryUrl(uri)) { - rep = getStoryPart(uri, login); + String value = params.get("value"); + if (value != null) { + rep = setStoryPart(uri, value, login); + } else { + rep = getStoryPart(uri, login); + } } else if (WebLibraryUrls.isViewUrl(uri)) { rep = getViewer(cookies, uri, login); } else if (WebLibraryUrls.LOGOUT_URL.equals(uri)) { session.getCookies().delete("cookie"); cookies.remove("cookie"); rep = loginPage(login(false, false), uri); + } else if (WebLibraryUrls.isImprtUrl(uri)) { + String url = params.get("url"); + if (url != null) { + rep = imprt(uri, url, login); + } else { + rep = imprtProgress(uri, login); + } + } 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: " @@ -205,13 +259,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; @@ -234,6 +287,10 @@ abstract class WebLibraryServerHtml implements Runnable { } } + protected void doStop() { + server.stop(); + } + /** * The traces handler for this {@link WebLibraryServerHtml}. * @@ -257,43 +314,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