1 package eu.svjatoslav.meviz.htmlindexer;
3 import java.io.BufferedInputStream;
4 import java.io.IOException;
5 import java.io.ObjectInputStream;
6 import java.net.MalformedURLException;
10 import eu.svjatoslav.commons.network.UrlParamEncoder;
11 import eu.svjatoslav.meviz.htmlindexer.layouts.MixedLayout;
12 import eu.svjatoslav.meviz.htmlindexer.metadata.Dimension;
13 import eu.svjatoslav.meviz.htmlindexer.metadata.DirectoryMetadata;
14 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.AbstractFile;
15 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.Picture;
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 String getDirectoryUrl(final AbstractFile directory,
30 final IndexingContext context) {
31 return jspPath + context.getLocalUrl() + "/"
32 + UrlParamEncoder.encode(directory.fileName);
35 public String getHtml(String requestPath) throws MalformedURLException,
36 IOException, ClassNotFoundException {
38 if (requestPath == null)
41 if (requestPath.equals("/"))
44 final MixedLayout layout = new MixedLayout();
45 final IndexingContext context = new IndexingContext(globalPrefix,
47 layout.init("Photos", context.getLocalPathComponents(), this, context);
49 final DirectoryMetadata directory = getMetadataForPath(requestPath);
51 compileHtml(layout, directory);
53 return layout.getHtml(false, false, directory);
56 public DirectoryMetadata getMetadataForPath(final String requestPath)
57 throws ClassNotFoundException, IOException {
59 final StringBuffer urlStringBuffer = new StringBuffer();
61 urlStringBuffer.append(globalPrefix);
62 urlStringBuffer.append(UrlParamEncoder.encode(requestPath));
63 urlStringBuffer.append("/.thumbnails/metadata_6.dat");
65 final String urlString = urlStringBuffer.toString();
67 IOException exception = null;
69 for (int i = 0; i < METADATA_LOAD_TRY_COUNT; i++)
72 final BufferedInputStream in = new BufferedInputStream(new URL(
73 urlString).openStream());
75 final ObjectInputStream oin = new ObjectInputStream(in);
76 final DirectoryMetadata directory = (DirectoryMetadata) oin
81 } catch (final IOException e) {
89 public String getParentDirectoryUrl(final IndexingContext context) {
90 final StringBuffer result = new StringBuffer();
92 result.append(jspPath);
94 final List<String> components = context.getLocalPathComponents();
96 for (final String pathComponent : components.subList(0,
97 components.size() - 1)) {
99 result.append(pathComponent);
102 return result.toString();
106 public String getThumbnailUrl(final Picture picture,
107 final Dimension desiredDimension, final IndexingContext context) {
109 // in case thumbnail size was equal to original, then return original
111 if (picture.getDimensions().equals(desiredDimension))
112 return context.getGlobalUrl() + context.getLocalUrl() + "/"
115 final String thumbnailFileName = picture
116 .getRelativeThumbnailFileName(desiredDimension);
118 return context.getGlobalUrl() + context.getLocalUrl() + "/"
119 + Constants.THUMBNAILS_DIRECTORY_NAME + "/"
120 + UrlParamEncoder.encode(thumbnailFileName);