X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;ds=inline;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Flibrary%2FWebLibraryServerHtml.java;h=42ebead137c05e8189fec17a9d57d4da91e4858a;hb=074a83257de30c3cd7409b0840ab6f27ed0e93b5;hp=e96edef95584b5f18a63f62ab630c75abe92f6b0;hpb=29ff5d3cf736a65f02325f373a33e79f591a2bd6;p=fanfix.git
diff --git a/src/be/nikiroo/fanfix/library/WebLibraryServerHtml.java b/src/be/nikiroo/fanfix/library/WebLibraryServerHtml.java
index e96edef..42ebead 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;
@@ -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,
@@ -70,6 +76,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 +179,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 +224,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,22 +259,19 @@ 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;
}
};
- if (ssf != null)
-
- {
+ if (ssf != null) {
getTraceHandler().trace("Install SSL on the web server...");
server.makeSecure(ssf, null);
getTraceHandler().trace("Done.");
@@ -274,6 +287,10 @@ abstract class WebLibraryServerHtml implements Runnable {
}
}
+ protected void doStop() {
+ server.stop();
+ }
+
/**
* The traces handler for this {@link WebLibraryServerHtml}.
*
@@ -297,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 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;
- }
+ content.add(templates.login(uri));
- builder.append(
- "\n");
-
- appendPostHtml(builder);
-
- 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();
@@ -357,45 +357,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())) {
@@ -466,39 +452,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,
@@ -551,9 +523,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)
@@ -574,7 +543,7 @@ abstract class WebLibraryServerHtml implements Runnable {
String first, previous, next, last;
- StringBuilder content = new StringBuilder();
+ String viewer = "";
String disabledLeft = "";
String disabledRight = "";
@@ -592,35 +561,37 @@ abstract class WebLibraryServerHtml implements Runnable {
last = WebLibraryUrls.getViewUrl(luid,
story.getChapters().size(), null);
- StringBuilder desc = new StringBuilder();
-
+ String desc = "";
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");
+ StringBuilder desclines = new StringBuilder();
Map details = BasicLibrary
.getMetaDesc(story.getMeta());
for (String key : details.keySet()) {
- appendTableRow(desc, 2, key, details.get(key));
+ desclines.append(getTemplate("viewer.descline") //
+ .replace("${key}", key) //
+ .replace("${value}", details.get(key)) //
+ );
}
- desc.append("\t
\n");
- desc.append("
\n");
- desc.append("Description
\n");
+
+ desc = getTemplate("viewer.desc") //
+ .replace("${title}", story.getMeta().getTitle()) //
+ .replace("${href}", next) //
+ .replace("${cover}",
+ WebLibraryUrls.getStoryUrlCover(luid)) //
+ .replace("${details}", desclines.toString()) //
+ ;
}
- 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");
+ viewer = getTemplate("viewer.text") //
+ .replace("${desc}", desc) //
+ ;
+ if (chap.getParagraphs().size() <= 0) {
+ viewer = viewer.replace("${content}",
+ "No content provided.");
+ } else {
+ viewer = viewer.replace("${content}",
+ new TextOutput(false).convert(chap, chapter > 0));
+ }
if (chapter <= 0)
disabledLeft = " disabled='disbaled'";
@@ -677,44 +648,39 @@ abstract class WebLibraryServerHtml implements Runnable {
}
}
- String javascript = "document.getElementById(\"previous\").click(); return false;";
- content.append(String.format("" //
- + ""
- + ""
- + "", //
- javascript, //
- next, //
- zoomStyle, //
- WebLibraryUrls.getStoryUrl(luid, chapter,
- paragraph)));
+ viewer = getTemplate("viewer.image") //
+ .replace("${href}", next) //
+ .replace("${zoomStyle}", zoomStyle) //
+ .replace("${src}", WebLibraryUrls.getStoryUrl(luid,
+ chapter, paragraph)) //
+ ;
} else {
- content.append(String.format("" //
- + "%s
", //
- para.getContent()));
+ viewer = getTemplate("viewer.text") //
+ .replace("${desc}", "") //
+ .replace("${content}",
+ 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
-
- appendItemA(builder, 3, WebLibraryUrls.getViewUrl(luid, 0, null),
- "Description", paragraph == 0 && chapter == 0);
+ StringBuilder links = new StringBuilder();
+ links.append(getTemplate("viewer.link") //
+ .replace("${link}",
+ WebLibraryUrls.getViewUrl(luid, 0, null)) //
+ .replace("${class}",
+ paragraph == 0 && chapter == 0 ? "selected" : "") //
+ .replace("${name}", "Description") //
+ );
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.append(getTemplate("viewer.link") //
+ .replace("${link}",
+ WebLibraryUrls.getViewUrl(luid, chapter, i)) //
+ .replace("${class}",
+ paragraph == i ? "selected" : "") //
+ .replace("${name}", "Image " + i) //
+ );
}
} else {
int i = 1;
@@ -724,51 +690,79 @@ abstract class WebLibraryServerHtml implements Runnable {
chapName += ": " + c.getName();
}
- appendItemA(builder, 3,
- WebLibraryUrls.getViewUrl(luid, i, null), chapName,
- chapter == i);
+ links.append(getTemplate("viewer.link") //
+ .replace("${link}",
+ WebLibraryUrls.getViewUrl(luid, i, null)) //
+ .replace("${class}", chapter == i ? "selected" : "") //
+ .replace("${name}", chapName) //
+ );
i++;
}
}
- builder.append(String.format("" //
- + "\t\t
\n" //
- + "\t
\n" //
- + "\t
>\n" //
- + "\t
>>\n"//
- + "
\n", //
- disabledRight, next, //
- disabledRight, last //
- ));
-
- builder.append(content);
+ // Buttons on the optionbar
- builder.append("\n", //
- disabledZoomReal,
- uri + "?optionName=zoom&optionValue=real", //
- disabledZoomWidth,
- uri + "?optionName=zoom&optionValue=width", //
- disabledZoomHeight,
- uri + "?optionName=zoom&optionValue=height" //
- ));
- }
+ // Full content
+
+ StringBuilder builder = new StringBuilder();
+
+ builder.append(getTemplateIndexPreBanner(false));
+
+ builder.append(getTemplate("viewer.navbar") //
+ .replace("${disabledFirst}", disabledLeft) //
+ .replace("${disabledPrevious}", disabledLeft) //
+ .replace("${disabledNext}", disabledRight) //
+ .replace("${disabledLast}", disabledRight) //
+ .replace("${hrefFirst}", first) //
+ .replace("${hrefPrevious}", previous) //
+ .replace("${hrefNext}", next) //
+ .replace("${hrefLast}", last) //
+ .replace("${current}",
+ "" + (paragraph > 0 ? paragraph : chapter)) //
+ .replace("${links}", links.toString()) //
+ );
+
+ builder.append(viewer);
+
+ builder.append(getTemplate("viewer.optionbar") //
+ .replace("${classSize}", (paragraph > 0 ? "s4" : "s1")) //
+ .replace("${buttons}", buttons.toString()) //
+ );
+
+ builder.append(getTemplate("index.post"));
- appendPostHtml(builder);
return NanoHTTPD.newFixedLengthResponse(Status.OK,
NanoHTTPD.MIME_HTML, builder.toString());
} catch (IOException e) {
@@ -787,22 +781,8 @@ 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) {
+ private String getTemplateIndexPreBanner(boolean banner)
+ throws IOException {
String favicon = "favicon.ico";
String icon = Instance.getInstance().getUiConfig()
.getString(UiConfig.PROGRAM_ICON);
@@ -810,70 +790,30 @@ abstract class WebLibraryServerHtml implements Runnable {
favicon = "icon_" + icon.replace("-", "_") + ".png";
}
- builder.append(
- getContentOf("index.pre.html").replace("favicon.ico", favicon));
+ String html = getTemplate("index.pre") //
+ .replace("${title}", "Fanfix") //
+ .replace("${favicon}", 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");
+ html += getTemplate("index.banner") //
+ .replace("${favicon}", favicon) //
+ .replace("${version}",
+ Version.getCurrentVersion().toString()) //
+ ;
}
- int col = 1;
- builder.append("");
- for (String td : tds) {
- builder.append("");
- builder.append(td);
- builder.append(" | ");
- }
- builder.append("
\n");
+ return html;
}
- 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");
}
}