1 package be
.nikiroo
.fanfix
.library
;
4 import java
.io
.FileInputStream
;
5 import java
.io
.IOException
;
6 import java
.io
.InputStream
;
7 import java
.security
.KeyStore
;
8 import java
.util
.HashMap
;
12 import javax
.net
.ssl
.KeyManagerFactory
;
13 import javax
.net
.ssl
.SSLServerSocketFactory
;
15 import be
.nikiroo
.fanfix
.Instance
;
16 import be
.nikiroo
.fanfix
.bundles
.Config
;
17 import be
.nikiroo
.fanfix
.bundles
.UiConfig
;
18 import be
.nikiroo
.fanfix
.data
.Chapter
;
19 import be
.nikiroo
.fanfix
.data
.MetaData
;
20 import be
.nikiroo
.fanfix
.data
.Paragraph
;
21 import be
.nikiroo
.fanfix
.data
.Paragraph
.ParagraphType
;
22 import be
.nikiroo
.fanfix
.data
.Story
;
23 import be
.nikiroo
.fanfix
.library
.WebLibraryServer
.WLoginResult
;
24 import be
.nikiroo
.fanfix
.library
.web
.WebLibraryServerIndex
;
25 import be
.nikiroo
.fanfix
.reader
.TextOutput
;
26 import be
.nikiroo
.utils
.IOUtils
;
27 import be
.nikiroo
.utils
.NanoHTTPD
;
28 import be
.nikiroo
.utils
.NanoHTTPD
.IHTTPSession
;
29 import be
.nikiroo
.utils
.NanoHTTPD
.Response
;
30 import be
.nikiroo
.utils
.NanoHTTPD
.Response
.Status
;
31 import be
.nikiroo
.utils
.TraceHandler
;
32 import be
.nikiroo
.utils
.Version
;
34 abstract class WebLibraryServerHtml
implements Runnable
{
35 private NanoHTTPD server
;
36 protected TraceHandler tracer
= new TraceHandler();
38 abstract protected WLoginResult
login(String who
, String cookie
);
40 abstract protected WLoginResult
login(String who
, String key
,
43 abstract protected WLoginResult
login(boolean badLogin
, boolean badCookie
);
45 abstract protected Response
getList(String uri
, WLoginResult login
)
48 abstract protected Response
getStoryPart(String uri
, WLoginResult login
);
50 abstract protected Response
setStoryPart(String uri
, String value
,
51 WLoginResult login
) throws IOException
;
53 abstract protected Response
getCover(String uri
, WLoginResult login
)
56 abstract protected Response
setCover(String uri
, String luid
,
57 WLoginResult login
) throws IOException
;
59 abstract protected List
<MetaData
> metas(WLoginResult login
)
62 abstract protected Story
story(String luid
, WLoginResult login
)
65 protected abstract Response
imprt(String uri
, String url
,
66 WLoginResult login
) throws IOException
;
68 protected abstract Response
imprtProgress(String uri
, WLoginResult login
);
70 protected abstract Response
delete(String uri
, WLoginResult login
)
74 * Wait until all operations are done and stop the server.
76 * All the new R/W operations will be refused after a call to stop.
78 protected abstract Response
stop(WLoginResult login
);
80 public WebLibraryServerHtml(boolean secure
) throws IOException
{
81 Integer port
= Instance
.getInstance().getConfig()
82 .getInteger(Config
.SERVER_PORT
);
84 throw new IOException(
85 "Cannot start web server: port not specified");
88 SSLServerSocketFactory ssf
= null;
90 String keystorePath
= Instance
.getInstance().getConfig()
91 .getString(Config
.SERVER_SSL_KEYSTORE
, "");
92 String keystorePass
= Instance
.getInstance().getConfig()
93 .getString(Config
.SERVER_SSL_KEYSTORE_PASS
);
95 if (secure
&& keystorePath
.isEmpty()) {
96 throw new IOException(
97 "Cannot start a secure web server: no keystore.jks file povided");
100 if (!keystorePath
.isEmpty()) {
101 File keystoreFile
= new File(keystorePath
);
103 KeyStore keystore
= KeyStore
104 .getInstance(KeyStore
.getDefaultType());
105 InputStream keystoreStream
= new FileInputStream(
108 keystore
.load(keystoreStream
,
109 keystorePass
.toCharArray());
110 KeyManagerFactory keyManagerFactory
= KeyManagerFactory
111 .getInstance(KeyManagerFactory
112 .getDefaultAlgorithm());
113 keyManagerFactory
.init(keystore
,
114 keystorePass
.toCharArray());
115 ssf
= NanoHTTPD
.makeSSLSocketFactory(keystore
,
118 keystoreStream
.close();
120 } catch (Exception e
) {
121 throw new IOException(e
.getMessage());
126 server
= new NanoHTTPD(port
) {
128 public Response
serve(final IHTTPSession session
) {
129 super.serve(session
);
131 String query
= session
.getQueryParameterString(); // a=a%20b&dd=2
132 Method method
= session
.getMethod(); // GET, POST..
133 String uri
= session
.getUri(); // /home.html
135 // need them in real time (not just those sent by the UA)
136 Map
<String
, String
> cookies
= new HashMap
<String
, String
>();
137 for (String cookie
: session
.getCookies()) {
138 cookies
.put(cookie
, session
.getCookies().read(cookie
));
141 WLoginResult login
= null;
142 Map
<String
, String
> params
= session
.getParms();
143 String who
= session
.getRemoteHostName()
144 + session
.getRemoteIpAddress();
145 if (params
.get("login") != null) {
146 login
= login(who
, params
.get("password"),
147 params
.get("login"));
149 String cookie
= cookies
.get("cookie");
150 login
= login(who
, cookie
);
153 if (login
.isSuccess()) {
155 session
.getCookies().set(new Cookie("cookie",
156 login
.getCookie(), "30; path=/"));
159 String optionName
= params
.get("optionName");
160 if (optionName
!= null && !optionName
.isEmpty()) {
161 String optionNo
= params
.get("optionNo");
162 String optionValue
= params
.get("optionValue");
163 if (optionNo
!= null || optionValue
== null
164 || optionValue
.isEmpty()) {
165 session
.getCookies().delete(optionName
);
166 cookies
.remove(optionName
);
168 session
.getCookies().set(new Cookie(optionName
,
169 optionValue
, "; path=/"));
170 cookies
.put(optionName
, optionValue
);
176 if (!login
.isSuccess()
177 && WebLibraryUrls
.isSupportedUrl(uri
, true)) {
178 rep
= loginPage(login
, uri
);
183 if (WebLibraryUrls
.isSupportedUrl(uri
, false)) {
184 if (WebLibraryUrls
.INDEX_URL
.equals(uri
)) {
185 rep
= root(session
, cookies
, login
);
186 } else if (WebLibraryUrls
.VERSION_URL
.equals(uri
)) {
187 rep
= newFixedLengthResponse(Status
.OK
,
189 Version
.getCurrentVersion().toString());
190 } else if (WebLibraryUrls
.isCoverUrl(uri
)) {
191 String luid
= params
.get("luid");
193 rep
= setCover(uri
, luid
, login
);
195 rep
= getCover(uri
, login
);
197 } else if (WebLibraryUrls
.isListUrl(uri
)) {
198 rep
= getList(uri
, login
);
199 } else if (WebLibraryUrls
.isStoryUrl(uri
)) {
200 String value
= params
.get("value");
202 rep
= setStoryPart(uri
, value
, login
);
204 rep
= getStoryPart(uri
, login
);
206 } else if (WebLibraryUrls
.isViewUrl(uri
)) {
207 rep
= getViewer(cookies
, uri
, login
);
208 } else if (WebLibraryUrls
.LOGOUT_URL
.equals(uri
)) {
209 session
.getCookies().delete("cookie");
210 cookies
.remove("cookie");
211 rep
= loginPage(login(false, false), uri
);
212 } else if (WebLibraryUrls
.isImprtUrl(uri
)) {
213 String url
= params
.get("url");
215 rep
= imprt(uri
, url
, login
);
217 rep
= imprtProgress(uri
, login
);
219 } else if (WebLibraryUrls
.isDeleteUrl(uri
)) {
220 rep
= delete(uri
, login
);
221 } else if (WebLibraryUrls
.EXIT_URL
.equals(uri
)) {
222 rep
= WebLibraryServerHtml
.this.stop(login
);
224 getTraceHandler().error(
225 "Supported URL was not processed: "
227 rep
= newFixedLengthResponse(
228 Status
.INTERNAL_ERROR
,
229 NanoHTTPD
.MIME_PLAINTEXT
,
230 "An error happened");
233 if (uri
.startsWith("/"))
234 uri
= uri
.substring(1);
235 InputStream in
= IOUtils
.openResource(
236 WebLibraryServerIndex
.class, uri
);
238 String mimeType
= MIME_PLAINTEXT
;
239 if (uri
.endsWith(".css")) {
240 mimeType
= "text/css";
241 } else if (uri
.endsWith(".html")) {
242 mimeType
= "text/html";
243 } else if (uri
.endsWith(".js")) {
244 mimeType
= "text/javascript";
246 rep
= newChunkedResponse(Status
.OK
, mimeType
,
251 getTraceHandler().trace("404: " + uri
);
252 rep
= newFixedLengthResponse(Status
.NOT_FOUND
,
253 NanoHTTPD
.MIME_PLAINTEXT
, "Not Found");
256 } catch (Exception e
) {
257 Instance
.getInstance().getTraceHandler().error(
258 new IOException("Cannot process web request",
260 rep
= newFixedLengthResponse(Status
.INTERNAL_ERROR
,
261 NanoHTTPD
.MIME_PLAINTEXT
, "An error occured");
270 getTraceHandler().trace("Install SSL on the web server...");
271 server
.makeSecure(ssf
, null);
272 getTraceHandler().trace("Done.");
279 server
.start(NanoHTTPD
.SOCKET_READ_TIMEOUT
, false);
280 } catch (IOException e
) {
281 tracer
.error(new IOException("Cannot start the web server", e
));
285 protected void doStop() {
290 * The traces handler for this {@link WebLibraryServerHtml}.
292 * @return the traces handler
294 public TraceHandler
getTraceHandler() {
299 * The traces handler for this {@link WebLibraryServerHtml}.
302 * the new traces handler
304 public void setTraceHandler(TraceHandler tracer
) {
305 if (tracer
== null) {
306 tracer
= new TraceHandler(false, false, false);
309 this.tracer
= tracer
;
312 private Response
loginPage(WLoginResult login
, String uri
) {
313 StringBuilder builder
= new StringBuilder();
315 appendPreHtml(builder
, true);
317 if (login
.isBadLogin()) {
319 "\t\t<div class='error'>Bad login or password</div>");
320 } else if (login
.isBadCookie()) {
322 "\t\t<div class='error'>Your session timed out</div>");
325 if (WebLibraryUrls
.LOGOUT_URL
.equals(uri
)) {
326 uri
= WebLibraryUrls
.INDEX_URL
;
329 builder
.append("\t\t<form method='POST' action='" + uri
330 + "' class='login'>\n");
332 "\t\t\t<p>You must be logged into the system to see the stories.</p>");
333 builder
.append("\t\t\t<input type='text' name='login' />\n");
334 builder
.append("\t\t\t<input type='password' name='password' />\n");
335 builder
.append("\t\t\t<input type='submit' value='Login' />\n");
336 builder
.append("\t\t</form>\n");
338 appendPostHtml(builder
);
340 return NanoHTTPD
.newFixedLengthResponse(Status
.FORBIDDEN
,
341 NanoHTTPD
.MIME_HTML
, builder
.toString());
344 private Response
root(IHTTPSession session
, Map
<String
, String
> cookies
,
345 WLoginResult login
) throws IOException
{
346 BasicLibrary lib
= Instance
.getInstance().getLibrary();
347 MetaResultList result
= new MetaResultList(metas(login
));
348 StringBuilder builder
= new StringBuilder();
350 appendPreHtml(builder
, true);
352 Map
<String
, String
> params
= session
.getParms();
354 String filter
= cookies
.get("filter");
355 if (params
.get("optionNo") != null)
357 if (filter
== null) {
361 String browser
= params
.get("browser") == null ?
""
362 : params
.get("browser");
363 String browser2
= params
.get("browser2") == null ?
""
364 : params
.get("browser2");
365 String browser3
= params
.get("browser3") == null ?
""
366 : params
.get("browser3");
368 String filterSource
= null;
369 String filterAuthor
= null;
370 String filterTag
= null;
372 // TODO: javascript in realtime, using visible=false + hide [submit]
374 builder
.append("\t\t<form class='browser'>\n");
375 builder
.append("\t\t\t<div class='breadcrumbs'>\n");
377 builder
.append("\t\t\t\t<select name='browser'>\n");
378 appendOption(builder
, 5, "", "", browser
);
379 appendOption(builder
, 5, "Sources", "sources", browser
);
380 appendOption(builder
, 5, "Authors", "authors", browser
);
381 appendOption(builder
, 5, "Tags", "tags", browser
);
382 builder
.append("\t\t\t\t</select>\n");
384 if (!browser
.isEmpty()) {
385 builder
.append("\t\t\t\t<select name='browser2'>\n");
386 if (browser
.equals("sources")) {
387 filterSource
= browser2
.isEmpty() ? filterSource
: browser2
;
388 // TODO: if 1 group -> no group
389 appendOption(builder
, 5, "", "", browser2
);
390 Map
<String
, List
<String
>> sources
= result
.getSourcesGrouped();
391 for (String source
: sources
.keySet()) {
392 appendOption(builder
, 5, source
, source
, browser2
);
394 } else if (browser
.equals("authors")) {
395 filterAuthor
= browser2
.isEmpty() ? filterAuthor
: browser2
;
396 // TODO: if 1 group -> no group
397 appendOption(builder
, 5, "", "", browser2
);
398 Map
<String
, List
<String
>> authors
= result
.getAuthorsGrouped();
399 for (String author
: authors
.keySet()) {
400 appendOption(builder
, 5, author
, author
, browser2
);
402 } else if (browser
.equals("tags")) {
403 filterTag
= browser2
.isEmpty() ? filterTag
: browser2
;
404 appendOption(builder
, 5, "", "", browser2
);
405 for (String tag
: result
.getTags()) {
406 appendOption(builder
, 5, tag
, tag
, browser2
);
409 builder
.append("\t\t\t\t</select>\n");
412 if (!browser2
.isEmpty()) {
413 if (browser
.equals("sources")) {
414 filterSource
= browser3
.isEmpty() ? filterSource
: browser3
;
415 Map
<String
, List
<String
>> sourcesGrouped
= result
416 .getSourcesGrouped();
417 List
<String
> sources
= sourcesGrouped
.get(browser2
);
418 if (sources
!= null && !sources
.isEmpty()) {
419 // TODO: single empty value
420 builder
.append("\t\t\t\t<select name='browser3'>\n");
421 appendOption(builder
, 5, "", "", browser3
);
422 for (String source
: sources
) {
423 appendOption(builder
, 5, source
, source
, browser3
);
425 builder
.append("\t\t\t\t</select>\n");
427 } else if (browser
.equals("authors")) {
428 filterAuthor
= browser3
.isEmpty() ? filterAuthor
: browser3
;
429 Map
<String
, List
<String
>> authorsGrouped
= result
430 .getAuthorsGrouped();
431 List
<String
> authors
= authorsGrouped
.get(browser2
);
432 if (authors
!= null && !authors
.isEmpty()) {
433 // TODO: single empty value
434 builder
.append("\t\t\t\t<select name='browser3'>\n");
435 appendOption(builder
, 5, "", "", browser3
);
436 for (String author
: authors
) {
437 appendOption(builder
, 5, author
, author
, browser3
);
439 builder
.append("\t\t\t\t</select>\n");
444 builder
.append("\t\t\t\t<input type='submit' value='Select'/>\n");
445 builder
.append("\t\t\t</div>\n");
447 // TODO: javascript in realtime, using visible=false + hide [submit]
448 builder
.append("\t\t\t<div class='filter'>\n");
449 builder
.append("\t\t\t\t<span class='label'>Filter: </span>\n");
451 "\t\t\t\t<input name='optionName' type='hidden' value='filter' />\n");
452 builder
.append("\t\t\t\t<input name='optionValue' type='text' value='"
453 + filter
+ "' place-holder='...' />\n");
455 "\t\t\t\t<input name='optionNo' type='submit' value='x' />\n");
457 "\t\t\t\t<input name='submit' type='submit' value='Filter' />\n");
458 builder
.append("\t\t\t</div>\n");
459 builder
.append("\t\t</form>\n");
461 builder
.append("\t\t<div class='books'>\n");
462 for (MetaData meta
: result
.getMetas()) {
463 if (!filter
.isEmpty() && !meta
.getTitle().toLowerCase()
464 .contains(filter
.toLowerCase())) {
469 if (filterSource
!= null
470 && !filterSource
.equals(meta
.getSource())) {
475 if (filterAuthor
!= null
476 && !filterAuthor
.equals(meta
.getAuthor())) {
480 if (filterTag
!= null && !meta
.getTags().contains(filterTag
)) {
484 builder
.append("\t\t\t<div class='book_line'>");
485 builder
.append("<a href='");
487 WebLibraryUrls
.getViewUrl(meta
.getLuid(), null, null));
489 builder
.append(" class='link'>");
491 if (lib
.isCached(meta
.getLuid())) {
494 "<span class='cache_icon cached'>◉</span>");
498 "<span class='cache_icon uncached'>○</span>");
500 builder
.append("<span class='luid'>");
501 builder
.append(meta
.getLuid());
502 builder
.append("</span>");
503 builder
.append("<span class='title'>");
504 builder
.append(meta
.getTitle());
505 builder
.append("</span>");
506 builder
.append("<span class='author'>");
507 if (meta
.getAuthor() != null && !meta
.getAuthor().isEmpty()) {
508 builder
.append("(").append(meta
.getAuthor()).append(")");
510 builder
.append("</span>");
511 builder
.append("</a></div>\n");
513 builder
.append("\t\t</div>\n");
515 appendPostHtml(builder
);
516 return NanoHTTPD
.newFixedLengthResponse(builder
.toString());
519 private Response
getViewer(Map
<String
, String
> cookies
, String uri
,
520 WLoginResult login
) {
521 String
[] cover
= uri
.split("/");
524 if (cover
.length
< off
+ 2) {
525 return NanoHTTPD
.newFixedLengthResponse(Status
.BAD_REQUEST
,
526 NanoHTTPD
.MIME_PLAINTEXT
, null);
529 String type
= cover
[off
+ 0];
530 String luid
= cover
[off
+ 1];
531 String chapterStr
= cover
.length
< off
+ 3 ?
null : cover
[off
+ 2];
532 String paragraphStr
= cover
.length
< off
+ 4 ?
null : cover
[off
+ 3];
534 // 1-based (0 = desc)
536 if (chapterStr
!= null) {
538 chapter
= Integer
.parseInt(chapterStr
);
540 throw new NumberFormatException();
542 } catch (NumberFormatException e
) {
543 return NanoHTTPD
.newFixedLengthResponse(Status
.BAD_REQUEST
,
544 NanoHTTPD
.MIME_PLAINTEXT
, "Chapter is not valid");
550 if (paragraphStr
!= null) {
552 paragraph
= Integer
.parseInt(paragraphStr
);
553 if (paragraph
<= 0) {
554 throw new NumberFormatException();
556 } catch (NumberFormatException e
) {
557 return NanoHTTPD
.newFixedLengthResponse(Status
.BAD_REQUEST
,
558 NanoHTTPD
.MIME_PLAINTEXT
, "Paragraph is not valid");
563 Story story
= story(luid
, login
);
565 return NanoHTTPD
.newFixedLengthResponse(Status
.NOT_FOUND
,
566 NanoHTTPD
.MIME_PLAINTEXT
, "Story not found");
569 StringBuilder builder
= new StringBuilder();
570 appendPreHtml(builder
, false);
572 // For images documents, always go to the images if not chap 0 desc
573 if (story
.getMeta().isImageDocument()) {
574 if (chapter
> 0 && paragraph
<= 0)
580 chap
= story
.getMeta().getResume();
583 chap
= story
.getChapters().get(chapter
- 1);
584 } catch (IndexOutOfBoundsException e
) {
585 return NanoHTTPD
.newFixedLengthResponse(Status
.NOT_FOUND
,
586 NanoHTTPD
.MIME_PLAINTEXT
, "Chapter not found");
590 String first
, previous
, next
, last
;
592 StringBuilder content
= new StringBuilder();
594 String disabledLeft
= "";
595 String disabledRight
= "";
596 String disabledZoomReal
= "";
597 String disabledZoomWidth
= "";
598 String disabledZoomHeight
= "";
600 if (paragraph
<= 0) {
601 first
= WebLibraryUrls
.getViewUrl(luid
, 0, null);
602 previous
= WebLibraryUrls
.getViewUrl(luid
,
603 (Math
.max(chapter
- 1, 0)), null);
604 next
= WebLibraryUrls
.getViewUrl(luid
,
605 (Math
.min(chapter
+ 1, story
.getChapters().size())),
607 last
= WebLibraryUrls
.getViewUrl(luid
,
608 story
.getChapters().size(), null);
610 StringBuilder desc
= new StringBuilder();
613 desc
.append("\t\t\t<h1 class='title'>")
614 .append(story
.getMeta().getTitle())
616 desc
.append("\t\t\t<div class='desc'>\n");
618 "\t\t\t\t<a href='" + next
+ "' class='cover'>\n");
619 desc
.append("\t\t\t\t\t<img src='/story/" + luid
621 desc
.append("\t\t\t\t</a>\n");
622 desc
.append("\t\t\t\t<table class='details'>\n");
623 Map
<String
, String
> details
= BasicLibrary
624 .getMetaDesc(story
.getMeta());
625 for (String key
: details
.keySet()) {
626 appendTableRow(desc
, 5, key
, details
.get(key
));
628 desc
.append("\t\t\t\t</table>\n");
629 desc
.append("\t\t\t</div>\n");
630 desc
.append("\t\t\t<h1 class='title'>Description</h1>\n");
633 content
.append("\t\t<div class='viewer text'>\n");
634 content
.append(desc
);
635 if (chap
.getParagraphs().size() <= 0) {
636 content
.append("\t\t\tNo content provided.\n");
639 "\t\t\t<!-- The text is in HTML 3.2, so it can also work in Java Swing: -->\n"); //
640 content
.append("\t\t\t").append(
641 new TextOutput(false).convert(chap
, chapter
> 0))
644 content
.append("\t\t</div>\n");
647 disabledLeft
= " disabled='disbaled'";
648 if (chapter
>= story
.getChapters().size())
649 disabledRight
= " disabled='disbaled'";
651 first
= WebLibraryUrls
.getViewUrl(luid
, chapter
, 1);
652 previous
= WebLibraryUrls
.getViewUrl(luid
, chapter
,
653 (Math
.max(paragraph
- 1, 1)));
654 next
= WebLibraryUrls
.getViewUrl(luid
, chapter
,
655 (Math
.min(paragraph
+ 1, chap
.getParagraphs().size())));
656 last
= WebLibraryUrls
.getViewUrl(luid
, chapter
,
657 chap
.getParagraphs().size());
660 disabledLeft
= " disabled='disbaled'";
661 if (paragraph
>= chap
.getParagraphs().size())
662 disabledRight
= " disabled='disbaled'";
664 // First -> previous *chapter*
667 first
= WebLibraryUrls
.getViewUrl(luid
,
668 (Math
.max(chapter
- 1, 0)), null);
669 if (paragraph
<= 1) {
673 Paragraph para
= null;
675 para
= chap
.getParagraphs().get(paragraph
- 1);
676 } catch (IndexOutOfBoundsException e
) {
677 return NanoHTTPD
.newFixedLengthResponse(Status
.NOT_FOUND
,
678 NanoHTTPD
.MIME_PLAINTEXT
,
679 "Paragraph " + paragraph
+ " not found");
682 if (para
.getType() == ParagraphType
.IMAGE
) {
683 String zoomStyle
= "max-width: 100%;";
684 disabledZoomWidth
= " disabled='disabled'";
685 String zoomOption
= cookies
.get("zoom");
686 if (zoomOption
!= null && !zoomOption
.isEmpty()) {
687 if (zoomOption
.equals("real")) {
689 disabledZoomWidth
= "";
690 disabledZoomReal
= " disabled='disabled'";
691 } else if (zoomOption
.equals("width")) {
692 zoomStyle
= "max-width: 100%;";
693 } else if (zoomOption
.equals("height")) {
694 // see height of navbar + optionbar
695 zoomStyle
= "max-height: calc(100% - 128px);";
696 disabledZoomWidth
= "";
697 disabledZoomHeight
= " disabled='disabled'";
701 String javascript
= "document.getElementById(\"previous\").click(); return false;";
702 content
.append(String
.format("" //
703 + "\t\t<a class='viewer link' oncontextmenu='%s' href='%s'>\n"
704 + "\t\t\t<img class='viewer img' style='%s' src='%s'/>\n"
709 WebLibraryUrls
.getStoryUrl(luid
, chapter
,
712 content
.append(String
.format("" //
713 + "\t\t<div class='viewer text'>" //
720 builder
.append(String
.format("" //
721 + "\t\t<div class='bar navbar'>\n" //
722 + "\t\t\t<a%s class='button first' href='%s'><<</a>\n"//
723 + "\t\t\t<a%s id='previous' class='button previous' href='%s'><</a>\n" //
724 + "\t\t\t<div class='gotobox itemsbox'>\n" //
725 + "\t\t\t\t<div class='button goto'>%d</div>\n" //
726 + "\t\t\t\t<div class='items goto'>\n", //
727 disabledLeft
, first
, //
728 disabledLeft
, previous
, //
729 paragraph
> 0 ? paragraph
: chapter
//
732 // List of chap/para links
734 appendItemA(builder
, 5, WebLibraryUrls
.getViewUrl(luid
, 0, null),
735 "Description", paragraph
== 0 && chapter
== 0);
737 for (int i
= 1; i
<= chap
.getParagraphs().size(); i
++) {
738 appendItemA(builder
, 5,
739 WebLibraryUrls
.getViewUrl(luid
, chapter
, i
),
740 "Image " + i
, paragraph
== i
);
744 for (Chapter c
: story
.getChapters()) {
745 String chapName
= "Chapter " + c
.getNumber();
746 if (c
.getName() != null && !c
.getName().isEmpty()) {
747 chapName
+= ": " + c
.getName();
750 appendItemA(builder
, 5,
751 WebLibraryUrls
.getViewUrl(luid
, i
, null), chapName
,
758 builder
.append(String
.format("" //
759 + "\t\t\t\t</div>\n" //
760 + "\t\t\t</div>\n" //
761 + "\t\t\t<a%s class='button next' href='%s'>></a>\n" //
762 + "\t\t\t<a%s class='button last' href='%s'>>></a>\n"//
764 disabledRight
, next
, //
765 disabledRight
, last
//
768 builder
.append(content
);
770 builder
.append("\t\t<div class='bar optionbar");
772 builder
.append(" s4");
774 builder
.append(" s1");
776 builder
.append("'>\n");
777 builder
.append("\t\t\t<a class='button back' href='/'>BACK</a>\n");
780 builder
.append(String
.format("" //
781 + "\t\t\t<a%s class='button zoomreal' href='%s'>REAL</a>\n"//
782 + "\t\t\t<a%s class='button zoomwidth' href='%s'>WIDTH</a>\n"//
783 + "\t\t\t<a%s class='button zoomheight' href='%s'>HEIGHT</a>\n", //
785 uri
+ "?optionName=zoom&optionValue=real", //
787 uri
+ "?optionName=zoom&optionValue=width", //
789 uri
+ "?optionName=zoom&optionValue=height" //
793 builder
.append("\t\t</div>\n");
795 appendPostHtml(builder
);
796 return NanoHTTPD
.newFixedLengthResponse(Status
.OK
,
797 NanoHTTPD
.MIME_HTML
, builder
.toString());
798 } catch (IOException e
) {
799 Instance
.getInstance().getTraceHandler()
800 .error(new IOException("Cannot get image: " + uri
, e
));
801 return NanoHTTPD
.newFixedLengthResponse(Status
.INTERNAL_ERROR
,
802 NanoHTTPD
.MIME_PLAINTEXT
, "Error when processing request");
806 protected Response
newInputStreamResponse(String mimeType
, InputStream in
) {
808 return NanoHTTPD
.newFixedLengthResponse(Status
.NO_CONTENT
, "",
811 return NanoHTTPD
.newChunkedResponse(Status
.OK
, mimeType
, in
);
814 private String
getContentOf(String file
) {
815 InputStream in
= IOUtils
.openResource(WebLibraryServerIndex
.class,
819 return IOUtils
.readSmallStream(in
);
820 } catch (IOException e
) {
821 Instance
.getInstance().getTraceHandler().error(
822 new IOException("Cannot get file: index.pre.html", e
));
829 private void appendPreHtml(StringBuilder builder
, boolean banner
) {
830 String favicon
= "favicon.ico";
831 String icon
= Instance
.getInstance().getUiConfig()
832 .getString(UiConfig
.PROGRAM_ICON
);
834 favicon
= "icon_" + icon
.replace("-", "_") + ".png";
838 getContentOf("index.pre.html").replace("favicon.ico", favicon
));
841 builder
.append("\t\t<div class='banner'>\n");
842 builder
.append("\t\t\t<img class='ico' src='/") //
845 builder
.append("\t\t\t<h1>Fanfix</h1>\n");
846 builder
.append("\t\t\t<h2>") //
847 .append(Version
.getCurrentVersion()) //
849 builder
.append("\t\t</div>\n");
853 private void appendPostHtml(StringBuilder builder
) {
854 builder
.append(getContentOf("index.post.html"));
857 private void appendOption(StringBuilder builder
, int depth
, String name
,
858 String value
, String selected
) {
859 for (int i
= 0; i
< depth
; i
++) {
860 builder
.append("\t");
862 builder
.append("<option value='").append(value
).append("'");
863 if (value
.equals(selected
)) {
864 builder
.append(" selected='selected'");
866 builder
.append(">").append(name
).append("</option>\n");
869 private void appendTableRow(StringBuilder builder
, int depth
,
871 for (int i
= 0; i
< depth
; i
++) {
872 builder
.append("\t");
876 builder
.append("<tr>");
877 for (String td
: tds
) {
878 builder
.append("<td class='col");
879 builder
.append(col
++);
880 builder
.append("'>");
882 builder
.append("</td>");
884 builder
.append("</tr>\n");
887 private void appendItemA(StringBuilder builder
, int depth
, String link
,
888 String name
, boolean selected
) {
889 for (int i
= 0; i
< depth
; i
++) {
890 builder
.append("\t");
893 builder
.append("<a href='");
894 builder
.append(link
);
895 builder
.append("' class='item goto");
897 builder
.append(" selected");
899 builder
.append("'>");
900 builder
.append(name
);
901 builder
.append("</a>\n");