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