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;
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;
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;
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,
}
Response rep = null;
- if (!login.isSuccess()
- && WebLibraryUrls.isSupportedUrl(uri, true)) {
- rep = loginPage(login, uri);
- }
+ try {
+ if (!login.isSuccess()
+ && WebLibraryUrls.isSupportedUrl(uri, true)) {
+ rep = loginPage(login, uri);
+ }
- if (rep == null) {
- try {
+ if (rep == null) {
if (WebLibraryUrls.isSupportedUrl(uri, false)) {
if (WebLibraryUrls.INDEX_URL.equals(uri)) {
rep = root(session, cookies, login);
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);
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;
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<Template> content = new ArrayList<Template>();
if (login.isBadLogin()) {
- builder.append(
- "\t\t<div class='error'>Bad login or password</div>");
+ content.add(templates.message("Bad login or password", true));
} else if (login.isBadCookie()) {
- builder.append(
- "\t\t<div class='error'>Your session timed out</div>");
- }
-
- if (WebLibraryUrls.LOGOUT_URL.equals(uri)) {
- uri = WebLibraryUrls.INDEX_URL;
+ content.add(templates.message("Your session timed out", true));
}
- builder.append("\t\t<form method='POST' action='" + uri
- + "' class='login'>\n");
- builder.append(
- "\t\t\t<p>You must be logged into the system to see the stories.</p>");
- builder.append("\t\t\t<input type='text' name='login' />\n");
- builder.append("\t\t\t<input type='password' name='password' />\n");
- builder.append("\t\t\t<input type='submit' value='Login' />\n");
- builder.append("\t\t</form>\n");
+ content.add(templates.login(uri));
- appendPostHtml(builder);
-
- 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<String, String> cookies,
WLoginResult login) throws IOException {
BasicLibrary lib = Instance.getInstance().getLibrary();
MetaResultList result = new MetaResultList(metas(login));
- StringBuilder builder = new StringBuilder();
-
- appendPreHtml(builder, true);
Map<String, String> params = session.getParms();
// TODO: javascript in realtime, using visible=false + hide [submit]
- StringBuilder selects = new StringBuilder();
+ List<Template> selects = new ArrayList<Template>();
boolean sourcesSel = false;
boolean authorsSel = false;
boolean tagsSel = false;
- String selectTemplate = getTemplate("browser.select");
-
if (!browser.isEmpty()) {
- StringBuilder options = new StringBuilder();
+ List<Template> options = new ArrayList<Template>();
if (browser.equals("sources")) {
sourcesSel = true;
// TODO: if 1 group -> no group
Map<String, List<String>> sources = result.getSourcesGrouped();
for (String source : sources.keySet()) {
- appendOption(options, 5, source, source, browser2);
+ options.add(
+ templates.browserOption(source, source, browser2));
}
} else if (browser.equals("authors")) {
authorsSel = true;
// TODO: if 1 group -> no group
Map<String, List<String>> authors = result.getAuthorsGrouped();
for (String author : authors.keySet()) {
- appendOption(options, 5, author, author, browser2);
+ options.add(
+ templates.browserOption(author, author, browser2));
}
} else if (browser.equals("tags")) {
tagsSel = true;
filterTag = browser2.isEmpty() ? filterTag : browser2;
for (String tag : result.getTags()) {
- appendOption(options, 5, tag, tag, browser2);
+ options.add(templates.browserOption(tag, tag, browser2));
}
}
- selects.append(selectTemplate //
- .replace("${name}", "browser2") //
- .replace("${value}", browser2) //
- .replace("${options}", options.toString()) //
- );
+ selects.add(templates.browserSelect("browser2", browser2, options));
}
if (!browser2.isEmpty()) {
- StringBuilder options = new StringBuilder();
+ List<Template> options = new ArrayList<Template>();
if (browser.equals("sources")) {
filterSource = browser3.isEmpty() ? filterSource : browser3;
if (sources != null && !sources.isEmpty()) {
// TODO: single empty value
for (String source : sources) {
- appendOption(options, 5, source, source, browser3);
+ options.add(templates.browserOption(source, source,
+ browser3));
}
}
} else if (browser.equals("authors")) {
if (authors != null && !authors.isEmpty()) {
// TODO: single empty value
for (String author : authors) {
- appendOption(options, 5, author, author, browser3);
+ options.add(templates.browserOption(author, author,
+ browser3));
}
}
}
- selects.append(selectTemplate //
- .replace("${name}", "browser3") //
- .replace("${value}", browser3) //
- .replace("${options}", options.toString()) //
- );
+ selects.add(templates.browserSelect("browser3", browser3, options));
}
- String sel = "selected='selected'";
- builder.append(getTemplate("browser") //
- .replace("${sourcesSelected}", sourcesSel ? sel : "") //
- .replace("${authorsSelected}", authorsSel ? sel : "") //
- .replace("${tagsSelected}", tagsSel ? sel : "") //
- .replace("${filter}", filter) //
- .replace("${selects}", selects.toString()) //
- );
-
- builder.append("\t\t<div class='books'>\n");
+ List<Template> booklines = new ArrayList<Template>();
for (MetaData meta : result.getMetas()) {
if (!filter.isEmpty() && !meta.getTitle().toLowerCase()
.contains(filter.toLowerCase())) {
author = "(" + meta.getAuthor() + ")";
}
- String cachedClass = "cached";
- String cached = "◉";
- if (!lib.isCached(meta.getLuid())) {
- cachedClass = "uncached";
- cached = "○";
- }
-
- builder.append(getTemplate("bookline") //
- .replace("${href}",
- WebLibraryUrls.getViewUrl(meta.getLuid(), null,
- null)) //
- .replace("${luid}", meta.getLuid()) //
- .replace("${title}", meta.getTitle()) //
- .replace("${author}", author) //
- .replace("${cachedClass}", cachedClass) //
- .replace("${cached}", cached) //
- );
+ booklines.add(templates.bookline( //
+ meta.getLuid(), //
+ WebLibraryUrls.getViewUrl(meta.getLuid(), null, null), //
+ meta.getTitle(), //
+ author, //
+ lib.isCached(meta.getLuid()) //
+ ));
}
- builder.append("\t\t</div>\n");
- 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<String, String> cookies, String uri,
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)
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,
last = WebLibraryUrls.getViewUrl(luid,
story.getChapters().size(), null);
- StringBuilder desc = new StringBuilder();
-
+ Template desc = null;
if (chapter <= 0) {
- desc.append("\t\t\t<h1 class='title'>")
- .append(story.getMeta().getTitle())
- .append("</h1>\n");
- desc.append("\t\t\t<div class='desc'>\n");
- desc.append(
- "\t\t\t\t<a href='" + next + "' class='cover'>\n");
- desc.append("\t\t\t\t\t<img src='/story/" + luid
- + "/cover'/>\n");
- desc.append("\t\t\t\t</a>\n");
- desc.append("\t\t\t\t<table class='details'>\n");
+ List<Template> desclines = new ArrayList<Template>();
Map<String, String> details = BasicLibrary
.getMetaDesc(story.getMeta());
for (String key : details.keySet()) {
- appendTableRow(desc, 5, key, details.get(key));
+ desclines.add(templates.viewerDescline(key,
+ details.get(key)));
}
- desc.append("\t\t\t\t</table>\n");
- desc.append("\t\t\t</div>\n");
- desc.append("\t\t\t<h1 class='title'>Description</h1>\n");
+
+ desc = templates.viewerDesc( //
+ story.getMeta().getTitle(), //
+ next, //
+ WebLibraryUrls.getStoryUrlCover(luid), //
+ desclines //
+ );
}
- content.append("\t\t<div class='viewer text'>\n");
- content.append(desc);
+ String content;
if (chap.getParagraphs().size() <= 0) {
- content.append("\t\t\tNo content provided.\n");
+ content = "No content provided.";
} else {
- content.append(
- "\t\t\t<!-- The text is in HTML 3.2, so it can also work in Java Swing: -->\n"); //
- content.append("\t\t\t").append(
- new TextOutput(false).convert(chap, chapter > 0))
- .append("\n");
+ content = new TextOutput(false).convert(chap, chapter > 0);
}
- content.append("\t\t</div>\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,
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) {
}
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("" //
- + "\t\t<a class='viewer link' oncontextmenu='%s' href='%s'>\n"
- + "\t\t\t<img class='viewer img' style='%s' src='%s'/>\n"
- + "\t\t</a>\n", //
- javascript, //
- next, //
- zoomStyle, //
+ viewerItem = templates.viewerImage(
WebLibraryUrls.getStoryUrl(luid, chapter,
- paragraph)));
+ paragraph), //
+ disabledRight ? null : next, //
+ zoomStyle //
+ );
} else {
- content.append(String.format("" //
- + "\t\t<div class='viewer text'>" //
- + "\t\t\t%s\n" //
- + "\t\t</div>\n", //
- para.getContent()));
+ viewerItem = templates.viewerText(null,
+ new TextOutput(false).convert(para));
}
}
- builder.append(String.format("" //
- + "\t\t<div class='bar navbar'>\n" //
- + "\t\t\t<a%s class='button first' href='%s'><<</a>\n"//
- + "\t\t\t<a%s id='previous' class='button previous' href='%s'><</a>\n" //
- + "\t\t\t<div class='gotobox itemsbox'>\n" //
- + "\t\t\t\t<div class='button goto'>%d</div>\n" //
- + "\t\t\t\t<div class='items goto'>\n", //
- disabledLeft, first, //
- disabledLeft, previous, //
- paragraph > 0 ? paragraph : chapter //
- ));
-
- // List of chap/para links
+ // List of chap/para links for navbar
- appendItemA(builder, 5, WebLibraryUrls.getViewUrl(luid, 0, null),
- "Description", paragraph == 0 && chapter == 0);
+ List<Template> links = new ArrayList<Template>();
+ 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, 5,
- 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;
chapName += ": " + c.getName();
}
- appendItemA(builder, 5,
- 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\t\t</div>\n" //
- + "\t\t\t</div>\n" //
- + "\t\t\t<a%s class='button next' href='%s'>></a>\n" //
- + "\t\t\t<a%s class='button last' href='%s'>>></a>\n"//
- + "\t\t</div>\n", //
- disabledRight, next, //
- disabledRight, last //
- ));
+ // Navbar
+
+ Template navbar = templates.viewerNavbar( //
+ paragraph > 0 ? paragraph : chapter, //
+ links, //
+ first, //
+ previous, //
+ next, //
+ last, //
+ disabledLeft, //
+ disabledLeft, //
+ disabledRight, //
+ disabledRight //
+ );
- builder.append(content);
+ // Buttons on the optionbar
- builder.append("\t\t<div class='bar optionbar");
+ List<Template> buttons = new ArrayList<Template>();
+ buttons.add(templates.viewerOptionbarButton( //
+ "Back", "/", "back", false));
if (paragraph > 0) {
- builder.append(" s4");
- } else {
- builder.append(" s1");
+ buttons.add(templates.viewerOptionbarButton( //
+ "1:1", uri + "?optionName=zoom&optionValue=real",
+ "zoomreal", disabledZoomReal));
+ buttons.add(templates.viewerOptionbarButton( //
+ "]width[",
+ uri + "?optionName=zoom&optionValue=widthlimited",
+ "zoomwidthlimited", disabledZoomWidthLimited));
+ buttons.add(templates.viewerOptionbarButton( //
+ "Width", uri + "?optionName=zoom&optionValue=width",
+ "zoomwidth", disabledZoomWidth));
+ buttons.add(templates.viewerOptionbarButton( //
+ "Height", uri + "?optionName=zoom&optionValue=height",
+ "zoomheight", disabledZoomHeight));
}
- builder.append("'>\n");
- builder.append("\t\t\t<a class='button back' href='/'>BACK</a>\n");
- if (paragraph > 0) {
- builder.append(String.format("" //
- + "\t\t\t<a%s class='button zoomreal' href='%s'>REAL</a>\n"//
- + "\t\t\t<a%s class='button zoomwidth' href='%s'>WIDTH</a>\n"//
- + "\t\t\t<a%s class='button zoomheight' href='%s'>HEIGHT</a>\n", //
- disabledZoomReal,
- uri + "?optionName=zoom&optionValue=real", //
- disabledZoomWidth,
- uri + "?optionName=zoom&optionValue=width", //
- disabledZoomHeight,
- uri + "?optionName=zoom&optionValue=height" //
- ));
- }
+ // Optionbar
+
+ Template optionbar = templates.viewerOptionbar( //
+ (paragraph > 0) ? 5 : 1, //
+ buttons //
+ );
- builder.append("\t\t</div>\n");
+ // Full content
- appendPostHtml(builder);
- return NanoHTTPD.newFixedLengthResponse(Status.OK,
- NanoHTTPD.MIME_HTML, builder.toString());
+ 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));
}
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("\t\t<div class='banner'>\n");
- builder.append("\t\t\t<img class='ico' src='/") //
- .append(favicon) //
- .append("'/>\n");
- builder.append("\t\t\t<h1>Fanfix</h1>\n");
- builder.append("\t\t\t<h2>") //
- .append(Version.getCurrentVersion()) //
- .append("</h2>\n");
- builder.append("\t\t</div>\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("<option value='").append(value).append("'");
- if (value.equals(selected)) {
- builder.append(" selected='selected'");
- }
- builder.append(">").append(name).append("</option>\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("<tr>");
- for (String td : tds) {
- builder.append("<td class='col");
- builder.append(col++);
- builder.append("'>");
- builder.append(td);
- builder.append("</td>");
- }
- builder.append("</tr>\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("<a href='");
- builder.append(link);
- builder.append("' class='item goto");
- if (selected) {
- builder.append(" selected");
- }
- builder.append("'>");
- builder.append(name);
- builder.append("</a>\n");
- }
-
- private String getTemplate(String template) throws IOException {
- InputStream in = IOUtils.openResource(WebLibraryServerIndex.class,
- template + ".html");
- try {
- return IOUtils.readSmallStream(in);
- } finally {
- in.close();
- }
- }
}