use more template, use replace input stream
authorNiki Roo <niki@nikiroo.be>
Wed, 20 May 2020 11:33:25 +0000 (13:33 +0200)
committerNiki Roo <niki@nikiroo.be>
Wed, 20 May 2020 11:33:25 +0000 (13:33 +0200)
src/be/nikiroo/fanfix/library/Template.java
src/be/nikiroo/fanfix/library/WebLibraryServerHtml.java
src/be/nikiroo/fanfix/library/web/style.css
src/be/nikiroo/fanfix/library/web/templates/WebLibraryServerTemplates.java
src/be/nikiroo/fanfix/library/web/templates/index.html
src/be/nikiroo/fanfix/library/web/templates/login.html [new file with mode: 0644]
src/be/nikiroo/fanfix/library/web/templates/message.html [new file with mode: 0644]

index e247e27962e6fe6d94d0a913a3a2afcadaa5c0dd..2dfbacfeb1092780f81686762eb6bf10bb6ee37f 100644 (file)
@@ -1,6 +1,5 @@
 package be.nikiroo.fanfix.library;
 
-import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.HashMap;
@@ -8,7 +7,6 @@ import java.util.List;
 import java.util.Map;
 
 import be.nikiroo.utils.IOUtils;
-import be.nikiroo.utils.StringUtils;
 import be.nikiroo.utils.streams.ReplaceInputStream;
 
 public class Template {
@@ -60,26 +58,24 @@ 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) {
index f3432c38cbdbc093941b2932e3817ea227754038..42ebead137c05e8189fec17a9d57d4da91e4858a 100644 (file)
@@ -316,35 +316,18 @@ abstract class WebLibraryServerHtml implements Runnable {
 
        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,
@@ -485,7 +468,7 @@ abstract class WebLibraryServerHtml implements Runnable {
 
                // 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());
        }
index b12a24fa6ea0c3018eb925200baa066d475aee75..bb91002920e94bd593d125ff8abe26c714f4c5db 100644 (file)
@@ -47,13 +47,9 @@ html, body, .main {
        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 */
index ba4deb83f290739407822fc4933802633fb5bf0e..31abe69919acaf217e85c53c754d7a7d0b54a626 100644 (file)
@@ -34,6 +34,45 @@ public class WebLibraryServerTemplates {
                ;
        }
 
+       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") //
@@ -71,32 +110,6 @@ public class WebLibraryServerTemplates {
                ;
        }
 
-       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;
index 29b8d2c754d6a71dc3d75d13bd43249d580a505d..6f3934e285f2d092d5054e6b013078b4ab6f41a0 100644 (file)
@@ -1,7 +1,7 @@
 <!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'>
diff --git a/src/be/nikiroo/fanfix/library/web/templates/login.html b/src/be/nikiroo/fanfix/library/web/templates/login.html
new file mode 100644 (file)
index 0000000..0019144
--- /dev/null
@@ -0,0 +1,6 @@
+               <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>
diff --git a/src/be/nikiroo/fanfix/library/web/templates/message.html b/src/be/nikiroo/fanfix/library/web/templates/message.html
new file mode 100644 (file)
index 0000000..a09fc4d
--- /dev/null
@@ -0,0 +1 @@
+               <div class='${class}'>${message}</div>