WebBox Klassenbibliothek
ulrich@undisclosed
2020-05-28 bf412a896b52694891bc4b72dee092e026f6e667
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
77     response.setContentType("text/html;charset=UTF-8");
78
79     /*
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       response.setContentType("application/pdf;charset=UTF-8");
97       if(!pdffile.exists() || adocfile.lastModified() > pdffile.lastModified()) {
98         transform(absname, PDF);
70be19 99       }
a22ef6 100     }
1385d2 101       
a22ef6 102     try (PrintWriter out = response.getWriter()) {
U 103       // abhaengig vom Parameter pdf HTML- oder PDF-Datei ausgeben
104       FileInputStream in = new FileInputStream(outfile);
1385d2 105       BufferedReader reader = new BufferedReader(new InputStreamReader(in));
U 106       String line;
107       while ((line = reader.readLine()) != null) {
108         out.println(line);
109       }
110     }
111   }
112   
d572ec 113   /**
U 114    * Nach HTML transformieren
115    * @param fileName der Dateiname der Quelldatei samt absoluter Pfadangabe
116    */
3bbfbc 117   private void transform(String fileName) {
U 118     transform(fileName, null);
1385d2 119   }  
3bbfbc 120   
d572ec 121   /**
U 122    * In ein Format transformieren, das von einem 'Backend' von Asciidoctor 
123    * unterstuetzt wird
124    * @param fileName der Dateiname der Quelldatei samt absoluter Pfadangabe
125    * @param backend das Kuerzel des Backends, z.B. der String 'pdf', wenn 
126    * nach PDF transformiert werden soll
127    */
3bbfbc 128   private void transform(String fileName, String backend) {    
bf412a 129     /*
U 130     Map<String, Object> attributes = new HashMap<>();
131     attributes.put("no_footer", false);
132     //attributes.put("source_highlighter", "highlightjs");
133
134     Map<String, Object> options = new HashMap<>();
135     options.put("attributes", attributes); 
136     options.put("in_place", false); 
3bbfbc 137     if(null != backend) {
bf412a 138       options.put("backend", backend);
3bbfbc 139     }
bf412a 140     */
U 141     
142     Map<String, Object> attributes = attributes().sourceHighlighter("highlightjs")
143                                        .asMap();
144     Map<String, Object> options = options().inPlace(false).attributes(attributes)
145                                        .asMap();
146
3bbfbc 147     
U 148     Asciidoctor asciidoctor = create();    
149     asciidoctor.convertFile(new File(fileName), options);    
150   }
1385d2 151
U 152   /**
d572ec 153    * Die HTTP-<code>GET</code>-Methode verarbeiten.
1385d2 154    *
d572ec 155    * @param request die Servlet-Anfrage
U 156    * @param response die Servlet-Antwort
157    * @throws ServletException wenn ein Servlet-spezifischer Fehler passiert
158    * @throws IOException wenn ein Eingabe- oder Ausgabe-Fehler passiert
1385d2 159    */
U 160   @Override
161   protected void doGet(HttpServletRequest request, HttpServletResponse response)
162           throws ServletException, IOException {
163     processRequest(request, response);
164   }
165
166   /**
d572ec 167    * Die HTTP-<code>POST</code>-Methode verarbeiten.
1385d2 168    *
d572ec 169    * @param request die Servlet-Anfrage
U 170    * @param response die Servlet-Antwort
171    * @throws ServletException wenn ein Servlet-spezifischer Fehler passiert
172    * @throws IOException wenn ein Eingabe- oder Ausgabe-Fehler passiert
1385d2 173    */
U 174   @Override
175   protected void doPost(HttpServletRequest request, HttpServletResponse response)
176           throws ServletException, IOException {
177     processRequest(request, response);
178   }
179
180   /**
d572ec 181    * Eine Kurzbeschreibung des Servlets ausgeben.
1385d2 182    *
d572ec 183    * @return einen String mit der Kurzbeschreibung des Servlets
1385d2 184    */
U 185   @Override
186   public String getServletInfo() {
70be19 187     return SERVLET_NAME;
U 188   }
1385d2 189
U 190 }