dba6f5aee7e857a2bb3e8378955b828f2689d423
[meviz.git] / src / main / java / eu / svjatoslav / meviz / htmlindexer / WebIndexer.java
1 package eu.svjatoslav.meviz.htmlindexer;
2
3 import eu.svjatoslav.meviz.htmlindexer.layouts.MixedLayout;
4 import eu.svjatoslav.meviz.htmlindexer.metadata.Dimension;
5 import eu.svjatoslav.meviz.htmlindexer.metadata.DirectoryMetadata;
6 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.AbstractFile;
7 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.Picture;
8
9 import java.io.BufferedInputStream;
10 import java.io.IOException;
11 import java.io.ObjectInputStream;
12 import java.io.UnsupportedEncodingException;
13 import java.net.URL;
14 import java.util.List;
15
16 public class WebIndexer extends AbstractIndexer {
17
18     private static final int METADATA_LOAD_TRY_COUNT = 10;
19     private final String globalPrefix;
20     private final String jspPath;
21
22     public WebIndexer(final String globalPrefix, final String jspPath) {
23         this.globalPrefix = globalPrefix;
24         this.jspPath = jspPath;
25     }
26
27     @Override
28     public void compileHtml(final Layout layout,
29                             final DirectoryMetadata directory)
30             throws UnsupportedEncodingException {
31         super.compileHtml(layout, directory);
32     }
33
34     @Override
35     public String getDirectoryUrl(final AbstractFile directory,
36                                   final IndexingContext context) {
37         return jspPath + context.getLocalUrl() + "/" + directory.fileName;
38     }
39
40     public String getHtml(String requestPath) throws
41             IOException, ClassNotFoundException {
42
43         if (requestPath == null)
44             requestPath = "";
45
46         if (requestPath.equals("/"))
47             requestPath = "";
48
49         final MixedLayout layout = new MixedLayout();
50         final IndexingContext context = new IndexingContext(globalPrefix,
51                 requestPath);
52         layout.init("Photos", context.getLocalPathComponents(), this, context);
53
54         final DirectoryMetadata directory = getMetadataForPath(requestPath);
55
56         compileHtml(layout, directory);
57
58         return layout.getHtml(false, false, directory);
59     }
60
61     private DirectoryMetadata getMetadataForPath(final String requestPath)
62             throws ClassNotFoundException, IOException {
63
64         final String urlString = globalPrefix + Utils.urlEncode(requestPath) +"/.thumbnails/metadata_6.dat";
65
66         for (int i = 0; true; i++)
67             try {
68                 return attemptDirectoryMetadataDownload(urlString);
69             } catch (final IOException e) {
70                 if (i > METADATA_LOAD_TRY_COUNT) throw e;
71             }
72     }
73
74     private DirectoryMetadata attemptDirectoryMetadataDownload(String urlString) throws IOException, ClassNotFoundException {
75         final BufferedInputStream inputStream = new BufferedInputStream(new URL(
76                 urlString).openStream());
77
78         final ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
79         final DirectoryMetadata directory = (DirectoryMetadata) objectInputStream
80                 .readObject();
81         inputStream.close();
82
83         return directory;
84     }
85
86     @Override
87     public String getParentDirectoryUrl(final IndexingContext context) {
88         final StringBuilder result = new StringBuilder();
89
90         result.append(jspPath);
91
92         final List<String> components = context.getLocalPathComponents();
93
94         for (final String pathComponent : components.subList(0,
95                 components.size() - 1)) {
96             result.append("/");
97             result.append(pathComponent);
98         }
99
100         return result.toString();
101     }
102
103     @Override
104     public String getThumbnailUrl(final Picture picture,
105                                   final Dimension desiredDimension, final IndexingContext context) {
106
107         // in case thumbnail size was equal to original, then return original
108         // file path
109         if (picture.getDimensions().equals(desiredDimension))
110             return context.getGlobalUrl() + context.getLocalUrl() + "/"
111                     + picture.fileName;
112
113         final String thumbnailFileName = picture
114                 .getRelativeThumbnailFileName(desiredDimension);
115
116         return context.getGlobalUrl() + context.getLocalUrl() + "/"
117                 + Constants.THUMBNAILS_DIRECTORY_NAME + "/"
118                 + Utils.urlEncode(thumbnailFileName);
119     }
120
121 }