package be.nikiroo.fanfix.library;
-import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import be.nikiroo.utils.IOUtils;
-import be.nikiroo.utils.StringUtils;
import be.nikiroo.utils.streams.ReplaceInputStream;
public class Template {
valueOne.close();
}
}
+
from[i] = "${" + key + "}";
to[i] = value.toString();
i++;
}
-
+
InputStream in = IOUtils.openResource(location, name);
- //TODO: pending fix in replace stream
- String data = IOUtils.readSmallStream(in);
- in.close();
- for(i = 0 ; i < from.length;i++) {
- data=data.replace(from[i], to[i]);
- }
+ InputStream stream;
+
+ stream = IOUtils.openResource(location, name);
+ System.out.println("SOURCE = ((" + IOUtils.readSmallStream(stream) + "))");
+ stream=new ReplaceInputStream(IOUtils.openResource(location, name), from, to);
+ System.out.println("RESULT = ((" + IOUtils.readSmallStream(stream) + "))");
- //in = new ReplaceInputStream(in, from, to);
- in = new ByteArrayInputStream(StringUtils.getBytes(data));
- // END TODO
- return in;
+ return new ReplaceInputStream(in, from, to);
}
public synchronized Template set(String key, String value) {
private Response loginPage(WLoginResult login, String uri)
throws IOException {
- StringBuilder builder = new StringBuilder();
-
- builder.append(getTemplateIndexPreBanner(true));
+ 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));
- builder.append(getTemplate("index.post"));
-
- 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<String, String> cookies,
// 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());
}
margin: 10px;
}
-.error {
+.message.error {
background-color: #ffdddd;
border: 1px solid #dd8888;
- clear: left;
- border-radius: 5px;
- padding: 5px;
- margin: 10px;
}
/* all links and clickable should show a pointer cursor */
;
}
+ public Template index(boolean banner, List<Template> content) {
+ String favicon = "favicon.ico";
+ String icon = Instance.getInstance().getUiConfig()
+ .getString(UiConfig.PROGRAM_ICON);
+ if (icon != null) {
+ favicon = "icon_" + icon.replace("-", "_") + ".png";
+ }
+
+ Template index = new Template(getClass(), "index.html") //
+ .set("title", "Fanfix") //
+ .set("favicon", favicon) //
+ .set("content", content) //
+ ;
+
+ if (banner) {
+ index.set("banner", new Template(getClass(), "index.banner.html") //
+ .set("favicon", favicon) //
+ .set("version", Version.getCurrentVersion().toString()) //
+ );
+ } else {
+ index.set("banner", "");
+ }
+
+ return index;
+ }
+
+ public Template login(String url) {
+ return new Template(getClass(), "login.html") //
+ .set("url", url) //
+ ;
+ }
+
+ public Template message(String message, boolean error) {
+ return new Template(getClass(), "message.html") //
+ .set("class", error ? "message error" : "message") //
+ .set("message", message) //
+ ;
+ }
+
public Template browser(String selectedValue, String filter,
List<Template> selects) {
return new Template(getClass(), "browser.html") //
;
}
- public Template index(boolean banner, List<Template> content) {
- String favicon = "favicon.ico";
- String icon = Instance.getInstance().getUiConfig()
- .getString(UiConfig.PROGRAM_ICON);
- if (icon != null) {
- favicon = "icon_" + icon.replace("-", "_") + ".png";
- }
-
- Template index = new Template(getClass(), "index.html") //
- .set("title", "Fanfix") //
- .set("favicon", favicon) //
- .set("content", content) //
- ;
-
- if (banner) {
- index.set("banner", new Template(getClass(), "index.banner.html") //
- .set("favicon", favicon) //
- .set("version", Version.getCurrentVersion().toString()) //
- );
- } else {
- index.set("banner", "");
- }
-
- return index;
- }
-
public Template viewer(Template browser, List<Template> booklines) {
// TODO
return null;
<!DOCTYPE html>
<html>
<head>
-<!--
+ <!--
Copyright 2020 David ROULET
This file is part of fanfix.
___________________________________________________________________________
-->
- <meta http-equiv="content-type" content="text/html; charset=utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
+ <meta http-equiv='content-type' content='text/html; charset=utf-8'>
+ <meta name='viewport' content='width=device-width, initial-scale=1.0'>
<title>${title}</title>
- <link rel="stylesheet" type="text/css" href="/style.css" />
- <link rel="icon" type="image/x-icon" href="/${favicon}" />
+ <link rel='stylesheet' type='text/css' href='/style.css' />
+ <link rel='icon' type='image/x-icon' href='/${favicon}' />
</head>
<body>
<div class='main'>
--- /dev/null
+ <form method='POST' action='${url}' class='login'>
+ <p>You must be logged into the system to see the stories.</p>
+ <input type='text' name='login' />
+ <input type='password' name='password' />
+ <input type='submit' value='Login' />
+ </form>
--- /dev/null
+ <div class='${class}'>${message}</div>