WebBox Klassenbibliothek
ulrich
2020-06-23 16c6c66ce77f5560e480ece4fdb7ae7d1c340702
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;
c5090a 36 import static org.asciidoctor.AttributesBuilder.attributes;
U 37 import static org.asciidoctor.OptionsBuilder.options;
1385d2 38
U 39
40
41 /**
42  * Das AdocServlet wandelt AsciiDoc-Inhalte (*.adoc) 
70be19 43  * zu HTML-Seiten und PDF-Dokumenten
d8a1ab 44  * 
d572ec 45  * Mit Angabe des Parameters ?pdf=true im URL wird PDF erzeugt, andernfalls HTML
1385d2 46  */
U 47 public class AdocServlet extends HttpServlet  {
48   
49   private static final String DOT = ".";
3bbfbc 50   private static final String HTML = "html";
70be19 51   private static final String PDF = "pdf";
U 52   private static final String SERVLET_NAME = "AdocServlet";
1385d2 53   
U 54   /**
d572ec 55    * Die Methode processRequest verarbeitet HTTP-Anfragen des Typs  
U 56    * <code>GET</code> und <code>POST</code>.
1385d2 57    *
d572ec 58    * @param request die Servlet-Anfrage
U 59    * @param response die Servlet-Antwort
60    * @throws ServletException wenn ein Servlet-spezifischer Fehler passiert
61    * @throws IOException wenn ein Eingabe- oder Ausgabe-Fehler passiert
1385d2 62    */
U 63   protected void processRequest(HttpServletRequest request, HttpServletResponse response)
64           throws ServletException, IOException 
65   {
66
a22ef6 67     // Asciidoc-Quelldatei aus HTTP-Request ermitteln
U 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));
75     File htmlfile = new File(adocfile.getParentFile(), fname + DOT + HTML);
76     File outfile = htmlfile; // Standardmaessig wird HTML zurueckgegeben
0fa5e7 77     response.setCharacterEncoding("UTF-8");
U 78     
a22ef6 79     /*
U 80       nach HTML transformieren, wenn die Quelle sich geandert hat oder 
81       die HTML-Datei noch nicht existiert
82     */
83     if(!htmlfile.exists() || adocfile.lastModified() > htmlfile.lastModified()) {
84       transform(absname);
85     }
86
87     /*
88       nach PDF transformieren, wenn der Parameter pdf=true existiert und 
89       wenn die Quelle sich geandert hat oder 
90       die PDF-Datei noch nicht existiert
91     */
92     String pdf = request.getParameter(PDF);
93     if(null != pdf && pdf.equalsIgnoreCase(Boolean.TRUE.toString())) {
94       File pdffile = new File(adocfile.getParentFile(), fname + DOT + PDF);
95       outfile = pdffile; // PDF soll zurueckgegeben werden
96       if(!pdffile.exists() || adocfile.lastModified() > pdffile.lastModified()) {
97         transform(absname, PDF);
70be19 98       }
a22ef6 99     }
1385d2 100       
a22ef6 101     try (PrintWriter out = response.getWriter()) {
U 102       // abhaengig vom Parameter pdf HTML- oder PDF-Datei ausgeben
103       FileInputStream in = new FileInputStream(outfile);
132307 104       BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
1385d2 105       String line;
U 106       while ((line = reader.readLine()) != null) {
107         out.println(line);
108       }
109     }
110   }
111   
d572ec 112   /**
U 113    * Nach HTML transformieren
114    * @param fileName der Dateiname der Quelldatei samt absoluter Pfadangabe
115    */
3bbfbc 116   private void transform(String fileName) {
U 117     transform(fileName, null);
1385d2 118   }  
3bbfbc 119   
d572ec 120   /**
U 121    * In ein Format transformieren, das von einem 'Backend' von Asciidoctor 
122    * unterstuetzt wird
123    * @param fileName der Dateiname der Quelldatei samt absoluter Pfadangabe
124    * @param backend das Kuerzel des Backends, z.B. der String 'pdf', wenn 
125    * nach PDF transformiert werden soll
126    */
3bbfbc 127   private void transform(String fileName, String backend) {    
bf412a 128     Map<String, Object> attributes = attributes().sourceHighlighter("highlightjs")
U 129                                        .asMap();
7bee9c 130     Map<String, Object> options;
U 131     if(null != backend) {
132       options = options().inPlace(false)
133               .backend(backend).attributes(attributes).asMap();
134       
135     } else {
136       options = options().inPlace(false)
137               .attributes(attributes).asMap();
138     }
3bbfbc 139     
U 140     Asciidoctor asciidoctor = create();    
16c6c6 141     asciidoctor.requireLibrary("asciidoctor-diagram");
3bbfbc 142     asciidoctor.convertFile(new File(fileName), options);    
U 143   }
1385d2 144
U 145   /**
d572ec 146    * Die HTTP-<code>GET</code>-Methode verarbeiten.
1385d2 147    *
d572ec 148    * @param request die Servlet-Anfrage
U 149    * @param response die Servlet-Antwort
150    * @throws ServletException wenn ein Servlet-spezifischer Fehler passiert
151    * @throws IOException wenn ein Eingabe- oder Ausgabe-Fehler passiert
1385d2 152    */
U 153   @Override
154   protected void doGet(HttpServletRequest request, HttpServletResponse response)
155           throws ServletException, IOException {
156     processRequest(request, response);
157   }
158
159   /**
d572ec 160    * Die HTTP-<code>POST</code>-Methode verarbeiten.
1385d2 161    *
d572ec 162    * @param request die Servlet-Anfrage
U 163    * @param response die Servlet-Antwort
164    * @throws ServletException wenn ein Servlet-spezifischer Fehler passiert
165    * @throws IOException wenn ein Eingabe- oder Ausgabe-Fehler passiert
1385d2 166    */
U 167   @Override
168   protected void doPost(HttpServletRequest request, HttpServletResponse response)
169           throws ServletException, IOException {
170     processRequest(request, response);
171   }
172
173   /**
d572ec 174    * Eine Kurzbeschreibung des Servlets ausgeben.
1385d2 175    *
d572ec 176    * @return einen String mit der Kurzbeschreibung des Servlets
1385d2 177    */
U 178   @Override
179   public String getServletInfo() {
70be19 180     return SERVLET_NAME;
U 181   }
1385d2 182
U 183 }