weblib: urls -> class
authorNiki Roo <niki@nikiroo.be>
Wed, 13 May 2020 20:51:39 +0000 (22:51 +0200)
committerNiki Roo <niki@nikiroo.be>
Wed, 13 May 2020 20:51:39 +0000 (22:51 +0200)
src/be/nikiroo/fanfix/library/BasicLibrary.java
src/be/nikiroo/fanfix/library/RemoteLibrary.java
src/be/nikiroo/fanfix/library/WebLibrary.java
src/be/nikiroo/fanfix/library/WebLibraryServer.java
src/be/nikiroo/fanfix/library/WebLibraryUrls.java [new file with mode: 0644]

index 78f0f6208d098dcaf410e85c631dc3808330a2bd..11ca03f48a7bdc912766968f2ac96168a5f2f38c 100644 (file)
@@ -41,8 +41,6 @@ abstract public class BasicLibrary {
                READ_WRITE,
                /** The library is ready, but read-only. */
                READ_ONLY,
-               /** The library is invalid (not correctly set up). */
-               INVALID,
                /** You are not allowed to access this library. */
                UNAUTHORIZED,
                /** The library is currently out of commission. */
index 83bb2124ecaaf9eb1bf66d92366670609c493610..3a60e02c9ed6fc845193574f897c3bd9b0ecc60a 100644 (file)
@@ -141,8 +141,7 @@ public class RemoteLibrary extends BasicLibrary {
        private Status getStatusDo() {
                final Status[] result = new Status[1];
 
-               result[0] = Status.INVALID;
-
+               result[0] = null;
                try {
                        new RemoteConnectAction() {
                                @Override
index 7f775830edf8c7641eeb3303abdddf3fcdb48fbd..1178e40c578b3bb2f2df46b3994de2b0770cc1ed 100644 (file)
@@ -22,6 +22,7 @@ import be.nikiroo.fanfix.data.Story;
 import be.nikiroo.utils.IOUtils;
 import be.nikiroo.utils.Image;
 import be.nikiroo.utils.Progress;
+import be.nikiroo.utils.Version;
 
 /**
  * This {@link BasicLibrary} will access a remote server to list the available
@@ -107,16 +108,30 @@ public class WebLibrary extends BasicLibrary {
                this.rw = false;
        }
 
+       public Version getVersion() {
+               try {
+                       InputStream in = download(WebLibraryUrls.VERSION_URL);
+                       try {
+                               return new Version(IOUtils.readSmallStream(in));
+                       } finally {
+                               in.close();
+                       }
+               } catch (IOException e) {
+               }
+
+               return null;
+       }
+
        @Override
        public Status getStatus() {
                try {
-                       download("/");
+                       download(WebLibraryUrls.INDEX_URL).close();
                } catch (IOException e) {
                        try {
-                               download("/style.css");
+                               download(WebLibraryUrls.VERSION_URL).close();
                                return Status.UNAUTHORIZED;
                        } catch (IOException ioe) {
-                               return Status.INVALID;
+                               return Status.UNAVAILABLE;
                        }
                }
 
@@ -130,12 +145,12 @@ public class WebLibrary extends BasicLibrary {
 
        @Override
        public Image getCover(String luid) throws IOException {
-               InputStream in = download("/story/" + luid + "/cover");
-               if (in != null) {
+               InputStream in = download(WebLibraryUrls.getStoryUrlCover(luid));
+               try {
                        return new Image(in);
+               } finally {
+                       in.close();
                }
-
-               return null;
        }
 
        @Override
@@ -169,7 +184,7 @@ public class WebLibrary extends BasicLibrary {
                // TODO: pg
 
                Story story;
-               InputStream in = download("/story/" + luid + "/json");
+               InputStream in = download(WebLibraryUrls.getStoryUrlJson(luid));
                try {
                        JSONObject json = new JSONObject(IOUtils.readSmallStream(in));
                        story = JsonIO.toStory(json);
@@ -184,7 +199,7 @@ public class WebLibrary extends BasicLibrary {
                        for (Paragraph para : chap) {
                                if (para.getType() == ParagraphType.IMAGE) {
                                        InputStream subin = download(
-                                                       "/story/" + luid + "/" + chapNum + "/" + number);
+                                                       WebLibraryUrls.getStoryUrl(luid, chapNum, number));
                                        try {
                                                para.setContentImage(new Image(subin));
                                        } finally {
@@ -204,7 +219,7 @@ public class WebLibrary extends BasicLibrary {
        @Override
        protected List<MetaData> getMetas(Progress pg) throws IOException {
                List<MetaData> metas = new ArrayList<MetaData>();
-               InputStream in = download("/list/luids");
+               InputStream in = download(WebLibraryUrls.LIST_URL_METADATA);
                JSONArray jsonArr = new JSONArray(IOUtils.readSmallStream(in));
                for (int i = 0; i < jsonArr.length(); i++) {
                        JSONObject json = jsonArr.getJSONObject(i);
@@ -276,7 +291,7 @@ public class WebLibrary extends BasicLibrary {
                                "Operation not supportorted on remote Libraries");
        }
 
-       // starts with "/"
+       // starts with "/", never NULL
        private InputStream download(String path) throws IOException {
                URL url = new URL(host + ":" + port + path);
 
index b01758e7f59a8696902ec8337bba545c82fe08af..b6d11f9baa2f1bf91c6743ba4d5e06da5c558639 100644 (file)
@@ -40,13 +40,6 @@ import be.nikiroo.utils.TraceHandler;
 import be.nikiroo.utils.Version;
 
 public class WebLibraryServer implements Runnable {
-       static private String VIEWER_URL_BASE = "/view/story/";
-       static private String VIEWER_URL = VIEWER_URL_BASE + "{luid}/{chap}/{para}";
-       static private String STORY_URL_BASE = "/story/";
-       static private String STORY_URL = STORY_URL_BASE + "{luid}/{chap}/{para}";
-       static private String STORY_URL_COVER = STORY_URL_BASE + "{luid}/cover";
-       static private String LIST_URL = "/list/";
-
        private class WLoginResult extends LoginResult {
                private boolean rw;
                private boolean wl;
@@ -201,27 +194,39 @@ public class WebLibraryServer implements Runnable {
                                }
 
                                Response rep = null;
-                               if (!login.isSuccess() && (uri.equals("/") //
-                                               || uri.startsWith(STORY_URL_BASE) //
-                                               || uri.startsWith(VIEWER_URL_BASE) //
-                                               || uri.startsWith(LIST_URL))) {
+                               if (!login.isSuccess() && WebLibraryUrls.isSupportedUrl(uri)) {
                                        rep = loginPage(login, uri);
                                }
 
                                if (rep == null) {
                                        try {
-                                               if (uri.equals("/")) {
-                                                       rep = root(session, cookies, login);
-                                               } else if (uri.startsWith(LIST_URL)) {
-                                                       rep = getList(uri, login);
-                                               } else if (uri.startsWith(STORY_URL_BASE)) {
-                                                       rep = getStoryPart(uri, login);
-                                               } else if (uri.startsWith(VIEWER_URL_BASE)) {
-                                                       rep = getViewer(cookies, uri, login);
-                                               } else if (uri.equals("/logout")) {
-                                                       session.getCookies().delete("cookie");
-                                                       cookies.remove("cookie");
-                                                       rep = loginPage(login, uri);
+                                               if (WebLibraryUrls.isSupportedUrl(uri)) {
+                                                       if (WebLibraryUrls.INDEX_URL.equals(uri)) {
+                                                               rep = root(session, cookies, login);
+                                                       } else if (WebLibraryUrls.VERSION_URL.equals(uri)) {
+                                                               rep = newFixedLengthResponse(Status.OK,
+                                                                               MIME_PLAINTEXT,
+                                                                               Version.getCurrentVersion().toString());
+                                                       } else if (WebLibraryUrls.isListUrl(uri)) {
+                                                               rep = getList(uri, login);
+                                                       } else if (WebLibraryUrls.isStoryUrl(uri)) {
+                                                               rep = getStoryPart(uri, login);
+                                                       } else if (WebLibraryUrls.isViewUrl(uri)) {
+                                                               rep = getViewer(cookies, uri, login);
+                                                       } else if (WebLibraryUrls.LOGOUT_URL.equals(uri)) {
+                                                               session.getCookies().delete("cookie");
+                                                               cookies.remove("cookie");
+                                                               rep = loginPage(new WLoginResult(false, false),
+                                                                               uri);
+                                                       } else {
+                                                               getTraceHandler().error(
+                                                                               "Supported URL was not processed: "
+                                                                                               + uri);
+                                                               rep = newFixedLengthResponse(
+                                                                               Status.INTERNAL_ERROR,
+                                                                               NanoHTTPD.MIME_PLAINTEXT,
+                                                                               "An error happened");
+                                                       }
                                                } else {
                                                        if (uri.startsWith("/"))
                                                                uri = uri.substring(1);
@@ -238,14 +243,13 @@ public class WebLibraryServer implements Runnable {
                                                                }
                                                                rep = newChunkedResponse(Status.OK, mimeType,
                                                                                in);
-                                                       } else {
-                                                               getTraceHandler().trace("404: " + uri);
                                                        }
-                                               }
 
-                                               if (rep == null) {
-                                                       rep = newFixedLengthResponse(Status.NOT_FOUND,
-                                                                       NanoHTTPD.MIME_PLAINTEXT, "Not Found");
+                                                       if (rep == null) {
+                                                               getTraceHandler().trace("404: " + uri);
+                                                               rep = newFixedLengthResponse(Status.NOT_FOUND,
+                                                                               NanoHTTPD.MIME_PLAINTEXT, "Not Found");
+                                                       }
                                                }
                                        } catch (Exception e) {
                                                Instance.getInstance().getTraceHandler().error(
@@ -374,8 +378,8 @@ public class WebLibraryServer implements Runnable {
                        builder.append("<div class='error'>Your session timed out</div>");
                }
 
-               if (uri.equals("/logout")) {
-                       uri = "/";
+               if (WebLibraryUrls.LOGOUT_URL.equals(uri)) {
+                       uri = WebLibraryUrls.INDEX_URL;
                }
 
                builder.append(
@@ -395,7 +399,7 @@ public class WebLibraryServer implements Runnable {
 
        protected Response getList(String uri, WLoginResult login)
                        throws IOException {
-               if (uri.equals("/list/luids")) {
+               if (WebLibraryUrls.LIST_URL_METADATA.equals(uri)) {
                        List<JSONObject> jsons = new ArrayList<JSONObject>();
                        for (MetaData meta : metas(login)) {
                                jsons.add(JsonIO.toJson(meta));
@@ -551,7 +555,8 @@ public class WebLibraryServer implements Runnable {
 
                        builder.append("<div class='book_line'>");
                        builder.append("<a href='");
-                       builder.append(getViewUrl(meta.getLuid(), null, null));
+                       builder.append(
+                                       WebLibraryUrls.getViewUrl(meta.getLuid(), null, null));
                        builder.append("'");
                        builder.append(" class='link'>");
 
@@ -775,12 +780,14 @@ public class WebLibraryServer implements Runnable {
                        String disabledZoomHeight = "";
 
                        if (paragraph <= 0) {
-                               first = getViewUrl(luid, 0, null);
-                               previous = getViewUrl(luid, (Math.max(chapter - 1, 0)), null);
-                               next = getViewUrl(luid,
+                               first = WebLibraryUrls.getViewUrl(luid, 0, null);
+                               previous = WebLibraryUrls.getViewUrl(luid,
+                                               (Math.max(chapter - 1, 0)), null);
+                               next = WebLibraryUrls.getViewUrl(luid,
                                                (Math.min(chapter + 1, story.getChapters().size())),
                                                null);
-                               last = getViewUrl(luid, story.getChapters().size(), null);
+                               last = WebLibraryUrls.getViewUrl(luid,
+                                               story.getChapters().size(), null);
 
                                StringBuilder desc = new StringBuilder();
 
@@ -817,12 +824,13 @@ public class WebLibraryServer implements Runnable {
                                if (chapter >= story.getChapters().size())
                                        disabledRight = " disabled='disbaled'";
                        } else {
-                               first = getViewUrl(luid, chapter, 1);
-                               previous = getViewUrl(luid, chapter,
+                               first = WebLibraryUrls.getViewUrl(luid, chapter, 1);
+                               previous = WebLibraryUrls.getViewUrl(luid, chapter,
                                                (Math.max(paragraph - 1, 1)));
-                               next = getViewUrl(luid, chapter,
+                               next = WebLibraryUrls.getViewUrl(luid, chapter,
                                                (Math.min(paragraph + 1, chap.getParagraphs().size())));
-                               last = getViewUrl(luid, chapter, chap.getParagraphs().size());
+                               last = WebLibraryUrls.getViewUrl(luid, chapter,
+                                               chap.getParagraphs().size());
 
                                if (paragraph <= 1)
                                        disabledLeft = " disabled='disbaled'";
@@ -832,7 +840,8 @@ public class WebLibraryServer implements Runnable {
                                // First -> previous *chapter*
                                if (chapter > 0)
                                        disabledLeft = "";
-                               first = getViewUrl(luid, (Math.max(chapter - 1, 0)), null);
+                               first = WebLibraryUrls.getViewUrl(luid,
+                                               (Math.max(chapter - 1, 0)), null);
                                if (paragraph <= 1) {
                                        previous = first;
                                }
@@ -873,7 +882,8 @@ public class WebLibraryServer implements Runnable {
                                                        javascript, //
                                                        next, //
                                                        zoomStyle, //
-                                                       getStoryUrl(luid, chapter, paragraph)));
+                                                       WebLibraryUrls.getStoryUrl(luid, chapter,
+                                                                       paragraph)));
                                } else {
                                        content.append(String.format("" //
                                                        + "<div class='viewer text'>%s</div>", //
@@ -895,11 +905,12 @@ public class WebLibraryServer implements Runnable {
 
                        // List of chap/para links
 
-                       appendItemA(builder, 3, getViewUrl(luid, 0, null), "Description",
-                                       paragraph == 0 && chapter == 0);
+                       appendItemA(builder, 3, WebLibraryUrls.getViewUrl(luid, 0, null),
+                                       "Description", paragraph == 0 && chapter == 0);
                        if (paragraph > 0) {
                                for (int i = 1; i <= chap.getParagraphs().size(); i++) {
-                                       appendItemA(builder, 3, getViewUrl(luid, chapter, i),
+                                       appendItemA(builder, 3,
+                                                       WebLibraryUrls.getViewUrl(luid, chapter, i),
                                                        "Image " + i, paragraph == i);
                                }
                        } else {
@@ -910,7 +921,8 @@ public class WebLibraryServer implements Runnable {
                                                chapName += ": " + c.getName();
                                        }
 
-                                       appendItemA(builder, 3, getViewUrl(luid, i, null), chapName,
+                                       appendItemA(builder, 3,
+                                                       WebLibraryUrls.getViewUrl(luid, i, null), chapName,
                                                        chapter == i);
 
                                        i++;
@@ -987,26 +999,6 @@ public class WebLibraryServer implements Runnable {
                return "";
        }
 
-       private String getViewUrl(String luid, Integer chap, Integer para) {
-               return VIEWER_URL //
-                               .replace("{luid}", luid) //
-                               .replace("/{chap}", chap == null ? "" : "/" + chap) //
-                               .replace("/{para}",
-                                               (chap == null || para == null) ? "" : "/" + para);
-       }
-
-       private String getStoryUrl(String luid, int chap, Integer para) {
-               return STORY_URL //
-                               .replace("{luid}", luid) //
-                               .replace("{chap}", Integer.toString(chap)) //
-                               .replace("{para}", para == null ? "" : Integer.toString(para));
-       }
-
-       private String getStoryUrlCover(String luid) {
-               return STORY_URL_COVER //
-                               .replace("{luid}", luid);
-       }
-
        private boolean isAllowed(MetaData meta, WLoginResult login) {
                if (login.isWl() && !whitelist.isEmpty()
                                && !whitelist.contains(meta.getSource())) {
diff --git a/src/be/nikiroo/fanfix/library/WebLibraryUrls.java b/src/be/nikiroo/fanfix/library/WebLibraryUrls.java
new file mode 100644 (file)
index 0000000..6a3b153
--- /dev/null
@@ -0,0 +1,67 @@
+package be.nikiroo.fanfix.library;
+
+public class WebLibraryUrls {
+       static public final String INDEX_URL = "/";
+
+       static public final String VERSION_URL = "/version";
+
+       static public final String LOGOUT_URL = "/logout";
+
+       static private final String VIEWER_URL_BASE = "/view/story/";
+       static private final String VIEWER_URL = VIEWER_URL_BASE
+                       + "{luid}/{chap}/{para}";
+
+       static private final String STORY_URL_BASE = "/story/";
+       static private final String STORY_URL = STORY_URL_BASE
+                       + "{luid}/{chap}/{para}";
+       static private final String STORY_URL_COVER = STORY_URL_BASE
+                       + "{luid}/cover";
+       static private final String STORY_URL_JSON = STORY_URL_BASE + "{luid}/json";
+
+       static private final String LIST_URL_BASE = "/list/";
+
+       static public final String LIST_URL_METADATA = LIST_URL_BASE + "luids";
+
+       static public String getViewUrl(String luid, Integer chap, Integer para) {
+               return VIEWER_URL //
+                               .replace("{luid}", luid) //
+                               .replace("/{chap}", chap == null ? "" : "/" + chap) //
+                               .replace("/{para}",
+                                               (chap == null || para == null) ? "" : "/" + para);
+       }
+
+       static public String getStoryUrl(String luid, int chap, Integer para) {
+               return STORY_URL //
+                               .replace("{luid}", luid) //
+                               .replace("{chap}", Integer.toString(chap)) //
+                               .replace("{para}", para == null ? "" : Integer.toString(para));
+       }
+
+       static public String getStoryUrlCover(String luid) {
+               return STORY_URL_COVER //
+                               .replace("{luid}", luid);
+       }
+
+       static public String getStoryUrlJson(String luid) {
+               return STORY_URL_JSON //
+                               .replace("{luid}", luid);
+       }
+
+       static public boolean isSupportedUrl(String url) {
+               return INDEX_URL.equals(url) || VERSION_URL.equals(url)
+                               || LOGOUT_URL.equals(url) || isViewUrl(url) || isStoryUrl(url)
+                               || isListUrl(url);
+       }
+
+       static public boolean isViewUrl(String url) {
+               return url != null && url.startsWith(VIEWER_URL_BASE);
+       }
+
+       static public boolean isStoryUrl(String url) {
+               return url != null && url.startsWith(STORY_URL_BASE);
+       }
+
+       static public boolean isListUrl(String url) {
+               return url != null && url.startsWith(LIST_URL_BASE);
+       }
+}