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
{
30 private final String key
;
31 private final String subkey
;
33 // informative only (server will make the actual checks)
36 // TODO: error handling is not up to par!
39 * Create a {@link RemoteLibrary} linked to the given server.
41 * Note that the key is structured:
42 * <tt><b><i>xxx</i></b>(|<b><i>yyy</i></b>|<b>wl</b>)(|<b>rw</b>)</tt>
44 * Note that anything before the first pipe (<tt>|</tt>) character is
45 * considered to be the encryption key, anything after that character is
46 * called the subkey (including the other pipe characters and flags!).
48 * This is important because the subkey (including the pipe characters and
49 * flags) must be present as-is in the server configuration file to be
52 * <li><b><i>xxx</i></b>: the encryption key used to communicate with the
54 * <li><b><i>yyy</i></b>: the secondary key</li>
55 * <li><b>rw</b>: flag to allow read and write access if it is not the
56 * default on this server</li>
57 * <li><b>wl</b>: flag to allow access to all the stories (bypassing the
58 * whitelist if it exists)</li>
63 * <li><b>my_key</b>: normal connection, will take the default server
65 * <li><b>my_key|agzyzz|wl</b>: will ask to bypass the white list (if it
67 * <li><b>my_key|agzyzz|rw</b>: will ask read-write access (if the default
69 * <li><b>my_key|agzyzz|wl|rw</b>: will ask both read-write access and white
74 * the key that will allow us to exchange information with the
77 * the host to contact or NULL for localhost
79 * the port to contact it on
81 public RemoteLibrary(String key
, String host
, int port
) {
84 index
= key
.indexOf('|');
88 this.key
= key
.substring(0, index
);
89 this.subkey
= key
.substring(index
+ 1);
100 public String
getLibraryName() {
101 return host
+ ":" + port
;
105 public Status
getStatus() {
106 Instance
.getTraceHandler().trace("Getting remote lib status...");
107 Status status
= getStatusDo();
108 Instance
.getTraceHandler().trace("Remote lib status: " + status
);
112 private Status
getStatusDo() {
113 final Status
[] result
= new Status
[1];
115 result
[0] = Status
.INVALID
;
118 new ConnectActionClientObject(host
, port
, key
) {
120 public void action(Version serverVersion
) throws Exception
{
121 Object rep
= send(new Object
[] { subkey
, "PING" });
123 if ("r/w".equals(rep
)) {
125 result
[0] = Status
.READY
;
126 } else if ("r/o".equals(rep
)) {
128 result
[0] = Status
.READY
;
130 result
[0] = Status
.UNAUTHORIZED
;
135 protected void onError(Exception e
) {
136 if (e
instanceof SSLException
) {
137 result
[0] = Status
.UNAUTHORIZED
;
139 result
[0] = Status
.UNAVAILABLE
;
143 } catch (UnknownHostException e
) {
144 result
[0] = Status
.INVALID
;
145 } catch (IllegalArgumentException e
) {
146 result
[0] = Status
.INVALID
;
147 } catch (Exception e
) {
148 result
[0] = Status
.UNAVAILABLE
;
155 public Image
getCover(final String luid
) {
156 final Image
[] result
= new Image
[1];
159 new ConnectActionClientObject(host
, port
, key
) {
161 public void action(Version serverVersion
) throws Exception
{
162 Object rep
= send(new Object
[] { subkey
, "GET_COVER", luid
});
163 result
[0] = (Image
) rep
;
167 protected void onError(Exception e
) {
168 if (e
instanceof SSLException
) {
169 Instance
.getTraceHandler().error(
170 "Connection refused (bad key)");
172 Instance
.getTraceHandler().error(e
);
176 } catch (Exception e
) {
177 Instance
.getTraceHandler().error(e
);
184 public Image
getCustomSourceCover(final String source
) {
185 return getCustomCover(source
, "SOURCE");
189 public Image
getCustomAuthorCover(final String author
) {
190 return getCustomCover(author
, "AUTHOR");
193 // type: "SOURCE" or "AUTHOR"
194 private Image
getCustomCover(final String source
, final String type
) {
195 final Image
[] result
= new Image
[1];
198 new ConnectActionClientObject(host
, port
, key
) {
200 public void action(Version serverVersion
) throws Exception
{
201 Object rep
= send(new Object
[] { subkey
,
202 "GET_CUSTOM_COVER", type
, source
});
203 result
[0] = (Image
) rep
;
207 protected void onError(Exception e
) {
208 if (e
instanceof SSLException
) {
209 Instance
.getTraceHandler().error(
210 "Connection refused (bad key)");
212 Instance
.getTraceHandler().error(e
);
216 } catch (Exception e
) {
217 Instance
.getTraceHandler().error(e
);
224 public synchronized Story
getStory(final String luid
, Progress pg
) {
225 final Progress pgF
= pg
;
226 final Story
[] result
= new Story
[1];
229 new ConnectActionClientObject(host
, port
, key
) {
231 public void action(Version serverVersion
) throws Exception
{
237 Object rep
= send(new Object
[] { subkey
, "GET_STORY", luid
});
239 MetaData meta
= null;
240 if (rep
instanceof MetaData
) {
241 meta
= (MetaData
) rep
;
242 if (meta
.getWords() <= Integer
.MAX_VALUE
) {
243 pg
.setMinMax(0, (int) meta
.getWords());
247 List
<Object
> list
= new ArrayList
<Object
>();
248 for (Object obj
= send(null); obj
!= null; obj
= send(null)) {
253 result
[0] = RemoteLibraryServer
.rebuildStory(list
);
258 protected void onError(Exception e
) {
259 if (e
instanceof SSLException
) {
260 Instance
.getTraceHandler().error(
261 "Connection refused (bad key)");
263 Instance
.getTraceHandler().error(e
);
267 } catch (Exception e
) {
268 Instance
.getTraceHandler().error(e
);
275 public synchronized Story
save(final Story story
, final String luid
,
276 Progress pg
) throws IOException
{
278 final String
[] luidSaved
= new String
[1];
279 Progress pgSave
= new Progress();
280 Progress pgRefresh
= new Progress();
286 pg
.addProgress(pgSave
, 9);
287 pg
.addProgress(pgRefresh
, 1);
289 final Progress pgF
= pgSave
;
291 new ConnectActionClientObject(host
, port
, key
) {
293 public void action(Version serverVersion
) throws Exception
{
295 if (story
.getMeta().getWords() <= Integer
.MAX_VALUE
) {
296 pg
.setMinMax(0, (int) story
.getMeta().getWords());
299 send(new Object
[] { subkey
, "SAVE_STORY", luid
});
301 List
<Object
> list
= RemoteLibraryServer
.breakStory(story
);
302 for (Object obj
: list
) {
307 luidSaved
[0] = (String
) send(null);
313 protected void onError(Exception e
) {
314 if (e
instanceof SSLException
) {
315 Instance
.getTraceHandler().error(
316 "Connection refused (bad key)");
318 Instance
.getTraceHandler().error(e
);
323 // because the meta changed:
324 MetaData meta
= getInfo(luidSaved
[0]);
325 if (story
.getMeta().getClass() != null) {
326 // If already available locally:
327 meta
.setCover(story
.getMeta().getCover());
330 meta
.setCover(getCover(meta
.getLuid()));
340 public synchronized void delete(final String luid
) throws IOException
{
341 new ConnectActionClientObject(host
, port
, key
) {
343 public void action(Version serverVersion
) throws Exception
{
344 send(new Object
[] { subkey
, "DELETE_STORY", luid
});
348 protected void onError(Exception e
) {
349 if (e
instanceof SSLException
) {
350 Instance
.getTraceHandler().error(
351 "Connection refused (bad key)");
353 Instance
.getTraceHandler().error(e
);
360 public void setSourceCover(final String source
, final String luid
) {
361 setCover(source
, luid
, "SOURCE");
365 public void setAuthorCover(final String author
, final String luid
) {
366 setCover(author
, luid
, "AUTHOR");
369 // type = "SOURCE" | "AUTHOR"
370 private void setCover(final String value
, final String luid
,
373 new ConnectActionClientObject(host
, port
, key
) {
375 public void action(Version serverVersion
) throws Exception
{
376 send(new Object
[] { subkey
, "SET_COVER", type
, value
, luid
});
380 protected void onError(Exception e
) {
381 if (e
instanceof SSLException
) {
382 Instance
.getTraceHandler().error(
383 "Connection refused (bad key)");
385 Instance
.getTraceHandler().error(e
);
389 } catch (IOException e
) {
390 Instance
.getTraceHandler().error(e
);
395 // Could work (more slowly) without it
396 public Story
imprt(final URL url
, Progress pg
) throws IOException
{
397 // Import the file locally if it is actually a file
398 if (url
== null || url
.getProtocol().equalsIgnoreCase("file")) {
399 return super.imprt(url
, pg
);
402 // Import it remotely if it is an URL
409 Progress pgImprt
= new Progress();
410 Progress pgGet
= new Progress();
411 pg
.addProgress(pgImprt
, 1);
412 pg
.addProgress(pgGet
, 1);
414 final Progress pgF
= pgImprt
;
415 final String
[] luid
= new String
[1];
418 new ConnectActionClientObject(host
, port
, key
) {
420 public void action(Version serverVersion
) throws Exception
{
423 Object rep
= send(new Object
[] { subkey
, "IMPORT",
427 if (!RemoteLibraryServer
.updateProgress(pg
, rep
)) {
435 luid
[0] = (String
) rep
;
439 protected void onError(Exception e
) {
440 if (e
instanceof SSLException
) {
441 Instance
.getTraceHandler().error(
442 "Connection refused (bad key)");
444 Instance
.getTraceHandler().error(e
);
448 } catch (IOException e
) {
449 Instance
.getTraceHandler().error(e
);
452 if (luid
[0] == null) {
453 throw new IOException("Remote failure");
456 Story story
= getStory(luid
[0], pgGet
);
464 // Could work (more slowly) without it
465 protected synchronized void changeSTA(final String luid
,
466 final String newSource
, final String newTitle
,
467 final String newAuthor
, Progress pg
) throws IOException
{
469 final Progress pgF
= pg
== null ?
new Progress() : pg
;
472 new ConnectActionClientObject(host
, port
, key
) {
474 public void action(Version serverVersion
) throws Exception
{
477 Object rep
= send(new Object
[] { subkey
, "CHANGE_STA",
478 luid
, newSource
, newTitle
, newAuthor
});
480 if (!RemoteLibraryServer
.updateProgress(pg
, rep
)) {
489 protected void onError(Exception e
) {
490 if (e
instanceof SSLException
) {
491 Instance
.getTraceHandler().error(
492 "Connection refused (bad key)");
494 Instance
.getTraceHandler().error(e
);
498 } catch (IOException e
) {
499 Instance
.getTraceHandler().error(e
);
504 public synchronized File
getFile(final String luid
, Progress pg
) {
505 throw new java
.lang
.InternalError(
506 "Operation not supportorted on remote Libraries");
514 new ConnectActionClientObject(host
, port
, key
) {
516 public void action(Version serverVersion
) throws Exception
{
517 send(new Object
[] { subkey
, "EXIT" });
521 protected void onError(Exception e
) {
522 if (e
instanceof SSLException
) {
523 Instance
.getTraceHandler().error(
524 "Connection refused (bad key)");
526 Instance
.getTraceHandler().error(e
);
530 } catch (IOException e
) {
531 Instance
.getTraceHandler().error(e
);
536 public synchronized MetaData
getInfo(String luid
) {
537 List
<MetaData
> metas
= getMetasList(luid
, null);
538 if (!metas
.isEmpty()) {
546 protected List
<MetaData
> getMetas(Progress pg
) {
547 return getMetasList("*", pg
);
551 protected void updateInfo(MetaData meta
) {
552 // Will be taken care of directly server side
556 protected void invalidateInfo(String luid
) {
557 // Will be taken care of directly server side
560 // The following methods are only used by Save and Delete in BasicLibrary:
563 protected int getNextId() {
564 throw new java
.lang
.InternalError("Should not have been called");
568 protected void doDelete(String luid
) throws IOException
{
569 throw new java
.lang
.InternalError("Should not have been called");
573 protected Story
doSave(Story story
, Progress pg
) throws IOException
{
574 throw new java
.lang
.InternalError("Should not have been called");
580 * Return the meta of the given story or a list of all known metas if the
583 * Will not get the covers.
586 * the luid of the story or *
588 * the optional progress
593 private List
<MetaData
> getMetasList(final String luid
, Progress pg
) {
594 final Progress pgF
= pg
;
595 final List
<MetaData
> metas
= new ArrayList
<MetaData
>();
598 new ConnectActionClientObject(host
, port
, key
) {
600 public void action(Version serverVersion
) throws Exception
{
606 Object rep
= send(new Object
[] { subkey
, "GET_METADATA",
610 if (!RemoteLibraryServer
.updateProgress(pg
, rep
)) {
617 if (rep
instanceof MetaData
[]) {
618 for (MetaData meta
: (MetaData
[]) rep
) {
621 } else if (rep
!= null) {
622 metas
.add((MetaData
) rep
);
627 protected void onError(Exception e
) {
628 if (e
instanceof SSLException
) {
629 Instance
.getTraceHandler().error(
630 "Connection refused (bad key)");
632 Instance
.getTraceHandler().error(e
);
636 } catch (Exception e
) {
637 Instance
.getTraceHandler().error(e
);