X-Git-Url: https://git.nikiroo.be/?a=blobdiff_plain;f=library%2FWebLibraryServerHtml.java;h=d5ba533f27b5ac746c631ccb2457ea3c3aa3649f;hb=868c706b81e33e99c5f57fb9fc200808e061c4bb;hp=ac0631dd60ba32abc8f34b7297510bbc78039c95;hpb=cd1aa392a6e17382f573ee816c26fd5f19301f02;p=fanfix.git
diff --git a/library/WebLibraryServerHtml.java b/library/WebLibraryServerHtml.java
index ac0631d..d5ba533 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;
@@ -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 content = new ArrayList();
if (login.isBadLogin()) {
- builder.append("Bad login or password
");
+ content.add(templates.message("Bad login or password", true));
} else if (login.isBadCookie()) {
- builder.append("Your session timed out
");
+ content.add(templates.message("Your session timed out", true));
}
- if (WebLibraryUrls.LOGOUT_URL.equals(uri)) {
- uri = WebLibraryUrls.INDEX_URL;
- }
-
- builder.append(
- "\n");
-
- appendPostHtml(builder);
+ content.add(templates.login(uri));
- return NanoHTTPD.newFixedLengthResponse(Status.FORBIDDEN,
- NanoHTTPD.MIME_HTML, builder.toString());
+ return NanoHTTPD.newChunkedResponse(Status.FORBIDDEN,
+ NanoHTTPD.MIME_HTML, templates.index(true, content).read());
}
private Response root(IHTTPSession session, Map cookies,
WLoginResult login) throws IOException {
BasicLibrary lib = Instance.getInstance().getLibrary();
MetaResultList result = new MetaResultList(metas(login));
- StringBuilder builder = new StringBuilder();
-
- appendPreHtml(builder, true);
Map params = session.getParms();
@@ -355,45 +356,49 @@ abstract class WebLibraryServerHtml implements Runnable {
// TODO: javascript in realtime, using visible=false + hide [submit]
- builder.append("\n");
-
- builder.append("\t");
+ List
booklines = new ArrayList();
for (MetaData meta : result.getMetas()) {
if (!filter.isEmpty() && !meta.getTitle().toLowerCase()
.contains(filter.toLowerCase())) {
@@ -464,39 +451,25 @@ abstract class WebLibraryServerHtml implements Runnable {
continue;
}
- builder.append("\n");
+
+ booklines.add(templates.bookline( //
+ meta.getLuid(), //
+ WebLibraryUrls.getViewUrl(meta.getLuid(), null, null), //
+ meta.getTitle(), //
+ author, //
+ lib.isCached(meta.getLuid()) //
+ ));
}
- builder.append(" ");
- appendPostHtml(builder);
- return NanoHTTPD.newFixedLengthResponse(builder.toString());
+ // Add the browser in front of the booklines
+ booklines.add(0, templates.browser(browser, filter, selects));
+
+ return newInputStreamResponse(NanoHTTPD.MIME_HTML,
+ templates.index(true, booklines).read());
}
private Response getViewer(Map cookies, String uri,
@@ -549,9 +522,6 @@ abstract class WebLibraryServerHtml implements Runnable {
NanoHTTPD.MIME_PLAINTEXT, "Story not found");
}
- StringBuilder builder = new StringBuilder();
- appendPreHtml(builder, false);
-
// For images documents, always go to the images if not chap 0 desc
if (story.getMeta().isImageDocument()) {
if (chapter > 0 && paragraph <= 0)
@@ -572,14 +542,13 @@ abstract class WebLibraryServerHtml implements Runnable {
String first, previous, next, last;
- StringBuilder content = new StringBuilder();
-
- String disabledLeft = "";
- String disabledRight = "";
- String disabledZoomReal = "";
- String disabledZoomWidth = "";
- String disabledZoomHeight = "";
+ boolean disabledLeft = false;
+ boolean disabledRight = false;
+ boolean disabledZoomReal = false;
+ boolean disabledZoomWidth = false;
+ boolean disabledZoomHeight = false;
+ Template viewerItem = null;
if (paragraph <= 0) {
first = WebLibraryUrls.getViewUrl(luid, 0, null);
previous = WebLibraryUrls.getViewUrl(luid,
@@ -590,40 +559,37 @@ abstract class WebLibraryServerHtml implements Runnable {
last = WebLibraryUrls.getViewUrl(luid,
story.getChapters().size(), null);
- StringBuilder desc = new StringBuilder();
-
+ Template desc = null;
if (chapter <= 0) {
- desc.append("");
- desc.append(story.getMeta().getTitle());
- desc.append("
\n");
- desc.append("\n");
- desc.append("\t
\n");
- desc.append("\t\t\n");
- desc.append("\t\n");
- desc.append("\t
\n");
+ List desclines = new ArrayList();
Map details = BasicLibrary
.getMetaDesc(story.getMeta());
for (String key : details.keySet()) {
- appendTableRow(desc, 2, key, details.get(key));
+ desclines.add(templates.viewerDescline(key,
+ details.get(key)));
}
- desc.append("\t
\n");
- desc.append("
\n");
- desc.append("Description
\n");
+
+ desc = templates.viewerDesc( //
+ story.getMeta().getTitle(), //
+ next, //
+ WebLibraryUrls.getStoryUrlCover(luid), //
+ desclines //
+ );
+ }
+
+ String content;
+ if (chap.getParagraphs().size() <= 0) {
+ content = "No content provided.";
+ } else {
+ content = new TextOutput(false).convert(chap, chapter > 0);
}
- content.append("\n");
- content.append(desc);
- String description = new TextOutput(false).convert(chap,
- chapter > 0);
- content.append(chap.getParagraphs().size() <= 0
- ? "No content provided."
- : description);
- content.append("
\n");
+ viewerItem = templates.viewerText(desc, content);
if (chapter <= 0)
- disabledLeft = " disabled='disbaled'";
+ disabledLeft = true;
if (chapter >= story.getChapters().size())
- disabledRight = " disabled='disbaled'";
+ disabledRight = true;
} else {
first = WebLibraryUrls.getViewUrl(luid, chapter, 1);
previous = WebLibraryUrls.getViewUrl(luid, chapter,
@@ -634,13 +600,13 @@ abstract class WebLibraryServerHtml implements Runnable {
chap.getParagraphs().size());
if (paragraph <= 1)
- disabledLeft = " disabled='disbaled'";
+ disabledLeft = true;
if (paragraph >= chap.getParagraphs().size())
- disabledRight = " disabled='disbaled'";
+ disabledRight = true;
// First -> previous *chapter*
if (chapter > 0)
- disabledLeft = "";
+ disabledLeft = false;
first = WebLibraryUrls.getViewUrl(luid,
(Math.max(chapter - 1, 0)), null);
if (paragraph <= 1) {
@@ -658,61 +624,47 @@ abstract class WebLibraryServerHtml implements Runnable {
if (para.getType() == ParagraphType.IMAGE) {
String zoomStyle = "max-width: 100%;";
- disabledZoomWidth = " disabled='disabled'";
+ disabledZoomWidth = true;
String zoomOption = cookies.get("zoom");
if (zoomOption != null && !zoomOption.isEmpty()) {
if (zoomOption.equals("real")) {
zoomStyle = "";
- disabledZoomWidth = "";
- disabledZoomReal = " disabled='disabled'";
+ disabledZoomWidth = false;
+ disabledZoomReal = true;
} else if (zoomOption.equals("width")) {
zoomStyle = "max-width: 100%;";
} else if (zoomOption.equals("height")) {
// see height of navbar + optionbar
zoomStyle = "max-height: calc(100% - 128px);";
- disabledZoomWidth = "";
- disabledZoomHeight = " disabled='disabled'";
+ disabledZoomWidth = false;
+ disabledZoomHeight = true;
}
}
- String javascript = "document.getElementById(\"previous\").click(); return false;";
- content.append(String.format("" //
- + ""
- + ""
- + "", //
- javascript, //
- next, //
- zoomStyle, //
- WebLibraryUrls.getStoryUrl(luid, chapter,
- paragraph)));
+ viewerItem = templates.viewerImage(WebLibraryUrls
+ .getStoryUrl(luid, chapter, paragraph), next,
+ zoomStyle);
} else {
- content.append(String.format("" //
- + "%s
", //
- para.getContent()));
+ viewerItem = templates.viewerText(null,
+ new TextOutput(false).convert(para));
}
}
- builder.append(String.format("" //
- + "\n" //
- + "\t
<<\n"//
- + "\t
<\n" //
- + "\t
\n" //
- + "\t\t
%d
\n" //
- + "\t\t
\n", //
- disabledLeft, first, //
- disabledLeft, previous, //
- paragraph > 0 ? paragraph : chapter //
- ));
-
- // List of chap/para links
+ // List of chap/para links for navbar
- appendItemA(builder, 3, WebLibraryUrls.getViewUrl(luid, 0, null),
- "Description", paragraph == 0 && chapter == 0);
+ List links = new ArrayList();
+ links.add(templates.viewerLink( //
+ "Description", //
+ WebLibraryUrls.getViewUrl(luid, 0, null), //
+ paragraph == 0 && chapter == 0 //
+ ));
if (paragraph > 0) {
for (int i = 1; i <= chap.getParagraphs().size(); i++) {
- appendItemA(builder, 3,
- WebLibraryUrls.getViewUrl(luid, chapter, i),
- "Image " + i, paragraph == i);
+ links.add(templates.viewerLink( //
+ "Image " + i, //
+ WebLibraryUrls.getViewUrl(luid, chapter, i), //
+ paragraph == i //
+ ));
}
} else {
int i = 1;
@@ -722,53 +674,63 @@ abstract class WebLibraryServerHtml implements Runnable {
chapName += ": " + c.getName();
}
- appendItemA(builder, 3,
- WebLibraryUrls.getViewUrl(luid, i, null), chapName,
- chapter == i);
+ links.add(templates.viewerLink( //
+ chapName, //
+ WebLibraryUrls.getViewUrl(luid, i, null), //
+ chapter == i //
+ ));
i++;
}
}
- builder.append(String.format("" //
- + "\t\t
\n" //
- + "\t
\n" //
- + "\t
>\n" //
- + "\t
>>\n"//
- + "
\n", //
- disabledRight, next, //
- disabledRight, last //
- ));
-
- builder.append(content);
-
- builder.append("\n", //
- disabledZoomReal,
- uri + "?optionName=zoom&optionValue=real", //
- disabledZoomWidth,
- uri + "?optionName=zoom&optionValue=width", //
- disabledZoomHeight,
- uri + "?optionName=zoom&optionValue=height" //
- ));
- }
+ // Optionbar
- appendPostHtml(builder);
- return NanoHTTPD.newFixedLengthResponse(Status.OK,
- NanoHTTPD.MIME_HTML, builder.toString());
+ Template optionbar = templates.viewerOptionbar( //
+ (paragraph > 0) ? 4 : 1, //
+ buttons //
+ );
+
+ // Full content
+
+ return newInputStreamResponse(NanoHTTPD.MIME_HTML, //
+ templates.index(false, Arrays.asList( //
+ navbar, //
+ viewerItem, //
+ optionbar //
+ )).read());
} catch (IOException e) {
Instance.getInstance().getTraceHandler()
.error(new IOException("Cannot get image: " + uri, e));
@@ -784,94 +746,4 @@ abstract class WebLibraryServerHtml implements Runnable {
}
return NanoHTTPD.newChunkedResponse(Status.OK, mimeType, in);
}
-
- private String getContentOf(String file) {
- InputStream in = IOUtils.openResource(WebLibraryServerIndex.class,
- file);
- if (in != null) {
- try {
- return IOUtils.readSmallStream(in);
- } catch (IOException e) {
- Instance.getInstance().getTraceHandler().error(
- new IOException("Cannot get file: index.pre.html", e));
- }
- }
-
- return "";
- }
-
- private void appendPreHtml(StringBuilder builder, boolean banner) {
- String favicon = "favicon.ico";
- String icon = Instance.getInstance().getUiConfig()
- .getString(UiConfig.PROGRAM_ICON);
- if (icon != null) {
- favicon = "icon_" + icon.replace("-", "_") + ".png";
- }
-
- builder.append(
- getContentOf("index.pre.html").replace("favicon.ico", favicon));
-
- if (banner) {
- builder.append("\n");
- builder.append("\t
\n");
- builder.append("\t
Fanfix
\n");
- builder.append("\t
") //
- .append(Version.getCurrentVersion()) //
- .append("
\n");
- builder.append("
\n");
- }
- }
-
- private void appendPostHtml(StringBuilder builder) {
- builder.append(getContentOf("index.post.html"));
- }
-
- private void appendOption(StringBuilder builder, int depth, String name,
- String value, String selected) {
- for (int i = 0; i < depth; i++) {
- builder.append("\t");
- }
- builder.append("\n");
- }
-
- private void appendTableRow(StringBuilder builder, int depth,
- String... tds) {
- for (int i = 0; i < depth; i++) {
- builder.append("\t");
- }
-
- int col = 1;
- builder.append("");
- for (String td : tds) {
- builder.append("");
- builder.append(td);
- builder.append(" | ");
- }
- builder.append("
\n");
- }
-
- private void appendItemA(StringBuilder builder, int depth, String link,
- String name, boolean selected) {
- for (int i = 0; i < depth; i++) {
- builder.append("\t");
- }
-
- builder.append("");
- builder.append(name);
- builder.append("\n");
- }
}