commit | author | age
|
1385d2
|
1 |
/* |
U |
2 |
WebBox - Dein Server. |
|
3 |
Copyright (C) 2020 Ulrich Hilger, http://uhilger.de |
|
4 |
|
|
5 |
This program is free software: you can redistribute it and/or modify |
|
6 |
it under the terms of the GNU Affero General Public License as |
|
7 |
published by the Free Software Foundation, either version 3 of the |
|
8 |
License, or (at your option) any later version. |
|
9 |
|
|
10 |
This program is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
GNU Affero General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU Affero General Public License |
|
16 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
18 |
|
|
19 |
package de.uhilger.wbx.web; |
|
20 |
|
|
21 |
import java.io.BufferedReader; |
|
22 |
import java.io.File; |
|
23 |
import java.io.FileInputStream; |
|
24 |
import java.io.IOException; |
|
25 |
import java.io.InputStreamReader; |
|
26 |
import java.io.PrintWriter; |
|
27 |
import javax.servlet.ServletException; |
|
28 |
import javax.servlet.http.HttpServlet; |
|
29 |
import javax.servlet.http.HttpServletRequest; |
|
30 |
import javax.servlet.http.HttpServletResponse; |
|
31 |
import java.util.Map; |
|
32 |
|
|
33 |
import static org.asciidoctor.AttributesBuilder.attributes; |
|
34 |
import static org.asciidoctor.OptionsBuilder.options; |
|
35 |
import static org.asciidoctor.Asciidoctor.Factory.create; |
|
36 |
import org.asciidoctor.Asciidoctor; |
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
/** |
|
41 |
* Das AdocServlet wandelt AsciiDoc-Inhalte (*.adoc) |
70be19
|
42 |
* zu HTML-Seiten und PDF-Dokumenten |
d8a1ab
|
43 |
* |
U |
44 |
* ?pdf=true im URL fuegt PDF-Ausgabe hinzu |
1385d2
|
45 |
*/ |
U |
46 |
public class AdocServlet extends HttpServlet { |
|
47 |
|
|
48 |
private static final String DOT = "."; |
|
49 |
private static final String HTMLEXT = ".html"; |
70be19
|
50 |
private static final String PDFEXT = ".pdf"; |
U |
51 |
private static final String PDF = "pdf"; |
|
52 |
private static final String SERVLET_NAME = "AdocServlet"; |
1385d2
|
53 |
|
U |
54 |
/** |
|
55 |
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> |
|
56 |
* methods. |
|
57 |
* |
|
58 |
* @param request servlet request |
|
59 |
* @param response servlet response |
|
60 |
* @throws ServletException if a servlet-specific error occurs |
|
61 |
* @throws IOException if an I/O error occurs |
|
62 |
*/ |
|
63 |
protected void processRequest(HttpServletRequest request, HttpServletResponse response) |
|
64 |
throws ServletException, IOException |
|
65 |
{ |
|
66 |
response.setContentType("text/html;charset=UTF-8"); |
|
67 |
try (PrintWriter out = response.getWriter()) { |
|
68 |
|
|
69 |
// Asciidoc-Quelldatei aus HTTP-Request ermitteln |
|
70 |
String vPath = request.getServletPath(); |
|
71 |
String absname = getServletContext().getRealPath(vPath); |
|
72 |
File adocfile = new File(absname); |
|
73 |
|
|
74 |
// HTML-Datei ermitteln |
|
75 |
String nameext = adocfile.getName(); |
|
76 |
String fname = nameext.substring(0, nameext.lastIndexOf(DOT)); |
|
77 |
File htmlfile = new File(adocfile.getParentFile(), fname + HTMLEXT); |
|
78 |
|
|
79 |
/* |
|
80 |
transformieren, wenn die Quelle sich geandert hat oder |
|
81 |
die HTML-Datei noch nicht existiert |
|
82 |
*/ |
|
83 |
if(!htmlfile.exists() || adocfile.lastModified() > htmlfile.lastModified()) { |
70be19
|
84 |
transform(absname, htmlfile); |
U |
85 |
} |
|
86 |
|
|
87 |
String pdf = request.getParameter(PDF); |
|
88 |
if(null != pdf && pdf.length() > 0 && pdf.equalsIgnoreCase(Boolean.TRUE.toString())) { |
|
89 |
File pdffile = new File(adocfile.getParentFile(), fname + PDFEXT); |
|
90 |
if(!pdffile.exists() || adocfile.lastModified() > pdffile.lastModified()) { |
|
91 |
transform(absname, pdffile); |
|
92 |
} |
1385d2
|
93 |
} |
U |
94 |
|
70be19
|
95 |
// HTML-Datei ausgeben |
1385d2
|
96 |
FileInputStream in = new FileInputStream(htmlfile); |
U |
97 |
BufferedReader reader = new BufferedReader(new InputStreamReader(in)); |
|
98 |
String line; |
|
99 |
while ((line = reader.readLine()) != null) { |
|
100 |
out.println(line); |
|
101 |
} |
|
102 |
} |
|
103 |
} |
|
104 |
|
70be19
|
105 |
private void transform(String absoluteFileName, File target) { |
U |
106 |
Map<String, Object> options = null; |
|
107 |
Map<String, Object> attributes; |
1385d2
|
108 |
Asciidoctor asciidoctor = create(); |
70be19
|
109 |
String fname = target.getName().toLowerCase(); |
U |
110 |
|
|
111 |
if(fname.endsWith(HTMLEXT)) { |
|
112 |
attributes = attributes().noFooter(false) |
|
113 |
.sourceHighlighter("highlightjs") |
|
114 |
//.tableOfContents(true) |
|
115 |
//.sectionNumbers(true) |
|
116 |
.asMap(); |
|
117 |
options = options().inPlace(false) |
|
118 |
.attributes(attributes) |
|
119 |
.asMap(); |
|
120 |
} else if(fname.endsWith(PDFEXT)) { |
|
121 |
attributes = attributes().noFooter(false) |
|
122 |
.asMap(); |
|
123 |
options = options().inPlace(false) |
|
124 |
.attributes(attributes) |
|
125 |
.backend("pdf") |
|
126 |
.asMap(); |
|
127 |
} |
|
128 |
asciidoctor.convertFile(new File(absoluteFileName), options); |
1385d2
|
129 |
} |
U |
130 |
|
|
131 |
/** |
|
132 |
* Handles the HTTP <code>GET</code> method. |
|
133 |
* |
|
134 |
* @param request servlet request |
|
135 |
* @param response servlet response |
|
136 |
* @throws ServletException if a servlet-specific error occurs |
|
137 |
* @throws IOException if an I/O error occurs |
|
138 |
*/ |
|
139 |
@Override |
|
140 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) |
|
141 |
throws ServletException, IOException { |
|
142 |
processRequest(request, response); |
|
143 |
} |
|
144 |
|
|
145 |
/** |
|
146 |
* Handles the HTTP <code>POST</code> method. |
|
147 |
* |
|
148 |
* @param request servlet request |
|
149 |
* @param response servlet response |
|
150 |
* @throws ServletException if a servlet-specific error occurs |
|
151 |
* @throws IOException if an I/O error occurs |
|
152 |
*/ |
|
153 |
@Override |
|
154 |
protected void doPost(HttpServletRequest request, HttpServletResponse response) |
|
155 |
throws ServletException, IOException { |
|
156 |
processRequest(request, response); |
|
157 |
} |
|
158 |
|
|
159 |
/** |
|
160 |
* Returns a short description of the servlet. |
|
161 |
* |
|
162 |
* @return a String containing servlet description |
|
163 |
*/ |
|
164 |
@Override |
|
165 |
public String getServletInfo() { |
70be19
|
166 |
return SERVLET_NAME; |
U |
167 |
} |
1385d2
|
168 |
|
U |
169 |
} |