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