test: improve flag files
[nikiroo-utils.git] / src / be / nikiroo / fanfix / library / RemoteLibrary.java
1 package be.nikiroo.fanfix.library;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.net.URL;
6 import java.net.UnknownHostException;
7 import java.util.ArrayList;
8 import java.util.List;
9
10 import javax.net.ssl.SSLException;
11
12 import be.nikiroo.fanfix.Instance;
13 import be.nikiroo.fanfix.data.MetaData;
14 import be.nikiroo.fanfix.data.Story;
15 import be.nikiroo.utils.Image;
16 import be.nikiroo.utils.Progress;
17 import be.nikiroo.utils.Version;
18 import be.nikiroo.utils.serial.server.ConnectActionClientObject;
19
20 /**
21 * This {@link BasicLibrary} will access a remote server to list the available
22 * stories, and download the ones you try to load to the local directory
23 * specified in the configuration.
24 *
25 * @author niki
26 */
27 public class RemoteLibrary extends BasicLibrary {
28 interface RemoteAction {
29 public void action(ConnectActionClientObject action) throws Exception;
30 }
31
32 class RemoteConnectAction extends ConnectActionClientObject {
33 public RemoteConnectAction() throws IOException {
34 super(host, port, key);
35 }
36
37 @Override
38 public Object send(Object data) throws IOException,
39 NoSuchFieldException, NoSuchMethodException,
40 ClassNotFoundException {
41 Object rep = super.send(data);
42 if (rep instanceof RemoteLibraryException) {
43 RemoteLibraryException remoteEx = (RemoteLibraryException) rep;
44 throw remoteEx.unwrapException();
45 }
46
47 return rep;
48 }
49 }
50
51 private String host;
52 private int port;
53 private final String key;
54 private final String subkey;
55
56 // informative only (server will make the actual checks)
57 private boolean rw;
58
59 /**
60 * Create a {@link RemoteLibrary} linked to the given server.
61 * <p>
62 * Note that the key is structured:
63 * <tt><b><i>xxx</i></b>(|<b><i>yyy</i></b>|<b>wl</b>)(|<b>rw</b>)</tt>
64 * <p>
65 * Note that anything before the first pipe (<tt>|</tt>) character is
66 * considered to be the encryption key, anything after that character is
67 * called the subkey (including the other pipe characters and flags!).
68 * <p>
69 * This is important because the subkey (including the pipe characters and
70 * flags) must be present as-is in the server configuration file to be
71 * allowed.
72 * <ul>
73 * <li><b><i>xxx</i></b>: the encryption key used to communicate with the
74 * server</li>
75 * <li><b><i>yyy</i></b>: the secondary key</li>
76 * <li><b>rw</b>: flag to allow read and write access if it is not the
77 * default on this server</li>
78 * <li><b>wl</b>: flag to allow access to all the stories (bypassing the
79 * whitelist if it exists)</li>
80 * </ul>
81 *
82 * Some examples:
83 * <ul>
84 * <li><b>my_key</b>: normal connection, will take the default server
85 * options</li>
86 * <li><b>my_key|agzyzz|wl</b>: will ask to bypass the white list (if it
87 * exists)</li>
88 * <li><b>my_key|agzyzz|rw</b>: will ask read-write access (if the default
89 * is read-only)</li>
90 * <li><b>my_key|agzyzz|wl|rw</b>: will ask both read-write access and white
91 * list bypass</li>
92 * </ul>
93 *
94 * @param key
95 * the key that will allow us to exchange information with the
96 * server
97 * @param host
98 * the host to contact or NULL for localhost
99 * @param port
100 * the port to contact it on
101 */
102 public RemoteLibrary(String key, String host, int port) {
103 int index = -1;
104 if (key != null) {
105 index = key.indexOf('|');
106 }
107
108 if (index >= 0) {
109 this.key = key.substring(0, index);
110 this.subkey = key.substring(index + 1);
111 } else {
112 this.key = key;
113 this.subkey = "";
114 }
115
116 this.host = host;
117 this.port = port;
118 }
119
120 @Override
121 public String getLibraryName() {
122 return (rw ? "[READ-ONLY] " : "") + host + ":" + port;
123 }
124
125 @Override
126 public Status getStatus() {
127 Instance.getTraceHandler().trace("Getting remote lib status...");
128 Status status = getStatusDo();
129 Instance.getTraceHandler().trace("Remote lib status: " + status);
130 return status;
131 }
132
133 private Status getStatusDo() {
134 final Status[] result = new Status[1];
135
136 result[0] = Status.INVALID;
137
138 try {
139 new RemoteConnectAction() {
140 @Override
141 public void action(Version serverVersion) throws Exception {
142 Object rep = send(new Object[] { subkey, "PING" });
143
144 if ("r/w".equals(rep)) {
145 rw = true;
146 result[0] = Status.READ_WRITE;
147 } else if ("r/o".equals(rep)) {
148 rw = false;
149 result[0] = Status.READ_ONLY;
150 } else {
151 result[0] = Status.UNAUTHORIZED;
152 }
153 }
154
155 @Override
156 protected void onError(Exception e) {
157 if (e instanceof SSLException) {
158 result[0] = Status.UNAUTHORIZED;
159 } else {
160 result[0] = Status.UNAVAILABLE;
161 }
162 }
163 }.connect();
164 } catch (UnknownHostException e) {
165 result[0] = Status.INVALID;
166 } catch (IllegalArgumentException e) {
167 result[0] = Status.INVALID;
168 } catch (Exception e) {
169 result[0] = Status.UNAVAILABLE;
170 }
171
172 return result[0];
173 }
174
175 @Override
176 public Image getCover(final String luid) throws IOException {
177 final Image[] result = new Image[1];
178
179 connectRemoteAction(new RemoteAction() {
180 @Override
181 public void action(ConnectActionClientObject action)
182 throws Exception {
183 Object rep = action.send(new Object[] { subkey, "GET_COVER",
184 luid });
185 result[0] = (Image) rep;
186 }
187 });
188
189 return result[0];
190 }
191
192 @Override
193 public Image getCustomSourceCover(final String source) throws IOException {
194 return getCustomCover(source, "SOURCE");
195 }
196
197 @Override
198 public Image getCustomAuthorCover(final String author) throws IOException {
199 return getCustomCover(author, "AUTHOR");
200 }
201
202 // type: "SOURCE" or "AUTHOR"
203 private Image getCustomCover(final String source, final String type)
204 throws IOException {
205 final Image[] result = new Image[1];
206
207 connectRemoteAction(new RemoteAction() {
208 @Override
209 public void action(ConnectActionClientObject action)
210 throws Exception {
211 Object rep = action.send(new Object[] { subkey,
212 "GET_CUSTOM_COVER", type, source });
213 result[0] = (Image) rep;
214 }
215 });
216
217 return result[0];
218 }
219
220 @Override
221 public synchronized Story getStory(final String luid, Progress pg)
222 throws IOException {
223 final Progress pgF = pg;
224 final Story[] result = new Story[1];
225
226 connectRemoteAction(new RemoteAction() {
227 @Override
228 public void action(ConnectActionClientObject action)
229 throws Exception {
230 Progress pg = pgF;
231 if (pg == null) {
232 pg = new Progress();
233 }
234
235 Object rep = action.send(new Object[] { subkey, "GET_STORY",
236 luid });
237
238 MetaData meta = null;
239 if (rep instanceof MetaData) {
240 meta = (MetaData) rep;
241 if (meta.getWords() <= Integer.MAX_VALUE) {
242 pg.setMinMax(0, (int) meta.getWords());
243 }
244 }
245
246 List<Object> list = new ArrayList<Object>();
247 for (Object obj = action.send(null); obj != null; obj = action
248 .send(null)) {
249 list.add(obj);
250 pg.add(1);
251 }
252
253 result[0] = RemoteLibraryServer.rebuildStory(list);
254 pg.done();
255 }
256 });
257
258 return result[0];
259 }
260
261 @Override
262 public synchronized Story save(final Story story, final String luid,
263 Progress pg) throws IOException {
264
265 final String[] luidSaved = new String[1];
266 Progress pgSave = new Progress();
267 Progress pgRefresh = new Progress();
268 if (pg == null) {
269 pg = new Progress();
270 }
271
272 pg.setMinMax(0, 10);
273 pg.addProgress(pgSave, 9);
274 pg.addProgress(pgRefresh, 1);
275
276 final Progress pgF = pgSave;
277
278 connectRemoteAction(new RemoteAction() {
279 @Override
280 public void action(ConnectActionClientObject action)
281 throws Exception {
282 Progress pg = pgF;
283 if (story.getMeta().getWords() <= Integer.MAX_VALUE) {
284 pg.setMinMax(0, (int) story.getMeta().getWords());
285 }
286
287 action.send(new Object[] { subkey, "SAVE_STORY", luid });
288
289 List<Object> list = RemoteLibraryServer.breakStory(story);
290 for (Object obj : list) {
291 action.send(obj);
292 pg.add(1);
293 }
294
295 luidSaved[0] = (String) action.send(null);
296
297 pg.done();
298 }
299 });
300
301 // because the meta changed:
302 MetaData meta = getInfo(luidSaved[0]);
303 if (story.getMeta().getClass() != null) {
304 // If already available locally:
305 meta.setCover(story.getMeta().getCover());
306 } else {
307 // If required:
308 meta.setCover(getCover(meta.getLuid()));
309 }
310 story.setMeta(meta);
311
312 pg.done();
313
314 return story;
315 }
316
317 @Override
318 public synchronized void delete(final String luid) throws IOException {
319 connectRemoteAction(new RemoteAction() {
320 @Override
321 public void action(ConnectActionClientObject action)
322 throws Exception {
323 action.send(new Object[] { subkey, "DELETE_STORY", luid });
324 }
325 });
326 }
327
328 @Override
329 public void setSourceCover(final String source, final String luid)
330 throws IOException {
331 setCover(source, luid, "SOURCE");
332 }
333
334 @Override
335 public void setAuthorCover(final String author, final String luid)
336 throws IOException {
337 setCover(author, luid, "AUTHOR");
338 }
339
340 // type = "SOURCE" | "AUTHOR"
341 private void setCover(final String value, final String luid,
342 final String type) throws IOException {
343 connectRemoteAction(new RemoteAction() {
344 @Override
345 public void action(ConnectActionClientObject action)
346 throws Exception {
347 action.send(new Object[] { subkey, "SET_COVER", type, value,
348 luid });
349 }
350 });
351 }
352
353 @Override
354 // Could work (more slowly) without it
355 public Story imprt(final URL url, Progress pg) throws IOException {
356 // Import the file locally if it is actually a file
357 if (url == null || url.getProtocol().equalsIgnoreCase("file")) {
358 return super.imprt(url, pg);
359 }
360
361 // Import it remotely if it is an URL
362
363 if (pg == null) {
364 pg = new Progress();
365 }
366
367 pg.setMinMax(0, 2);
368 Progress pgImprt = new Progress();
369 Progress pgGet = new Progress();
370 pg.addProgress(pgImprt, 1);
371 pg.addProgress(pgGet, 1);
372
373 final Progress pgF = pgImprt;
374 final String[] luid = new String[1];
375
376 connectRemoteAction(new RemoteAction() {
377 @Override
378 public void action(ConnectActionClientObject action)
379 throws Exception {
380 Progress pg = pgF;
381
382 Object rep = action.send(new Object[] { subkey, "IMPORT",
383 url.toString() });
384
385 while (true) {
386 if (!RemoteLibraryServer.updateProgress(pg, rep)) {
387 break;
388 }
389
390 rep = action.send(null);
391 }
392
393 pg.done();
394 luid[0] = (String) rep;
395 }
396 });
397
398 if (luid[0] == null) {
399 throw new IOException("Remote failure");
400 }
401
402 Story story = getStory(luid[0], pgGet);
403 pgGet.done();
404
405 pg.done();
406 return story;
407 }
408
409 @Override
410 // Could work (more slowly) without it
411 protected synchronized void changeSTA(final String luid,
412 final String newSource, final String newTitle,
413 final String newAuthor, Progress pg) throws IOException {
414
415 final Progress pgF = pg == null ? new Progress() : pg;
416
417 connectRemoteAction(new RemoteAction() {
418 @Override
419 public void action(ConnectActionClientObject action)
420 throws Exception {
421 Progress pg = pgF;
422
423 Object rep = action.send(new Object[] { subkey, "CHANGE_STA",
424 luid, newSource, newTitle, newAuthor });
425 while (true) {
426 if (!RemoteLibraryServer.updateProgress(pg, rep)) {
427 break;
428 }
429
430 rep = action.send(null);
431 }
432 }
433 });
434 }
435
436 @Override
437 public synchronized File getFile(final String luid, Progress pg) {
438 throw new java.lang.InternalError(
439 "Operation not supportorted on remote Libraries");
440 }
441
442 /**
443 * Stop the server.
444 *
445 * @throws IOException
446 * in case of I/O error (including bad key)
447 */
448 public void exit() throws IOException {
449 connectRemoteAction(new RemoteAction() {
450 @Override
451 public void action(ConnectActionClientObject action)
452 throws Exception {
453 action.send(new Object[] { subkey, "EXIT" });
454 }
455 });
456 }
457
458 @Override
459 public synchronized MetaData getInfo(String luid) throws IOException {
460 List<MetaData> metas = getMetasList(luid, null);
461 if (!metas.isEmpty()) {
462 return metas.get(0);
463 }
464
465 return null;
466 }
467
468 @Override
469 protected List<MetaData> getMetas(Progress pg) throws IOException {
470 return getMetasList("*", pg);
471 }
472
473 @Override
474 protected void updateInfo(MetaData meta) {
475 // Will be taken care of directly server side
476 }
477
478 @Override
479 protected void invalidateInfo(String luid) {
480 // Will be taken care of directly server side
481 }
482
483 // The following methods are only used by Save and Delete in BasicLibrary:
484
485 @Override
486 protected int getNextId() {
487 throw new java.lang.InternalError("Should not have been called");
488 }
489
490 @Override
491 protected void doDelete(String luid) throws IOException {
492 throw new java.lang.InternalError("Should not have been called");
493 }
494
495 @Override
496 protected Story doSave(Story story, Progress pg) throws IOException {
497 throw new java.lang.InternalError("Should not have been called");
498 }
499
500 //
501
502 /**
503 * Return the meta of the given story or a list of all known metas if the
504 * luid is "*".
505 * <p>
506 * Will not get the covers.
507 *
508 * @param luid
509 * the luid of the story or *
510 * @param pg
511 * the optional progress
512 *
513 * @return the metas
514 *
515 * @throws IOException
516 * in case of I/O error or bad key (SSLException)
517 */
518 private List<MetaData> getMetasList(final String luid, Progress pg)
519 throws IOException {
520 final Progress pgF = pg;
521 final List<MetaData> metas = new ArrayList<MetaData>();
522
523 connectRemoteAction(new RemoteAction() {
524 @Override
525 public void action(ConnectActionClientObject action)
526 throws Exception {
527 Progress pg = pgF;
528 if (pg == null) {
529 pg = new Progress();
530 }
531
532 Object rep = action.send(new Object[] { subkey, "GET_METADATA",
533 luid });
534
535 while (true) {
536 if (!RemoteLibraryServer.updateProgress(pg, rep)) {
537 break;
538 }
539
540 rep = action.send(null);
541 }
542
543 if (rep instanceof MetaData[]) {
544 for (MetaData meta : (MetaData[]) rep) {
545 metas.add(meta);
546 }
547 } else if (rep != null) {
548 metas.add((MetaData) rep);
549 }
550 }
551 });
552
553 return metas;
554 }
555
556 private void connectRemoteAction(final RemoteAction runAction)
557 throws IOException {
558 final IOException[] err = new IOException[1];
559 try {
560 final RemoteConnectAction[] array = new RemoteConnectAction[1];
561 RemoteConnectAction ra = new RemoteConnectAction() {
562 @Override
563 public void action(Version serverVersion) throws Exception {
564 runAction.action(array[0]);
565 }
566
567 @Override
568 protected void onError(Exception e) {
569 if (!(e instanceof IOException)) {
570 Instance.getTraceHandler().error(e);
571 return;
572 }
573
574 err[0] = (IOException) e;
575 }
576 };
577 array[0] = ra;
578 ra.connect();
579 } catch (Exception e) {
580 err[0] = (IOException) e;
581 }
582
583 if (err[0] != null) {
584 throw err[0];
585 }
586 }
587 }