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