61013a42fb475d856613bfbda867a99c02418aa7
[meviz.git] / src / main / java / eu / svjatoslav / meviz / htmlindexer / WebIndexer.java
1 package eu.svjatoslav.meviz.htmlindexer;
2
3 import java.io.BufferedInputStream;
4 import java.io.IOException;
5 import java.io.ObjectInputStream;
6 import java.net.MalformedURLException;
7 import java.net.URL;
8
9 import eu.svjatoslav.commons.network.UrlParamEncoder;
10 import eu.svjatoslav.meviz.htmlindexer.layouts.MixedLayout;
11 import eu.svjatoslav.meviz.htmlindexer.metadata.Dimension;
12 import eu.svjatoslav.meviz.htmlindexer.metadata.DirectoryMetadata;
13 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.AbstractFile;
14 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.Picture;
15
16 public class WebIndexer extends AbstractIndexer {
17
18         private final String localPrefix;
19         private final String globalPrefix;
20
21         public WebIndexer(final String globalPrefix, final String localPrefix) {
22                 this.globalPrefix = globalPrefix;
23                 this.localPrefix = localPrefix;
24         }
25
26         @Override
27         public String getDirectoryUrl(final AbstractFile directory) {
28                 return "photos.jsp?path=/" + UrlParamEncoder.encode(directory.fileName);
29         }
30
31         public String getHtml(final String requestPath)
32                         throws MalformedURLException, IOException, ClassNotFoundException {
33
34                 final MixedLayout layout = new MixedLayout();
35                 layout.init("Photos", new String[] {}, this, localPrefix + requestPath);
36
37                 final DirectoryMetadata metadata = getMetadataForPath(requestPath);
38
39                 compileHtml(layout, metadata);
40
41                 return layout.getHtml();
42         }
43
44         public DirectoryMetadata getMetadataForPath(final String requestPath)
45                         throws IOException, MalformedURLException, ClassNotFoundException {
46
47                 final StringBuffer urlStringBuffer = new StringBuffer();
48
49                 urlStringBuffer.append(globalPrefix);
50                 urlStringBuffer.append(UrlParamEncoder.encode(localPrefix));
51                 urlStringBuffer.append(UrlParamEncoder.encode(requestPath));
52                 if (!requestPath.equals("/"))
53                         urlStringBuffer.append("/");
54                 urlStringBuffer.append(".thumbnails/metadata_6.dat");
55
56                 final String urlString = urlStringBuffer.toString();
57
58                 final BufferedInputStream in = new BufferedInputStream(new URL(
59                                 urlString).openStream());
60
61                 final ObjectInputStream oin = new ObjectInputStream(in);
62                 final DirectoryMetadata directory = (DirectoryMetadata) oin
63                                 .readObject();
64                 in.close();
65
66                 return directory;
67         }
68
69         @Override
70         public String getThumbnailPath(final Picture picture,
71                         final Dimension desiredDimension) {
72
73                 // in case thumbnail size was equal to original, then return original
74                 // file path
75                 if (picture.getDimensions().equals(desiredDimension))
76                         return localPrefix + picture.fileName;
77
78                 final String thumbnailFileName = picture
79                                 .getRelativeThumbnailFileName(desiredDimension);
80
81                 return localPrefix + "/" + Constants.THUMBNAILS_DIRECTORY_NAME + "/"
82                                 + thumbnailFileName;
83         }
84
85 }