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; |
3bbfbc
|
27 |
import java.util.HashMap; |
1385d2
|
28 |
import javax.servlet.ServletException; |
U |
29 |
import javax.servlet.http.HttpServlet; |
|
30 |
import javax.servlet.http.HttpServletRequest; |
|
31 |
import javax.servlet.http.HttpServletResponse; |
|
32 |
import java.util.Map; |
|
33 |
|
|
34 |
import static org.asciidoctor.Asciidoctor.Factory.create; |
|
35 |
import org.asciidoctor.Asciidoctor; |
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
/** |
|
40 |
* Das AdocServlet wandelt AsciiDoc-Inhalte (*.adoc) |
70be19
|
41 |
* zu HTML-Seiten und PDF-Dokumenten |
d8a1ab
|
42 |
* |
U |
43 |
* ?pdf=true im URL fuegt PDF-Ausgabe hinzu |
1385d2
|
44 |
*/ |
U |
45 |
public class AdocServlet extends HttpServlet { |
|
46 |
|
|
47 |
private static final String DOT = "."; |
3bbfbc
|
48 |
private static final String HTML = "html"; |
70be19
|
49 |
private static final String PDF = "pdf"; |
U |
50 |
private static final String SERVLET_NAME = "AdocServlet"; |
1385d2
|
51 |
|
U |
52 |
/** |
|
53 |
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> |
|
54 |
* methods. |
|
55 |
* |
|
56 |
* @param request servlet request |
|
57 |
* @param response servlet response |
|
58 |
* @throws ServletException if a servlet-specific error occurs |
|
59 |
* @throws IOException if an I/O error occurs |
|
60 |
*/ |
|
61 |
protected void processRequest(HttpServletRequest request, HttpServletResponse response) |
|
62 |
throws ServletException, IOException |
|
63 |
{ |
|
64 |
response.setContentType("text/html;charset=UTF-8"); |
|
65 |
try (PrintWriter out = response.getWriter()) { |
|
66 |
|
|
67 |
// Asciidoc-Quelldatei aus HTTP-Request ermitteln |
|
68 |
String vPath = request.getServletPath(); |
|
69 |
String absname = getServletContext().getRealPath(vPath); |
|
70 |
File adocfile = new File(absname); |
|
71 |
|
|
72 |
// HTML-Datei ermitteln |
|
73 |
String nameext = adocfile.getName(); |
|
74 |
String fname = nameext.substring(0, nameext.lastIndexOf(DOT)); |
3bbfbc
|
75 |
File htmlfile = new File(adocfile.getParentFile(), fname + DOT + HTML); |
1385d2
|
76 |
|
U |
77 |
/* |
3bbfbc
|
78 |
nach HTML transformieren, wenn die Quelle sich geandert hat oder |
1385d2
|
79 |
die HTML-Datei noch nicht existiert |
U |
80 |
*/ |
|
81 |
if(!htmlfile.exists() || adocfile.lastModified() > htmlfile.lastModified()) { |
3bbfbc
|
82 |
transform(absname); |
70be19
|
83 |
} |
U |
84 |
|
3bbfbc
|
85 |
/* |
U |
86 |
nach PDF transformieren, wenn der Parameter pdf=true existiert und |
|
87 |
wenn die Quelle sich geandert hat oder |
|
88 |
die PDF-Datei noch nicht existiert |
|
89 |
*/ |
70be19
|
90 |
String pdf = request.getParameter(PDF); |
dd8e57
|
91 |
if(null != pdf && pdf.equalsIgnoreCase(Boolean.TRUE.toString())) { |
3bbfbc
|
92 |
File pdffile = new File(adocfile.getParentFile(), fname + DOT + PDF); |
70be19
|
93 |
if(!pdffile.exists() || adocfile.lastModified() > pdffile.lastModified()) { |
3bbfbc
|
94 |
transform(absname, PDF); |
70be19
|
95 |
} |
1385d2
|
96 |
} |
U |
97 |
|
70be19
|
98 |
// HTML-Datei ausgeben |
1385d2
|
99 |
FileInputStream in = new FileInputStream(htmlfile); |
U |
100 |
BufferedReader reader = new BufferedReader(new InputStreamReader(in)); |
|
101 |
String line; |
|
102 |
while ((line = reader.readLine()) != null) { |
|
103 |
out.println(line); |
|
104 |
} |
|
105 |
} |
|
106 |
} |
|
107 |
|
3bbfbc
|
108 |
private void transform(String fileName) { |
U |
109 |
transform(fileName, null); |
1385d2
|
110 |
} |
3bbfbc
|
111 |
|
U |
112 |
private void transform(String fileName, String backend) { |
|
113 |
Map<String, Object> attributes = new HashMap<>(); |
|
114 |
attributes.put("no_footer", false); |
|
115 |
attributes.put("source_highlighter", "highlightjs"); |
|
116 |
|
|
117 |
Map<String, Object> options = new HashMap<>(); |
|
118 |
options.put("attributes", attributes); |
|
119 |
options.put("in_place", false); |
|
120 |
if(null != backend) { |
|
121 |
options.put("backend", backend); |
|
122 |
} |
|
123 |
|
|
124 |
Asciidoctor asciidoctor = create(); |
|
125 |
asciidoctor.convertFile(new File(fileName), options); |
|
126 |
} |
1385d2
|
127 |
|
U |
128 |
/** |
|
129 |
* Handles the HTTP <code>GET</code> method. |
|
130 |
* |
|
131 |
* @param request servlet request |
|
132 |
* @param response servlet response |
|
133 |
* @throws ServletException if a servlet-specific error occurs |
|
134 |
* @throws IOException if an I/O error occurs |
|
135 |
*/ |
|
136 |
@Override |
|
137 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) |
|
138 |
throws ServletException, IOException { |
|
139 |
processRequest(request, response); |
|
140 |
} |
|
141 |
|
|
142 |
/** |
|
143 |
* Handles the HTTP <code>POST</code> method. |
|
144 |
* |
|
145 |
* @param request servlet request |
|
146 |
* @param response servlet response |
|
147 |
* @throws ServletException if a servlet-specific error occurs |
|
148 |
* @throws IOException if an I/O error occurs |
|
149 |
*/ |
|
150 |
@Override |
|
151 |
protected void doPost(HttpServletRequest request, HttpServletResponse response) |
|
152 |
throws ServletException, IOException { |
|
153 |
processRequest(request, response); |
|
154 |
} |
|
155 |
|
|
156 |
/** |
|
157 |
* Returns a short description of the servlet. |
|
158 |
* |
|
159 |
* @return a String containing servlet description |
|
160 |
*/ |
|
161 |
@Override |
|
162 |
public String getServletInfo() { |
70be19
|
163 |
return SERVLET_NAME; |
U |
164 |
} |
1385d2
|
165 |
|
U |
166 |
} |