1 package be
.nikiroo
.fanfix
.library
;
3 import java
.io
.ByteArrayInputStream
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
7 import java
.util
.ArrayList
;
8 import java
.util
.Arrays
;
9 import java
.util
.HashMap
;
10 import java
.util
.LinkedList
;
11 import java
.util
.List
;
14 import org
.json
.JSONArray
;
15 import org
.json
.JSONObject
;
17 import be
.nikiroo
.fanfix
.Instance
;
18 import be
.nikiroo
.fanfix
.bundles
.Config
;
19 import be
.nikiroo
.fanfix
.data
.Chapter
;
20 import be
.nikiroo
.fanfix
.data
.JsonIO
;
21 import be
.nikiroo
.fanfix
.data
.MetaData
;
22 import be
.nikiroo
.fanfix
.data
.Paragraph
;
23 import be
.nikiroo
.fanfix
.data
.Paragraph
.ParagraphType
;
24 import be
.nikiroo
.fanfix
.data
.Story
;
25 import be
.nikiroo
.utils
.Image
;
26 import be
.nikiroo
.utils
.LoginResult
;
27 import be
.nikiroo
.utils
.NanoHTTPD
;
28 import be
.nikiroo
.utils
.NanoHTTPD
.Response
;
29 import be
.nikiroo
.utils
.NanoHTTPD
.Response
.Status
;
30 import be
.nikiroo
.utils
.Progress
;
32 public class WebLibraryServer
extends WebLibraryServerHtml
{
33 class WLoginResult
extends LoginResult
{
34 public WLoginResult(boolean badLogin
, boolean badCookie
) {
35 super(badLogin
, badCookie
);
38 public WLoginResult(String who
, String key
, String subkey
, boolean rw
,
39 boolean wl
, boolean bl
) {
40 super(who
, key
, subkey
, (rw ?
"|rw" : "") + (wl ?
"|wl" : "")
41 + (bl ?
"|bl" : "") + "|");
44 public WLoginResult(String cookie
, String who
, String key
,
45 List
<String
> subkeys
) {
46 super(cookie
, who
, key
, subkeys
,
47 subkeys
== null || subkeys
.isEmpty());
50 public boolean isRw() {
51 return getOption().contains("|rw|");
54 public boolean isWl() {
55 return getOption().contains("|wl|");
58 public boolean isBl() {
59 return getOption().contains("|bl|");
63 private Map
<String
, Story
> storyCache
= new HashMap
<String
, Story
>();
64 private LinkedList
<String
> storyCacheOrder
= new LinkedList
<String
>();
65 private long storyCacheSize
= 0;
66 private long maxStoryCacheSize
;
68 private List
<String
> whitelist
;
69 private List
<String
> blacklist
;
71 private Map
<String
, Progress
> imprts
= new HashMap
<String
, Progress
>();
73 private boolean exiting
;
75 public WebLibraryServer(boolean secure
) throws IOException
{
78 int cacheMb
= Instance
.getInstance().getConfig()
79 .getInteger(Config
.SERVER_MAX_CACHE_MB
, 100);
80 maxStoryCacheSize
= cacheMb
* 1024 * 1024;
82 setTraceHandler(Instance
.getInstance().getTraceHandler());
84 whitelist
= Instance
.getInstance().getConfig()
85 .getList(Config
.SERVER_WHITELIST
, new ArrayList
<String
>());
86 blacklist
= Instance
.getInstance().getConfig()
87 .getList(Config
.SERVER_BLACKLIST
, new ArrayList
<String
>());
91 * Start the server (listen on the network for new connections).
93 * Can only be called once.
95 * This call is asynchronous, and will just start a new {@link Thread} on
96 * itself (see {@link WebLibraryServer#run()}).
99 new Thread(this).start();
103 protected Response
stop(WLoginResult login
) {
105 return NanoHTTPD
.newFixedLengthResponse(Status
.FORBIDDEN
,
106 NanoHTTPD
.MIME_PLAINTEXT
, "Exit not allowed");
110 return NanoHTTPD
.newFixedLengthResponse(Status
.SERVICE_UNAVAILABLE
,
111 NanoHTTPD
.MIME_PLAINTEXT
, "Server is already exiting...");
115 Instance
.getInstance().getTraceHandler().trace("Exiting");
119 synchronized (imprts
) {
120 ok
= imprts
.isEmpty();
125 } catch (InterruptedException e
) {
126 Instance
.getInstance().getTraceHandler()
127 .trace("Waiting to exit...");
134 new Thread(new Runnable() {
139 } catch (InterruptedException e
) {
142 Instance
.getInstance().getTraceHandler()
143 .trace("Exit timeout: force-quit");
146 }, "Exit program after timeout of 1500 ms").start();
148 return NanoHTTPD
.newFixedLengthResponse(Status
.OK
,
149 NanoHTTPD
.MIME_PLAINTEXT
, "Exited");
153 protected WLoginResult
login(boolean badLogin
, boolean badCookie
) {
154 return new WLoginResult(false, false);
158 protected WLoginResult
login(String who
, String cookie
) {
159 List
<String
> subkeys
= Instance
.getInstance().getConfig()
160 .getList(Config
.SERVER_ALLOWED_SUBKEYS
);
161 String realKey
= Instance
.getInstance().getConfig()
162 .getString(Config
.SERVER_KEY
);
164 return new WLoginResult(cookie
, who
, realKey
, subkeys
);
169 protected WLoginResult
login(String who
, String key
, String subkey
) {
170 String realKey
= Instance
.getInstance().getConfig()
171 .getString(Config
.SERVER_KEY
, "");
173 // I don't like NULLs...
174 key
= key
== null ?
"" : key
;
175 subkey
= subkey
== null ?
"" : subkey
;
177 if (!realKey
.equals(key
)) {
178 return new WLoginResult(true, false);
181 // defaults are true (as previous versions without the feature)
186 rw
= Instance
.getInstance().getConfig().getBoolean(Config
.SERVER_RW
,
189 List
<String
> allowed
= Instance
.getInstance().getConfig().getList(
190 Config
.SERVER_ALLOWED_SUBKEYS
, new ArrayList
<String
>());
192 if (!allowed
.isEmpty()) {
193 if (!allowed
.contains(subkey
)) {
194 return new WLoginResult(true, false);
197 if ((subkey
+ "|").contains("|rw|")) {
200 if ((subkey
+ "|").contains("|wl|")) {
201 wl
= false; // |wl| = bypass whitelist
203 if ((subkey
+ "|").contains("|bl|")) {
204 bl
= false; // |bl| = bypass blacklist
208 return new WLoginResult(who
, key
, subkey
, rw
, wl
, bl
);
212 protected Response
getList(String uri
, WLoginResult login
)
214 if (WebLibraryUrls
.LIST_URL_METADATA
.equals(uri
)) {
215 List
<JSONObject
> jsons
= new ArrayList
<JSONObject
>();
216 for (MetaData meta
: metas(login
)) {
217 jsons
.add(JsonIO
.toJson(meta
));
220 return newInputStreamResponse("application/json",
221 new ByteArrayInputStream(
222 new JSONArray(jsons
).toString().getBytes()));
225 return NanoHTTPD
.newFixedLengthResponse(Status
.BAD_REQUEST
,
226 NanoHTTPD
.MIME_PLAINTEXT
, null);
229 // /story/luid/chapter/para <-- text/image
230 // /story/luid/cover <-- image
231 // /story/luid/metadata <-- json
232 // /story/luid/json <-- json, whole chapter (no images)
234 protected Response
getStoryPart(String uri
, WLoginResult login
) {
235 String
[] uriParts
= uri
.split("/");
238 if (uriParts
.length
< off
+ 2) {
239 return NanoHTTPD
.newFixedLengthResponse(Status
.BAD_REQUEST
,
240 NanoHTTPD
.MIME_PLAINTEXT
, null);
243 String luid
= uriParts
[off
+ 0];
244 String chapterStr
= uriParts
[off
+ 1];
245 String imageStr
= uriParts
.length
< off
+ 3 ?
null : uriParts
[off
+ 2];
247 // 1-based (0 = desc)
249 if (chapterStr
!= null && !"cover".equals(chapterStr
)
250 && !"metadata".equals(chapterStr
)
251 && !"json".equals(chapterStr
)) {
253 chapter
= Integer
.parseInt(chapterStr
);
255 throw new NumberFormatException();
257 } catch (NumberFormatException e
) {
258 return NanoHTTPD
.newFixedLengthResponse(Status
.BAD_REQUEST
,
259 NanoHTTPD
.MIME_PLAINTEXT
, "Chapter is not valid");
265 if (imageStr
!= null) {
267 paragraph
= Integer
.parseInt(imageStr
);
269 throw new NumberFormatException();
271 } catch (NumberFormatException e
) {
272 return NanoHTTPD
.newFixedLengthResponse(Status
.BAD_REQUEST
,
273 NanoHTTPD
.MIME_PLAINTEXT
, "Paragraph is not valid");
277 String mimeType
= NanoHTTPD
.MIME_PLAINTEXT
;
278 InputStream in
= null;
280 if ("cover".equals(chapterStr
)) {
281 Image img
= storyCover(luid
, login
);
283 in
= img
.newInputStream();
285 // TODO: get correct image type
286 mimeType
= "image/png";
287 } else if ("metadata".equals(chapterStr
)) {
288 MetaData meta
= meta(luid
, login
);
289 JSONObject json
= JsonIO
.toJson(meta
);
290 mimeType
= "application/json";
291 in
= new ByteArrayInputStream(json
.toString().getBytes());
292 } else if ("json".equals(chapterStr
)) {
293 Story story
= story(luid
, login
);
294 JSONObject json
= JsonIO
.toJson(story
);
295 mimeType
= "application/json";
296 in
= new ByteArrayInputStream(json
.toString().getBytes());
298 Story story
= story(luid
, login
);
301 StringBuilder builder
= new StringBuilder();
302 for (Paragraph p
: story
.getMeta().getResume()) {
303 if (builder
.length() == 0) {
304 builder
.append("\n");
306 builder
.append(p
.getContent());
309 in
= new ByteArrayInputStream(
310 builder
.toString().getBytes("utf-8"));
312 Paragraph para
= story
.getChapters().get(chapter
- 1)
313 .getParagraphs().get(paragraph
- 1);
314 Image img
= para
.getContentImage();
315 if (para
.getType() == ParagraphType
.IMAGE
) {
316 // TODO: get correct image type
317 mimeType
= "image/png";
318 in
= img
.newInputStream();
320 in
= new ByteArrayInputStream(
321 para
.getContent().getBytes("utf-8"));
326 } catch (IndexOutOfBoundsException e
) {
327 return NanoHTTPD
.newFixedLengthResponse(Status
.NOT_FOUND
,
328 NanoHTTPD
.MIME_PLAINTEXT
,
329 "Chapter or paragraph does not exist");
330 } catch (IOException e
) {
331 Instance
.getInstance().getTraceHandler()
332 .error(new IOException("Cannot get image: " + uri
, e
));
333 return NanoHTTPD
.newFixedLengthResponse(Status
.INTERNAL_ERROR
,
334 NanoHTTPD
.MIME_PLAINTEXT
, "Error when processing request");
337 return newInputStreamResponse(mimeType
, in
);
340 // /story/luid/source
342 // /story/luid/author
344 protected Response
setStoryPart(String uri
, String value
,
345 WLoginResult login
) throws IOException
{
346 String
[] uriParts
= uri
.split("/");
347 int off
= 2; // "" and "story"
349 if (uriParts
.length
< off
+ 2) {
350 return NanoHTTPD
.newFixedLengthResponse(Status
.BAD_REQUEST
,
351 NanoHTTPD
.MIME_PLAINTEXT
, "Invalid story part request");
355 return NanoHTTPD
.newFixedLengthResponse(Status
.FORBIDDEN
,
356 NanoHTTPD
.MIME_PLAINTEXT
, "SET story part not allowed");
360 return NanoHTTPD
.newFixedLengthResponse(Status
.SERVICE_UNAVAILABLE
,
361 NanoHTTPD
.MIME_PLAINTEXT
, "Server is exiting...");
364 String luid
= uriParts
[off
+ 0];
365 String type
= uriParts
[off
+ 1];
367 if (!Arrays
.asList("source", "title", "author").contains(type
)) {
368 return NanoHTTPD
.newFixedLengthResponse(Status
.BAD_REQUEST
,
369 NanoHTTPD
.MIME_PLAINTEXT
,
370 "Invalid SET story part: " + type
);
373 if (meta(luid
, login
) != null) {
374 BasicLibrary lib
= Instance
.getInstance().getLibrary();
375 if ("source".equals(type
)) {
376 lib
.changeSource(luid
, value
, null);
377 } else if ("title".equals(type
)) {
378 lib
.changeTitle(luid
, value
, null);
379 } else if ("author".equals(type
)) {
380 lib
.changeAuthor(luid
, value
, null);
384 return newInputStreamResponse(NanoHTTPD
.MIME_PLAINTEXT
, null);
388 protected Response
getCover(String uri
, WLoginResult login
)
390 String
[] uriParts
= uri
.split("/");
391 int off
= 2; // "" and "cover"
393 if (uriParts
.length
< off
+ 2) {
394 return NanoHTTPD
.newFixedLengthResponse(Status
.BAD_REQUEST
,
395 NanoHTTPD
.MIME_PLAINTEXT
, "Invalid cover request");
398 String type
= uriParts
[off
+ 0];
399 String id
= uriParts
[off
+ 1];
401 InputStream in
= null;
403 if ("story".equals(type
)) {
404 Image img
= storyCover(id
, login
);
406 in
= img
.newInputStream();
408 } else if ("source".equals(type
)) {
409 Image img
= sourceCover(id
, login
);
411 in
= img
.newInputStream();
413 } else if ("author".equals(type
)) {
414 Image img
= authorCover(id
, login
);
416 in
= img
.newInputStream();
419 return NanoHTTPD
.newFixedLengthResponse(Status
.BAD_REQUEST
,
420 NanoHTTPD
.MIME_PLAINTEXT
,
421 "Invalid GET cover type: " + type
);
424 // TODO: get correct image type
425 return newInputStreamResponse("image/png", in
);
429 protected Response
setCover(String uri
, String luid
, WLoginResult login
)
431 String
[] uriParts
= uri
.split("/");
432 int off
= 2; // "" and "cover"
434 if (uriParts
.length
< off
+ 2) {
435 return NanoHTTPD
.newFixedLengthResponse(Status
.BAD_REQUEST
,
436 NanoHTTPD
.MIME_PLAINTEXT
, "Invalid cover request");
440 return NanoHTTPD
.newFixedLengthResponse(Status
.FORBIDDEN
,
441 NanoHTTPD
.MIME_PLAINTEXT
, "Cover request not allowed");
445 return NanoHTTPD
.newFixedLengthResponse(Status
.SERVICE_UNAVAILABLE
,
446 NanoHTTPD
.MIME_PLAINTEXT
, "Server is exiting...");
449 String type
= uriParts
[off
+ 0];
450 String id
= uriParts
[off
+ 1];
452 if ("source".equals(type
)) {
453 sourceCover(id
, login
, luid
);
454 } else if ("author".equals(type
)) {
455 authorCover(id
, login
, luid
);
457 return NanoHTTPD
.newFixedLengthResponse(Status
.BAD_REQUEST
,
458 NanoHTTPD
.MIME_PLAINTEXT
,
459 "Invalid SET cover type: " + type
);
462 return newInputStreamResponse(NanoHTTPD
.MIME_PLAINTEXT
, null);
466 protected Response
imprt(String uri
, String urlStr
, WLoginResult login
)
468 final BasicLibrary lib
= Instance
.getInstance().getLibrary();
471 return NanoHTTPD
.newFixedLengthResponse(Status
.FORBIDDEN
,
472 NanoHTTPD
.MIME_PLAINTEXT
, "Import not allowed");
476 return NanoHTTPD
.newFixedLengthResponse(Status
.SERVICE_UNAVAILABLE
,
477 NanoHTTPD
.MIME_PLAINTEXT
, "Server is exiting...");
480 final URL url
= new URL(urlStr
);
481 final Progress pg
= new Progress();
482 final String luid
= lib
.getNextId();
484 synchronized (imprts
) {
485 imprts
.put(luid
, pg
);
488 new Thread(new Runnable() {
492 lib
.imprt(url
, luid
, pg
);
493 } catch (IOException e
) {
494 Instance
.getInstance().getTraceHandler().error(e
);
496 synchronized (imprts
) {
501 }, "Import story: " + urlStr
).start();
503 return NanoHTTPD
.newFixedLengthResponse(Status
.OK
,
504 NanoHTTPD
.MIME_PLAINTEXT
, luid
);
508 protected Response
imprtProgress(String uri
, WLoginResult login
) {
509 String
[] uriParts
= uri
.split("/");
510 int off
= 2; // "" and "import"
512 if (uriParts
.length
< off
+ 1) {
513 return NanoHTTPD
.newFixedLengthResponse(Status
.BAD_REQUEST
,
514 NanoHTTPD
.MIME_PLAINTEXT
, "Invalid cover request");
517 String luid
= uriParts
[off
+ 0];
520 synchronized (imprts
) {
521 pg
= imprts
.get(luid
);
524 return NanoHTTPD
.newFixedLengthResponse(Status
.OK
,
525 "application/json", JsonIO
.toJson(pg
).toString());
528 return newInputStreamResponse(NanoHTTPD
.MIME_PLAINTEXT
, null);
532 protected Response
delete(String uri
, WLoginResult login
)
534 String
[] uriParts
= uri
.split("/");
535 int off
= 2; // "" and "delete"
537 if (uriParts
.length
< off
+ 1) {
538 return NanoHTTPD
.newFixedLengthResponse(Status
.BAD_REQUEST
,
539 NanoHTTPD
.MIME_PLAINTEXT
, "Invalid delete request");
543 return NanoHTTPD
.newFixedLengthResponse(Status
.FORBIDDEN
,
544 NanoHTTPD
.MIME_PLAINTEXT
, "Delete not allowed");
548 return NanoHTTPD
.newFixedLengthResponse(Status
.SERVICE_UNAVAILABLE
,
549 NanoHTTPD
.MIME_PLAINTEXT
, "Server is exiting...");
552 String luid
= uriParts
[off
+ 0];
554 BasicLibrary lib
= Instance
.getInstance().getLibrary();
557 return newInputStreamResponse(NanoHTTPD
.MIME_PLAINTEXT
, null);
561 protected List
<MetaData
> metas(WLoginResult login
) throws IOException
{
562 BasicLibrary lib
= Instance
.getInstance().getLibrary();
563 List
<MetaData
> metas
= new ArrayList
<MetaData
>();
564 for (MetaData meta
: lib
.getList().getMetas()) {
565 if (isAllowed(meta
, login
)) {
573 // NULL if not whitelist OK or if not found
575 protected Story
story(String luid
, WLoginResult login
) throws IOException
{
576 synchronized (storyCache
) {
577 if (storyCache
.containsKey(luid
)) {
578 Story story
= storyCache
.get(luid
);
579 if (!isAllowed(story
.getMeta(), login
))
587 MetaData meta
= meta(luid
, login
);
589 BasicLibrary lib
= Instance
.getInstance().getLibrary();
590 story
= lib
.getStory(luid
, null);
591 long size
= sizeOf(story
);
593 synchronized (storyCache
) {
594 // Could have been added by another request
595 if (!storyCache
.containsKey(luid
)) {
596 while (!storyCacheOrder
.isEmpty()
597 && storyCacheSize
+ size
> maxStoryCacheSize
) {
598 String oldestLuid
= storyCacheOrder
.removeFirst();
599 Story oldestStory
= storyCache
.remove(oldestLuid
);
600 maxStoryCacheSize
-= sizeOf(oldestStory
);
603 storyCacheOrder
.add(luid
);
604 storyCache
.put(luid
, story
);
612 private MetaData
meta(String luid
, WLoginResult login
) throws IOException
{
613 BasicLibrary lib
= Instance
.getInstance().getLibrary();
614 MetaData meta
= lib
.getInfo(luid
);
615 if (!isAllowed(meta
, login
))
621 private Image
storyCover(String luid
, WLoginResult login
)
623 MetaData meta
= meta(luid
, login
);
625 BasicLibrary lib
= Instance
.getInstance().getLibrary();
626 return lib
.getCover(meta
.getLuid());
632 private Image
authorCover(String author
, WLoginResult login
)
636 List
<MetaData
> metas
= new MetaResultList(metas(login
)).filter(null,
638 if (metas
.size() > 0) {
639 BasicLibrary lib
= Instance
.getInstance().getLibrary();
640 img
= lib
.getCustomAuthorCover(author
);
642 img
= lib
.getCover(metas
.get(0).getLuid());
649 private void authorCover(String author
, WLoginResult login
, String luid
)
651 if (meta(luid
, login
) != null) {
652 List
<MetaData
> metas
= new MetaResultList(metas(login
)).filter(null,
654 if (metas
.size() > 0) {
655 BasicLibrary lib
= Instance
.getInstance().getLibrary();
656 lib
.setAuthorCover(author
, luid
);
661 private Image
sourceCover(String source
, WLoginResult login
)
665 List
<MetaData
> metas
= new MetaResultList(metas(login
)).filter(source
,
667 if (metas
.size() > 0) {
668 BasicLibrary lib
= Instance
.getInstance().getLibrary();
669 img
= lib
.getCustomSourceCover(source
);
671 img
= lib
.getCover(metas
.get(0).getLuid());
677 private void sourceCover(String source
, WLoginResult login
, String luid
)
679 if (meta(luid
, login
) != null) {
680 List
<MetaData
> metas
= new MetaResultList(metas(login
))
681 .filter(source
, null, null);
682 if (metas
.size() > 0) {
683 BasicLibrary lib
= Instance
.getInstance().getLibrary();
684 lib
.setSourceCover(source
, luid
);
689 private boolean isAllowed(MetaData meta
, WLoginResult login
) {
690 MetaResultList one
= new MetaResultList(Arrays
.asList(meta
));
691 if (login
.isWl() && !whitelist
.isEmpty()) {
692 if (one
.filter(whitelist
, null, null).isEmpty()) {
696 if (login
.isBl() && !blacklist
.isEmpty()) {
697 if (!one
.filter(blacklist
, null, null).isEmpty()) {
705 private long sizeOf(Story story
) {
707 for (Chapter chap
: story
) {
708 for (Paragraph para
: chap
) {
709 if (para
.getType() == ParagraphType
.IMAGE
) {
710 size
+= para
.getContentImage().getSize();
712 size
+= para
.getContent().length();
720 public static void main(String
[] args
) throws IOException
{
722 WebLibraryServer web
= new WebLibraryServer(false);