1 package be
.nikiroo
.fanfix
.library
;
3 import java
.io
.IOException
;
4 import java
.security
.InvalidParameterException
;
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
;
15 * Create a new remote server that will listen for order on the given port.
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).
20 * The key is always a String, the commands are also Strings; the parameters
21 * vary depending upon the command.
23 * <li>[key] GET_METADATA *: will return the metadata of all the stories in the
25 * <li>[key] GET_STORY [luid]: will return the given story if it exists (or NULL
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>
38 public class RemoteLibraryServer
extends ServerObject
{
39 private final String key
;
42 * Create a new remote server (will not be active until
43 * {@link RemoteLibraryServer#start()} is called).
46 * the key that will restrict access to this server
48 * the port to listen on
51 * in case of I/O error
53 public RemoteLibraryServer(String key
, int port
) throws IOException
{
54 super("Fanfix remote library", port
, true);
59 protected Object
onRequest(ConnectActionServerObject action
,
60 Version clientVersion
, Object data
) throws Exception
{
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
];
72 key
= "" + dataArray
[0];
73 command
= "" + dataArray
[1];
77 System
.out
.print("[" + command
+ "] ");
78 for (Object arg
: args
) {
79 System
.out
.print(arg
+ " ");
81 System
.out
.println("");
83 if (!key
.equals(this.key
)) {
84 System
.out
.println("Key rejected.");
85 throw new SecurityException("Invalid key");
88 // TODO: progress (+send name + %age info back to client)
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
[] {});
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
)) {