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