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
getCover(String uri
, WLoginResult login
)
53 abstract protected List
<MetaData
> metas(WLoginResult login
)
56 abstract protected Story
story(String luid
, WLoginResult login
)
59 public WebLibraryServerHtml(boolean secure
) throws IOException
{
60 Integer port
= Instance
.getInstance().getConfig()
61 .getInteger(Config
.SERVER_PORT
);
63 throw new IOException(
64 "Cannot start web server: port not specified");
67 SSLServerSocketFactory ssf
= null;
69 String keystorePath
= Instance
.getInstance().getConfig()
70 .getString(Config
.SERVER_SSL_KEYSTORE
, "");
71 String keystorePass
= Instance
.getInstance().getConfig()
72 .getString(Config
.SERVER_SSL_KEYSTORE_PASS
);
74 if (secure
&& keystorePath
.isEmpty()) {
75 throw new IOException(
76 "Cannot start a secure web server: no keystore.jks file povided");
79 if (!keystorePath
.isEmpty()) {
80 File keystoreFile
= new File(keystorePath
);
82 KeyStore keystore
= KeyStore
83 .getInstance(KeyStore
.getDefaultType());
84 InputStream keystoreStream
= new FileInputStream(
87 keystore
.load(keystoreStream
,
88 keystorePass
.toCharArray());
89 KeyManagerFactory keyManagerFactory
= KeyManagerFactory
90 .getInstance(KeyManagerFactory
91 .getDefaultAlgorithm());
92 keyManagerFactory
.init(keystore
,
93 keystorePass
.toCharArray());
94 ssf
= NanoHTTPD
.makeSSLSocketFactory(keystore
,
97 keystoreStream
.close();
99 } catch (Exception e
) {
100 throw new IOException(e
.getMessage());
105 server
= new NanoHTTPD(port
) {
107 public Response
serve(final IHTTPSession session
) {
108 super.serve(session
);
110 String query
= session
.getQueryParameterString(); // a=a%20b&dd=2
111 Method method
= session
.getMethod(); // GET, POST..
112 String uri
= session
.getUri(); // /home.html
114 // need them in real time (not just those sent by the UA)
115 Map
<String
, String
> cookies
= new HashMap
<String
, String
>();
116 for (String cookie
: session
.getCookies()) {
117 cookies
.put(cookie
, session
.getCookies().read(cookie
));
120 WLoginResult login
= null;
121 Map
<String
, String
> params
= session
.getParms();
122 String who
= session
.getRemoteHostName()
123 + session
.getRemoteIpAddress();
124 if (params
.get("login") != null) {
125 login
= login(who
, params
.get("password"),
126 params
.get("login"));
128 String cookie
= cookies
.get("cookie");
129 login
= login(who
, cookie
);
132 if (login
.isSuccess()) {
134 session
.getCookies().set(new Cookie("cookie",
135 login
.getCookie(), "30; path=/"));
138 String optionName
= params
.get("optionName");
139 if (optionName
!= null && !optionName
.isEmpty()) {
140 String optionNo
= params
.get("optionNo");
141 String optionValue
= params
.get("optionValue");
142 if (optionNo
!= null || optionValue
== null
143 || optionValue
.isEmpty()) {
144 session
.getCookies().delete(optionName
);
145 cookies
.remove(optionName
);
147 session
.getCookies().set(new Cookie(optionName
,
148 optionValue
, "; path=/"));
149 cookies
.put(optionName
, optionValue
);
155 if (!login
.isSuccess() && WebLibraryUrls
.isSupportedUrl(uri
)) {
156 rep
= loginPage(login
, uri
);
161 if (WebLibraryUrls
.isSupportedUrl(uri
)) {
162 if (WebLibraryUrls
.INDEX_URL
.equals(uri
)) {
163 rep
= root(session
, cookies
, login
);
164 } else if (WebLibraryUrls
.VERSION_URL
.equals(uri
)) {
165 rep
= newFixedLengthResponse(Status
.OK
,
167 Version
.getCurrentVersion().toString());
168 } else if (WebLibraryUrls
.isCoverUrl(uri
)) {
169 rep
= getCover(uri
, login
);
170 } else if (WebLibraryUrls
.isListUrl(uri
)) {
171 rep
= getList(uri
, login
);
172 } else if (WebLibraryUrls
.isStoryUrl(uri
)) {
173 rep
= getStoryPart(uri
, login
);
174 } else if (WebLibraryUrls
.isViewUrl(uri
)) {
175 rep
= getViewer(cookies
, uri
, login
);
176 } else if (WebLibraryUrls
.LOGOUT_URL
.equals(uri
)) {
177 session
.getCookies().delete("cookie");
178 cookies
.remove("cookie");
179 rep
= loginPage(login(false, false), uri
);
181 getTraceHandler().error(
182 "Supported URL was not processed: "
184 rep
= newFixedLengthResponse(
185 Status
.INTERNAL_ERROR
,
186 NanoHTTPD
.MIME_PLAINTEXT
,
187 "An error happened");
190 if (uri
.startsWith("/"))
191 uri
= uri
.substring(1);
192 InputStream in
= IOUtils
.openResource(
193 WebLibraryServerIndex
.class, uri
);
195 String mimeType
= MIME_PLAINTEXT
;
196 if (uri
.endsWith(".css")) {
197 mimeType
= "text/css";
198 } else if (uri
.endsWith(".html")) {
199 mimeType
= "text/html";
200 } else if (uri
.endsWith(".js")) {
201 mimeType
= "text/javascript";
203 rep
= newChunkedResponse(Status
.OK
, mimeType
,
208 getTraceHandler().trace("404: " + uri
);
209 rep
= newFixedLengthResponse(Status
.NOT_FOUND
,
210 NanoHTTPD
.MIME_PLAINTEXT
, "Not Found");
213 } catch (Exception e
) {
214 Instance
.getInstance().getTraceHandler().error(
215 new IOException("Cannot process web request",
217 rep
= newFixedLengthResponse(Status
.INTERNAL_ERROR
,
218 NanoHTTPD
.MIME_PLAINTEXT
, "An error occured");
229 getTraceHandler().trace("Install SSL on the web server...");
230 server
.makeSecure(ssf
, null);
231 getTraceHandler().trace("Done.");
238 server
.start(NanoHTTPD
.SOCKET_READ_TIMEOUT
, false);
239 } catch (IOException e
) {
240 tracer
.error(new IOException("Cannot start the web server", e
));
245 * The traces handler for this {@link WebLibraryServerHtml}.
247 * @return the traces handler
249 public TraceHandler
getTraceHandler() {
254 * The traces handler for this {@link WebLibraryServerHtml}.
257 * the new traces handler
259 public void setTraceHandler(TraceHandler tracer
) {
260 if (tracer
== null) {
261 tracer
= new TraceHandler(false, false, false);
264 this.tracer
= tracer
;
267 private Response
loginPage(WLoginResult login
, String uri
) {
268 StringBuilder builder
= new StringBuilder();
270 appendPreHtml(builder
, true);
272 if (login
.isBadLogin()) {
273 builder
.append("<div class='error'>Bad login or password</div>");
274 } else if (login
.isBadCookie()) {
275 builder
.append("<div class='error'>Your session timed out</div>");
278 if (WebLibraryUrls
.LOGOUT_URL
.equals(uri
)) {
279 uri
= WebLibraryUrls
.INDEX_URL
;
283 "<form method='POST' action='" + uri
+ "' class='login'>\n");
285 "<p>You must be logged into the system to see the stories.</p>");
286 builder
.append("\t<input type='text' name='login' />\n");
287 builder
.append("\t<input type='password' name='password' />\n");
288 builder
.append("\t<input type='submit' value='Login' />\n");
289 builder
.append("</form>\n");
291 appendPostHtml(builder
);
293 return NanoHTTPD
.newFixedLengthResponse(Status
.FORBIDDEN
,
294 NanoHTTPD
.MIME_HTML
, builder
.toString());
297 private Response
root(IHTTPSession session
, Map
<String
, String
> cookies
,
298 WLoginResult login
) throws IOException
{
299 BasicLibrary lib
= Instance
.getInstance().getLibrary();
300 MetaResultList result
= new MetaResultList(metas(login
));
301 StringBuilder builder
= new StringBuilder();
303 appendPreHtml(builder
, true);
305 Map
<String
, String
> params
= session
.getParms();
307 String filter
= cookies
.get("filter");
308 if (params
.get("optionNo") != null)
310 if (filter
== null) {
314 String browser
= params
.get("browser") == null ?
""
315 : params
.get("browser");
316 String browser2
= params
.get("browser2") == null ?
""
317 : params
.get("browser2");
318 String browser3
= params
.get("browser3") == null ?
""
319 : params
.get("browser3");
321 String filterSource
= null;
322 String filterAuthor
= null;
323 String filterTag
= null;
325 // TODO: javascript in realtime, using visible=false + hide [submit]
327 builder
.append("<form class='browser'>\n");
328 builder
.append("<div class='breadcrumbs'>\n");
330 builder
.append("\t<select name='browser'>");
331 appendOption(builder
, 2, "", "", browser
);
332 appendOption(builder
, 2, "Sources", "sources", browser
);
333 appendOption(builder
, 2, "Authors", "authors", browser
);
334 appendOption(builder
, 2, "Tags", "tags", browser
);
335 builder
.append("\t</select>\n");
337 if (!browser
.isEmpty()) {
338 builder
.append("\t<select name='browser2'>");
339 if (browser
.equals("sources")) {
340 filterSource
= browser2
.isEmpty() ? filterSource
: browser2
;
341 // TODO: if 1 group -> no group
342 appendOption(builder
, 2, "", "", browser2
);
343 Map
<String
, List
<String
>> sources
= result
.getSourcesGrouped();
344 for (String source
: sources
.keySet()) {
345 appendOption(builder
, 2, source
, source
, browser2
);
347 } else if (browser
.equals("authors")) {
348 filterAuthor
= browser2
.isEmpty() ? filterAuthor
: browser2
;
349 // TODO: if 1 group -> no group
350 appendOption(builder
, 2, "", "", browser2
);
351 Map
<String
, List
<String
>> authors
= result
.getAuthorsGrouped();
352 for (String author
: authors
.keySet()) {
353 appendOption(builder
, 2, author
, author
, browser2
);
355 } else if (browser
.equals("tags")) {
356 filterTag
= browser2
.isEmpty() ? filterTag
: browser2
;
357 appendOption(builder
, 2, "", "", browser2
);
358 for (String tag
: result
.getTags()) {
359 appendOption(builder
, 2, tag
, tag
, browser2
);
362 builder
.append("\t</select>\n");
365 if (!browser2
.isEmpty()) {
366 if (browser
.equals("sources")) {
367 filterSource
= browser3
.isEmpty() ? filterSource
: browser3
;
368 Map
<String
, List
<String
>> sourcesGrouped
= result
369 .getSourcesGrouped();
370 List
<String
> sources
= sourcesGrouped
.get(browser2
);
371 if (sources
!= null && !sources
.isEmpty()) {
372 // TODO: single empty value
373 builder
.append("\t<select name='browser3'>");
374 appendOption(builder
, 2, "", "", browser3
);
375 for (String source
: sources
) {
376 appendOption(builder
, 2, source
, source
, browser3
);
378 builder
.append("\t</select>\n");
380 } else if (browser
.equals("authors")) {
381 filterAuthor
= browser3
.isEmpty() ? filterAuthor
: browser3
;
382 Map
<String
, List
<String
>> authorsGrouped
= result
383 .getAuthorsGrouped();
384 List
<String
> authors
= authorsGrouped
.get(browser2
);
385 if (authors
!= null && !authors
.isEmpty()) {
386 // TODO: single empty value
387 builder
.append("\t<select name='browser3'>");
388 appendOption(builder
, 2, "", "", browser3
);
389 for (String author
: authors
) {
390 appendOption(builder
, 2, author
, author
, browser3
);
392 builder
.append("\t</select>\n");
397 builder
.append("\t<input type='submit' value='Select'/>\n");
398 builder
.append("</div>\n");
400 // TODO: javascript in realtime, using visible=false + hide [submit]
401 builder
.append("<div class='filter'>\n");
402 builder
.append("\t<span class='label'>Filter: </span>\n");
404 "\t<input name='optionName' type='hidden' value='filter' />\n");
405 builder
.append("\t<input name='optionValue' type='text' value='"
406 + filter
+ "' place-holder='...' />\n");
407 builder
.append("\t<input name='optionNo' type='submit' value='x' />");
409 "\t<input name='submit' type='submit' value='Filter' />\n");
410 builder
.append("</div>\n");
411 builder
.append("</form>\n");
413 builder
.append("\t<div class='books'>");
414 for (MetaData meta
: result
.getMetas()) {
415 if (!filter
.isEmpty() && !meta
.getTitle().toLowerCase()
416 .contains(filter
.toLowerCase())) {
421 if (filterSource
!= null
422 && !filterSource
.equals(meta
.getSource())) {
427 if (filterAuthor
!= null
428 && !filterAuthor
.equals(meta
.getAuthor())) {
432 if (filterTag
!= null && !meta
.getTags().contains(filterTag
)) {
436 builder
.append("<div class='book_line'>");
437 builder
.append("<a href='");
439 WebLibraryUrls
.getViewUrl(meta
.getLuid(), null, null));
441 builder
.append(" class='link'>");
443 if (lib
.isCached(meta
.getLuid())) {
446 "<span class='cache_icon cached'>◉</span>");
450 "<span class='cache_icon uncached'>○</span>");
452 builder
.append("<span class='luid'>");
453 builder
.append(meta
.getLuid());
454 builder
.append("</span>");
455 builder
.append("<span class='title'>");
456 builder
.append(meta
.getTitle());
457 builder
.append("</span>");
458 builder
.append("<span class='author'>");
459 if (meta
.getAuthor() != null && !meta
.getAuthor().isEmpty()) {
460 builder
.append("(").append(meta
.getAuthor()).append(")");
462 builder
.append("</span>");
463 builder
.append("</a></div>\n");
465 builder
.append("</div>");
467 appendPostHtml(builder
);
468 return NanoHTTPD
.newFixedLengthResponse(builder
.toString());
471 private Response
getViewer(Map
<String
, String
> cookies
, String uri
,
472 WLoginResult login
) {
473 String
[] cover
= uri
.split("/");
476 if (cover
.length
< off
+ 2) {
477 return NanoHTTPD
.newFixedLengthResponse(Status
.BAD_REQUEST
,
478 NanoHTTPD
.MIME_PLAINTEXT
, null);
481 String type
= cover
[off
+ 0];
482 String luid
= cover
[off
+ 1];
483 String chapterStr
= cover
.length
< off
+ 3 ?
null : cover
[off
+ 2];
484 String paragraphStr
= cover
.length
< off
+ 4 ?
null : cover
[off
+ 3];
486 // 1-based (0 = desc)
488 if (chapterStr
!= null) {
490 chapter
= Integer
.parseInt(chapterStr
);
492 throw new NumberFormatException();
494 } catch (NumberFormatException e
) {
495 return NanoHTTPD
.newFixedLengthResponse(Status
.BAD_REQUEST
,
496 NanoHTTPD
.MIME_PLAINTEXT
, "Chapter is not valid");
502 if (paragraphStr
!= null) {
504 paragraph
= Integer
.parseInt(paragraphStr
);
505 if (paragraph
<= 0) {
506 throw new NumberFormatException();
508 } catch (NumberFormatException e
) {
509 return NanoHTTPD
.newFixedLengthResponse(Status
.BAD_REQUEST
,
510 NanoHTTPD
.MIME_PLAINTEXT
, "Paragraph is not valid");
515 Story story
= story(luid
, login
);
517 return NanoHTTPD
.newFixedLengthResponse(Status
.NOT_FOUND
,
518 NanoHTTPD
.MIME_PLAINTEXT
, "Story not found");
521 StringBuilder builder
= new StringBuilder();
522 appendPreHtml(builder
, false);
524 // For images documents, always go to the images if not chap 0 desc
525 if (story
.getMeta().isImageDocument()) {
526 if (chapter
> 0 && paragraph
<= 0)
532 chap
= story
.getMeta().getResume();
535 chap
= story
.getChapters().get(chapter
- 1);
536 } catch (IndexOutOfBoundsException e
) {
537 return NanoHTTPD
.newFixedLengthResponse(Status
.NOT_FOUND
,
538 NanoHTTPD
.MIME_PLAINTEXT
, "Chapter not found");
542 String first
, previous
, next
, last
;
544 StringBuilder content
= new StringBuilder();
546 String disabledLeft
= "";
547 String disabledRight
= "";
548 String disabledZoomReal
= "";
549 String disabledZoomWidth
= "";
550 String disabledZoomHeight
= "";
552 if (paragraph
<= 0) {
553 first
= WebLibraryUrls
.getViewUrl(luid
, 0, null);
554 previous
= WebLibraryUrls
.getViewUrl(luid
,
555 (Math
.max(chapter
- 1, 0)), null);
556 next
= WebLibraryUrls
.getViewUrl(luid
,
557 (Math
.min(chapter
+ 1, story
.getChapters().size())),
559 last
= WebLibraryUrls
.getViewUrl(luid
,
560 story
.getChapters().size(), null);
562 StringBuilder desc
= new StringBuilder();
565 desc
.append("<h1 class='title'>");
566 desc
.append(story
.getMeta().getTitle());
567 desc
.append("</h1>\n");
568 desc
.append("<div class='desc'>\n");
569 desc
.append("\t<a href='" + next
+ "' class='cover'>\n");
570 desc
.append("\t\t<img src='/story/" + luid
+ "/cover'/>\n");
571 desc
.append("\t</a>\n");
572 desc
.append("\t<table class='details'>\n");
573 Map
<String
, String
> details
= BasicLibrary
574 .getMetaDesc(story
.getMeta());
575 for (String key
: details
.keySet()) {
576 appendTableRow(desc
, 2, key
, details
.get(key
));
578 desc
.append("\t</table>\n");
579 desc
.append("</div>\n");
580 desc
.append("<h1 class='title'>Description</h1>\n");
583 content
.append("<div class='viewer text'>\n");
584 content
.append(desc
);
585 String description
= new TextOutput(false).convert(chap
,
587 content
.append(chap
.getParagraphs().size() <= 0
588 ?
"No content provided."
590 content
.append("</div>\n");
593 disabledLeft
= " disabled='disbaled'";
594 if (chapter
>= story
.getChapters().size())
595 disabledRight
= " disabled='disbaled'";
597 first
= WebLibraryUrls
.getViewUrl(luid
, chapter
, 1);
598 previous
= WebLibraryUrls
.getViewUrl(luid
, chapter
,
599 (Math
.max(paragraph
- 1, 1)));
600 next
= WebLibraryUrls
.getViewUrl(luid
, chapter
,
601 (Math
.min(paragraph
+ 1, chap
.getParagraphs().size())));
602 last
= WebLibraryUrls
.getViewUrl(luid
, chapter
,
603 chap
.getParagraphs().size());
606 disabledLeft
= " disabled='disbaled'";
607 if (paragraph
>= chap
.getParagraphs().size())
608 disabledRight
= " disabled='disbaled'";
610 // First -> previous *chapter*
613 first
= WebLibraryUrls
.getViewUrl(luid
,
614 (Math
.max(chapter
- 1, 0)), null);
615 if (paragraph
<= 1) {
619 Paragraph para
= null;
621 para
= chap
.getParagraphs().get(paragraph
- 1);
622 } catch (IndexOutOfBoundsException e
) {
623 return NanoHTTPD
.newFixedLengthResponse(Status
.NOT_FOUND
,
624 NanoHTTPD
.MIME_PLAINTEXT
,
625 "Paragraph " + paragraph
+ " not found");
628 if (para
.getType() == ParagraphType
.IMAGE
) {
629 String zoomStyle
= "max-width: 100%;";
630 disabledZoomWidth
= " disabled='disabled'";
631 String zoomOption
= cookies
.get("zoom");
632 if (zoomOption
!= null && !zoomOption
.isEmpty()) {
633 if (zoomOption
.equals("real")) {
635 disabledZoomWidth
= "";
636 disabledZoomReal
= " disabled='disabled'";
637 } else if (zoomOption
.equals("width")) {
638 zoomStyle
= "max-width: 100%;";
639 } else if (zoomOption
.equals("height")) {
640 // see height of navbar + optionbar
641 zoomStyle
= "max-height: calc(100% - 128px);";
642 disabledZoomWidth
= "";
643 disabledZoomHeight
= " disabled='disabled'";
647 String javascript
= "document.getElementById(\"previous\").click(); return false;";
648 content
.append(String
.format("" //
649 + "<a class='viewer link' oncontextmenu='%s' href='%s'>"
650 + "<img class='viewer img' style='%s' src='%s'/>"
655 WebLibraryUrls
.getStoryUrl(luid
, chapter
,
658 content
.append(String
.format("" //
659 + "<div class='viewer text'>%s</div>", //
664 builder
.append(String
.format("" //
665 + "<div class='bar navbar'>\n" //
666 + "\t<a%s class='button first' href='%s'><<</a>\n"//
667 + "\t<a%s id='previous' class='button previous' href='%s'><</a>\n" //
668 + "\t<div class='gotobox itemsbox'>\n" //
669 + "\t\t<div class='button goto'>%d</div>\n" //
670 + "\t\t<div class='items goto'>\n", //
671 disabledLeft
, first
, //
672 disabledLeft
, previous
, //
673 paragraph
> 0 ? paragraph
: chapter
//
676 // List of chap/para links
678 appendItemA(builder
, 3, WebLibraryUrls
.getViewUrl(luid
, 0, null),
679 "Description", paragraph
== 0 && chapter
== 0);
681 for (int i
= 1; i
<= chap
.getParagraphs().size(); i
++) {
682 appendItemA(builder
, 3,
683 WebLibraryUrls
.getViewUrl(luid
, chapter
, i
),
684 "Image " + i
, paragraph
== i
);
688 for (Chapter c
: story
.getChapters()) {
689 String chapName
= "Chapter " + c
.getNumber();
690 if (c
.getName() != null && !c
.getName().isEmpty()) {
691 chapName
+= ": " + c
.getName();
694 appendItemA(builder
, 3,
695 WebLibraryUrls
.getViewUrl(luid
, i
, null), chapName
,
702 builder
.append(String
.format("" //
705 + "\t<a%s class='button next' href='%s'>></a>\n" //
706 + "\t<a%s class='button last' href='%s'>>></a>\n"//
708 disabledRight
, next
, //
709 disabledRight
, last
//
712 builder
.append(content
);
714 builder
.append("<div class='bar optionbar ");
716 builder
.append("s4");
718 builder
.append("s1");
720 builder
.append("'>\n");
721 builder
.append(" <a class='button back' href='/'>BACK</a>\n");
724 builder
.append(String
.format("" //
725 + "\t<a%s class='button zoomreal' href='%s'>REAL</a>\n"//
726 + "\t<a%s class='button zoomwidth' href='%s'>WIDTH</a>\n"//
727 + "\t<a%s class='button zoomheight' href='%s'>HEIGHT</a>\n"//
730 uri
+ "?optionName=zoom&optionValue=real", //
732 uri
+ "?optionName=zoom&optionValue=width", //
734 uri
+ "?optionName=zoom&optionValue=height" //
738 appendPostHtml(builder
);
739 return NanoHTTPD
.newFixedLengthResponse(Status
.OK
,
740 NanoHTTPD
.MIME_HTML
, builder
.toString());
741 } catch (IOException e
) {
742 Instance
.getInstance().getTraceHandler()
743 .error(new IOException("Cannot get image: " + uri
, e
));
744 return NanoHTTPD
.newFixedLengthResponse(Status
.INTERNAL_ERROR
,
745 NanoHTTPD
.MIME_PLAINTEXT
, "Error when processing request");
749 protected Response
newInputStreamResponse(String mimeType
, InputStream in
) {
751 return NanoHTTPD
.newFixedLengthResponse(Status
.NO_CONTENT
, "",
754 return NanoHTTPD
.newChunkedResponse(Status
.OK
, mimeType
, in
);
757 private String
getContentOf(String file
) {
758 InputStream in
= IOUtils
.openResource(WebLibraryServerIndex
.class,
762 return IOUtils
.readSmallStream(in
);
763 } catch (IOException e
) {
764 Instance
.getInstance().getTraceHandler().error(
765 new IOException("Cannot get file: index.pre.html", e
));
772 private void appendPreHtml(StringBuilder builder
, boolean banner
) {
773 String favicon
= "favicon.ico";
774 String icon
= Instance
.getInstance().getUiConfig()
775 .getString(UiConfig
.PROGRAM_ICON
);
777 favicon
= "icon_" + icon
.replace("-", "_") + ".png";
781 getContentOf("index.pre.html").replace("favicon.ico", favicon
));
784 builder
.append("<div class='banner'>\n");
785 builder
.append("\t<img class='ico' src='/") //
788 builder
.append("\t<h1>Fanfix</h1>\n");
789 builder
.append("\t<h2>") //
790 .append(Version
.getCurrentVersion()) //
792 builder
.append("</div>\n");
796 private void appendPostHtml(StringBuilder builder
) {
797 builder
.append(getContentOf("index.post.html"));
800 private void appendOption(StringBuilder builder
, int depth
, String name
,
801 String value
, String selected
) {
802 for (int i
= 0; i
< depth
; i
++) {
803 builder
.append("\t");
805 builder
.append("<option value='").append(value
).append("'");
806 if (value
.equals(selected
)) {
807 builder
.append(" selected='selected'");
809 builder
.append(">").append(name
).append("</option>\n");
812 private void appendTableRow(StringBuilder builder
, int depth
,
814 for (int i
= 0; i
< depth
; i
++) {
815 builder
.append("\t");
819 builder
.append("<tr>");
820 for (String td
: tds
) {
821 builder
.append("<td class='col");
822 builder
.append(col
++);
823 builder
.append("'>");
825 builder
.append("</td>");
827 builder
.append("</tr>\n");
830 private void appendItemA(StringBuilder builder
, int depth
, String link
,
831 String name
, boolean selected
) {
832 for (int i
= 0; i
< depth
; i
++) {
833 builder
.append("\t");
836 builder
.append("<a href='");
837 builder
.append(link
);
838 builder
.append("' class='item goto");
840 builder
.append(" selected");
842 builder
.append("'>");
843 builder
.append(name
);
844 builder
.append("</a>\n");