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.util.List;
16 public class WebIndexer extends AbstractIndexer {
18 private static final int METADATA_LOAD_TRY_COUNT = 10;
19 private final String globalPrefix;
20 private final String jspPath;
22 public WebIndexer(final String globalPrefix, final String jspPath) {
23 this.globalPrefix = globalPrefix;
24 this.jspPath = jspPath;
28 public void compileHtml(final Layout layout,
29 final DirectoryMetadata directory)
30 throws UnsupportedEncodingException {
31 super.compileHtml(layout, directory);
35 public String getDirectoryUrl(final AbstractFile directory,
36 final IndexingContext context) {
37 return jspPath + context.getLocalUrl() + "/" + directory.fileName;
40 public String getHtml(String requestPath) throws
41 IOException, ClassNotFoundException {
43 if (requestPath == null)
46 if (requestPath.equals("/"))
49 final MixedLayout layout = new MixedLayout();
50 final IndexingContext context = new IndexingContext(globalPrefix,
52 layout.init("Photos", context.getLocalPathComponents(), this, context);
54 final DirectoryMetadata directory = getMetadataForPath(requestPath);
56 compileHtml(layout, directory);
58 return layout.getHtml(false, false, directory);
61 private DirectoryMetadata getMetadataForPath(final String requestPath)
62 throws ClassNotFoundException, IOException {
64 final String urlString = globalPrefix + Utils.urlEncode(requestPath) + "/.thumbnails/metadata_6.dat";
66 for (int i = 0; true; i++)
68 return attemptDirectoryMetadataDownload(urlString);
69 } catch (final IOException e) {
70 if (i > METADATA_LOAD_TRY_COUNT) throw e;
74 private DirectoryMetadata attemptDirectoryMetadataDownload(String urlString) throws IOException, ClassNotFoundException {
75 final BufferedInputStream inputStream = new BufferedInputStream(new URL(
76 urlString).openStream());
78 final ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
79 final DirectoryMetadata directory = (DirectoryMetadata) objectInputStream
87 public String getParentDirectoryUrl(final IndexingContext context) {
88 final StringBuilder result = new StringBuilder();
90 result.append(jspPath);
92 final List<String> components = context.getLocalPathComponents();
94 for (final String pathComponent : components.subList(0,
95 components.size() - 1)) {
97 result.append(pathComponent);
100 return result.toString();
104 public String getThumbnailUrl(final Picture picture,
105 final Dimension desiredDimension, final IndexingContext context) {
107 // in case thumbnail size was equal to original, then return original
109 if (picture.getDimensions().equals(desiredDimension))
110 return context.getGlobalUrl() + context.getLocalUrl() + "/"
113 final String thumbnailFileName = picture
114 .getRelativeThumbnailFileName(desiredDimension);
116 return context.getGlobalUrl() + context.getLocalUrl() + "/"
117 + Constants.THUMBNAILS_DIRECTORY_NAME + "/"
118 + Utils.urlEncode(thumbnailFileName);