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 return NanoHTTPD
.newFixedLengthResponse(Status
.OK
,
135 NanoHTTPD
.MIME_PLAINTEXT
, "Exited");
139 protected WLoginResult
login(boolean badLogin
, boolean badCookie
) {
140 return new WLoginResult(false, false);
144 protected WLoginResult
login(String who
, String cookie
) {
145 List
<String
> subkeys
= Instance
.getInstance().getConfig()
146 .getList(Config
.SERVER_ALLOWED_SUBKEYS
);
147 String realKey
= Instance
.getInstance().getConfig()
148 .getString(Config
.SERVER_KEY
);
150 return new WLoginResult(cookie
, who
, realKey
, subkeys
);
155 protected WLoginResult
login(String who
, String key
, String subkey
) {
156 String realKey
= Instance
.getInstance().getConfig()
157 .getString(Config
.SERVER_KEY
, "");
159 // I don't like NULLs...
160 key
= key
== null ?
"" : key
;
161 subkey
= subkey
== null ?
"" : subkey
;
163 if (!realKey
.equals(key
)) {
164 return new WLoginResult(true, false);
167 // defaults are true (as previous versions without the feature)
172 rw
= Instance
.getInstance().getConfig().getBoolean(Config
.SERVER_RW
,
175 List
<String
> allowed
= Instance
.getInstance().getConfig().getList(
176 Config
.SERVER_ALLOWED_SUBKEYS
, new ArrayList
<String
>());
178 if (!allowed
.isEmpty()) {
179 if (!allowed
.contains(subkey
)) {
180 return new WLoginResult(true, false);
183 if ((subkey
+ "|").contains("|rw|")) {
186 if ((subkey
+ "|").contains("|wl|")) {
187 wl
= false; // |wl| = bypass whitelist
189 if ((subkey
+ "|").contains("|bl|")) {
190 bl
= false; // |bl| = bypass blacklist
194 return new WLoginResult(who
, key
, subkey
, rw
, wl
, bl
);
198 protected Response
getList(String uri
, WLoginResult login
)
200 if (WebLibraryUrls
.LIST_URL_METADATA
.equals(uri
)) {
201 List
<JSONObject
> jsons
= new ArrayList
<JSONObject
>();
202 for (MetaData meta
: metas(login
)) {
203 jsons
.add(JsonIO
.toJson(meta
));
206 return newInputStreamResponse("application/json",
207 new ByteArrayInputStream(
208 new JSONArray(jsons
).toString().getBytes()));
211 return NanoHTTPD
.newFixedLengthResponse(Status
.BAD_REQUEST
,
212 NanoHTTPD
.MIME_PLAINTEXT
, null);
215 // /story/luid/chapter/para <-- text/image
216 // /story/luid/cover <-- image
217 // /story/luid/metadata <-- json
218 // /story/luid/json <-- json, whole chapter (no images)
220 protected Response
getStoryPart(String uri
, WLoginResult login
) {
221 String
[] uriParts
= uri
.split("/");
224 if (uriParts
.length
< off
+ 2) {
225 return NanoHTTPD
.newFixedLengthResponse(Status
.BAD_REQUEST
,
226 NanoHTTPD
.MIME_PLAINTEXT
, null);
229 String luid
= uriParts
[off
+ 0];
230 String chapterStr
= uriParts
[off
+ 1];
231 String imageStr
= uriParts
.length
< off
+ 3 ?
null : uriParts
[off
+ 2];
233 // 1-based (0 = desc)
235 if (chapterStr
!= null && !"cover".equals(chapterStr
)
236 && !"metadata".equals(chapterStr
)
237 && !"json".equals(chapterStr
)) {
239 chapter
= Integer
.parseInt(chapterStr
);
241 throw new NumberFormatException();
243 } catch (NumberFormatException e
) {
244 return NanoHTTPD
.newFixedLengthResponse(Status
.BAD_REQUEST
,
245 NanoHTTPD
.MIME_PLAINTEXT
, "Chapter is not valid");
251 if (imageStr
!= null) {
253 paragraph
= Integer
.parseInt(imageStr
);
255 throw new NumberFormatException();
257 } catch (NumberFormatException e
) {
258 return NanoHTTPD
.newFixedLengthResponse(Status
.BAD_REQUEST
,
259 NanoHTTPD
.MIME_PLAINTEXT
, "Paragraph is not valid");
263 String mimeType
= NanoHTTPD
.MIME_PLAINTEXT
;
264 InputStream in
= null;
266 if ("cover".equals(chapterStr
)) {
267 Image img
= storyCover(luid
, login
);
269 in
= img
.newInputStream();
271 // TODO: get correct image type
272 mimeType
= "image/png";
273 } else if ("metadata".equals(chapterStr
)) {
274 MetaData meta
= meta(luid
, login
);
275 JSONObject json
= JsonIO
.toJson(meta
);
276 mimeType
= "application/json";
277 in
= new ByteArrayInputStream(json
.toString().getBytes());
278 } else if ("json".equals(chapterStr
)) {
279 Story story
= story(luid
, login
);
280 JSONObject json
= JsonIO
.toJson(story
);
281 mimeType
= "application/json";
282 in
= new ByteArrayInputStream(json
.toString().getBytes());
284 Story story
= story(luid
, login
);
287 StringBuilder builder
= new StringBuilder();
288 for (Paragraph p
: story
.getMeta().getResume()) {
289 if (builder
.length() == 0) {
290 builder
.append("\n");
292 builder
.append(p
.getContent());
295 in
= new ByteArrayInputStream(
296 builder
.toString().getBytes("utf-8"));
298 Paragraph para
= story
.getChapters().get(chapter
- 1)
299 .getParagraphs().get(paragraph
- 1);
300 Image img
= para
.getContentImage();
301 if (para
.getType() == ParagraphType
.IMAGE
) {
302 // TODO: get correct image type
303 mimeType
= "image/png";
304 in
= img
.newInputStream();
306 in
= new ByteArrayInputStream(
307 para
.getContent().getBytes("utf-8"));
312 } catch (IndexOutOfBoundsException e
) {
313 return NanoHTTPD
.newFixedLengthResponse(Status
.NOT_FOUND
,
314 NanoHTTPD
.MIME_PLAINTEXT
,
315 "Chapter or paragraph does not exist");
316 } catch (IOException e
) {
317 Instance
.getInstance().getTraceHandler()
318 .error(new IOException("Cannot get image: " + uri
, e
));
319 return NanoHTTPD
.newFixedLengthResponse(Status
.INTERNAL_ERROR
,
320 NanoHTTPD
.MIME_PLAINTEXT
, "Error when processing request");
323 return newInputStreamResponse(mimeType
, in
);
326 // /story/luid/source
328 // /story/luid/author
330 protected Response
setStoryPart(String uri
, String value
,
331 WLoginResult login
) throws IOException
{
332 String
[] uriParts
= uri
.split("/");
333 int off
= 2; // "" and "story"
335 if (uriParts
.length
< off
+ 2) {
336 return NanoHTTPD
.newFixedLengthResponse(Status
.BAD_REQUEST
,
337 NanoHTTPD
.MIME_PLAINTEXT
, "Invalid story part request");
341 return NanoHTTPD
.newFixedLengthResponse(Status
.FORBIDDEN
,
342 NanoHTTPD
.MIME_PLAINTEXT
, "SET story part not allowed");
346 return NanoHTTPD
.newFixedLengthResponse(Status
.SERVICE_UNAVAILABLE
,
347 NanoHTTPD
.MIME_PLAINTEXT
, "Server is exiting...");
350 String luid
= uriParts
[off
+ 0];
351 String type
= uriParts
[off
+ 1];
353 if (!Arrays
.asList("source", "title", "author").contains(type
)) {
354 return NanoHTTPD
.newFixedLengthResponse(Status
.BAD_REQUEST
,
355 NanoHTTPD
.MIME_PLAINTEXT
,
356 "Invalid SET story part: " + type
);
359 if (meta(luid
, login
) != null) {
360 BasicLibrary lib
= Instance
.getInstance().getLibrary();
361 if ("source".equals(type
)) {
362 lib
.changeSource(luid
, value
, null);
363 } else if ("title".equals(type
)) {
364 lib
.changeTitle(luid
, value
, null);
365 } else if ("author".equals(type
)) {
366 lib
.changeAuthor(luid
, value
, null);
370 return newInputStreamResponse(NanoHTTPD
.MIME_PLAINTEXT
, null);
374 protected Response
getCover(String uri
, WLoginResult login
)
376 String
[] uriParts
= uri
.split("/");
377 int off
= 2; // "" and "cover"
379 if (uriParts
.length
< off
+ 2) {
380 return NanoHTTPD
.newFixedLengthResponse(Status
.BAD_REQUEST
,
381 NanoHTTPD
.MIME_PLAINTEXT
, "Invalid cover request");
384 String type
= uriParts
[off
+ 0];
385 String id
= uriParts
[off
+ 1];
387 InputStream in
= null;
389 if ("story".equals(type
)) {
390 Image img
= storyCover(id
, login
);
392 in
= img
.newInputStream();
394 } else if ("source".equals(type
)) {
395 Image img
= sourceCover(id
, login
);
397 in
= img
.newInputStream();
399 } else if ("author".equals(type
)) {
400 Image img
= authorCover(id
, login
);
402 in
= img
.newInputStream();
405 return NanoHTTPD
.newFixedLengthResponse(Status
.BAD_REQUEST
,
406 NanoHTTPD
.MIME_PLAINTEXT
,
407 "Invalid GET cover type: " + type
);
410 // TODO: get correct image type
411 return newInputStreamResponse("image/png", in
);
415 protected Response
setCover(String uri
, String luid
, WLoginResult login
)
417 String
[] uriParts
= uri
.split("/");
418 int off
= 2; // "" and "cover"
420 if (uriParts
.length
< off
+ 2) {
421 return NanoHTTPD
.newFixedLengthResponse(Status
.BAD_REQUEST
,
422 NanoHTTPD
.MIME_PLAINTEXT
, "Invalid cover request");
426 return NanoHTTPD
.newFixedLengthResponse(Status
.FORBIDDEN
,
427 NanoHTTPD
.MIME_PLAINTEXT
, "Cover request not allowed");
431 return NanoHTTPD
.newFixedLengthResponse(Status
.SERVICE_UNAVAILABLE
,
432 NanoHTTPD
.MIME_PLAINTEXT
, "Server is exiting...");
435 String type
= uriParts
[off
+ 0];
436 String id
= uriParts
[off
+ 1];
438 if ("source".equals(type
)) {
439 sourceCover(id
, login
, luid
);
440 } else if ("author".equals(type
)) {
441 authorCover(id
, login
, luid
);
443 return NanoHTTPD
.newFixedLengthResponse(Status
.BAD_REQUEST
,
444 NanoHTTPD
.MIME_PLAINTEXT
,
445 "Invalid SET cover type: " + type
);
448 return newInputStreamResponse(NanoHTTPD
.MIME_PLAINTEXT
, null);
452 protected Response
imprt(String uri
, String urlStr
, WLoginResult login
)
454 final BasicLibrary lib
= Instance
.getInstance().getLibrary();
457 return NanoHTTPD
.newFixedLengthResponse(Status
.FORBIDDEN
,
458 NanoHTTPD
.MIME_PLAINTEXT
, "Import not allowed");
462 return NanoHTTPD
.newFixedLengthResponse(Status
.SERVICE_UNAVAILABLE
,
463 NanoHTTPD
.MIME_PLAINTEXT
, "Server is exiting...");
466 final URL url
= new URL(urlStr
);
467 final Progress pg
= new Progress();
468 final String luid
= lib
.getNextId();
470 synchronized (imprts
) {
471 imprts
.put(luid
, pg
);
474 new Thread(new Runnable() {
478 lib
.imprt(url
, luid
, pg
);
479 } catch (IOException e
) {
480 Instance
.getInstance().getTraceHandler().error(e
);
482 synchronized (imprts
) {
487 }, "Import story: " + urlStr
).start();
489 return NanoHTTPD
.newFixedLengthResponse(Status
.OK
,
490 NanoHTTPD
.MIME_PLAINTEXT
, luid
);
494 protected Response
imprtProgress(String uri
, WLoginResult login
) {
495 String
[] uriParts
= uri
.split("/");
496 int off
= 2; // "" and "import"
498 if (uriParts
.length
< off
+ 1) {
499 return NanoHTTPD
.newFixedLengthResponse(Status
.BAD_REQUEST
,
500 NanoHTTPD
.MIME_PLAINTEXT
, "Invalid cover request");
503 String luid
= uriParts
[off
+ 0];
506 synchronized (imprts
) {
507 pg
= imprts
.get(luid
);
510 return NanoHTTPD
.newFixedLengthResponse(Status
.OK
,
511 "application/json", JsonIO
.toJson(pg
).toString());
514 return newInputStreamResponse(NanoHTTPD
.MIME_PLAINTEXT
, null);
518 protected Response
delete(String uri
, WLoginResult login
)
520 String
[] uriParts
= uri
.split("/");
521 int off
= 2; // "" and "delete"
523 if (uriParts
.length
< off
+ 1) {
524 return NanoHTTPD
.newFixedLengthResponse(Status
.BAD_REQUEST
,
525 NanoHTTPD
.MIME_PLAINTEXT
, "Invalid delete request");
529 return NanoHTTPD
.newFixedLengthResponse(Status
.FORBIDDEN
,
530 NanoHTTPD
.MIME_PLAINTEXT
, "Delete not allowed");
534 return NanoHTTPD
.newFixedLengthResponse(Status
.SERVICE_UNAVAILABLE
,
535 NanoHTTPD
.MIME_PLAINTEXT
, "Server is exiting...");
538 String luid
= uriParts
[off
+ 0];
540 BasicLibrary lib
= Instance
.getInstance().getLibrary();
543 return newInputStreamResponse(NanoHTTPD
.MIME_PLAINTEXT
, null);
547 protected List
<MetaData
> metas(WLoginResult login
) throws IOException
{
548 BasicLibrary lib
= Instance
.getInstance().getLibrary();
549 List
<MetaData
> metas
= new ArrayList
<MetaData
>();
550 for (MetaData meta
: lib
.getList().getMetas()) {
551 if (isAllowed(meta
, login
)) {
559 // NULL if not whitelist OK or if not found
561 protected Story
story(String luid
, WLoginResult login
) throws IOException
{
562 synchronized (storyCache
) {
563 if (storyCache
.containsKey(luid
)) {
564 Story story
= storyCache
.get(luid
);
565 if (!isAllowed(story
.getMeta(), login
))
573 MetaData meta
= meta(luid
, login
);
575 BasicLibrary lib
= Instance
.getInstance().getLibrary();
576 story
= lib
.getStory(luid
, null);
577 long size
= sizeOf(story
);
579 synchronized (storyCache
) {
580 // Could have been added by another request
581 if (!storyCache
.containsKey(luid
)) {
582 while (!storyCacheOrder
.isEmpty()
583 && storyCacheSize
+ size
> maxStoryCacheSize
) {
584 String oldestLuid
= storyCacheOrder
.removeFirst();
585 Story oldestStory
= storyCache
.remove(oldestLuid
);
586 maxStoryCacheSize
-= sizeOf(oldestStory
);
589 storyCacheOrder
.add(luid
);
590 storyCache
.put(luid
, story
);
598 private MetaData
meta(String luid
, WLoginResult login
) throws IOException
{
599 BasicLibrary lib
= Instance
.getInstance().getLibrary();
600 MetaData meta
= lib
.getInfo(luid
);
601 if (!isAllowed(meta
, login
))
607 private Image
storyCover(String luid
, WLoginResult login
)
609 MetaData meta
= meta(luid
, login
);
611 BasicLibrary lib
= Instance
.getInstance().getLibrary();
612 return lib
.getCover(meta
.getLuid());
618 private Image
authorCover(String author
, WLoginResult login
)
622 List
<MetaData
> metas
= new MetaResultList(metas(login
)).filter(null,
624 if (metas
.size() > 0) {
625 BasicLibrary lib
= Instance
.getInstance().getLibrary();
626 img
= lib
.getCustomAuthorCover(author
);
628 img
= lib
.getCover(metas
.get(0).getLuid());
635 private void authorCover(String author
, WLoginResult login
, String luid
)
637 if (meta(luid
, login
) != null) {
638 List
<MetaData
> metas
= new MetaResultList(metas(login
)).filter(null,
640 if (metas
.size() > 0) {
641 BasicLibrary lib
= Instance
.getInstance().getLibrary();
642 lib
.setAuthorCover(author
, luid
);
647 private Image
sourceCover(String source
, WLoginResult login
)
651 List
<MetaData
> metas
= new MetaResultList(metas(login
)).filter(source
,
653 if (metas
.size() > 0) {
654 BasicLibrary lib
= Instance
.getInstance().getLibrary();
655 img
= lib
.getCustomSourceCover(source
);
657 img
= lib
.getCover(metas
.get(0).getLuid());
663 private void sourceCover(String source
, WLoginResult login
, String luid
)
665 if (meta(luid
, login
) != null) {
666 List
<MetaData
> metas
= new MetaResultList(metas(login
))
667 .filter(source
, null, null);
668 if (metas
.size() > 0) {
669 BasicLibrary lib
= Instance
.getInstance().getLibrary();
670 lib
.setSourceCover(source
, luid
);
675 private boolean isAllowed(MetaData meta
, WLoginResult login
) {
676 MetaResultList one
= new MetaResultList(Arrays
.asList(meta
));
677 if (login
.isWl() && !whitelist
.isEmpty()) {
678 if (one
.filter(whitelist
, null, null).isEmpty()) {
682 if (login
.isBl() && !blacklist
.isEmpty()) {
683 if (!one
.filter(blacklist
, null, null).isEmpty()) {
691 private long sizeOf(Story story
) {
693 for (Chapter chap
: story
) {
694 for (Paragraph para
: chap
) {
695 if (para
.getType() == ParagraphType
.IMAGE
) {
696 size
+= para
.getContentImage().getSize();
698 size
+= para
.getContent().length();
706 public static void main(String
[] args
) throws IOException
{
708 WebLibraryServer web
= new WebLibraryServer(false);