Update nikiroo-utils, bugfixes:
[nikiroo-utils.git] / src / be / nikiroo / fanfix / library / RemoteLibraryServer.java
1 package be.nikiroo.fanfix.library;
2
3 import java.io.IOException;
4 import java.security.InvalidParameterException;
5 import java.util.List;
6
7 import be.nikiroo.fanfix.Instance;
8 import be.nikiroo.fanfix.data.MetaData;
9 import be.nikiroo.fanfix.data.Story;
10 import be.nikiroo.utils.Version;
11 import be.nikiroo.utils.serial.server.ConnectActionServerObject;
12 import be.nikiroo.utils.serial.server.ServerObject;
13
14 /**
15 * Create a new remote server that will listen for order on the given port.
16 * <p>
17 * The available commands are given as arrays of objects (first item is the key,
18 * second is the command, the rest are the arguments).
19 * <p>
20 * The key is always a String, the commands are also Strings; the parameters
21 * vary depending upon the command.
22 * <ul>
23 * <li>[key] GET_METADATA *: will return the metadata of all the stories in the
24 * library</li>
25 * <li>[key] GET_STORY [luid]: will return the given story if it exists (or NULL
26 * if not)</li>
27 * <li>[key] SAVE_STORY [story] [luid]: save the story with the given LUID</li>
28 * <li>[key] DELETE_STORY [luid]: delete the story of LUID luid</li>
29 * <li>[key] GET_COVER [luid]: return the cover of the story</li>
30 * <li>[key] GET_SOURCE_COVER [source]: return the cover for this source</li>
31 * <li>[key] SET_SOURCE_COVER [source], [luid]: set the default cover for the
32 * given source to the cover of the story denoted by luid</li>
33 * <li>[key] EXIT: stop the server</li>
34 * </ul>
35 *
36 * @author niki
37 */
38 public class RemoteLibraryServer extends ServerObject {
39 private final String key;
40
41 /**
42 * Create a new remote server (will not be active until
43 * {@link RemoteLibraryServer#start()} is called).
44 *
45 * @param key
46 * the key that will restrict access to this server
47 * @param port
48 * the port to listen on
49 *
50 * @throws IOException
51 * in case of I/O error
52 */
53 public RemoteLibraryServer(String key, int port) throws IOException {
54 super(port, true);
55 this.key = key;
56 }
57
58 @Override
59 protected Object onRequest(ConnectActionServerObject action,
60 Version clientVersion, Object data) throws Exception {
61 String key = "";
62 String command = "";
63 Object[] args = new Object[0];
64 if (data instanceof Object[]) {
65 Object[] dataArray = (Object[]) data;
66 if (dataArray.length >= 2) {
67 args = new Object[dataArray.length - 2];
68 for (int i = 2; i < dataArray.length; i++) {
69 args[i - 2] = dataArray[i];
70 }
71
72 key = "" + dataArray[0];
73 command = "" + dataArray[1];
74 }
75 }
76
77 System.out.print("[" + command + "] ");
78 for (Object arg : args) {
79 System.out.print(arg + " ");
80 }
81 System.out.println("");
82
83 if (!key.equals(this.key)) {
84 System.out.println("Key rejected.");
85 throw new SecurityException("Invalid key");
86 }
87
88 // TODO: progress (+send name + %age info back to client)
89
90 if ("GET_METADATA".equals(command)) {
91 if (args[0].equals("*")) {
92 List<MetaData> metas = Instance.getLibrary().getMetas(null);
93 return metas.toArray(new MetaData[] {});
94 }
95 throw new InvalidParameterException(
96 "only * is valid here, but you passed: " + args[0]);
97 } else if ("GET_STORY".equals(command)) {
98 return Instance.getLibrary().getStory("" + args[0], null);
99 } else if ("SAVE_STORY".equals(command)) {
100 Instance.getLibrary().save((Story) args[0], "" + args[1], null);
101 } else if ("DELETE_STORY".equals(command)) {
102 Instance.getLibrary().delete("" + args[0]);
103 } else if ("GET_COVER".equals(command)) {
104 return Instance.getLibrary().getCover("" + args[0]);
105 } else if ("GET_SOURCE_COVER".equals(command)) {
106 return Instance.getLibrary().getSourceCover("" + args[0]);
107 } else if ("SET_SOURCE_COVER".equals(command)) {
108 Instance.getLibrary().setSourceCover("" + args[0], "" + args[1]);
109 } else if ("EXIT".equals(command)) {
110 stop(0, false);
111 }
112
113 return null;
114 }
115 }