29ceac1b607979fb3d7ebdc60887b65e94416578
[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 import java.util.List;
9
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;
16
17 public class WebIndexer extends AbstractIndexer {
18
19         private final String globalPrefix;
20
21         public WebIndexer(final String globalPrefix) {
22                 this.globalPrefix = globalPrefix;
23         }
24
25         @Override
26         public String getDirectoryUrl(final AbstractFile directory,
27                         final IndexingContext context) {
28                 return "photos.jsp?path=" + context.getLocalUrl() + "/"
29                                 + UrlParamEncoder.encode(directory.fileName);
30         }
31
32         public String getHtml(String requestPath) throws MalformedURLException,
33                         IOException, ClassNotFoundException {
34
35                 if (requestPath.equals("/"))
36                         requestPath = "";
37
38                 final MixedLayout layout = new MixedLayout();
39                 final IndexingContext context = new IndexingContext(globalPrefix,
40                                 requestPath);
41                 layout.init("Photos", context.getLocalPathComponents(), this, context);
42
43                 final DirectoryMetadata metadata = getMetadataForPath(requestPath);
44
45                 compileHtml(layout, metadata);
46
47                 return layout.getHtml();
48         }
49
50         public DirectoryMetadata getMetadataForPath(final String requestPath)
51                         throws IOException, MalformedURLException, ClassNotFoundException {
52
53                 final StringBuffer urlStringBuffer = new StringBuffer();
54
55                 urlStringBuffer.append(globalPrefix);
56                 urlStringBuffer.append(UrlParamEncoder.encode(requestPath));
57                 urlStringBuffer.append("/.thumbnails/metadata_6.dat");
58
59                 final String urlString = urlStringBuffer.toString();
60
61                 final BufferedInputStream in = new BufferedInputStream(new URL(
62                                 urlString).openStream());
63
64                 final ObjectInputStream oin = new ObjectInputStream(in);
65                 final DirectoryMetadata directory = (DirectoryMetadata) oin
66                                 .readObject();
67                 in.close();
68
69                 return directory;
70         }
71
72         @Override
73         public String getParentDirectoryUrl(final IndexingContext context) {
74                 final StringBuffer result = new StringBuffer();
75
76                 result.append("photos.jsp?path=");
77
78                 final List<String> components = context.getLocalPathComponents();
79
80                 for (final String pathComponent : components.subList(0,
81                                 components.size() - 1)) {
82                         result.append("/");
83                         result.append(pathComponent);
84                 }
85
86                 return result.toString();
87         }
88
89         @Override
90         public String getThumbnailUrl(final Picture picture,
91                         final Dimension desiredDimension, final IndexingContext context) {
92
93                 // in case thumbnail size was equal to original, then return original
94                 // file path
95                 if (picture.getDimensions().equals(desiredDimension))
96                         return context.getGlobalUrl() + context.getLocalUrl() + "/"
97                                         + picture.fileName;
98
99                 final String thumbnailFileName = picture
100                                 .getRelativeThumbnailFileName(desiredDimension);
101
102                 return context.getGlobalUrl() + context.getLocalUrl() + "/"
103                                 + Constants.THUMBNAILS_DIRECTORY_NAME + "/"
104                                 + UrlParamEncoder.encode(thumbnailFileName);
105         }
106
107 }