1 package eu.svjatoslav.meviz.htmlindexer;
3 import eu.svjatoslav.meviz.htmlindexer.layouts.MixedLayout;
4 import eu.svjatoslav.meviz.htmlindexer.metadata.Dimension;
5 import eu.svjatoslav.meviz.htmlindexer.metadata.DirectoryMetadata;
6 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.AbstractFile;
7 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.Picture;
9 import java.io.BufferedInputStream;
10 import java.io.IOException;
11 import java.io.ObjectInputStream;
12 import java.io.UnsupportedEncodingException;
14 import java.net.URLEncoder;
15 import java.util.List;
17 public class WebIndexer extends AbstractIndexer {
19 private static final int METADATA_LOAD_TRY_COUNT = 10;
20 private final String globalPrefix;
21 private final String jspPath;
23 public WebIndexer(final String globalPrefix, final String jspPath) {
24 this.globalPrefix = globalPrefix;
25 this.jspPath = jspPath;
29 public void compileHtml(final Layout layout,
30 final DirectoryMetadata directory)
31 throws UnsupportedEncodingException {
32 super.compileHtml(layout, directory);
36 public String getDirectoryUrl(final AbstractFile directory,
37 final IndexingContext context) {
40 return jspPath + context.getLocalUrl() + "/"
41 + URLEncoder.encode(directory.fileName, "UTF-8");
42 } catch (final UnsupportedEncodingException e) {
43 throw new RuntimeException(e);
47 public String getHtml(String requestPath) throws
48 IOException, ClassNotFoundException {
50 if (requestPath == null)
53 if (requestPath.equals("/"))
56 final MixedLayout layout = new MixedLayout();
57 final IndexingContext context = new IndexingContext(globalPrefix,
59 layout.init("Photos", context.getLocalPathComponents(), this, context);
61 final DirectoryMetadata directory = getMetadataForPath(requestPath);
63 compileHtml(layout, directory);
65 return layout.getHtml(false, false, directory);
68 public DirectoryMetadata getMetadataForPath(final String requestPath)
69 throws ClassNotFoundException, IOException {
71 final String urlString = globalPrefix +
72 Utils.urlEncode(requestPath) +
73 "/.thumbnails/metadata_6.dat";
75 IOException exception = null;
77 for (int i = 0; i < METADATA_LOAD_TRY_COUNT; i++)
80 final BufferedInputStream in = new BufferedInputStream(new URL(
81 urlString).openStream());
83 final ObjectInputStream oin = new ObjectInputStream(in);
84 final DirectoryMetadata directory = (DirectoryMetadata) oin
89 } catch (final IOException e) {
97 public String getParentDirectoryUrl(final IndexingContext context) {
98 final StringBuilder result = new StringBuilder();
100 result.append(jspPath);
102 final List<String> components = context.getLocalPathComponents();
104 for (final String pathComponent : components.subList(0,
105 components.size() - 1)) {
107 result.append(pathComponent);
110 return result.toString();
114 public String getThumbnailUrl(final Picture picture,
115 final Dimension desiredDimension, final IndexingContext context) {
117 // in case thumbnail size was equal to original, then return original
119 if (picture.getDimensions().equals(desiredDimension))
120 return context.getGlobalUrl() + context.getLocalUrl() + "/"
123 final String thumbnailFileName = picture
124 .getRelativeThumbnailFileName(desiredDimension);
126 return context.getGlobalUrl() + context.getLocalUrl() + "/"
127 + Constants.THUMBNAILS_DIRECTORY_NAME + "/"
128 + Utils.urlEncode(thumbnailFileName);