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