1 package be
.nikiroo
.fanfix
.library
;
4 import java
.io
.IOException
;
6 import java
.net
.UnknownHostException
;
7 import java
.util
.ArrayList
;
10 import javax
.net
.ssl
.SSLException
;
12 import be
.nikiroo
.fanfix
.Instance
;
13 import be
.nikiroo
.fanfix
.data
.MetaData
;
14 import be
.nikiroo
.fanfix
.data
.Story
;
15 import be
.nikiroo
.utils
.Image
;
16 import be
.nikiroo
.utils
.Progress
;
17 import be
.nikiroo
.utils
.Version
;
18 import be
.nikiroo
.utils
.serial
.server
.ConnectActionClientObject
;
21 * This {@link BasicLibrary} will access a remote server to list the available
22 * stories, and download the ones you try to load to the local directory
23 * specified in the configuration.
27 public class RemoteLibrary
extends BasicLibrary
{
28 interface RemoteAction
{
29 public void action(ConnectActionClientObject action
) throws Exception
;
32 class RemoteConnectAction
extends ConnectActionClientObject
{
33 public RemoteConnectAction() throws IOException
{
34 super(host
, port
, key
);
38 public Object
send(Object data
) throws IOException
,
39 NoSuchFieldException
, NoSuchMethodException
,
40 ClassNotFoundException
{
41 Object rep
= super.send(data
);
42 if (rep
instanceof RemoteLibraryException
) {
43 RemoteLibraryException remoteEx
= (RemoteLibraryException
) rep
;
44 throw remoteEx
.unwrapException();
53 private final String key
;
54 private final String subkey
;
56 // informative only (server will make the actual checks)
60 * Create a {@link RemoteLibrary} linked to the given server.
62 * Note that the key is structured:
63 * <tt><b><i>xxx</i></b>(|<b><i>yyy</i></b>|<b>wl</b>)(|<b>rw</b>)</tt>
65 * Note that anything before the first pipe (<tt>|</tt>) character is
66 * considered to be the encryption key, anything after that character is
67 * called the subkey (including the other pipe characters and flags!).
69 * This is important because the subkey (including the pipe characters and
70 * flags) must be present as-is in the server configuration file to be
73 * <li><b><i>xxx</i></b>: the encryption key used to communicate with the
75 * <li><b><i>yyy</i></b>: the secondary key</li>
76 * <li><b>rw</b>: flag to allow read and write access if it is not the
77 * default on this server</li>
78 * <li><b>wl</b>: flag to allow access to all the stories (bypassing the
79 * whitelist if it exists)</li>
84 * <li><b>my_key</b>: normal connection, will take the default server
86 * <li><b>my_key|agzyzz|wl</b>: will ask to bypass the white list (if it
88 * <li><b>my_key|agzyzz|rw</b>: will ask read-write access (if the default
90 * <li><b>my_key|agzyzz|wl|rw</b>: will ask both read-write access and white
95 * the key that will allow us to exchange information with the
98 * the host to contact or NULL for localhost
100 * the port to contact it on
102 public RemoteLibrary(String key
, String host
, int port
) {
105 index
= key
.indexOf('|');
109 this.key
= key
.substring(0, index
);
110 this.subkey
= key
.substring(index
+ 1);
121 public String
getLibraryName() {
122 return (rw ?
"[READ-ONLY] " : "") + host
+ ":" + port
;
126 public Status
getStatus() {
127 Instance
.getTraceHandler().trace("Getting remote lib status...");
128 Status status
= getStatusDo();
129 Instance
.getTraceHandler().trace("Remote lib status: " + status
);
133 private Status
getStatusDo() {
134 final Status
[] result
= new Status
[1];
136 result
[0] = Status
.INVALID
;
139 new RemoteConnectAction() {
141 public void action(Version serverVersion
) throws Exception
{
142 Object rep
= send(new Object
[] { subkey
, "PING" });
144 if ("r/w".equals(rep
)) {
146 result
[0] = Status
.READ_WRITE
;
147 } else if ("r/o".equals(rep
)) {
149 result
[0] = Status
.READ_ONLY
;
151 result
[0] = Status
.UNAUTHORIZED
;
156 protected void onError(Exception e
) {
157 if (e
instanceof SSLException
) {
158 result
[0] = Status
.UNAUTHORIZED
;
160 result
[0] = Status
.UNAVAILABLE
;
164 } catch (UnknownHostException e
) {
165 result
[0] = Status
.INVALID
;
166 } catch (IllegalArgumentException e
) {
167 result
[0] = Status
.INVALID
;
168 } catch (Exception e
) {
169 result
[0] = Status
.UNAVAILABLE
;
176 public Image
getCover(final String luid
) throws IOException
{
177 final Image
[] result
= new Image
[1];
179 connectRemoteAction(new RemoteAction() {
181 public void action(ConnectActionClientObject action
)
183 Object rep
= action
.send(new Object
[] { subkey
, "GET_COVER",
185 result
[0] = (Image
) rep
;
193 public Image
getCustomSourceCover(final String source
) throws IOException
{
194 return getCustomCover(source
, "SOURCE");
198 public Image
getCustomAuthorCover(final String author
) throws IOException
{
199 return getCustomCover(author
, "AUTHOR");
202 // type: "SOURCE" or "AUTHOR"
203 private Image
getCustomCover(final String source
, final String type
)
205 final Image
[] result
= new Image
[1];
207 connectRemoteAction(new RemoteAction() {
209 public void action(ConnectActionClientObject action
)
211 Object rep
= action
.send(new Object
[] { subkey
,
212 "GET_CUSTOM_COVER", type
, source
});
213 result
[0] = (Image
) rep
;
221 public synchronized Story
getStory(final String luid
, Progress pg
)
223 final Progress pgF
= pg
;
224 final Story
[] result
= new Story
[1];
226 connectRemoteAction(new RemoteAction() {
228 public void action(ConnectActionClientObject action
)
235 Object rep
= action
.send(new Object
[] { subkey
, "GET_STORY",
238 MetaData meta
= null;
239 if (rep
instanceof MetaData
) {
240 meta
= (MetaData
) rep
;
241 if (meta
.getWords() <= Integer
.MAX_VALUE
) {
242 pg
.setMinMax(0, (int) meta
.getWords());
246 List
<Object
> list
= new ArrayList
<Object
>();
247 for (Object obj
= action
.send(null); obj
!= null; obj
= action
253 result
[0] = RemoteLibraryServer
.rebuildStory(list
);
262 public synchronized Story
save(final Story story
, final String luid
,
263 Progress pg
) throws IOException
{
265 final String
[] luidSaved
= new String
[1];
266 Progress pgSave
= new Progress();
267 Progress pgRefresh
= new Progress();
273 pg
.addProgress(pgSave
, 9);
274 pg
.addProgress(pgRefresh
, 1);
276 final Progress pgF
= pgSave
;
278 connectRemoteAction(new RemoteAction() {
280 public void action(ConnectActionClientObject action
)
283 if (story
.getMeta().getWords() <= Integer
.MAX_VALUE
) {
284 pg
.setMinMax(0, (int) story
.getMeta().getWords());
287 action
.send(new Object
[] { subkey
, "SAVE_STORY", luid
});
289 List
<Object
> list
= RemoteLibraryServer
.breakStory(story
);
290 for (Object obj
: list
) {
295 luidSaved
[0] = (String
) action
.send(null);
301 // because the meta changed:
302 MetaData meta
= getInfo(luidSaved
[0]);
303 if (story
.getMeta().getClass() != null) {
304 // If already available locally:
305 meta
.setCover(story
.getMeta().getCover());
308 meta
.setCover(getCover(meta
.getLuid()));
318 public synchronized void delete(final String luid
) throws IOException
{
319 connectRemoteAction(new RemoteAction() {
321 public void action(ConnectActionClientObject action
)
323 action
.send(new Object
[] { subkey
, "DELETE_STORY", luid
});
329 public void setSourceCover(final String source
, final String luid
)
331 setCover(source
, luid
, "SOURCE");
335 public void setAuthorCover(final String author
, final String luid
)
337 setCover(author
, luid
, "AUTHOR");
340 // type = "SOURCE" | "AUTHOR"
341 private void setCover(final String value
, final String luid
,
342 final String type
) throws IOException
{
343 connectRemoteAction(new RemoteAction() {
345 public void action(ConnectActionClientObject action
)
347 action
.send(new Object
[] { subkey
, "SET_COVER", type
, value
,
354 // Could work (more slowly) without it
355 public Story
imprt(final URL url
, Progress pg
) throws IOException
{
356 // Import the file locally if it is actually a file
357 if (url
== null || url
.getProtocol().equalsIgnoreCase("file")) {
358 return super.imprt(url
, pg
);
361 // Import it remotely if it is an URL
368 Progress pgImprt
= new Progress();
369 Progress pgGet
= new Progress();
370 pg
.addProgress(pgImprt
, 1);
371 pg
.addProgress(pgGet
, 1);
373 final Progress pgF
= pgImprt
;
374 final String
[] luid
= new String
[1];
376 connectRemoteAction(new RemoteAction() {
378 public void action(ConnectActionClientObject action
)
382 Object rep
= action
.send(new Object
[] { subkey
, "IMPORT",
386 if (!RemoteLibraryServer
.updateProgress(pg
, rep
)) {
390 rep
= action
.send(null);
394 luid
[0] = (String
) rep
;
398 if (luid
[0] == null) {
399 throw new IOException("Remote failure");
402 Story story
= getStory(luid
[0], pgGet
);
410 // Could work (more slowly) without it
411 protected synchronized void changeSTA(final String luid
,
412 final String newSource
, final String newTitle
,
413 final String newAuthor
, Progress pg
) throws IOException
{
415 final Progress pgF
= pg
== null ?
new Progress() : pg
;
417 connectRemoteAction(new RemoteAction() {
419 public void action(ConnectActionClientObject action
)
423 Object rep
= action
.send(new Object
[] { subkey
, "CHANGE_STA",
424 luid
, newSource
, newTitle
, newAuthor
});
426 if (!RemoteLibraryServer
.updateProgress(pg
, rep
)) {
430 rep
= action
.send(null);
437 public synchronized File
getFile(final String luid
, Progress pg
) {
438 throw new java
.lang
.InternalError(
439 "Operation not supportorted on remote Libraries");
445 * @throws IOException
446 * in case of I/O error (including bad key)
448 public void exit() throws IOException
{
449 connectRemoteAction(new RemoteAction() {
451 public void action(ConnectActionClientObject action
)
453 action
.send(new Object
[] { subkey
, "EXIT" });
459 public synchronized MetaData
getInfo(String luid
) throws IOException
{
460 List
<MetaData
> metas
= getMetasList(luid
, null);
461 if (!metas
.isEmpty()) {
469 protected List
<MetaData
> getMetas(Progress pg
) throws IOException
{
470 return getMetasList("*", pg
);
474 protected void updateInfo(MetaData meta
) {
475 // Will be taken care of directly server side
479 protected void invalidateInfo(String luid
) {
480 // Will be taken care of directly server side
483 // The following methods are only used by Save and Delete in BasicLibrary:
486 protected int getNextId() {
487 throw new java
.lang
.InternalError("Should not have been called");
491 protected void doDelete(String luid
) throws IOException
{
492 throw new java
.lang
.InternalError("Should not have been called");
496 protected Story
doSave(Story story
, Progress pg
) throws IOException
{
497 throw new java
.lang
.InternalError("Should not have been called");
503 * Return the meta of the given story or a list of all known metas if the
506 * Will not get the covers.
509 * the luid of the story or *
511 * the optional progress
515 * @throws IOException
516 * in case of I/O error or bad key (SSLException)
518 private List
<MetaData
> getMetasList(final String luid
, Progress pg
)
520 final Progress pgF
= pg
;
521 final List
<MetaData
> metas
= new ArrayList
<MetaData
>();
523 connectRemoteAction(new RemoteAction() {
525 public void action(ConnectActionClientObject action
)
532 Object rep
= action
.send(new Object
[] { subkey
, "GET_METADATA",
536 if (!RemoteLibraryServer
.updateProgress(pg
, rep
)) {
540 rep
= action
.send(null);
543 if (rep
instanceof MetaData
[]) {
544 for (MetaData meta
: (MetaData
[]) rep
) {
547 } else if (rep
!= null) {
548 metas
.add((MetaData
) rep
);
556 private void connectRemoteAction(final RemoteAction runAction
)
558 final IOException
[] err
= new IOException
[1];
560 final RemoteConnectAction
[] array
= new RemoteConnectAction
[1];
561 RemoteConnectAction ra
= new RemoteConnectAction() {
563 public void action(Version serverVersion
) throws Exception
{
564 runAction
.action(array
[0]);
568 protected void onError(Exception e
) {
569 if (!(e
instanceof IOException
)) {
570 Instance
.getTraceHandler().error(e
);
574 err
[0] = (IOException
) e
;
579 } catch (Exception e
) {
580 err
[0] = (IOException
) e
;
583 if (err
[0] != null) {