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