Commit | Line | Data |
---|---|---|
f433d153 NR |
1 | package be.nikiroo.fanfix.library; |
2 | ||
3 | import java.io.File; | |
4 | import java.io.IOException; | |
5 | import java.io.InputStream; | |
6 | import java.net.URL; | |
7 | import java.util.ArrayList; | |
8 | import java.util.HashMap; | |
9 | import java.util.List; | |
10 | import java.util.Map; | |
11 | ||
12 | import org.json.JSONArray; | |
13 | import org.json.JSONObject; | |
14 | ||
15 | import be.nikiroo.fanfix.Instance; | |
c5103223 | 16 | import be.nikiroo.fanfix.data.Chapter; |
f433d153 NR |
17 | import be.nikiroo.fanfix.data.JsonIO; |
18 | import be.nikiroo.fanfix.data.MetaData; | |
c5103223 NR |
19 | import be.nikiroo.fanfix.data.Paragraph; |
20 | import be.nikiroo.fanfix.data.Paragraph.ParagraphType; | |
f433d153 NR |
21 | import be.nikiroo.fanfix.data.Story; |
22 | import be.nikiroo.utils.IOUtils; | |
23 | import be.nikiroo.utils.Image; | |
24 | import be.nikiroo.utils.Progress; | |
5ee0fc14 | 25 | import be.nikiroo.utils.Version; |
f433d153 NR |
26 | |
27 | /** | |
28 | * This {@link BasicLibrary} will access a remote server to list the available | |
29 | * stories, and download the ones you try to load to the local directory | |
30 | * specified in the configuration. | |
31 | * <p> | |
32 | * This remote library uses http:// or https://. | |
33 | * | |
34 | * @author niki | |
35 | */ | |
36 | public class WebLibrary extends BasicLibrary { | |
37 | private String host; | |
38 | private int port; | |
39 | private final String key; | |
40 | private final String subkey; | |
41 | ||
42 | // informative only (server will make the actual checks) | |
43 | private boolean rw; | |
44 | ||
45 | /** | |
46 | * Create a {@link RemoteLibrary} linked to the given server. | |
47 | * <p> | |
48 | * Note that the key is structured: | |
49 | * <tt><b><i>xxx</i></b>(|<b><i>yyy</i></b>|<b>wl</b>)(|<b>rw</b>)</tt> | |
50 | * <p> | |
51 | * Note that anything before the first pipe (<tt>|</tt>) character is | |
52 | * considered to be the encryption key, anything after that character is | |
53 | * called the subkey (including the other pipe characters and flags!). | |
54 | * <p> | |
55 | * This is important because the subkey (including the pipe characters and | |
56 | * flags) must be present as-is in the server configuration file to be | |
57 | * allowed. | |
58 | * <ul> | |
59 | * <li><b><i>xxx</i></b>: the encryption key used to communicate with the | |
60 | * server</li> | |
61 | * <li><b><i>yyy</i></b>: the secondary key</li> | |
62 | * <li><b>rw</b>: flag to allow read and write access if it is not the | |
63 | * default on this server</li> | |
64 | * <li><b>wl</b>: flag to allow access to all the stories (bypassing the | |
65 | * whitelist if it exists)</li> | |
66 | * </ul> | |
67 | * <p> | |
68 | * Some examples: | |
69 | * <ul> | |
70 | * <li><b>my_key</b>: normal connection, will take the default server | |
71 | * options</li> | |
72 | * <li><b>my_key|agzyzz|wl</b>: will ask to bypass the white list (if it | |
73 | * exists)</li> | |
74 | * <li><b>my_key|agzyzz|rw</b>: will ask read-write access (if the default | |
75 | * is read-only)</li> | |
76 | * <li><b>my_key|agzyzz|wl|rw</b>: will ask both read-write access and white | |
77 | * list bypass</li> | |
78 | * </ul> | |
79 | * | |
80 | * @param key | |
81 | * the key that will allow us to exchange information with the | |
82 | * server | |
83 | * @param host | |
84 | * the host to contact or NULL for localhost | |
85 | * @param port | |
86 | * the port to contact it on | |
87 | */ | |
88 | public WebLibrary(String key, String host, int port) { | |
89 | int index = -1; | |
90 | if (key != null) { | |
91 | index = key.indexOf('|'); | |
92 | } | |
93 | ||
94 | if (index >= 0) { | |
95 | this.key = key.substring(0, index); | |
96 | this.subkey = key.substring(index + 1); | |
97 | } else { | |
98 | this.key = key; | |
99 | this.subkey = ""; | |
100 | } | |
101 | ||
102 | this.rw = subkey.contains("|rw"); | |
103 | ||
104 | this.host = host; | |
105 | this.port = port; | |
f433d153 NR |
106 | } |
107 | ||
f70bcacf NR |
108 | /** |
109 | * Return the version of the program running server-side. | |
110 | * <p> | |
111 | * Never returns NULL. | |
112 | * | |
113 | * @return the version or an empty {@link Version} if not known | |
114 | */ | |
5ee0fc14 NR |
115 | public Version getVersion() { |
116 | try { | |
e4b1b70c | 117 | InputStream in = post(WebLibraryUrls.VERSION_URL); |
5ee0fc14 NR |
118 | try { |
119 | return new Version(IOUtils.readSmallStream(in)); | |
120 | } finally { | |
121 | in.close(); | |
122 | } | |
123 | } catch (IOException e) { | |
124 | } | |
125 | ||
a4efa44c | 126 | return new Version(); |
5ee0fc14 NR |
127 | } |
128 | ||
09c2396e NR |
129 | /** |
130 | * Stop the server. | |
131 | * | |
132 | * @throws IOException | |
133 | * in case of I/O errors | |
134 | */ | |
135 | public void stop() throws IOException { | |
136 | post(WebLibraryUrls.EXIT_URL, null).close(); | |
137 | } | |
138 | ||
f433d153 NR |
139 | @Override |
140 | public Status getStatus() { | |
141 | try { | |
e4b1b70c | 142 | post(WebLibraryUrls.INDEX_URL).close(); |
f433d153 NR |
143 | } catch (IOException e) { |
144 | try { | |
e4b1b70c | 145 | post("/style.css").close(); |
f433d153 NR |
146 | return Status.UNAUTHORIZED; |
147 | } catch (IOException ioe) { | |
5ee0fc14 | 148 | return Status.UNAVAILABLE; |
f433d153 NR |
149 | } |
150 | } | |
151 | ||
152 | return rw ? Status.READ_WRITE : Status.READ_ONLY; | |
153 | } | |
154 | ||
155 | @Override | |
156 | public String getLibraryName() { | |
a4efa44c NR |
157 | return (rw ? "[READ-ONLY] " : "") + host + ":" + port + " (" |
158 | + getVersion() + ")"; | |
f433d153 NR |
159 | } |
160 | ||
161 | @Override | |
162 | public Image getCover(String luid) throws IOException { | |
e4b1b70c | 163 | InputStream in = post(WebLibraryUrls.getStoryUrlCover(luid)); |
5ee0fc14 | 164 | try { |
a1226ce0 NR |
165 | Image img = new Image(in); |
166 | if (img.getSize() > 0) { | |
167 | return img; | |
168 | } | |
169 | ||
170 | return null; | |
5ee0fc14 NR |
171 | } finally { |
172 | in.close(); | |
f433d153 | 173 | } |
f433d153 NR |
174 | } |
175 | ||
c5103223 | 176 | @Override |
6673ec59 | 177 | public Image getCustomSourceCover(String source) throws IOException { |
e4b1b70c | 178 | InputStream in = post(WebLibraryUrls.getCoverUrlSource(source)); |
6673ec59 | 179 | try { |
a1226ce0 NR |
180 | Image img = new Image(in); |
181 | if (img.getSize() > 0) { | |
182 | return img; | |
183 | } | |
184 | ||
185 | return null; | |
6673ec59 NR |
186 | } finally { |
187 | in.close(); | |
188 | } | |
c5103223 NR |
189 | } |
190 | ||
191 | @Override | |
6673ec59 | 192 | public Image getCustomAuthorCover(String author) throws IOException { |
e4b1b70c | 193 | InputStream in = post(WebLibraryUrls.getCoverUrlAuthor(author)); |
6673ec59 | 194 | try { |
a1226ce0 NR |
195 | Image img = new Image(in); |
196 | if (img.getSize() > 0) { | |
197 | return img; | |
198 | } | |
199 | ||
200 | return null; | |
6673ec59 NR |
201 | } finally { |
202 | in.close(); | |
203 | } | |
c5103223 NR |
204 | } |
205 | ||
f433d153 NR |
206 | @Override |
207 | public void setSourceCover(String source, String luid) throws IOException { | |
e4b1b70c NR |
208 | Map<String, String> post = new HashMap<String, String>(); |
209 | post.put("luid", luid); | |
210 | post(WebLibraryUrls.getCoverUrlSource(source), post).close(); | |
f433d153 NR |
211 | } |
212 | ||
213 | @Override | |
214 | public void setAuthorCover(String author, String luid) throws IOException { | |
e4b1b70c NR |
215 | Map<String, String> post = new HashMap<String, String>(); |
216 | post.put("luid", luid); | |
217 | post(WebLibraryUrls.getCoverUrlAuthor(author), post).close(); | |
f433d153 NR |
218 | } |
219 | ||
c5103223 NR |
220 | @Override |
221 | public synchronized Story getStory(final String luid, Progress pg) | |
222 | throws IOException { | |
f70bcacf NR |
223 | if (pg == null) { |
224 | pg = new Progress(); | |
225 | } | |
c5103223 NR |
226 | |
227 | Story story; | |
e4b1b70c | 228 | InputStream in = post(WebLibraryUrls.getStoryUrlJson(luid)); |
c5103223 NR |
229 | try { |
230 | JSONObject json = new JSONObject(IOUtils.readSmallStream(in)); | |
231 | story = JsonIO.toStory(json); | |
232 | } finally { | |
233 | in.close(); | |
234 | } | |
235 | ||
f70bcacf NR |
236 | int max = 0; |
237 | for (Chapter chap : story) { | |
238 | max += chap.getParagraphs().size(); | |
239 | } | |
240 | pg.setMinMax(0, max); | |
241 | ||
c5103223 NR |
242 | story.getMeta().setCover(getCover(luid)); |
243 | int chapNum = 1; | |
244 | for (Chapter chap : story) { | |
245 | int number = 1; | |
246 | for (Paragraph para : chap) { | |
247 | if (para.getType() == ParagraphType.IMAGE) { | |
e4b1b70c | 248 | InputStream subin = post( |
5ee0fc14 | 249 | WebLibraryUrls.getStoryUrl(luid, chapNum, number)); |
c5103223 | 250 | try { |
a1226ce0 NR |
251 | Image img = new Image(subin); |
252 | if (img.getSize() > 0) { | |
253 | para.setContentImage(img); | |
254 | } | |
c5103223 NR |
255 | } finally { |
256 | subin.close(); | |
257 | } | |
258 | } | |
259 | ||
f70bcacf | 260 | pg.add(1); |
c5103223 NR |
261 | number++; |
262 | } | |
263 | ||
264 | chapNum++; | |
265 | } | |
266 | ||
f70bcacf | 267 | pg.done(); |
c5103223 NR |
268 | return story; |
269 | } | |
270 | ||
f433d153 NR |
271 | @Override |
272 | protected List<MetaData> getMetas(Progress pg) throws IOException { | |
273 | List<MetaData> metas = new ArrayList<MetaData>(); | |
e4b1b70c | 274 | InputStream in = post(WebLibraryUrls.LIST_URL_METADATA); |
f433d153 NR |
275 | JSONArray jsonArr = new JSONArray(IOUtils.readSmallStream(in)); |
276 | for (int i = 0; i < jsonArr.length(); i++) { | |
277 | JSONObject json = jsonArr.getJSONObject(i); | |
278 | metas.add(JsonIO.toMetaData(json)); | |
279 | } | |
280 | ||
281 | return metas; | |
282 | } | |
283 | ||
284 | @Override | |
285 | // Could work (more slowly) without it | |
286 | public MetaData imprt(final URL url, Progress pg) throws IOException { | |
f70bcacf NR |
287 | if (pg == null) { |
288 | pg = new Progress(); | |
289 | } | |
f433d153 NR |
290 | |
291 | // Import the file locally if it is actually a file | |
292 | ||
293 | if (url == null || url.getProtocol().equalsIgnoreCase("file")) { | |
294 | return super.imprt(url, pg); | |
295 | } | |
296 | ||
297 | // Import it remotely if it is an URL | |
298 | ||
f70bcacf NR |
299 | try { |
300 | String luid = null; | |
301 | ||
302 | Map<String, String> post = new HashMap<String, String>(); | |
303 | post.put("url", url.toString()); | |
304 | InputStream in = post(WebLibraryUrls.IMPRT_URL_IMPORT, post); | |
305 | try { | |
306 | luid = IOUtils.readSmallStream(in); | |
307 | } finally { | |
308 | in.close(); | |
309 | } | |
310 | ||
311 | Progress subPg = null; | |
312 | do { | |
313 | try { | |
314 | Thread.sleep(2000); | |
315 | } catch (InterruptedException e) { | |
316 | } | |
317 | ||
318 | in = post(WebLibraryUrls.getImprtProgressUrl(luid)); | |
319 | try { | |
320 | subPg = JsonIO.toProgress( | |
321 | new JSONObject(IOUtils.readSmallStream(in))); | |
ce7d5787 NR |
322 | pg.setName(subPg.getName()); |
323 | pg.setMinMax(subPg.getMin(), subPg.getMax()); | |
324 | pg.setProgress(subPg.getProgress()); | |
a1226ce0 NR |
325 | } catch (Exception e) { |
326 | subPg = null; | |
f70bcacf NR |
327 | } finally { |
328 | in.close(); | |
329 | } | |
330 | } while (subPg != null); | |
331 | ||
332 | in = post(WebLibraryUrls.getStoryUrlMetadata(luid)); | |
333 | try { | |
334 | return JsonIO.toMetaData( | |
335 | new JSONObject(IOUtils.readSmallStream(in))); | |
336 | } finally { | |
337 | in.close(); | |
338 | } | |
339 | } finally { | |
340 | pg.done(); | |
341 | } | |
f433d153 NR |
342 | } |
343 | ||
344 | @Override | |
345 | // Could work (more slowly) without it | |
346 | protected synchronized void changeSTA(final String luid, | |
347 | final String newSource, final String newTitle, | |
348 | final String newAuthor, Progress pg) throws IOException { | |
089e354e NR |
349 | MetaData meta = getInfo(luid); |
350 | if (meta != null) { | |
351 | if (!meta.getSource().equals(newSource)) { | |
352 | Map<String, String> post = new HashMap<String, String>(); | |
353 | post.put("value", newSource); | |
354 | post(WebLibraryUrls.getStoryUrlSource(luid), post).close(); | |
355 | } | |
356 | if (!meta.getTitle().equals(newTitle)) { | |
357 | Map<String, String> post = new HashMap<String, String>(); | |
358 | post.put("value", newTitle); | |
359 | post(WebLibraryUrls.getStoryUrlTitle(luid), post).close(); | |
360 | } | |
361 | if (!meta.getAuthor().equals(newAuthor)) { | |
362 | Map<String, String> post = new HashMap<String, String>(); | |
363 | post.put("value", newAuthor); | |
364 | post(WebLibraryUrls.getStoryUrlAuthor(luid), post).close(); | |
365 | } | |
366 | } | |
f433d153 NR |
367 | } |
368 | ||
acbec0d2 NR |
369 | @Override |
370 | public synchronized void delete(String luid) throws IOException { | |
371 | post(WebLibraryUrls.getDeleteUrlStory(luid), null).close(); | |
372 | } | |
373 | ||
f433d153 NR |
374 | @Override |
375 | protected void updateInfo(MetaData meta) { | |
376 | // Will be taken care of directly server side | |
377 | } | |
378 | ||
379 | @Override | |
380 | protected void invalidateInfo(String luid) { | |
381 | // Will be taken care of directly server side | |
382 | } | |
383 | ||
384 | // The following methods are only used by Save and Delete in BasicLibrary: | |
385 | ||
386 | @Override | |
f70bcacf | 387 | protected String getNextId() { |
f433d153 NR |
388 | throw new java.lang.InternalError("Should not have been called"); |
389 | } | |
390 | ||
391 | @Override | |
392 | protected void doDelete(String luid) throws IOException { | |
393 | throw new java.lang.InternalError("Should not have been called"); | |
394 | } | |
395 | ||
396 | @Override | |
397 | protected Story doSave(Story story, Progress pg) throws IOException { | |
398 | throw new java.lang.InternalError("Should not have been called"); | |
399 | } | |
400 | ||
401 | // | |
402 | ||
403 | @Override | |
404 | public File getFile(final String luid, Progress pg) { | |
405 | throw new java.lang.InternalError( | |
406 | "Operation not supportorted on remote Libraries"); | |
407 | } | |
408 | ||
5ee0fc14 | 409 | // starts with "/", never NULL |
e4b1b70c NR |
410 | private InputStream post(String path) throws IOException { |
411 | return post(path, null); | |
412 | } | |
413 | ||
414 | // starts with "/", never NULL | |
415 | private InputStream post(String path, Map<String, String> post) | |
416 | throws IOException { | |
f433d153 NR |
417 | URL url = new URL(host + ":" + port + path); |
418 | ||
e4b1b70c NR |
419 | if (post == null) { |
420 | post = new HashMap<String, String>(); | |
421 | } | |
f433d153 NR |
422 | post.put("login", subkey); |
423 | post.put("password", key); | |
424 | ||
425 | return Instance.getInstance().getCache().openNoCache(url, null, post, | |
426 | null, null); | |
427 | } | |
428 | } |