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