WebBox Klassenbibliothek
ulrich
2021-01-28 96dfd62fbfe771616045a253bbeb1415538e815f
commit | author | age
2ef1bb 1 /*
U 2     WebBox - Dein Server.
3     Copyright (C) 2017 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 package de.uhilger.wbx.web;
19
20 import java.io.IOException;
21 import java.io.OutputStream;
22 import java.util.logging.Logger;
23 import javax.servlet.FilterChain;
24 import javax.servlet.FilterConfig;
25 import javax.servlet.ServletException;
26 import javax.servlet.ServletRequest;
27 import javax.servlet.ServletResponse;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30
31
32 /**
33  * Ein Filter zur Verwandlung von Markdown-Text in HTML
34  *
35  * @author ulrich
36  */
37 public class MarkdownFilter implements javax.servlet.Filter {
38
39   private static final Logger logger = Logger.getLogger(MarkdownFilter.class.getName());
40   private static final String EXT_MD = ".md";
41   public FilterConfig filterConfig;
42
43   @Override
44   public void doFilter(final ServletRequest request,
45           final ServletResponse response,
46           FilterChain chain)
47           throws IOException, ServletException {
48     response.setCharacterEncoding("UTF-8");
49     HttpServletRequest hr = (HttpServletRequest) request;
50     String servletPath = hr.getServletPath();
51     if(servletPath.toLowerCase().endsWith(EXT_MD)) {
52       OutputStream out = response.getOutputStream();
53       //out.write(new String("<HR>PRE<HR>").getBytes());
54       GenericResponseWrapper wrapper
55               = new GenericResponseWrapper((HttpServletResponse) response);
56       wrapper.setCharacterEncoding("UTF-8");
57       //wrapper.setContentLength(100000);
58       chain.doFilter(request, wrapper);
59
60       String responseContent = new String(wrapper.getData(), "UTF-8");
61       
882cad 62       String html = "hier kommt eigener Inhalt hinein";
U 63       //String html = new Markdown4jProcessor().process(responseContent);
2ef1bb 64       //String result = Processor.process(responseContent);
U 65       
66       out.write(html.getBytes());
67       //out.write(new String("<HR>POST<HR>").getBytes());
68       out.close();
69     } else {
70       chain.doFilter(request, response);
71     }
72     /*
73     logger.info("requestURL: " + hr.getRequestURL().toString());
74     logger.info("ContextPath: " + hr.getContextPath());
75     logger.info("ServletPath: " + hr.getServletPath());
76     logger.info("PathInfo: " + hr.getPathInfo());
77      */
78   }
79
80   @Override
81   public void init(FilterConfig filterConfig) throws ServletException {
82     this.filterConfig = filterConfig;
83   }
84
85   @Override
86   public void destroy() {
87     // ..
88   }
89
90 }