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.equals("/"))
41 final MixedLayout layout = new MixedLayout();
42 final IndexingContext context = new IndexingContext(globalPrefix,
44 layout.init("Photos", context.getLocalPathComponents(), this, context);
46 final DirectoryMetadata metadata = getMetadataForPath(requestPath);
48 compileHtml(layout, metadata);
50 return layout.getHtml(false, false);
53 public DirectoryMetadata getMetadataForPath(final String requestPath)
54 throws ClassNotFoundException, IOException {
56 final StringBuffer urlStringBuffer = new StringBuffer();
58 urlStringBuffer.append(globalPrefix);
59 urlStringBuffer.append(UrlParamEncoder.encode(requestPath));
60 urlStringBuffer.append("/.thumbnails/metadata_6.dat");
62 final String urlString = urlStringBuffer.toString();
64 IOException exception = null;
66 for (int i = 0; i < METADATA_LOAD_TRY_COUNT; i++)
69 final BufferedInputStream in = new BufferedInputStream(new URL(
70 urlString).openStream());
72 final ObjectInputStream oin = new ObjectInputStream(in);
73 final DirectoryMetadata directory = (DirectoryMetadata) oin
78 } catch (final IOException e) {
86 public String getParentDirectoryUrl(final IndexingContext context) {
87 final StringBuffer result = new StringBuffer();
89 result.append(jspPath);
91 final List<String> components = context.getLocalPathComponents();
93 for (final String pathComponent : components.subList(0,
94 components.size() - 1)) {
96 result.append(pathComponent);
99 return result.toString();
103 public String getThumbnailUrl(final Picture picture,
104 final Dimension desiredDimension, final IndexingContext context) {
106 // in case thumbnail size was equal to original, then return original
108 if (picture.getDimensions().equals(desiredDimension))
109 return context.getGlobalUrl() + context.getLocalUrl() + "/"
112 final String thumbnailFileName = picture
113 .getRelativeThumbnailFileName(desiredDimension);
115 return context.getGlobalUrl() + context.getLocalUrl() + "/"
116 + Constants.THUMBNAILS_DIRECTORY_NAME + "/"
117 + UrlParamEncoder.encode(thumbnailFileName);