• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package fi.iki.elonen;
2 
3 /*
4  * #%L
5  * NanoHttpd-Webserver-Markdown-Plugin
6  * %%
7  * Copyright (C) 2012 - 2015 nanohttpd
8  * %%
9  * Redistribution and use in source and binary forms, with or without modification,
10  * are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice, this
13  *    list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  *    this list of conditions and the following disclaimer in the documentation
17  *    and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the nanohttpd nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software without
21  *    specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
31  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
32  * OF THE POSSIBILITY OF SUCH DAMAGE.
33  * #L%
34  */
35 
36 import static fi.iki.elonen.NanoHTTPD.Response.Status.OK;
37 
38 import java.io.BufferedReader;
39 import java.io.ByteArrayInputStream;
40 import java.io.File;
41 import java.io.FileReader;
42 import java.io.IOException;
43 import java.io.UnsupportedEncodingException;
44 import java.util.Map;
45 import java.util.logging.Level;
46 import java.util.logging.Logger;
47 
48 import org.pegdown.PegDownProcessor;
49 
50 /**
51  * @author Paul S. Hawke (paul.hawke@gmail.com) On: 9/13/13 at 4:03 AM
52  */
53 public class MarkdownWebServerPlugin implements WebServerPlugin {
54 
55     /**
56      * logger to log to.
57      */
58     private static final Logger LOG = Logger.getLogger(MarkdownWebServerPlugin.class.getName());
59 
60     private final PegDownProcessor processor;
61 
MarkdownWebServerPlugin()62     public MarkdownWebServerPlugin() {
63         this.processor = new PegDownProcessor();
64     }
65 
66     @Override
canServeUri(String uri, File rootDir)67     public boolean canServeUri(String uri, File rootDir) {
68         File f = new File(rootDir, uri);
69         return f.exists();
70     }
71 
72     @Override
initialize(Map<String, String> commandLineOptions)73     public void initialize(Map<String, String> commandLineOptions) {
74     }
75 
readSource(File file)76     private String readSource(File file) {
77         FileReader fileReader = null;
78         BufferedReader reader = null;
79         try {
80             fileReader = new FileReader(file);
81             reader = new BufferedReader(fileReader);
82             String line = null;
83             StringBuilder sb = new StringBuilder();
84             do {
85                 line = reader.readLine();
86                 if (line != null) {
87                     sb.append(line).append("\n");
88                 }
89             } while (line != null);
90             reader.close();
91             return sb.toString();
92         } catch (Exception e) {
93             MarkdownWebServerPlugin.LOG.log(Level.SEVERE, "could not read source", e);
94             return null;
95         } finally {
96             try {
97                 if (fileReader != null) {
98                     fileReader.close();
99                 }
100                 if (reader != null) {
101                     reader.close();
102                 }
103             } catch (IOException ignored) {
104                 MarkdownWebServerPlugin.LOG.log(Level.FINEST, "close failed", ignored);
105             }
106         }
107     }
108 
109     @Override
serveFile(String uri, Map<String, String> headers, NanoHTTPD.IHTTPSession session, File file, String mimeType)110     public NanoHTTPD.Response serveFile(String uri, Map<String, String> headers, NanoHTTPD.IHTTPSession session, File file, String mimeType) {
111         String markdownSource = readSource(file);
112         byte[] bytes;
113         try {
114             bytes = this.processor.markdownToHtml(markdownSource).getBytes("UTF-8");
115         } catch (UnsupportedEncodingException e) {
116             MarkdownWebServerPlugin.LOG.log(Level.SEVERE, "encoding problem, responding nothing", e);
117             bytes = new byte[0];
118         }
119         return markdownSource == null ? null : new NanoHTTPD.Response(OK, NanoHTTPD.MIME_HTML, new ByteArrayInputStream(bytes), bytes.length);
120     }
121 }
122