1 package eu.svjatoslav.meviz.htmlindexer.indexer;
3 import eu.svjatoslav.meviz.htmlindexer.Constants;
4 import eu.svjatoslav.meviz.htmlindexer.IndexingContext;
5 import eu.svjatoslav.meviz.htmlindexer.layouts.Layout;
6 import eu.svjatoslav.meviz.htmlindexer.Utils;
7 import eu.svjatoslav.meviz.htmlindexer.layouts.MixedLayout;
8 import eu.svjatoslav.meviz.htmlindexer.metadata.Dimension;
9 import eu.svjatoslav.meviz.htmlindexer.metadata.DirectoryMetadata;
10 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.AbstractFile;
11 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.Picture;
13 import java.io.BufferedInputStream;
14 import java.io.IOException;
15 import java.io.ObjectInputStream;
16 import java.io.UnsupportedEncodingException;
18 import java.util.List;
20 public class WebIndexer extends AbstractIndexer {
22 private static final int METADATA_LOAD_TRY_COUNT = 10;
23 private final String globalPrefix;
24 private final String jspPath;
26 public WebIndexer(final String globalPrefix, final String jspPath) {
27 this.globalPrefix = globalPrefix;
28 this.jspPath = jspPath;
32 public void compileHtml(final Layout layout,
33 final DirectoryMetadata directory)
34 throws UnsupportedEncodingException {
35 super.compileHtml(layout, directory);
39 public String getDirectoryUrl(final AbstractFile directory,
40 final IndexingContext context) {
41 return jspPath + context.getLocalUrl() + "/" + directory.fileName;
44 public String getHtml(String requestPath) throws
45 IOException, ClassNotFoundException {
47 if (requestPath == null)
50 if (requestPath.equals("/"))
53 final MixedLayout layout = new MixedLayout();
54 final IndexingContext context = new IndexingContext(globalPrefix,
56 layout.init("Photos", context.getLocalPathComponents(), this, context);
58 final DirectoryMetadata directory = getMetadataForPath(requestPath);
60 compileHtml(layout, directory);
62 return layout.getHtml(false, false, directory);
65 private DirectoryMetadata getMetadataForPath(final String requestPath)
66 throws ClassNotFoundException, IOException {
68 final String urlString = globalPrefix + Utils.urlEncode(requestPath) + "/.thumbnails/metadata_6.dat";
70 for (int i = 0; true; i++)
72 return attemptDirectoryMetadataDownload(urlString);
73 } catch (final IOException e) {
74 if (i > METADATA_LOAD_TRY_COUNT) throw e;
78 private DirectoryMetadata attemptDirectoryMetadataDownload(String urlString) throws IOException, ClassNotFoundException {
79 final BufferedInputStream inputStream = new BufferedInputStream(new URL(
80 urlString).openStream());
82 final ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
83 final DirectoryMetadata directory = (DirectoryMetadata) objectInputStream
91 public String getParentDirectoryUrl(final IndexingContext context) {
92 final StringBuilder result = new StringBuilder();
94 result.append(jspPath);
96 final List<String> components = context.getLocalPathComponents();
98 for (final String pathComponent : components.subList(0,
99 components.size() - 1)) {
101 result.append(pathComponent);
104 return result.toString();
108 public String getThumbnailUrl(final Picture picture,
109 final Dimension desiredDimension, final IndexingContext context) {
111 // in case thumbnail size was equal to original, then return original
113 if (picture.getDimensions().equals(desiredDimension))
114 return context.getGlobalUrl() + context.getLocalUrl() + "/"
117 final String thumbnailFileName = picture
118 .getRelativeThumbnailFileName(desiredDimension);
120 return context.getGlobalUrl() + context.getLocalUrl() + "/"
121 + Constants.THUMBNAILS_DIRECTORY_NAME + "/"
122 + Utils.urlEncode(thumbnailFileName);