use latest svjatoslavcommons
[meviz.git] / src / main / java / eu / svjatoslav / meviz / htmlindexer / WebIndexer.java
index 61013a4..7cf1d20 100644 (file)
@@ -3,10 +3,12 @@ package eu.svjatoslav.meviz.htmlindexer;
 import java.io.BufferedInputStream;
 import java.io.IOException;
 import java.io.ObjectInputStream;
+import java.io.UnsupportedEncodingException;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.net.URLEncoder;
+import java.util.List;
 
-import eu.svjatoslav.commons.network.UrlParamEncoder;
 import eu.svjatoslav.meviz.htmlindexer.layouts.MixedLayout;
 import eu.svjatoslav.meviz.htmlindexer.metadata.Dimension;
 import eu.svjatoslav.meviz.htmlindexer.metadata.DirectoryMetadata;
@@ -15,71 +17,120 @@ import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.Picture;
 
 public class WebIndexer extends AbstractIndexer {
 
-       private final String localPrefix;
+       private static final int METADATA_LOAD_TRY_COUNT = 10;
        private final String globalPrefix;
+       private final String jspPath;
 
-       public WebIndexer(final String globalPrefix, final String localPrefix) {
+       public WebIndexer(final String globalPrefix, final String jspPath) {
                this.globalPrefix = globalPrefix;
-               this.localPrefix = localPrefix;
+               this.jspPath = jspPath;
        }
 
        @Override
-       public String getDirectoryUrl(final AbstractFile directory) {
-               return "photos.jsp?path=/" + UrlParamEncoder.encode(directory.fileName);
+       public void compileHtml(final Layout layout,
+                       final DirectoryMetadata directory)
+                       throws UnsupportedEncodingException {
+               super.compileHtml(layout, directory);
        }
 
-       public String getHtml(final String requestPath)
-                       throws MalformedURLException, IOException, ClassNotFoundException {
+       @Override
+       public String getDirectoryUrl(final AbstractFile directory,
+                       final IndexingContext context) {
+
+               try {
+                       return jspPath + context.getLocalUrl() + "/"
+                                       + URLEncoder.encode(directory.fileName, "UTF-8");
+               } catch (final UnsupportedEncodingException e) {
+                       throw new RuntimeException(e);
+               }
+       }
+
+       public String getHtml(String requestPath) throws MalformedURLException,
+                       IOException, ClassNotFoundException {
+
+               if (requestPath == null)
+                       requestPath = "";
+
+               if (requestPath.equals("/"))
+                       requestPath = "";
 
                final MixedLayout layout = new MixedLayout();
-               layout.init("Photos", new String[] {}, this, localPrefix + requestPath);
+               final IndexingContext context = new IndexingContext(globalPrefix,
+                               requestPath);
+               layout.init("Photos", context.getLocalPathComponents(), this, context);
 
-               final DirectoryMetadata metadata = getMetadataForPath(requestPath);
+               final DirectoryMetadata directory = getMetadataForPath(requestPath);
 
-               compileHtml(layout, metadata);
+               compileHtml(layout, directory);
 
-               return layout.getHtml();
+               return layout.getHtml(false, false, directory);
        }
 
        public DirectoryMetadata getMetadataForPath(final String requestPath)
-                       throws IOException, MalformedURLException, ClassNotFoundException {
+                       throws ClassNotFoundException, IOException {
 
                final StringBuffer urlStringBuffer = new StringBuffer();
 
                urlStringBuffer.append(globalPrefix);
-               urlStringBuffer.append(UrlParamEncoder.encode(localPrefix));
-               urlStringBuffer.append(UrlParamEncoder.encode(requestPath));
-               if (!requestPath.equals("/"))
-                       urlStringBuffer.append("/");
-               urlStringBuffer.append(".thumbnails/metadata_6.dat");
+               urlStringBuffer.append(Utils.urlEncode(requestPath));
+               urlStringBuffer.append("/.thumbnails/metadata_6.dat");
 
                final String urlString = urlStringBuffer.toString();
 
-               final BufferedInputStream in = new BufferedInputStream(new URL(
-                               urlString).openStream());
+               IOException exception = null;
+
+               for (int i = 0; i < METADATA_LOAD_TRY_COUNT; i++)
+                       try {
+
+                               final BufferedInputStream in = new BufferedInputStream(new URL(
+                                               urlString).openStream());
+
+                               final ObjectInputStream oin = new ObjectInputStream(in);
+                               final DirectoryMetadata directory = (DirectoryMetadata) oin
+                                               .readObject();
+                               in.close();
+
+                               return directory;
+                       } catch (final IOException e) {
+                               exception = e;
+                       }
+
+               throw exception;
+       }
+
+       @Override
+       public String getParentDirectoryUrl(final IndexingContext context) {
+               final StringBuffer result = new StringBuffer();
+
+               result.append(jspPath);
+
+               final List<String> components = context.getLocalPathComponents();
 
-               final ObjectInputStream oin = new ObjectInputStream(in);
-               final DirectoryMetadata directory = (DirectoryMetadata) oin
-                               .readObject();
-               in.close();
+               for (final String pathComponent : components.subList(0,
+                               components.size() - 1)) {
+                       result.append("/");
+                       result.append(pathComponent);
+               }
 
-               return directory;
+               return result.toString();
        }
 
        @Override
-       public String getThumbnailPath(final Picture picture,
-                       final Dimension desiredDimension) {
+       public String getThumbnailUrl(final Picture picture,
+                       final Dimension desiredDimension, final IndexingContext context) {
 
                // in case thumbnail size was equal to original, then return original
                // file path
                if (picture.getDimensions().equals(desiredDimension))
-                       return localPrefix + picture.fileName;
+                       return context.getGlobalUrl() + context.getLocalUrl() + "/"
+                                       + picture.fileName;
 
                final String thumbnailFileName = picture
                                .getRelativeThumbnailFileName(desiredDimension);
 
-               return localPrefix + "/" + Constants.THUMBNAILS_DIRECTORY_NAME + "/"
-                               + thumbnailFileName;
+               return context.getGlobalUrl() + context.getLocalUrl() + "/"
+                               + Constants.THUMBNAILS_DIRECTORY_NAME + "/"
+                               + Utils.urlEncode(thumbnailFileName);
        }
 
 }