Possibility to specify separate metadata URL
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sat, 19 Jan 2019 14:53:47 +0000 (16:53 +0200)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sat, 19 Jan 2019 14:53:47 +0000 (16:53 +0200)
.gitignore
src/main/java/eu/svjatoslav/meviz/htmlindexer/indexer/WebIndexer.java

index 4de04cd..721e35e 100755 (executable)
@@ -4,3 +4,4 @@
 /target/
 /.idea/
 /meviz.iml
+src/main/resources/rebel.xml
index 966d10f..f266797 100644 (file)
@@ -3,7 +3,6 @@ package eu.svjatoslav.meviz.htmlindexer.indexer;
 import eu.svjatoslav.meviz.htmlindexer.Constants;
 import eu.svjatoslav.meviz.htmlindexer.IndexingContext;
 import eu.svjatoslav.meviz.htmlindexer.layouts.Layout;
-import eu.svjatoslav.meviz.htmlindexer.Utils;
 import eu.svjatoslav.meviz.htmlindexer.layouts.MixedLayout;
 import eu.svjatoslav.meviz.htmlindexer.metadata.Dimension;
 import eu.svjatoslav.meviz.htmlindexer.metadata.DirectoryMetadata;
@@ -17,15 +16,42 @@ import java.io.UnsupportedEncodingException;
 import java.net.URL;
 import java.util.List;
 
+import static eu.svjatoslav.meviz.htmlindexer.Constants.METADATA_FILE_NAME;
+import static eu.svjatoslav.meviz.htmlindexer.Utils.urlEncode;
+
 public class WebIndexer extends AbstractIndexer {
 
     private static final int METADATA_LOAD_TRY_COUNT = 10;
-    private final String globalPrefix;
-    private final String jspPath;
 
-    public WebIndexer(final String globalPrefix, final String jspPath) {
-        this.globalPrefix = globalPrefix;
-        this.jspPath = jspPath;
+    /**
+     * Example: "https://www3.svjatoslav.eu/web/photos"
+     */
+    private final String contentServerRootUrl;
+
+    /**
+     * Example: "http://www3.svjatoslav.eu/web/photos"
+     */
+    private String metadataServerRootUrl;
+
+    /**
+     * Path relative to frontend server. For example, if full URL is "http://127.0.0.1:8080/photos"
+     * then relative URL would be "/photos"
+     */
+    private final String frontendServerRelativeRootUrl;
+
+    /**
+     * Example full URL: http://127.0.0.1:8080/photos/Spain
+     *
+     * @param contentServerRootUrl {@link #contentServerRootUrl}
+     * @param frontendServerRelativeRootUrl {@link #frontendServerRelativeRootUrl}
+     */
+    public WebIndexer(
+            final String contentServerRootUrl,
+            final String metadataServerRootUrl,
+            final String frontendServerRelativeRootUrl) {
+        this.contentServerRootUrl = contentServerRootUrl;
+        this.metadataServerRootUrl = metadataServerRootUrl;
+        this.frontendServerRelativeRootUrl = frontendServerRelativeRootUrl;
     }
 
     @Override
@@ -38,24 +64,30 @@ public class WebIndexer extends AbstractIndexer {
     @Override
     public String getDirectoryUrl(final AbstractFile directory,
                                   final IndexingContext context) {
-        return jspPath + context.getLocalUrl() + "/" + directory.fileName;
+        return frontendServerRelativeRootUrl + context.getLocalUrl() + "/" + directory.fileName;
     }
 
-    public String getHtml(String requestPath) throws
+    /**
+     * @param frontendServerRequestRelativeUrl example: /Spain
+     */
+    public String getHtml(String frontendServerRequestRelativeUrl) throws
             IOException, ClassNotFoundException {
 
-        if (requestPath == null)
-            requestPath = "";
+        if (frontendServerRequestRelativeUrl == null)
+            frontendServerRequestRelativeUrl = "";
 
-        if (requestPath.equals("/"))
-            requestPath = "";
+        if (frontendServerRequestRelativeUrl.equals("/"))
+            frontendServerRequestRelativeUrl = "";
 
         final MixedLayout layout = new MixedLayout();
-        final IndexingContext context = new IndexingContext(globalPrefix,
-                requestPath);
+
+        final IndexingContext context = new IndexingContext(
+                contentServerRootUrl,
+                frontendServerRequestRelativeUrl);
+
         layout.init("Photos", context.getLocalPathComponents(), this, context);
 
-        final DirectoryMetadata directory = getMetadataForPath(requestPath);
+        final DirectoryMetadata directory = getMetadataForPath(frontendServerRequestRelativeUrl);
 
         compileHtml(layout, directory);
 
@@ -65,23 +97,22 @@ public class WebIndexer extends AbstractIndexer {
     private DirectoryMetadata getMetadataForPath(final String requestPath)
             throws ClassNotFoundException, IOException {
 
-        final String urlString = globalPrefix + Utils.urlEncode(requestPath) + "/.thumbnails/metadata_6.dat";
+        final String url = metadataServerRootUrl + urlEncode(requestPath) + "/.thumbnails/"+ METADATA_FILE_NAME;
 
-        for (int i = 0; true; i++)
+        for (int attempt = 0; true; attempt++)
             try {
-                return attemptDirectoryMetadataDownload(urlString);
+                return attemptDirectoryMetadataDownload(url);
             } catch (final IOException e) {
-                if (i > METADATA_LOAD_TRY_COUNT) throw e;
+                if (attempt > METADATA_LOAD_TRY_COUNT) throw e;
             }
     }
 
-    private DirectoryMetadata attemptDirectoryMetadataDownload(String urlString) throws IOException, ClassNotFoundException {
-        final BufferedInputStream inputStream = new BufferedInputStream(new URL(
-                urlString).openStream());
+    private DirectoryMetadata attemptDirectoryMetadataDownload(String urlString)
+            throws IOException, ClassNotFoundException {
 
+        final BufferedInputStream inputStream = new BufferedInputStream(new URL(urlString).openStream());
         final ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
-        final DirectoryMetadata directory = (DirectoryMetadata) objectInputStream
-                .readObject();
+        final DirectoryMetadata directory = (DirectoryMetadata) objectInputStream.readObject();
         inputStream.close();
 
         return directory;
@@ -91,7 +122,7 @@ public class WebIndexer extends AbstractIndexer {
     public String getParentDirectoryUrl(final IndexingContext context) {
         final StringBuilder result = new StringBuilder();
 
-        result.append(jspPath);
+        result.append(frontendServerRelativeRootUrl);
 
         final List<String> components = context.getLocalPathComponents();
 
@@ -119,7 +150,7 @@ public class WebIndexer extends AbstractIndexer {
 
         return context.getGlobalUrl() + context.getLocalUrl() + "/"
                 + Constants.THUMBNAILS_DIRECTORY_NAME + "/"
-                + Utils.urlEncode(thumbnailFileName);
+                + urlEncode(thumbnailFileName);
     }
 
 }