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