978dd9af51c05e5b56f93c2d61948e34fcbcc968
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");
109 * Return the version of the program running server-side.
111 * Never returns NULL.
113 * @return the version or an empty {@link Version} if not known
115 public Version
getVersion() {
117 InputStream in
= post(WebLibraryUrls
.VERSION_URL
);
119 return new Version(IOUtils
.readSmallStream(in
));
123 } catch (IOException e
) {
126 return new Version();
130 public Status
getStatus() {
132 post(WebLibraryUrls
.INDEX_URL
).close();
133 } catch (IOException e
) {
135 post("/style.css").close();
136 return Status
.UNAUTHORIZED
;
137 } catch (IOException ioe
) {
138 return Status
.UNAVAILABLE
;
142 return rw ? Status
.READ_WRITE
: Status
.READ_ONLY
;
146 public String
getLibraryName() {
147 return (rw ?
"[READ-ONLY] " : "") + host
+ ":" + port
+ " ("
148 + getVersion() + ")";
152 public Image
getCover(String luid
) throws IOException
{
153 InputStream in
= post(WebLibraryUrls
.getStoryUrlCover(luid
));
155 Image img
= new Image(in
);
156 if (img
.getSize() > 0) {
167 public Image
getCustomSourceCover(String source
) throws IOException
{
168 InputStream in
= post(WebLibraryUrls
.getCoverUrlSource(source
));
170 Image img
= new Image(in
);
171 if (img
.getSize() > 0) {
182 public Image
getCustomAuthorCover(String author
) throws IOException
{
183 InputStream in
= post(WebLibraryUrls
.getCoverUrlAuthor(author
));
185 Image img
= new Image(in
);
186 if (img
.getSize() > 0) {
197 public void setSourceCover(String source
, String luid
) throws IOException
{
198 Map
<String
, String
> post
= new HashMap
<String
, String
>();
199 post
.put("luid", luid
);
200 post(WebLibraryUrls
.getCoverUrlSource(source
), post
).close();
204 public void setAuthorCover(String author
, String luid
) throws IOException
{
205 Map
<String
, String
> post
= new HashMap
<String
, String
>();
206 post
.put("luid", luid
);
207 post(WebLibraryUrls
.getCoverUrlAuthor(author
), post
).close();
211 public synchronized Story
getStory(final String luid
, Progress pg
)
218 InputStream in
= post(WebLibraryUrls
.getStoryUrlJson(luid
));
220 JSONObject json
= new JSONObject(IOUtils
.readSmallStream(in
));
221 story
= JsonIO
.toStory(json
);
227 for (Chapter chap
: story
) {
228 max
+= chap
.getParagraphs().size();
230 pg
.setMinMax(0, max
);
232 story
.getMeta().setCover(getCover(luid
));
234 for (Chapter chap
: story
) {
236 for (Paragraph para
: chap
) {
237 if (para
.getType() == ParagraphType
.IMAGE
) {
238 InputStream subin
= post(
239 WebLibraryUrls
.getStoryUrl(luid
, chapNum
, number
));
241 Image img
= new Image(subin
);
242 if (img
.getSize() > 0) {
243 para
.setContentImage(img
);
262 protected List
<MetaData
> getMetas(Progress pg
) throws IOException
{
263 List
<MetaData
> metas
= new ArrayList
<MetaData
>();
264 InputStream in
= post(WebLibraryUrls
.LIST_URL_METADATA
);
265 JSONArray jsonArr
= new JSONArray(IOUtils
.readSmallStream(in
));
266 for (int i
= 0; i
< jsonArr
.length(); i
++) {
267 JSONObject json
= jsonArr
.getJSONObject(i
);
268 metas
.add(JsonIO
.toMetaData(json
));
275 // Could work (more slowly) without it
276 public MetaData
imprt(final URL url
, Progress pg
) throws IOException
{
281 // Import the file locally if it is actually a file
283 if (url
== null || url
.getProtocol().equalsIgnoreCase("file")) {
284 return super.imprt(url
, pg
);
287 // Import it remotely if it is an URL
292 Map
<String
, String
> post
= new HashMap
<String
, String
>();
293 post
.put("url", url
.toString());
294 InputStream in
= post(WebLibraryUrls
.IMPRT_URL_IMPORT
, post
);
296 luid
= IOUtils
.readSmallStream(in
);
301 Progress subPg
= null;
305 } catch (InterruptedException e
) {
308 in
= post(WebLibraryUrls
.getImprtProgressUrl(luid
));
310 subPg
= JsonIO
.toProgress(
311 new JSONObject(IOUtils
.readSmallStream(in
)));
312 } catch (Exception e
) {
317 } while (subPg
!= null);
319 in
= post(WebLibraryUrls
.getStoryUrlMetadata(luid
));
321 return JsonIO
.toMetaData(
322 new JSONObject(IOUtils
.readSmallStream(in
)));
332 // Could work (more slowly) without it
333 protected synchronized void changeSTA(final String luid
,
334 final String newSource
, final String newTitle
,
335 final String newAuthor
, Progress pg
) throws IOException
{
336 MetaData meta
= getInfo(luid
);
338 if (!meta
.getSource().equals(newSource
)) {
339 Map
<String
, String
> post
= new HashMap
<String
, String
>();
340 post
.put("value", newSource
);
341 post(WebLibraryUrls
.getStoryUrlSource(luid
), post
).close();
343 if (!meta
.getTitle().equals(newTitle
)) {
344 Map
<String
, String
> post
= new HashMap
<String
, String
>();
345 post
.put("value", newTitle
);
346 post(WebLibraryUrls
.getStoryUrlTitle(luid
), post
).close();
348 if (!meta
.getAuthor().equals(newAuthor
)) {
349 Map
<String
, String
> post
= new HashMap
<String
, String
>();
350 post
.put("value", newAuthor
);
351 post(WebLibraryUrls
.getStoryUrlAuthor(luid
), post
).close();
357 public synchronized void delete(String luid
) throws IOException
{
358 post(WebLibraryUrls
.getDeleteUrlStory(luid
), null).close();
362 protected void updateInfo(MetaData meta
) {
363 // Will be taken care of directly server side
367 protected void invalidateInfo(String luid
) {
368 // Will be taken care of directly server side
371 // The following methods are only used by Save and Delete in BasicLibrary:
374 protected String
getNextId() {
375 throw new java
.lang
.InternalError("Should not have been called");
379 protected void doDelete(String luid
) throws IOException
{
380 throw new java
.lang
.InternalError("Should not have been called");
384 protected Story
doSave(Story story
, Progress pg
) throws IOException
{
385 throw new java
.lang
.InternalError("Should not have been called");
391 public File
getFile(final String luid
, Progress pg
) {
392 throw new java
.lang
.InternalError(
393 "Operation not supportorted on remote Libraries");
396 // starts with "/", never NULL
397 private InputStream
post(String path
) throws IOException
{
398 return post(path
, null);
401 // starts with "/", never NULL
402 private InputStream
post(String path
, Map
<String
, String
> post
)
404 URL url
= new URL(host
+ ":" + port
+ path
);
407 post
= new HashMap
<String
, String
>();
409 post
.put("login", subkey
);
410 post
.put("password", key
);
412 return Instance
.getInstance().getCache().openNoCache(url
, null, post
,