X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;ds=sidebyside;f=library%2FWebLibraryServerHtml.java;h=63c50c54df64c4ad5ed5810d030a5783e30ad12e;hb=af6e65daddff2d602017013f498f989fefbb6be9;hp=e96edef95584b5f18a63f62ab630c75abe92f6b0;hpb=159e970c23d9634d39bb8cf2a194f7088f4d59ab;p=fanfix.git
diff --git a/library/WebLibraryServerHtml.java b/library/WebLibraryServerHtml.java
index e96edef..63c50c5 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: "
@@ -232,6 +247,12 @@ abstract class WebLibraryServerHtml implements Runnable {
mimeType = "text/html";
} else if (uri.endsWith(".js")) {
mimeType = "text/javascript";
+ } else if (uri.endsWith(".png")) {
+ mimeType = "image/png";
+ } else if (uri.endsWith(".ico")) {
+ mimeType = "image/x-icon";
+ } else if (uri.endsWith(".java")) {
+ mimeType = "text/plain";
}
rep = newChunkedResponse(Status.OK, mimeType,
in);
@@ -243,22 +264,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 +292,10 @@ abstract class WebLibraryServerHtml implements Runnable {
}
}
+ protected void doStop() {
+ server.stop();
+ }
+
/**
* The traces handler for this {@link WebLibraryServerHtml}.
*
@@ -297,43 +319,27 @@ 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, false, 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 +363,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 +458,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, false, booklines).read());
}
private Response getViewer(Map cookies, String uri,
@@ -551,9 +529,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,14 +549,14 @@ 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 disabledZoomWidthLimited = false;
+ boolean disabledZoomHeight = false;
+ Template viewerItem = null;
if (paragraph <= 0) {
first = WebLibraryUrls.getViewUrl(luid, 0, null);
previous = WebLibraryUrls.getViewUrl(luid,
@@ -592,40 +567,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 //
+ );
}
- 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");
+ String content;
+ if (chap.getParagraphs().size() <= 0) {
+ content = "No content provided.";
+ } else {
+ content = new TextOutput(false).convert(chap, chapter > 0);
+ }
+
+ 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,
@@ -636,13 +608,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) {
@@ -659,62 +631,57 @@ abstract class WebLibraryServerHtml implements Runnable {
}
if (para.getType() == ParagraphType.IMAGE) {
- String zoomStyle = "max-width: 100%;";
- disabledZoomWidth = " disabled='disabled'";
+ // default values:
+ String zoomStyle = "max-width: 800px;";
+ disabledZoomWidthLimited = true;
+
String zoomOption = cookies.get("zoom");
if (zoomOption != null && !zoomOption.isEmpty()) {
if (zoomOption.equals("real")) {
zoomStyle = "";
- disabledZoomWidth = "";
- disabledZoomReal = " disabled='disabled'";
+ disabledZoomWidthLimited = false;
+ disabledZoomReal = true;
+ } else if (zoomOption.equals("widthlimited")) {
+ // default
} else if (zoomOption.equals("width")) {
zoomStyle = "max-width: 100%;";
+ disabledZoomWidthLimited = false;
+ disabledZoomWidth = true;
} else if (zoomOption.equals("height")) {
// see height of navbar + optionbar
zoomStyle = "max-height: calc(100% - 128px);";
- disabledZoomWidth = "";
- disabledZoomHeight = " disabled='disabled'";
+ disabledZoomWidthLimited = false;
+ disabledZoomHeight = true;
}
}
- String javascript = "document.getElementById(\"previous\").click(); return false;";
- content.append(String.format("" //
- + ""
- + "
"
- + "", //
- javascript, //
- next, //
- zoomStyle, //
+ viewerItem = templates.viewerImage(
WebLibraryUrls.getStoryUrl(luid, chapter,
- paragraph)));
+ paragraph), //
+ disabledRight ? null : 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;
@@ -724,53 +691,67 @@ 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) ? 5 : 1, //
+ buttons //
+ );
+
+ // Full content
+
+ return newInputStreamResponse(NanoHTTPD.MIME_HTML, //
+ templates.index(false, (paragraph > 0), Arrays.asList( //
+ navbar, //
+ viewerItem, //
+ optionbar //
+ )).read());
} catch (IOException e) {
Instance.getInstance().getTraceHandler()
.error(new IOException("Cannot get image: " + uri, e));
@@ -778,7 +759,7 @@ abstract class WebLibraryServerHtml implements Runnable {
NanoHTTPD.MIME_PLAINTEXT, "Error when processing request");
}
}
-
+
protected Response newInputStreamResponse(String mimeType, InputStream in) {
if (in == null) {
return NanoHTTPD.newFixedLengthResponse(Status.NO_CONTENT, "",
@@ -786,94 +767,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
![](/") //
- .append(favicon) //
- .append(")
\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");
- }
}