1 package eu.svjatoslav.meviz.htmlindexer.indexer;
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.layouts.MixedLayout;
7 import eu.svjatoslav.meviz.htmlindexer.metadata.Dimension;
8 import eu.svjatoslav.meviz.htmlindexer.metadata.DirectoryMetadata;
9 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.AbstractFile;
10 import eu.svjatoslav.meviz.htmlindexer.metadata.fileTypes.Picture;
12 import java.io.BufferedInputStream;
13 import java.io.IOException;
14 import java.io.ObjectInputStream;
15 import java.io.UnsupportedEncodingException;
17 import java.util.List;
19 import static eu.svjatoslav.meviz.htmlindexer.Constants.METADATA_FILE_NAME;
20 import static eu.svjatoslav.meviz.htmlindexer.Utils.urlEncode;
22 public class WebIndexer extends AbstractIndexer {
24 private static final int METADATA_LOAD_TRY_COUNT = 10;
27 * Example: "https://www3.svjatoslav.eu/web/photos"
29 private final String contentServerRootUrl;
32 * Example: "http://www3.svjatoslav.eu/web/photos"
34 private String metadataServerRootUrl;
37 * Path relative to frontend server. For example, if full URL is "http://127.0.0.1:8080/photos"
38 * then relative URL would be "/photos"
40 private final String frontendServerRelativeRootUrl;
43 * Example full URL: http://127.0.0.1:8080/photos/Spain
45 * @param contentServerRootUrl {@link #contentServerRootUrl}
46 * @param frontendServerRelativeRootUrl {@link #frontendServerRelativeRootUrl}
49 final String contentServerRootUrl,
50 final String metadataServerRootUrl,
51 final String frontendServerRelativeRootUrl) {
52 this.contentServerRootUrl = contentServerRootUrl;
53 this.metadataServerRootUrl = metadataServerRootUrl;
54 this.frontendServerRelativeRootUrl = frontendServerRelativeRootUrl;
58 public void compileHtml(final Layout layout,
59 final DirectoryMetadata directory)
60 throws UnsupportedEncodingException {
61 super.compileHtml(layout, directory);
65 public String getDirectoryUrl(final AbstractFile directory,
66 final IndexingContext context) {
67 return frontendServerRelativeRootUrl + context.getLocalUrl() + "/" + directory.fileName;
71 * @param frontendServerRequestRelativeUrl example: /Spain
73 public String getHtml(String frontendServerRequestRelativeUrl) throws
74 IOException, ClassNotFoundException {
76 if (frontendServerRequestRelativeUrl == null)
77 frontendServerRequestRelativeUrl = "";
79 if (frontendServerRequestRelativeUrl.equals("/"))
80 frontendServerRequestRelativeUrl = "";
82 final MixedLayout layout = new MixedLayout();
84 final IndexingContext context = new IndexingContext(
86 frontendServerRequestRelativeUrl);
88 layout.init("Photos", context.getLocalPathComponents(), this, context);
90 final DirectoryMetadata directory = getMetadataForPath(frontendServerRequestRelativeUrl);
92 compileHtml(layout, directory);
94 return layout.getHtml(false, false, directory);
97 private DirectoryMetadata getMetadataForPath(final String requestPath)
98 throws ClassNotFoundException, IOException {
100 final String url = metadataServerRootUrl + urlEncode(requestPath) + "/.thumbnails/"+ METADATA_FILE_NAME;
102 for (int attempt = 0; true; attempt++)
104 return attemptDirectoryMetadataDownload(url);
105 } catch (final IOException e) {
106 if (attempt > METADATA_LOAD_TRY_COUNT) throw e;
110 private DirectoryMetadata attemptDirectoryMetadataDownload(String urlString)
111 throws IOException, ClassNotFoundException {
113 final BufferedInputStream inputStream = new BufferedInputStream(new URL(urlString).openStream());
114 final ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
115 final DirectoryMetadata directory = (DirectoryMetadata) objectInputStream.readObject();
122 public String getParentDirectoryUrl(final IndexingContext context) {
123 final StringBuilder result = new StringBuilder();
125 result.append(frontendServerRelativeRootUrl);
127 final List<String> components = context.getLocalPathComponents();
129 for (final String pathComponent : components.subList(0,
130 components.size() - 1)) {
132 result.append(pathComponent);
135 return result.toString();
139 public String getThumbnailUrl(final Picture picture,
140 final Dimension desiredDimension, final IndexingContext context) {
142 // in case thumbnail size was equal to original, then return original
144 if (picture.getDimensions().equals(desiredDimension))
145 return context.getGlobalUrl() + context.getLocalUrl() + "/"
148 final String thumbnailFileName = picture
149 .getRelativeThumbnailFileName(desiredDimension);
151 return context.getGlobalUrl() + context.getLocalUrl() + "/"
152 + Constants.THUMBNAILS_DIRECTORY_NAME + "/"
153 + urlEncode(thumbnailFileName);