RemoteLibrary: also forward the meta
[nikiroo-utils.git] / library / RemoteLibraryServer.java
CommitLineData
e42573a0 1package be.nikiroo.fanfix.library;
b0e88ebd 2
b0e88ebd 3import java.io.IOException;
edf79e5e 4import java.net.URL;
74a40dfb 5import java.util.ArrayList;
9b863b20 6import java.util.Date;
9b558341 7import java.util.HashMap;
68e2c6d2 8import java.util.List;
9b558341 9import java.util.Map;
b0e88ebd 10
7e51d91c
NR
11import javax.net.ssl.SSLException;
12
e42573a0 13import be.nikiroo.fanfix.Instance;
fb25273c 14import be.nikiroo.fanfix.bundles.Config;
74a40dfb 15import be.nikiroo.fanfix.data.Chapter;
b0e88ebd 16import be.nikiroo.fanfix.data.MetaData;
74a40dfb 17import be.nikiroo.fanfix.data.Paragraph;
085a2f9a 18import be.nikiroo.fanfix.data.Story;
b9ce9cad
NR
19import be.nikiroo.utils.Progress;
20import be.nikiroo.utils.Progress.ProgressListener;
416c54f8 21import be.nikiroo.utils.StringUtils;
fb25273c 22import be.nikiroo.utils.Version;
62c63b07
NR
23import be.nikiroo.utils.serial.server.ConnectActionServerObject;
24import be.nikiroo.utils.serial.server.ServerObject;
b0e88ebd 25
a85e8077 26/**
ea734ab4 27 * Create a new remote server that will listen for orders on the given port.
a85e8077 28 * <p>
ea734ab4
N
29 * The available commands are given as arrays of objects (first item is the
30 * command, the rest are the arguments).
2a25f781 31 * <p>
fb25273c
NR
32 * All the commands are always prefixed by the subkey (which can be EMPTY if
33 * none).
34 * <p>
a85e8077 35 * <ul>
fb25273c
NR
36 * <li>PING: will return the mode if the key is accepted (mode can be: "r/o" or
37 * "r/w")</li>
ea734ab4
N
38 * <li>GET_METADATA *: will return the metadata of all the stories in the
39 * library (array)</li> *
37abe20c 40 * <li>GET_METADATA [luid]: will return the metadata of the story of LUID luid</li>
ea734ab4
N
41 * <li>GET_STORY [luid]: will return the given story if it exists (or NULL if
42 * not)</li>
43 * <li>SAVE_STORY [luid]: save the story (that must be sent just after the
0fa0fe95 44 * command) with the given LUID, then return the LUID</li>
37abe20c
NR
45 * <li>IMPORT [url]: save the story found at the given URL, then return the LUID
46 * </li>
ea734ab4
N
47 * <li>DELETE_STORY [luid]: delete the story of LUID luid</li>
48 * <li>GET_COVER [luid]: return the cover of the story</li>
49 * <li>GET_CUSTOM_COVER ["SOURCE"|"AUTHOR"] [source]: return the cover for this
50 * source/author</li>
51 * <li>SET_COVER ["SOURCE"|"AUTHOR"] [value] [luid]: set the default cover for
52 * the given source/author to the cover of the story denoted by luid</li>
53 * <li>CHANGE_SOURCE [luid] [new source]: change the source of the story of LUID
54 * luid</li>
55 * <li>EXIT: stop the server</li>
a85e8077
NR
56 * </ul>
57 *
58 * @author niki
59 */
62c63b07 60public class RemoteLibraryServer extends ServerObject {
9b558341
NR
61 private Map<Long, String> commands = new HashMap<Long, String>();
62 private Map<Long, Long> times = new HashMap<Long, Long>();
fb25273c
NR
63 private Map<Long, Boolean> wls = new HashMap<Long, Boolean>();
64 private Map<Long, Boolean> rws = new HashMap<Long, Boolean>();
9b558341 65
a85e8077
NR
66 /**
67 * Create a new remote server (will not be active until
68 * {@link RemoteLibraryServer#start()} is called).
fb25273c
NR
69 * <p>
70 * Note: the key we use here is the encryption key (it must not contain a
71 * subkey).
a85e8077 72 *
2070ced5
NR
73 * @param key
74 * the key that will restrict access to this server
a85e8077
NR
75 * @param port
76 * the port to listen on
77 *
78 * @throws IOException
79 * in case of I/O error
80 */
2070ced5 81 public RemoteLibraryServer(String key, int port) throws IOException {
3040c4f0 82 super("Fanfix remote library", port, key);
d66deb8d 83 setTraceHandler(Instance.getInstance().getTraceHandler());
b0e88ebd
NR
84 }
85
86 @Override
fb25273c
NR
87 protected Object onRequest(ConnectActionServerObject action,
88 Version clientVersion, Object data, long id) throws Exception {
ea734ab4
N
89 long start = new Date().getTime();
90
fb25273c
NR
91 // defaults are positive (as previous versions without the feature)
92 boolean rw = true;
93 boolean wl = true;
94
95 String subkey = "";
085a2f9a
NR
96 String command = "";
97 Object[] args = new Object[0];
98 if (data instanceof Object[]) {
2070ced5 99 Object[] dataArray = (Object[]) data;
27eba894 100 if (dataArray.length > 0) {
fb25273c
NR
101 subkey = "" + dataArray[0];
102 }
103 if (dataArray.length > 1) {
104 command = "" + dataArray[1];
105
106 args = new Object[dataArray.length - 2];
107 for (int i = 2; i < dataArray.length; i++) {
108 args[i - 2] = dataArray[i];
109 }
110 }
111 }
112
d66deb8d 113 List<String> whitelist = Instance.getInstance().getConfig().getList(Config.SERVER_WHITELIST);
fb25273c
NR
114 if (whitelist == null) {
115 whitelist = new ArrayList<String>();
116 }
117
118 if (whitelist.isEmpty()) {
119 wl = false;
120 }
edf79e5e 121
d66deb8d 122 rw = Instance.getInstance().getConfig().getBoolean(Config.SERVER_RW, rw);
fb25273c 123 if (!subkey.isEmpty()) {
d66deb8d 124 List<String> allowed = Instance.getInstance().getConfig().getList(Config.SERVER_ALLOWED_SUBKEYS);
fb25273c
NR
125 if (allowed.contains(subkey)) {
126 if ((subkey + "|").contains("|rw|")) {
127 rw = true;
128 }
129 if ((subkey + "|").contains("|wl|")) {
130 wl = false; // |wl| = bypass whitelist
651072f3 131 whitelist = new ArrayList<String>();
2070ced5 132 }
b0e88ebd
NR
133 }
134 }
135
fb25273c
NR
136 String mode = display(wl, rw);
137
138 String trace = mode + "[ " + command + "] ";
085a2f9a 139 for (Object arg : args) {
b9ce9cad 140 trace += arg + " ";
085a2f9a 141 }
c1bb921e
NR
142 long now = System.currentTimeMillis();
143 System.out.println(StringUtils.fromTime(now) + ": " + trace);
b0e88ebd 144
c1b31971 145 Object rep = null;
c1b31971
NR
146 try {
147 rep = doRequest(action, command, args, rw, whitelist);
5db598bc 148 } catch (IOException e) {
533dc2b8 149 rep = new RemoteLibraryException(e, true);
c1b31971 150 }
9f51d8ab 151
9b558341 152 commands.put(id, command);
fb25273c
NR
153 wls.put(id, wl);
154 rws.put(id, rw);
9b558341 155 times.put(id, (new Date().getTime() - start));
9f51d8ab
NR
156
157 return rep;
158 }
159
fb25273c
NR
160 private String display(boolean whitelist, boolean rw) {
161 String mode = "";
162 if (!rw) {
163 mode += "RO: ";
164 }
165 if (whitelist) {
166 mode += "WL: ";
167 }
168
169 return mode;
170 }
171
9b558341
NR
172 @Override
173 protected void onRequestDone(long id, long bytesReceived, long bytesSent) {
fb25273c
NR
174 boolean whitelist = wls.get(id);
175 boolean rw = rws.get(id);
176 wls.remove(id);
177 rws.remove(id);
178
9b558341
NR
179 String rec = StringUtils.formatNumber(bytesReceived) + "b";
180 String sent = StringUtils.formatNumber(bytesSent) + "b";
c1bb921e
NR
181 long now = System.currentTimeMillis();
182 System.out.println(StringUtils.fromTime(now)
183 + ": "
184 + String.format("%s[>%s]: (%s sent, %s rec) in %d ms",
185 display(whitelist, rw), commands.get(id), sent, rec,
186 times.get(id)));
fb25273c 187
9b558341
NR
188 commands.remove(id);
189 times.remove(id);
190 }
191
9f51d8ab 192 private Object doRequest(ConnectActionServerObject action, String command,
fb25273c
NR
193 Object[] args, boolean rw, List<String> whitelist)
194 throws NoSuchFieldException, NoSuchMethodException,
9f51d8ab 195 ClassNotFoundException, IOException {
3bbc86a5 196 if ("PING".equals(command)) {
fb25273c 197 return rw ? "r/w" : "r/o";
3bbc86a5 198 } else if ("GET_METADATA".equals(command)) {
651072f3
NR
199 List<MetaData> metas = new ArrayList<MetaData>();
200
7efece85 201 if ("*".equals(args[0])) {
9b863b20 202 Progress pg = createPgForwarder(action);
9f51d8ab 203
d66deb8d 204 for (MetaData meta : Instance.getInstance().getLibrary().getMetas(pg)) {
a5d1f0e6 205 metas.add(removeCover(meta));
9f51d8ab
NR
206 }
207
9b863b20 208 forcePgDoneSent(pg);
651072f3 209 } else {
d66deb8d 210 MetaData meta = Instance.getInstance().getLibrary().getInfo((String) args[0]);
210465c3
NR
211 MetaData light;
212 if (meta.getCover() == null) {
213 light = meta;
214 } else {
215 light = meta.clone();
216 light.setCover(null);
217 }
218
219 metas.add(light);
651072f3
NR
220 }
221
222 if (!whitelist.isEmpty()) {
223 for (int i = 0; i < metas.size(); i++) {
224 if (!whitelist.contains(metas.get(i).getSource())) {
225 metas.remove(i);
226 i--;
227 }
228 }
a85e8077 229 }
e272f05f 230
651072f3 231 return metas.toArray(new MetaData[0]);
a85e8077 232 } else if ("GET_STORY".equals(command)) {
d66deb8d 233 MetaData meta = Instance.getInstance().getLibrary().getInfo((String) args[0]);
651072f3
NR
234 if (meta == null) {
235 return null;
236 }
237
238 if (!whitelist.isEmpty()) {
239 if (!whitelist.contains(meta.getSource())) {
240 return null;
241 }
242 }
243
b9ce9cad
NR
244 meta = meta.clone();
245 meta.setCover(null);
246
247 action.send(meta);
248 action.rec();
249
d66deb8d 250 Story story = Instance.getInstance().getLibrary().getStory((String) args[0], null);
b9ce9cad
NR
251 for (Object obj : breakStory(story)) {
252 action.send(obj);
253 action.rec();
254 }
085a2f9a 255 } else if ("SAVE_STORY".equals(command)) {
651072f3 256 if (!rw) {
533dc2b8
NR
257 throw new RemoteLibraryException("Read-Only remote library: "
258 + args[0], false);
651072f3
NR
259 }
260
b9ce9cad
NR
261 List<Object> list = new ArrayList<Object>();
262
263 action.send(null);
264 Object obj = action.rec();
265 while (obj != null) {
266 list.add(obj);
267 action.send(null);
268 obj = action.rec();
269 }
270
271 Story story = rebuildStory(list);
d66deb8d 272 Instance.getInstance().getLibrary().save(story, (String) args[0], null);
0fa0fe95 273 return story.getMeta().getLuid();
edf79e5e 274 } else if ("IMPORT".equals(command)) {
651072f3 275 if (!rw) {
533dc2b8
NR
276 throw new RemoteLibraryException("Read-Only remote library: "
277 + args[0], false);
651072f3
NR
278 }
279
9b863b20 280 Progress pg = createPgForwarder(action);
d66deb8d 281 MetaData meta = Instance.getInstance().getLibrary().imprt(new URL((String) args[0]), pg);
9b863b20 282 forcePgDoneSent(pg);
b6b65795 283 return meta.getLuid();
085a2f9a 284 } else if ("DELETE_STORY".equals(command)) {
651072f3 285 if (!rw) {
533dc2b8
NR
286 throw new RemoteLibraryException("Read-Only remote library: "
287 + args[0], false);
651072f3
NR
288 }
289
d66deb8d 290 Instance.getInstance().getLibrary().delete((String) args[0]);
e604986c 291 } else if ("GET_COVER".equals(command)) {
d66deb8d 292 return Instance.getInstance().getLibrary().getCover((String) args[0]);
3989dfc5
NR
293 } else if ("GET_CUSTOM_COVER".equals(command)) {
294 if ("SOURCE".equals(args[0])) {
d66deb8d 295 return Instance.getInstance().getLibrary().getCustomSourceCover((String) args[1]);
3989dfc5 296 } else if ("AUTHOR".equals(args[0])) {
d66deb8d 297 return Instance.getInstance().getLibrary().getCustomAuthorCover((String) args[1]);
3989dfc5
NR
298 } else {
299 return null;
300 }
301 } else if ("SET_COVER".equals(command)) {
651072f3 302 if (!rw) {
533dc2b8
NR
303 throw new RemoteLibraryException("Read-Only remote library: "
304 + args[0] + ", " + args[1], false);
651072f3
NR
305 }
306
3989dfc5 307 if ("SOURCE".equals(args[0])) {
d66deb8d 308 Instance.getInstance().getLibrary().setSourceCover((String) args[1], (String) args[2]);
3989dfc5 309 } else if ("AUTHOR".equals(args[0])) {
d66deb8d 310 Instance.getInstance().getLibrary().setAuthorCover((String) args[1], (String) args[2]);
3989dfc5 311 }
c8d48938 312 } else if ("CHANGE_STA".equals(command)) {
651072f3 313 if (!rw) {
d66deb8d 314 throw new RemoteLibraryException("Read-Only remote library: " + args[0] + ", " + args[1], false);
651072f3
NR
315 }
316
9b863b20 317 Progress pg = createPgForwarder(action);
d66deb8d
NR
318 Instance.getInstance().getLibrary().changeSTA((String) args[0], (String) args[1], (String) args[2],
319 (String) args[3], pg);
9b863b20 320 forcePgDoneSent(pg);
5e848e6a 321 } else if ("EXIT".equals(command)) {
651072f3 322 if (!rw) {
533dc2b8
NR
323 throw new RemoteLibraryException(
324 "Read-Only remote library: EXIT", false);
651072f3
NR
325 }
326
c08c6ca1 327 stop(10000, false);
b0e88ebd
NR
328 }
329
330 return null;
331 }
74a40dfb 332
b9ce9cad
NR
333 @Override
334 protected void onError(Exception e) {
7e51d91c 335 if (e instanceof SSLException) {
c1bb921e
NR
336 long now = System.currentTimeMillis();
337 System.out.println(StringUtils.fromTime(now) + ": "
338 + "[Client connection refused (bad key)]");
7e51d91c
NR
339 } else {
340 getTraceHandler().error(e);
341 }
b9ce9cad 342 }
74a40dfb 343
b9ce9cad
NR
344 /**
345 * Break a story in multiple {@link Object}s for easier serialisation.
346 *
347 * @param story
348 * the {@link Story} to break
349 *
350 * @return the list of {@link Object}s
351 */
352 static List<Object> breakStory(Story story) {
353 List<Object> list = new ArrayList<Object>();
74a40dfb
NR
354
355 story = story.clone();
b9ce9cad 356 list.add(story);
74a40dfb 357
b9ce9cad
NR
358 if (story.getMeta().isImageDocument()) {
359 for (Chapter chap : story) {
360 list.add(chap);
361 list.addAll(chap.getParagraphs());
362 chap.setParagraphs(new ArrayList<Paragraph>());
74a40dfb 363 }
b9ce9cad 364 story.setChapters(new ArrayList<Chapter>());
74a40dfb 365 }
74a40dfb 366
b9ce9cad
NR
367 return list;
368 }
74a40dfb 369
b9ce9cad
NR
370 /**
371 * Rebuild a story from a list of broke up {@link Story} parts.
372 *
373 * @param list
374 * the list of {@link Story} parts
375 *
376 * @return the reconstructed {@link Story}
377 */
378 static Story rebuildStory(List<Object> list) {
74a40dfb 379 Story story = null;
b9ce9cad 380 Chapter chap = null;
74a40dfb 381
b9ce9cad
NR
382 for (Object obj : list) {
383 if (obj instanceof Story) {
384 story = (Story) obj;
385 } else if (obj instanceof Chapter) {
386 chap = (Chapter) obj;
387 story.getChapters().add(chap);
388 } else if (obj instanceof Paragraph) {
389 chap.getParagraphs().add((Paragraph) obj);
74a40dfb
NR
390 }
391 }
392
393 return story;
394 }
395
b9ce9cad
NR
396 /**
397 * Update the {@link Progress} with the adequate {@link Object} received
398 * from the network via {@link RemoteLibraryServer}.
399 *
400 * @param pg
401 * the {@link Progress} to update
402 * @param rep
403 * the object received from the network
404 *
405 * @return TRUE if it was a progress event, FALSE if not
406 */
407 static boolean updateProgress(Progress pg, Object rep) {
a5d1f0e6
NR
408 if (rep instanceof Object[]) {
409 Object[] a = (Object[]) rep;
410 if (a.length >= 3) {
411 int min = (Integer)a[0];
412 int max = (Integer)a[1];
413 int progress = (Integer)a[2];
b9ce9cad
NR
414
415 if (min >= 0 && min <= max) {
416 pg.setMinMax(min, max);
417 pg.setProgress(progress);
a5d1f0e6
NR
418 if (a.length >= 4) {
419 pg.put("meta", a[3]);
420 }
b9ce9cad
NR
421
422 return true;
423 }
424 }
74a40dfb 425 }
b9ce9cad
NR
426
427 return false;
74a40dfb
NR
428 }
429
b9ce9cad
NR
430 /**
431 * Create a {@link Progress} that will forward its progress over the
432 * network.
433 *
434 * @param action
435 * the {@link ConnectActionServerObject} to use to forward it
436 *
437 * @return the {@link Progress}
438 */
49f3dec5 439 private Progress createPgForwarder(final ConnectActionServerObject action) {
9b863b20
NR
440 final Boolean[] isDoneForwarded = new Boolean[] { false };
441 final Progress pg = new Progress() {
442 @Override
443 public boolean isDone() {
444 return isDoneForwarded[0];
445 }
446 };
447
b9ce9cad 448 final Integer[] p = new Integer[] { -1, -1, -1 };
a5d1f0e6 449 final Object[] pMeta = new MetaData[1];
9b863b20 450 final Long[] lastTime = new Long[] { new Date().getTime() };
b9ce9cad
NR
451 pg.addProgressListener(new ProgressListener() {
452 @Override
453 public void progress(Progress progress, String name) {
a5d1f0e6
NR
454 Object meta = pg.get("meta");
455 if (meta instanceof MetaData) {
456 meta = removeCover((MetaData)meta);
457 }
458
b9ce9cad
NR
459 int min = pg.getMin();
460 int max = pg.getMax();
a5d1f0e6 461 int rel = min
37abe20c
NR
462 + (int) Math.round(pg.getRelativeProgress()
463 * (max - min));
a5d1f0e6
NR
464
465 boolean samePg = p[0] == min && p[1] == max && p[2] == rel;
466
9b863b20
NR
467 // Do not re-send the same value twice over the wire,
468 // unless more than 2 seconds have elapsed (to maintain the
469 // connection)
a5d1f0e6 470 if (!samePg || !same(pMeta[0], meta) //
9b863b20 471 || (new Date().getTime() - lastTime[0] > 2000)) {
b9ce9cad
NR
472 p[0] = min;
473 p[1] = max;
a5d1f0e6
NR
474 p[2] = rel;
475 pMeta[0] = meta;
b9ce9cad
NR
476
477 try {
a5d1f0e6 478 action.send(new Object[] { min, max, rel, meta });
b9ce9cad
NR
479 action.rec();
480 } catch (Exception e) {
49f3dec5 481 getTraceHandler().error(e);
b9ce9cad 482 }
9b863b20 483
9b863b20 484 lastTime[0] = new Date().getTime();
b9ce9cad 485 }
652fd9b0
NR
486
487 isDoneForwarded[0] = (pg.getProgress() >= pg.getMax());
b9ce9cad
NR
488 }
489 });
490
491 return pg;
74a40dfb 492 }
a5d1f0e6
NR
493
494 private boolean same(Object obj1, Object obj2) {
495 if (obj1 == null || obj2 == null)
496 return obj1 == null && obj2 == null;
497
498 return obj1.equals(obj2);
499 }
9b863b20
NR
500
501 // with 30 seconds timeout
49f3dec5 502 private void forcePgDoneSent(Progress pg) {
9b863b20
NR
503 long start = new Date().getTime();
504 pg.done();
505 while (!pg.isDone() && new Date().getTime() - start < 30000) {
506 try {
507 Thread.sleep(100);
508 } catch (InterruptedException e) {
49f3dec5 509 getTraceHandler().error(e);
9b863b20
NR
510 }
511 }
512 }
a5d1f0e6
NR
513
514 private MetaData removeCover(MetaData meta) {
515 MetaData light;
516 if (meta.getCover() == null) {
517 light = meta;
518 } else {
519 light = meta.clone();
520 light.setCover(null);
521 }
522
523 return light;
524 }
b0e88ebd 525}