Merge branch 'subtree'
[fanfix.git] / src / be / nikiroo / fanfix / library / WebLibraryUrls.java
index 2f36731871991449f63fead0265d8c5f79a9e499..5d628e023d33e47b92542ff9c4273614864c7d33 100644 (file)
@@ -1,5 +1,8 @@
 package be.nikiroo.fanfix.library;
 
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+
 class WebLibraryUrls {
        static public final String INDEX_URL = "/";
 
@@ -7,6 +10,8 @@ class WebLibraryUrls {
 
        static public final String LOGOUT_URL = "/logout";
 
+       static public final String EXIT_URL = "/exit";
+
        static private final String VIEWER_URL_BASE = "/view/story/";
        static private final String VIEWER_URL = VIEWER_URL_BASE
                        + "{luid}/{chap}/{para}";
@@ -17,11 +22,30 @@ class WebLibraryUrls {
        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 STORY_URL_METADATA = STORY_URL_BASE
+                       + "{luid}/metadata";
+
+       // GET/SET ("value" param -> set STA to this value)
+       static private final String STORY_URL_SOURCE = STORY_URL_BASE
+                       + "{luid}/source";
+       static private final String STORY_URL_TITLE = STORY_URL_BASE
+                       + "{luid}/title";
+       static private final String STORY_URL_AUTHOR = STORY_URL_BASE
+                       + "{luid}/author";
 
        static private final String LIST_URL_BASE = "/list/";
 
        static public final String LIST_URL_METADATA = LIST_URL_BASE + "metadata";
 
+       // "import" requires param "url" and return an luid, "/{luid}" return
+       // progress status as a JSON Progress or 404 if none (done or failed)
+       static private final String IMPRT_URL_BASE = "/import/";
+       static private final String IMPRT_URL_PROGRESS = IMPRT_URL_BASE + "{luid}";
+       static public final String IMPRT_URL_IMPORT = IMPRT_URL_BASE + "import";
+
+       static private final String DELETE_URL_BASE = "/delete/";
+       static private final String DELETE_URL_STORY = DELETE_URL_BASE + "{luid}";
+
        // GET/SET ("luid" param -> set cover to the cover of this story -- not ok
        // for /cover/story/)
        static private final String COVER_URL_BASE = "/cover/";
@@ -31,7 +55,7 @@ class WebLibraryUrls {
                        + "author/{author}";
        static private final String COVER_URL_SOURCE = COVER_URL_BASE
                        + "source/{source}";
-
+       
        static public String getViewUrl(String luid, Integer chap, Integer para) {
                return VIEWER_URL //
                                .replace("{luid}", luid) //
@@ -57,10 +81,41 @@ class WebLibraryUrls {
                                .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) || isCoverUrl(url);
+       static public String getStoryUrlSource(String luid) {
+               return STORY_URL_SOURCE //
+                               .replace("{luid}", luid);
+       }
+
+       static public String getStoryUrlTitle(String luid) {
+               return STORY_URL_TITLE//
+                               .replace("{luid}", luid);
+       }
+
+       static public String getStoryUrlAuthor(String luid) {
+               return STORY_URL_AUTHOR //
+                               .replace("{luid}", luid);
+       }
+
+       static public String getStoryUrlMetadata(String luid) {
+               return STORY_URL_METADATA //
+                               .replace("{luid}", luid);
+       }
+
+       static public String getImprtProgressUrl(String luid) {
+               return IMPRT_URL_PROGRESS //
+                               .replace("{luid}", luid);
+       }
+
+       static public boolean isSupportedUrl(String url,
+                       boolean requiresLoginOnly) {
+               if (requiresLoginOnly) {
+                       return INDEX_URL.equals(url) || LOGOUT_URL.equals(url)
+                                       || EXIT_URL.equals(url) || isViewUrl(url) || isStoryUrl(url)
+                                       || isListUrl(url) || isCoverUrl(url) || isImprtUrl(url)
+                                       || isDeleteUrl(url);
+               }
+
+               return isSupportedUrl(url, true) || VERSION_URL.equals(url);
        }
 
        static public String getCoverUrlStory(String luid) {
@@ -70,12 +125,17 @@ class WebLibraryUrls {
 
        static public String getCoverUrlSource(String source) {
                return COVER_URL_SOURCE //
-                               .replace("{source}", source);
+                               .replace("{source}", url(source));
        }
 
        static public String getCoverUrlAuthor(String author) {
                return COVER_URL_AUTHOR //
-                               .replace("{author}", author);
+                               .replace("{author}", url(author));
+       }
+
+       static public String getDeleteUrlStory(String luid) {
+               return DELETE_URL_STORY //
+                               .replace("{luid}", luid);
        }
 
        static public boolean isViewUrl(String url) {
@@ -93,4 +153,22 @@ class WebLibraryUrls {
        static public boolean isCoverUrl(String url) {
                return url != null && url.startsWith(COVER_URL_BASE);
        }
+
+       static public boolean isImprtUrl(String url) {
+               return url != null && url.startsWith(IMPRT_URL_BASE);
+       }
+
+       static public boolean isDeleteUrl(String url) {
+               return url != null && url.startsWith(DELETE_URL_BASE);
+       }
+       
+       static private String url(String value) {
+               try {
+                       return URLEncoder.encode(value, "UTF-8");
+               } catch (UnsupportedEncodingException e) {
+                       // UTF-8 is always supported
+                       e.printStackTrace();
+                       return value;
+               }
+       }
 }