dfbceeeb7a19ce09cdbd398fa45a36789a758811
[fanfix.git] / src / be / nikiroo / fanfix / library / WebLibraryServer.java
1 package be.nikiroo.fanfix.library;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.util.ArrayList;
7 import java.util.Arrays;
8 import java.util.HashMap;
9 import java.util.LinkedList;
10 import java.util.List;
11 import java.util.Map;
12
13 import org.json.JSONArray;
14 import org.json.JSONObject;
15
16 import be.nikiroo.fanfix.Instance;
17 import be.nikiroo.fanfix.bundles.Config;
18 import be.nikiroo.fanfix.data.Chapter;
19 import be.nikiroo.fanfix.data.JsonIO;
20 import be.nikiroo.fanfix.data.MetaData;
21 import be.nikiroo.fanfix.data.Paragraph;
22 import be.nikiroo.fanfix.data.Paragraph.ParagraphType;
23 import be.nikiroo.fanfix.data.Story;
24 import be.nikiroo.utils.Image;
25 import be.nikiroo.utils.LoginResult;
26 import be.nikiroo.utils.NanoHTTPD;
27 import be.nikiroo.utils.NanoHTTPD.Response;
28 import be.nikiroo.utils.NanoHTTPD.Response.Status;
29
30 public class WebLibraryServer extends WebLibraryServerHtml {
31 class WLoginResult extends LoginResult {
32 private boolean rw;
33 private boolean wl;
34 private boolean bl;
35
36 public WLoginResult(boolean badLogin, boolean badCookie) {
37 super(badLogin, badCookie);
38 }
39
40 public WLoginResult(String who, String key, String subkey, boolean rw,
41 boolean wl, boolean bl) {
42 super(who, key, subkey, (rw ? "|rw" : "") + (wl ? "|wl" : "")
43 + (bl ? "|bl" : "") + "|");
44 this.rw = rw;
45 this.wl = wl;
46 this.bl = bl;
47 }
48
49 public WLoginResult(String cookie, String who, String key,
50 List<String> subkeys) {
51 super(cookie, who, key, subkeys,
52 subkeys == null || subkeys.isEmpty());
53 }
54
55 public boolean isRw() {
56 return getOption().contains("|rw|");
57 }
58
59 public boolean isWl() {
60 return getOption().contains("|wl|");
61 }
62
63 public boolean isBl() {
64 return getOption().contains("|bl|");
65 }
66 }
67
68 private Map<String, Story> storyCache = new HashMap<String, Story>();
69 private LinkedList<String> storyCacheOrder = new LinkedList<String>();
70 private long storyCacheSize = 0;
71 private long maxStoryCacheSize;
72
73 private List<String> whitelist;
74 private List<String> blacklist;
75
76 public WebLibraryServer(boolean secure) throws IOException {
77 super(secure);
78
79 int cacheMb = Instance.getInstance().getConfig()
80 .getInteger(Config.SERVER_MAX_CACHE_MB, 100);
81 maxStoryCacheSize = cacheMb * 1024 * 1024;
82
83 setTraceHandler(Instance.getInstance().getTraceHandler());
84
85 whitelist = Instance.getInstance().getConfig()
86 .getList(Config.SERVER_WHITELIST, new ArrayList<String>());
87 blacklist = Instance.getInstance().getConfig()
88 .getList(Config.SERVER_BLACKLIST, new ArrayList<String>());
89 }
90
91 /**
92 * Start the server (listen on the network for new connections).
93 * <p>
94 * Can only be called once.
95 * <p>
96 * This call is asynchronous, and will just start a new {@link Thread} on
97 * itself (see {@link WebLibraryServer#run()}).
98 */
99 public void start() {
100 new Thread(this).start();
101 }
102
103 @Override
104 protected WLoginResult login(boolean badLogin, boolean badCookie) {
105 return new WLoginResult(false, false);
106 }
107
108 @Override
109 protected WLoginResult login(String who, String cookie) {
110 List<String> subkeys = Instance.getInstance().getConfig()
111 .getList(Config.SERVER_ALLOWED_SUBKEYS);
112 String realKey = Instance.getInstance().getConfig()
113 .getString(Config.SERVER_KEY);
114
115 return new WLoginResult(cookie, who, realKey, subkeys);
116 }
117
118 // allow rw/wl
119 @Override
120 protected WLoginResult login(String who, String key, String subkey) {
121 String realKey = Instance.getInstance().getConfig()
122 .getString(Config.SERVER_KEY, "");
123
124 // I don't like NULLs...
125 key = key == null ? "" : key;
126 subkey = subkey == null ? "" : subkey;
127
128 if (!realKey.equals(key)) {
129 return new WLoginResult(true, false);
130 }
131
132 // defaults are true (as previous versions without the feature)
133 boolean rw = true;
134 boolean wl = true;
135 boolean bl = true;
136
137 rw = Instance.getInstance().getConfig().getBoolean(Config.SERVER_RW,
138 rw);
139
140 List<String> allowed = Instance.getInstance().getConfig().getList(
141 Config.SERVER_ALLOWED_SUBKEYS, new ArrayList<String>());
142
143 if (!allowed.isEmpty()) {
144 if (!allowed.contains(subkey)) {
145 return new WLoginResult(true, false);
146 }
147
148 if ((subkey + "|").contains("|rw|")) {
149 rw = true;
150 }
151 if ((subkey + "|").contains("|wl|")) {
152 wl = false; // |wl| = bypass whitelist
153 }
154 if ((subkey + "|").contains("|bl|")) {
155 bl = false; // |bl| = bypass blacklist
156 }
157 }
158
159 return new WLoginResult(who, key, subkey, rw, wl, bl);
160 }
161
162 @Override
163 protected Response getList(String uri, WLoginResult login)
164 throws IOException {
165 if (WebLibraryUrls.LIST_URL_METADATA.equals(uri)) {
166 List<JSONObject> jsons = new ArrayList<JSONObject>();
167 for (MetaData meta : metas(login)) {
168 jsons.add(JsonIO.toJson(meta));
169 }
170
171 return newInputStreamResponse("application/json",
172 new ByteArrayInputStream(
173 new JSONArray(jsons).toString().getBytes()));
174 }
175
176 return NanoHTTPD.newFixedLengthResponse(Status.BAD_REQUEST,
177 NanoHTTPD.MIME_PLAINTEXT, null);
178 }
179
180 // /story/luid/chapter/para <-- text/image
181 // /story/luid/cover <-- image
182 // /story/luid/metadata <-- json
183 // /story/luid/json <-- json, whole chapter (no images)
184 @Override
185 protected Response getStoryPart(String uri, WLoginResult login) {
186 String[] uriParts = uri.split("/");
187 int off = 2;
188
189 if (uriParts.length < off + 2) {
190 return NanoHTTPD.newFixedLengthResponse(Status.BAD_REQUEST,
191 NanoHTTPD.MIME_PLAINTEXT, null);
192 }
193
194 String luid = uriParts[off + 0];
195 String chapterStr = uriParts[off + 1];
196 String imageStr = uriParts.length < off + 3 ? null : uriParts[off + 2];
197
198 // 1-based (0 = desc)
199 int chapter = 0;
200 if (chapterStr != null && !"cover".equals(chapterStr)
201 && !"metadata".equals(chapterStr)
202 && !"json".equals(chapterStr)) {
203 try {
204 chapter = Integer.parseInt(chapterStr);
205 if (chapter < 0) {
206 throw new NumberFormatException();
207 }
208 } catch (NumberFormatException e) {
209 return NanoHTTPD.newFixedLengthResponse(Status.BAD_REQUEST,
210 NanoHTTPD.MIME_PLAINTEXT, "Chapter is not valid");
211 }
212 }
213
214 // 1-based
215 int paragraph = 1;
216 if (imageStr != null) {
217 try {
218 paragraph = Integer.parseInt(imageStr);
219 if (paragraph < 0) {
220 throw new NumberFormatException();
221 }
222 } catch (NumberFormatException e) {
223 return NanoHTTPD.newFixedLengthResponse(Status.BAD_REQUEST,
224 NanoHTTPD.MIME_PLAINTEXT, "Paragraph is not valid");
225 }
226 }
227
228 String mimeType = NanoHTTPD.MIME_PLAINTEXT;
229 InputStream in = null;
230 try {
231 if ("cover".equals(chapterStr)) {
232 Image img = storyCover(luid, login);
233 if (img != null) {
234 in = img.newInputStream();
235 }
236 // TODO: get correct image type
237 mimeType = "image/png";
238 } else if ("metadata".equals(chapterStr)) {
239 MetaData meta = meta(luid, login);
240 JSONObject json = JsonIO.toJson(meta);
241 mimeType = "application/json";
242 in = new ByteArrayInputStream(json.toString().getBytes());
243 } else if ("json".equals(chapterStr)) {
244 Story story = story(luid, login);
245 JSONObject json = JsonIO.toJson(story);
246 mimeType = "application/json";
247 in = new ByteArrayInputStream(json.toString().getBytes());
248 } else {
249 Story story = story(luid, login);
250 if (story != null) {
251 if (chapter == 0) {
252 StringBuilder builder = new StringBuilder();
253 for (Paragraph p : story.getMeta().getResume()) {
254 if (builder.length() == 0) {
255 builder.append("\n");
256 }
257 builder.append(p.getContent());
258 }
259
260 in = new ByteArrayInputStream(
261 builder.toString().getBytes("utf-8"));
262 } else {
263 Paragraph para = story.getChapters().get(chapter - 1)
264 .getParagraphs().get(paragraph - 1);
265 Image img = para.getContentImage();
266 if (para.getType() == ParagraphType.IMAGE) {
267 // TODO: get correct image type
268 mimeType = "image/png";
269 in = img.newInputStream();
270 } else {
271 in = new ByteArrayInputStream(
272 para.getContent().getBytes("utf-8"));
273 }
274 }
275 }
276 }
277 } catch (IndexOutOfBoundsException e) {
278 return NanoHTTPD.newFixedLengthResponse(Status.NOT_FOUND,
279 NanoHTTPD.MIME_PLAINTEXT,
280 "Chapter or paragraph does not exist");
281 } catch (IOException e) {
282 Instance.getInstance().getTraceHandler()
283 .error(new IOException("Cannot get image: " + uri, e));
284 return NanoHTTPD.newFixedLengthResponse(Status.INTERNAL_ERROR,
285 NanoHTTPD.MIME_PLAINTEXT, "Error when processing request");
286 }
287
288 return newInputStreamResponse(mimeType, in);
289 }
290
291 @Override
292 protected Response getCover(String uri, WLoginResult login)
293 throws IOException {
294 String[] uriParts = uri.split("/");
295 int off = 2; // "" and "cover"
296
297 if (uriParts.length < off + 2) {
298 return NanoHTTPD.newFixedLengthResponse(Status.BAD_REQUEST,
299 NanoHTTPD.MIME_PLAINTEXT, "Invalid cover request");
300 }
301
302 String type = uriParts[off + 0];
303 String id = uriParts[off + 1];
304
305 InputStream in = null;
306
307 if ("story".equals(type)) {
308 Image img = storyCover(id, login);
309 if (img != null) {
310 in = img.newInputStream();
311 }
312 } else if ("source".equals(type)) {
313 Image img = sourceCover(id, login);
314 if (img != null) {
315 in = img.newInputStream();
316 }
317 } else if ("author".equals(type)) {
318 Image img = authorCover(id, login);
319 if (img != null) {
320 in = img.newInputStream();
321 }
322 } else {
323 return NanoHTTPD.newFixedLengthResponse(Status.BAD_REQUEST,
324 NanoHTTPD.MIME_PLAINTEXT,
325 "Invalid GET cover type: " + type);
326 }
327
328 // TODO: get correct image type
329 return newInputStreamResponse("image/png", in);
330 }
331
332 @Override
333 protected Response setCover(String uri, String luid, WLoginResult login)
334 throws IOException {
335 String[] uriParts = uri.split("/");
336 int off = 2; // "" and "cover"
337
338 if (uriParts.length < off + 2) {
339 return NanoHTTPD.newFixedLengthResponse(Status.BAD_REQUEST,
340 NanoHTTPD.MIME_PLAINTEXT, "Invalid cover request");
341 }
342
343 String type = uriParts[off + 0];
344 String id = uriParts[off + 1];
345
346 if ("source".equals(type)) {
347 sourceCover(id, login, luid);
348 } else if ("author".equals(type)) {
349 authorCover(id, login, luid);
350 } else {
351 return NanoHTTPD.newFixedLengthResponse(Status.BAD_REQUEST,
352 NanoHTTPD.MIME_PLAINTEXT,
353 "Invalid SET cover type: " + type);
354 }
355
356 return newInputStreamResponse(NanoHTTPD.MIME_PLAINTEXT, null);
357 }
358
359 @Override
360 protected List<MetaData> metas(WLoginResult login) throws IOException {
361 BasicLibrary lib = Instance.getInstance().getLibrary();
362 List<MetaData> metas = new ArrayList<MetaData>();
363 for (MetaData meta : lib.getList().getMetas()) {
364 if (isAllowed(meta, login)) {
365 metas.add(meta);
366 }
367 }
368
369 return metas;
370 }
371
372 // NULL if not whitelist OK or if not found
373 @Override
374 protected Story story(String luid, WLoginResult login) throws IOException {
375 synchronized (storyCache) {
376 if (storyCache.containsKey(luid)) {
377 Story story = storyCache.get(luid);
378 if (!isAllowed(story.getMeta(), login))
379 return null;
380
381 return story;
382 }
383 }
384
385 Story story = null;
386 MetaData meta = meta(luid, login);
387 if (meta != null) {
388 BasicLibrary lib = Instance.getInstance().getLibrary();
389 story = lib.getStory(luid, null);
390 long size = sizeOf(story);
391
392 synchronized (storyCache) {
393 // Could have been added by another request
394 if (!storyCache.containsKey(luid)) {
395 while (!storyCacheOrder.isEmpty()
396 && storyCacheSize + size > maxStoryCacheSize) {
397 String oldestLuid = storyCacheOrder.removeFirst();
398 Story oldestStory = storyCache.remove(oldestLuid);
399 maxStoryCacheSize -= sizeOf(oldestStory);
400 }
401
402 storyCacheOrder.add(luid);
403 storyCache.put(luid, story);
404 }
405 }
406 }
407
408 return story;
409 }
410
411 private MetaData meta(String luid, WLoginResult login) throws IOException {
412 BasicLibrary lib = Instance.getInstance().getLibrary();
413 MetaData meta = lib.getInfo(luid);
414 if (!isAllowed(meta, login))
415 return null;
416
417 return meta;
418 }
419
420 private Image storyCover(String luid, WLoginResult login)
421 throws IOException {
422 MetaData meta = meta(luid, login);
423 if (meta != null) {
424 BasicLibrary lib = Instance.getInstance().getLibrary();
425 return lib.getCover(meta.getLuid());
426 }
427
428 return null;
429 }
430
431 private Image authorCover(String author, WLoginResult login)
432 throws IOException {
433 Image img = null;
434
435 List<MetaData> metas = new MetaResultList(metas(login)).filter(null,
436 author, null);
437 if (metas.size() > 0) {
438 BasicLibrary lib = Instance.getInstance().getLibrary();
439 img = lib.getCustomAuthorCover(author);
440 if (img == null)
441 img = lib.getCover(metas.get(0).getLuid());
442 }
443
444 return img;
445
446 }
447
448 private void authorCover(String author, WLoginResult login, String luid)
449 throws IOException {
450 if (meta(luid, login) != null) {
451 List<MetaData> metas = new MetaResultList(metas(login)).filter(null,
452 author, null);
453 if (metas.size() > 0) {
454 BasicLibrary lib = Instance.getInstance().getLibrary();
455 lib.setAuthorCover(author, luid);
456 }
457 }
458 }
459
460 private Image sourceCover(String source, WLoginResult login)
461 throws IOException {
462 Image img = null;
463
464 List<MetaData> metas = new MetaResultList(metas(login)).filter(source,
465 null, null);
466 if (metas.size() > 0) {
467 BasicLibrary lib = Instance.getInstance().getLibrary();
468 img = lib.getCustomSourceCover(source);
469 if (img == null)
470 img = lib.getCover(metas.get(0).getLuid());
471 }
472
473 return img;
474 }
475
476 private void sourceCover(String source, WLoginResult login, String luid)
477 throws IOException {
478 if (meta(luid, login) != null) {
479 List<MetaData> metas = new MetaResultList(metas(login))
480 .filter(source, null, null);
481 if (metas.size() > 0) {
482 BasicLibrary lib = Instance.getInstance().getLibrary();
483 lib.setSourceCover(source, luid);
484 }
485 }
486 }
487
488 private boolean isAllowed(MetaData meta, WLoginResult login) {
489 MetaResultList one = new MetaResultList(Arrays.asList(meta));
490 if (login.isWl() && !whitelist.isEmpty()) {
491 if (one.filter(whitelist, null, null).isEmpty()) {
492 return false;
493 }
494 }
495 if (login.isBl() && !blacklist.isEmpty()) {
496 if (!one.filter(blacklist, null, null).isEmpty()) {
497 return false;
498 }
499 }
500
501 return true;
502 }
503
504 private long sizeOf(Story story) {
505 long size = 0;
506 for (Chapter chap : story) {
507 for (Paragraph para : chap) {
508 if (para.getType() == ParagraphType.IMAGE) {
509 size += para.getContentImage().getSize();
510 } else {
511 size += para.getContent().length();
512 }
513 }
514 }
515
516 return size;
517 }
518
519 public static void main(String[] args) throws IOException {
520 Instance.init();
521 WebLibraryServer web = new WebLibraryServer(false);
522 web.run();
523 }
524 }