1178e40c578b3bb2f2df46b3994de2b0770cc1ed
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
.Chapter
;
17 import be
.nikiroo
.fanfix
.data
.JsonIO
;
18 import be
.nikiroo
.fanfix
.data
.MetaData
;
19 import be
.nikiroo
.fanfix
.data
.Paragraph
;
20 import be
.nikiroo
.fanfix
.data
.Paragraph
.ParagraphType
;
21 import be
.nikiroo
.fanfix
.data
.Story
;
22 import be
.nikiroo
.utils
.IOUtils
;
23 import be
.nikiroo
.utils
.Image
;
24 import be
.nikiroo
.utils
.Progress
;
25 import be
.nikiroo
.utils
.Version
;
28 * This {@link BasicLibrary} will access a remote server to list the available
29 * stories, and download the ones you try to load to the local directory
30 * specified in the configuration.
32 * This remote library uses http:// or https://.
36 public class WebLibrary
extends BasicLibrary
{
39 private final String key
;
40 private final String subkey
;
42 // informative only (server will make the actual checks)
46 * Create a {@link RemoteLibrary} linked to the given server.
48 * Note that the key is structured:
49 * <tt><b><i>xxx</i></b>(|<b><i>yyy</i></b>|<b>wl</b>)(|<b>rw</b>)</tt>
51 * Note that anything before the first pipe (<tt>|</tt>) character is
52 * considered to be the encryption key, anything after that character is
53 * called the subkey (including the other pipe characters and flags!).
55 * This is important because the subkey (including the pipe characters and
56 * flags) must be present as-is in the server configuration file to be
59 * <li><b><i>xxx</i></b>: the encryption key used to communicate with the
61 * <li><b><i>yyy</i></b>: the secondary key</li>
62 * <li><b>rw</b>: flag to allow read and write access if it is not the
63 * default on this server</li>
64 * <li><b>wl</b>: flag to allow access to all the stories (bypassing the
65 * whitelist if it exists)</li>
70 * <li><b>my_key</b>: normal connection, will take the default server
72 * <li><b>my_key|agzyzz|wl</b>: will ask to bypass the white list (if it
74 * <li><b>my_key|agzyzz|rw</b>: will ask read-write access (if the default
76 * <li><b>my_key|agzyzz|wl|rw</b>: will ask both read-write access and white
81 * the key that will allow us to exchange information with the
84 * the host to contact or NULL for localhost
86 * the port to contact it on
88 public WebLibrary(String key
, String host
, int port
) {
91 index
= key
.indexOf('|');
95 this.key
= key
.substring(0, index
);
96 this.subkey
= key
.substring(index
+ 1);
102 this.rw
= subkey
.contains("|rw");
107 // TODO: not supported yet
111 public Version
getVersion() {
113 InputStream in
= download(WebLibraryUrls
.VERSION_URL
);
115 return new Version(IOUtils
.readSmallStream(in
));
119 } catch (IOException e
) {
126 public Status
getStatus() {
128 download(WebLibraryUrls
.INDEX_URL
).close();
129 } catch (IOException e
) {
131 download(WebLibraryUrls
.VERSION_URL
).close();
132 return Status
.UNAUTHORIZED
;
133 } catch (IOException ioe
) {
134 return Status
.UNAVAILABLE
;
138 return rw ? Status
.READ_WRITE
: Status
.READ_ONLY
;
142 public String
getLibraryName() {
143 return (rw ?
"[READ-ONLY] " : "") + host
+ ":" + port
;
147 public Image
getCover(String luid
) throws IOException
{
148 InputStream in
= download(WebLibraryUrls
.getStoryUrlCover(luid
));
150 return new Image(in
);
157 public Image
getCustomSourceCover(final String source
) throws IOException
{
158 // TODO maybe global system in BasicLib ?
163 public Image
getCustomAuthorCover(final String author
) throws IOException
{
164 // TODO maybe global system in BasicLib ?
169 public void setSourceCover(String source
, String luid
) throws IOException
{
170 // TODO Auto-generated method stub
171 throw new IOException("Not implemented yet");
175 public void setAuthorCover(String author
, String luid
) throws IOException
{
176 // TODO Auto-generated method stub
177 throw new IOException("Not implemented yet");
181 public synchronized Story
getStory(final String luid
, Progress pg
)
187 InputStream in
= download(WebLibraryUrls
.getStoryUrlJson(luid
));
189 JSONObject json
= new JSONObject(IOUtils
.readSmallStream(in
));
190 story
= JsonIO
.toStory(json
);
195 story
.getMeta().setCover(getCover(luid
));
197 for (Chapter chap
: story
) {
199 for (Paragraph para
: chap
) {
200 if (para
.getType() == ParagraphType
.IMAGE
) {
201 InputStream subin
= download(
202 WebLibraryUrls
.getStoryUrl(luid
, chapNum
, number
));
204 para
.setContentImage(new Image(subin
));
220 protected List
<MetaData
> getMetas(Progress pg
) throws IOException
{
221 List
<MetaData
> metas
= new ArrayList
<MetaData
>();
222 InputStream in
= download(WebLibraryUrls
.LIST_URL_METADATA
);
223 JSONArray jsonArr
= new JSONArray(IOUtils
.readSmallStream(in
));
224 for (int i
= 0; i
< jsonArr
.length(); i
++) {
225 JSONObject json
= jsonArr
.getJSONObject(i
);
226 metas
.add(JsonIO
.toMetaData(json
));
233 // Could work (more slowly) without it
234 public MetaData
imprt(final URL url
, Progress pg
) throws IOException
{
236 throw new IOException("Not implemented yet");
238 // Import the file locally if it is actually a file
240 if (url
== null || url
.getProtocol().equalsIgnoreCase("file")) {
241 return super.imprt(url
, pg
);
244 // Import it remotely if it is an URL
247 return super.imprt(url
, pg
);
251 // Could work (more slowly) without it
252 protected synchronized void changeSTA(final String luid
,
253 final String newSource
, final String newTitle
,
254 final String newAuthor
, Progress pg
) throws IOException
{
256 super.changeSTA(luid
, newSource
, newTitle
, newAuthor
, pg
);
260 protected void updateInfo(MetaData meta
) {
261 // Will be taken care of directly server side
265 protected void invalidateInfo(String luid
) {
266 // Will be taken care of directly server side
269 // The following methods are only used by Save and Delete in BasicLibrary:
272 protected int getNextId() {
273 throw new java
.lang
.InternalError("Should not have been called");
277 protected void doDelete(String luid
) throws IOException
{
278 throw new java
.lang
.InternalError("Should not have been called");
282 protected Story
doSave(Story story
, Progress pg
) throws IOException
{
283 throw new java
.lang
.InternalError("Should not have been called");
289 public File
getFile(final String luid
, Progress pg
) {
290 throw new java
.lang
.InternalError(
291 "Operation not supportorted on remote Libraries");
294 // starts with "/", never NULL
295 private InputStream
download(String path
) throws IOException
{
296 URL url
= new URL(host
+ ":" + port
+ path
);
298 Map
<String
, String
> post
= new HashMap
<String
, String
>();
299 post
.put("login", subkey
);
300 post
.put("password", key
);
302 return Instance
.getInstance().getCache().openNoCache(url
, null, post
,