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