1 package be
.nikiroo
.fanfix
.library
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
7 import java
.util
.ArrayList
;
8 import java
.util
.HashMap
;
12 import org
.json
.JSONArray
;
13 import org
.json
.JSONObject
;
15 import be
.nikiroo
.fanfix
.Instance
;
16 import be
.nikiroo
.fanfix
.data
.JsonIO
;
17 import be
.nikiroo
.fanfix
.data
.MetaData
;
18 import be
.nikiroo
.fanfix
.data
.Story
;
19 import be
.nikiroo
.utils
.IOUtils
;
20 import be
.nikiroo
.utils
.Image
;
21 import be
.nikiroo
.utils
.Progress
;
24 * This {@link BasicLibrary} will access a remote server to list the available
25 * stories, and download the ones you try to load to the local directory
26 * specified in the configuration.
28 * This remote library uses http:// or https://.
32 public class WebLibrary
extends BasicLibrary
{
35 private final String key
;
36 private final String subkey
;
38 // informative only (server will make the actual checks)
42 * Create a {@link RemoteLibrary} linked to the given server.
44 * Note that the key is structured:
45 * <tt><b><i>xxx</i></b>(|<b><i>yyy</i></b>|<b>wl</b>)(|<b>rw</b>)</tt>
47 * Note that anything before the first pipe (<tt>|</tt>) character is
48 * considered to be the encryption key, anything after that character is
49 * called the subkey (including the other pipe characters and flags!).
51 * This is important because the subkey (including the pipe characters and
52 * flags) must be present as-is in the server configuration file to be
55 * <li><b><i>xxx</i></b>: the encryption key used to communicate with the
57 * <li><b><i>yyy</i></b>: the secondary key</li>
58 * <li><b>rw</b>: flag to allow read and write access if it is not the
59 * default on this server</li>
60 * <li><b>wl</b>: flag to allow access to all the stories (bypassing the
61 * whitelist if it exists)</li>
66 * <li><b>my_key</b>: normal connection, will take the default server
68 * <li><b>my_key|agzyzz|wl</b>: will ask to bypass the white list (if it
70 * <li><b>my_key|agzyzz|rw</b>: will ask read-write access (if the default
72 * <li><b>my_key|agzyzz|wl|rw</b>: will ask both read-write access and white
77 * the key that will allow us to exchange information with the
80 * the host to contact or NULL for localhost
82 * the port to contact it on
84 public WebLibrary(String key
, String host
, int port
) {
87 index
= key
.indexOf('|');
91 this.key
= key
.substring(0, index
);
92 this.subkey
= key
.substring(index
+ 1);
98 this.rw
= subkey
.contains("|rw");
103 // TODO: not supported yet
108 public Status
getStatus() {
111 } catch (IOException e
) {
113 download("/style.css");
114 return Status
.UNAUTHORIZED
;
115 } catch (IOException ioe
) {
116 return Status
.INVALID
;
120 return rw ? Status
.READ_WRITE
: Status
.READ_ONLY
;
124 public String
getLibraryName() {
125 return (rw ?
"[READ-ONLY] " : "") + host
+ ":" + port
;
129 public Image
getCover(String luid
) throws IOException
{
130 InputStream in
= download("/story/" + luid
+ "/cover");
132 return new Image(in
);
139 public void setSourceCover(String source
, String luid
) throws IOException
{
140 // TODO Auto-generated method stub
141 throw new IOException("Not implemented yet");
145 public void setAuthorCover(String author
, String luid
) throws IOException
{
146 // TODO Auto-generated method stub
147 throw new IOException("Not implemented yet");
151 protected List
<MetaData
> getMetas(Progress pg
) throws IOException
{
152 List
<MetaData
> metas
= new ArrayList
<MetaData
>();
153 InputStream in
= download("/list/luids");
154 JSONArray jsonArr
= new JSONArray(IOUtils
.readSmallStream(in
));
155 for (int i
= 0; i
< jsonArr
.length(); i
++) {
156 JSONObject json
= jsonArr
.getJSONObject(i
);
157 metas
.add(JsonIO
.toMetaData(json
));
164 // Could work (more slowly) without it
165 public MetaData
imprt(final URL url
, Progress pg
) throws IOException
{
167 throw new IOException("Not implemented yet");
169 // Import the file locally if it is actually a file
171 if (url
== null || url
.getProtocol().equalsIgnoreCase("file")) {
172 return super.imprt(url
, pg
);
175 // Import it remotely if it is an URL
178 return super.imprt(url
, pg
);
182 // Could work (more slowly) without it
183 protected synchronized void changeSTA(final String luid
,
184 final String newSource
, final String newTitle
,
185 final String newAuthor
, Progress pg
) throws IOException
{
187 super.changeSTA(luid
, newSource
, newTitle
, newAuthor
, pg
);
191 protected void updateInfo(MetaData meta
) {
192 // Will be taken care of directly server side
196 protected void invalidateInfo(String luid
) {
197 // Will be taken care of directly server side
200 // The following methods are only used by Save and Delete in BasicLibrary:
203 protected int getNextId() {
204 throw new java
.lang
.InternalError("Should not have been called");
208 protected void doDelete(String luid
) throws IOException
{
209 throw new java
.lang
.InternalError("Should not have been called");
213 protected Story
doSave(Story story
, Progress pg
) throws IOException
{
214 throw new java
.lang
.InternalError("Should not have been called");
220 public File
getFile(final String luid
, Progress pg
) {
221 throw new java
.lang
.InternalError(
222 "Operation not supportorted on remote Libraries");
226 private InputStream
download(String path
) throws IOException
{
227 URL url
= new URL(host
+ ":" + port
+ path
);
229 Map
<String
, String
> post
= new HashMap
<String
, String
>();
230 post
.put("login", subkey
);
231 post
.put("password", key
);
233 return Instance
.getInstance().getCache().openNoCache(url
, null, post
,