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