commit | author | age
|
fe6b5c
|
1 |
package de.uhilger.wbx.web; |
U |
2 |
|
|
3 |
/* |
|
4 |
WebBox - Dein Server. |
|
5 |
Copyright (C) 2019 Ulrich Hilger, http://uhilger.de |
|
6 |
|
|
7 |
This program is free software: you can redistribute it and/or modify |
|
8 |
it under the terms of the GNU Affero General Public License as |
|
9 |
published by the Free Software Foundation, either version 3 of the |
|
10 |
License, or (at your option) any later version. |
|
11 |
|
|
12 |
This program is distributed in the hope that it will be useful, |
|
13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15 |
GNU Affero General Public License for more details. |
|
16 |
|
|
17 |
You should have received a copy of the GNU Affero General Public License |
|
18 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
19 |
*/ |
|
20 |
|
|
21 |
|
|
22 |
import java.io.IOException; |
|
23 |
import java.security.Principal; |
|
24 |
import java.util.logging.Level; |
|
25 |
import java.util.logging.Logger; |
|
26 |
import javax.servlet.ServletException; |
|
27 |
import javax.servlet.ServletOutputStream; |
828ffa
|
28 |
import javax.servlet.http.HttpServlet; |
fe6b5c
|
29 |
import javax.servlet.http.HttpServletRequest; |
U |
30 |
import javax.servlet.http.HttpServletResponse; |
|
31 |
|
|
32 |
/** |
|
33 |
* Das MdServlet fügt dem DefaultServlet von Tomcat |
|
34 |
* Methoden hinzu, mit denen Markdown-Inhalte (*.md) |
|
35 |
* zu ganzen HTML-Seiten |
|
36 |
* mit head und body tags sowie Stylesheet-Verweisen |
|
37 |
* ergänzt werden |
|
38 |
* |
|
39 |
* Die hier hinzugefuegten Inhalte verweisen lediglich auf |
|
40 |
* die Javscript-Biliothek strapdown.js |
|
41 |
* (http://strapdownjs.com), das Rendern |
|
42 |
* des Markdown zu HTML geschieht dann auf dem Client. |
|
43 |
* |
|
44 |
* TODO: Stylesheets dynamisch einbinden |
|
45 |
*/ |
828ffa
|
46 |
public class MdServlet extends HttpServlet { |
fe6b5c
|
47 |
|
U |
48 |
private static final Logger logger = Logger.getLogger(MdServlet.class.getName()); |
|
49 |
private static final String HOME_CTX = "/home"; |
|
50 |
|
|
51 |
private String getTitle() { |
|
52 |
String title = null; |
|
53 |
return title; |
|
54 |
} |
|
55 |
|
|
56 |
private void seiteAusgeben(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { |
|
57 |
ServletOutputStream out = response.getOutputStream(); |
|
58 |
printHeader(out, request); |
|
59 |
super.doGet(request, response); |
|
60 |
printFooter(out); |
|
61 |
} |
|
62 |
|
|
63 |
private void printHeader(ServletOutputStream out, HttpServletRequest request) throws IOException { |
|
64 |
out.print("<!DOCTYPE html>\r\n"); |
|
65 |
out.print("<html><head>\r\n"); |
|
66 |
out.print("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"/>\r\n"); |
|
67 |
out.print("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n"); |
|
68 |
out.print("<link rel=\"stylesheet\" type=\"text/css\" href=\"/jslib/sd/strapdown.css\">\r\n"); |
|
69 |
String title = getTitle(); |
|
70 |
if(title != null) { |
|
71 |
out.print("<title>"); |
|
72 |
out.print(title); |
|
73 |
out.print(" "); |
|
74 |
out.print(request.getRequestURI()); |
|
75 |
out.print("</title>\r\n"); |
|
76 |
} |
|
77 |
out.print("</head><body>\r\n"); |
|
78 |
out.print("<xmp theme=\"united\" style=\"display:none;\">\r\n"); |
|
79 |
} |
|
80 |
|
|
81 |
private void printFooter(ServletOutputStream out) throws IOException { |
|
82 |
out.print("\r\n</xmp>\r\n"); |
|
83 |
out.print("<script src=\"/jslib/sd/strapdown.js\"></script>\r\n"); |
|
84 |
out.print("</body></html>"); |
|
85 |
} |
|
86 |
|
|
87 |
/** |
|
88 |
* Handles the HTTP <code>GET</code> method. |
|
89 |
* |
|
90 |
* @param request servlet request |
|
91 |
* @param response servlet response |
|
92 |
* @throws ServletException if a servlet-specific error occurs |
|
93 |
* @throws IOException if an I/O error occurs |
|
94 |
*/ |
|
95 |
@Override |
|
96 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
|
97 |
String contextPath = request.getContextPath(); |
|
98 |
if(HOME_CTX.equals(contextPath)) { |
|
99 |
String userName = getUserName(request); |
|
100 |
if (userName != null) { |
|
101 |
String urlUser = getUrlUser(request, userName); |
|
102 |
if(userName.equals(urlUser)) { |
|
103 |
seiteAusgeben(request, response); |
|
104 |
} else { |
|
105 |
logger.fine("Wrong user."); |
|
106 |
} |
|
107 |
} else { |
|
108 |
logger.fine("Missing login."); |
|
109 |
} |
|
110 |
} else { |
|
111 |
seiteAusgeben(request, response); |
|
112 |
} |
|
113 |
} |
|
114 |
|
|
115 |
protected String getUrlUser(HttpServletRequest request, String userName) { |
|
116 |
String result = ""; |
|
117 |
String requestUrlStr = request.getRequestURL().toString(); |
|
118 |
String contextPath = request.getContextPath(); |
|
119 |
if(contextPath != null && requestUrlStr != null && userName != null) { |
|
120 |
int start = requestUrlStr.indexOf(contextPath); |
|
121 |
start += contextPath.length(); |
|
122 |
start++; |
|
123 |
int end = start + userName.length(); |
|
124 |
try { |
|
125 |
result = requestUrlStr.substring(start, end); |
|
126 |
} catch(Exception ex) { |
|
127 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
128 |
} |
|
129 |
} |
|
130 |
return result; |
|
131 |
} |
|
132 |
|
|
133 |
protected String getUserName(HttpServletRequest hr) { |
|
134 |
String userName = null; |
|
135 |
Object p = hr.getUserPrincipal(); |
|
136 |
if (p instanceof Principal) { |
|
137 |
userName = ((Principal) p).getName(); |
|
138 |
} |
|
139 |
return userName; |
|
140 |
} |
|
141 |
|
|
142 |
/** |
|
143 |
* Returns a short description of the servlet. |
|
144 |
* |
|
145 |
* @return a String containing servlet description |
|
146 |
*/ |
|
147 |
@Override |
|
148 |
public String getServletInfo() { |
|
149 |
return "MarkdownWrapper Servlet"; |
|
150 |
} |
|
151 |
|
|
152 |
} |