WebBox Klassenbibliothek
ulrich
2020-04-16 1385d2dd22c7bf8189c68a876037779555a22443
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) 
42  * zu HTML-Seiten
43  * 
44  */
45 public class AdocServlet extends HttpServlet  {
46   
47   private static final String DOT = ".";
48   private static final String HTMLEXT = ".html";
49   
50   /**
51    * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
52    * methods.
53    *
54    * @param request servlet request
55    * @param response servlet response
56    * @throws ServletException if a servlet-specific error occurs
57    * @throws IOException if an I/O error occurs
58    */
59   protected void processRequest(HttpServletRequest request, HttpServletResponse response)
60           throws ServletException, IOException 
61   {
62     response.setContentType("text/html;charset=UTF-8");
63     try (PrintWriter out = response.getWriter()) {
64
65       // Asciidoc-Quelldatei aus HTTP-Request ermitteln
66       String vPath = request.getServletPath();
67       String absname = getServletContext().getRealPath(vPath);
68       File adocfile = new File(absname);
69       
70       // HTML-Datei ermitteln
71       String nameext = adocfile.getName();
72       String fname = nameext.substring(0, nameext.lastIndexOf(DOT));
73       File htmlfile = new File(adocfile.getParentFile(), fname + HTMLEXT);
74       
75       /*
76         transformieren, wenn die Quelle sich geandert hat oder 
77         die HTML-Datei noch nicht existiert
78       */
79       if(!htmlfile.exists() || adocfile.lastModified() > htmlfile.lastModified()) {
80         transform(absname);
81       }
82       
83       FileInputStream in = new FileInputStream(htmlfile);
84       BufferedReader reader = new BufferedReader(new InputStreamReader(in));
85       String line;
86       while ((line = reader.readLine()) != null) {
87         out.println(line);
88       }
89     }
90   }
91   
92   private void transform(String absoluteFileName) {
93     Asciidoctor asciidoctor = create();    
94     Map<String, Object> attributes = attributes()
95                                         .noFooter(false)
96                                         //.tableOfContents(true) 
97                                         //.sectionNumbers(true)
98                                         .asMap();
99     Map<String, Object> options = options().inPlace(false)
100                                        .attributes(attributes) 
101                                        .asMap();     
102     asciidoctor.convertFile(new File(absoluteFileName), options);
103   }  
104
105   private void test(HttpServletRequest request, HttpServletResponse response) 
106           throws IOException 
107   {
108     response.setContentType("text/html;charset=UTF-8");
109     try (PrintWriter out = response.getWriter()) {
110       /* TODO output your page here. You may use following sample code. */
111       out.println("<!DOCTYPE html>");
112       out.println("<html>");
113       out.println("<head>");
114       out.println("<title>Servlet AdocServlet</title>");      
115       out.println("</head>");
116       out.println("<body>");
117       out.println("<h1>Servlet AdocServlet at " + request.getContextPath() + "</h1>");
118       
119       out.println("<p>request.getRequestURI(): " + request.getRequestURI() + "</p>");
120       out.println("<p>request.getRequestURL(): " + request.getRequestURL() + "</p>");
121       out.println("<p>request.getServletPath(): " + request.getServletPath() + "</p>");
122       out.println("<p>request.getPathInfo(): " + request.getPathInfo() + "</p>");
123
124       String vPath = request.getServletPath();
125       out.println("<p>vPath: " + vPath);
126       out.println("<p>getServletContext().getRealPath(vPath): " + 
127               getServletContext().getRealPath(vPath) + "</p>");
128       
129       out.println("</body>");
130       out.println("</html>");
131     }
132     
133   }
134   
135   // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
136   /**
137    * Handles the HTTP <code>GET</code> method.
138    *
139    * @param request servlet request
140    * @param response servlet response
141    * @throws ServletException if a servlet-specific error occurs
142    * @throws IOException if an I/O error occurs
143    */
144   @Override
145   protected void doGet(HttpServletRequest request, HttpServletResponse response)
146           throws ServletException, IOException {
147     processRequest(request, response);
148   }
149
150   /**
151    * Handles the HTTP <code>POST</code> method.
152    *
153    * @param request servlet request
154    * @param response servlet response
155    * @throws ServletException if a servlet-specific error occurs
156    * @throws IOException if an I/O error occurs
157    */
158   @Override
159   protected void doPost(HttpServletRequest request, HttpServletResponse response)
160           throws ServletException, IOException {
161     processRequest(request, response);
162   }
163
164   /**
165    * Returns a short description of the servlet.
166    *
167    * @return a String containing servlet description
168    */
169   @Override
170   public String getServletInfo() {
171     return "AdocServlet";
172   }// </editor-fold>
173
174 }