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
.ConnectActionServer
;
12 import be
.nikiroo
.utils
.serial
.Server
;
15 * Create a new remote server that will listen for order on the given port.
17 * The available commands are given as String arrays (first item is the key,
18 * second is the command, the rest are the arguments):
20 * <li>KEY GET_METADATA *: will return the metadata of all the stories in the
22 * <li>KEY GET_STORY [luid]: will return the given story if it exists (or NULL
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>
35 public class RemoteLibraryServer
extends Server
{
36 private final String key
;
39 * Create a new remote server (will not be active until
40 * {@link RemoteLibraryServer#start()} is called).
43 * the key that will restrict access to this server
45 * the port to listen on
48 * in case of I/O error
50 public RemoteLibraryServer(String key
, int port
) throws IOException
{
56 protected Object
onRequest(ConnectActionServer action
,
57 Version clientVersion
, Object data
) throws Exception
{
60 Object
[] args
= new Object
[0];
61 if (data
instanceof Object
[]) {
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
++) {
66 args
[i
- 2] = dataArray
[i
];
69 key
= "" + dataArray
[0];
70 command
= "" + dataArray
[1];
74 System
.out
.print("[" + command
+ "] ");
75 for (Object arg
: args
) {
76 System
.out
.print(arg
+ " ");
78 System
.out
.println("");
80 if (!key
.equals(this.key
)) {
81 System
.out
.println("Key rejected.");
82 throw new SecurityException("Invalid key");
85 // TODO: progress (+send name + %age info back to client)
87 if ("GET_METADATA".equals(command
)) {
88 if (args
[0].equals("*")) {
89 List
<MetaData
> metas
= Instance
.getLibrary().getMetas(null);
90 return metas
.toArray(new MetaData
[] {});
92 throw new InvalidParameterException(
93 "only * is valid here, but you passed: " + args
[0]);
94 } else if ("GET_STORY".equals(command
)) {
95 return Instance
.getLibrary().getStory("" + args
[0], null);
96 } else if ("SAVE_STORY".equals(command
)) {
97 Instance
.getLibrary().save((Story
) args
[0], "" + args
[1], null);
98 } else if ("DELETE_STORY".equals(command
)) {
99 Instance
.getLibrary().delete("" + args
[0]);
100 } else if ("GET_COVER".equals(command
)) {
101 return Instance
.getLibrary().getCover("" + args
[0]);
102 } else if ("GET_SOURCE_COVER".equals(command
)) {
103 return Instance
.getLibrary().getSourceCover("" + args
[0]);
104 } else if ("SET_SOURCE_COVER".equals(command
)) {
105 Instance
.getLibrary().setSourceCover("" + args
[0], "" + args
[1]);
106 } else if ("EXIT".equals(command
)) {