commit | author | age
|
7bcebf
|
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 |
*/
|
22f5de
|
18 |
package de.uhilger.wbx.web;
|
U |
19 |
|
|
20 |
import java.io.IOException;
|
bc9f6a
|
21 |
import java.security.Principal;
|
U |
22 |
import java.util.logging.Level;
|
22f5de
|
23 |
import java.util.logging.Logger;
|
U |
24 |
import javax.servlet.ServletException;
|
|
25 |
import javax.servlet.ServletOutputStream;
|
|
26 |
import javax.servlet.http.HttpServletRequest;
|
|
27 |
import javax.servlet.http.HttpServletResponse;
|
bc9f6a
|
28 |
import org.apache.catalina.servlets.DefaultServlet;
|
22f5de
|
29 |
|
U |
30 |
/**
|
7bcebf
|
31 |
* Das ViewServlet fügt dem DefaultServlet von Tomcat
|
U |
32 |
* Methoden hinzu, mit denen HTML-Inhalte wie sie z.B. mit
|
|
33 |
* TinyMCE erzeugt werden zu ganzen HTML-Seiten
|
|
34 |
* mit head und body tags sowie Stylesheet-Verweisen
|
|
35 |
* ergänzt werden
|
|
36 |
*
|
|
37 |
* TODO: Stylesheets dynamisch einbinden
|
22f5de
|
38 |
*/
|
bc9f6a
|
39 |
public class ViewServlet extends DefaultServlet {
|
22f5de
|
40 |
|
U |
41 |
private static final Logger logger = Logger.getLogger(ViewServlet.class.getName());
|
bc9f6a
|
42 |
private static final String HOME_CTX = "/home";
|
22f5de
|
43 |
|
bc9f6a
|
44 |
|
U |
45 |
private void seiteAusgeben(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
|
2a7560
|
46 |
ServletOutputStream out = response.getOutputStream();
|
U |
47 |
printHeader(out);
|
|
48 |
super.doGet(request, response);
|
|
49 |
printFooter(out);
|
|
50 |
}
|
|
51 |
|
|
52 |
private void printHeader(ServletOutputStream out) throws IOException {
|
066b66
|
53 |
out.print("<!DOCTYPE html><html><head>\r\n");
|
a123ba
|
54 |
out.print("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"/>\r\n");
|
066b66
|
55 |
out.print("<link rel=\"stylesheet\" type=\"text/css\" href=\"/jslib/bootstrap/css/bootstrap.min.css\">\r\n");
|
U |
56 |
out.print("<link rel=\"stylesheet\" type=\"text/css\" href=\"/jslib/lightbox/lightbox.css\">\r\n");
|
|
57 |
out.print("<link rel=\"stylesheet\" type=\"text/css\" href=\"stile.css\">\r\n");
|
|
58 |
out.print("</head><body class=\"p-3\">\r\n");
|
2a7560
|
59 |
}
|
U |
60 |
|
|
61 |
private void printFooter(ServletOutputStream out) throws IOException {
|
066b66
|
62 |
out.print("<script src=\"/jslib/jquery/jquery.min.js\"></script>\r\n");
|
U |
63 |
out.print("<script src=\"/jslib/lightbox/lightbox.min.js\"></script>\r\n");
|
2a7560
|
64 |
out.print("</body></html>");
|
U |
65 |
}
|
|
66 |
|
bc9f6a
|
67 |
/**
|
U |
68 |
* Handles the HTTP <code>GET</code> method.
|
|
69 |
*
|
|
70 |
* @param request servlet request
|
|
71 |
* @param response servlet response
|
|
72 |
* @throws ServletException if a servlet-specific error occurs
|
|
73 |
* @throws IOException if an I/O error occurs
|
|
74 |
*/
|
|
75 |
@Override
|
|
76 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
|
77 |
String contextPath = request.getContextPath();
|
|
78 |
if(HOME_CTX.equals(contextPath)) {
|
|
79 |
String userName = getUserName(request);
|
|
80 |
if (userName != null) {
|
|
81 |
String urlUser = getUrlUser(request, userName);
|
|
82 |
if(userName.equals(urlUser)) {
|
|
83 |
seiteAusgeben(request, response);
|
|
84 |
} else {
|
|
85 |
logger.fine("Wrong user.");
|
|
86 |
}
|
|
87 |
} else {
|
|
88 |
logger.fine("Missing login.");
|
|
89 |
}
|
|
90 |
} else {
|
|
91 |
seiteAusgeben(request, response);
|
|
92 |
}
|
|
93 |
}
|
|
94 |
|
|
95 |
protected String getUrlUser(HttpServletRequest request, String userName) {
|
2a7560
|
96 |
String result = "";
|
U |
97 |
String requestUrlStr = request.getRequestURL().toString();
|
|
98 |
String contextPath = request.getContextPath();
|
22f5de
|
99 |
if(contextPath != null && requestUrlStr != null && userName != null) {
|
U |
100 |
int start = requestUrlStr.indexOf(contextPath);
|
|
101 |
start += contextPath.length();
|
|
102 |
start++;
|
|
103 |
int end = start + userName.length();
|
|
104 |
try {
|
|
105 |
result = requestUrlStr.substring(start, end);
|
|
106 |
} catch(Exception ex) {
|
2a7560
|
107 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
|
22f5de
|
108 |
}
|
U |
109 |
}
|
|
110 |
return result;
|
|
111 |
}
|
|
112 |
|
bc9f6a
|
113 |
protected String getUserName(HttpServletRequest hr) {
|
22f5de
|
114 |
String userName = null;
|
U |
115 |
Object p = hr.getUserPrincipal();
|
|
116 |
if (p instanceof Principal) {
|
|
117 |
userName = ((Principal) p).getName();
|
|
118 |
}
|
|
119 |
return userName;
|
|
120 |
}
|
bc9f6a
|
121 |
|
22f5de
|
122 |
/**
|
U |
123 |
* Returns a short description of the servlet.
|
|
124 |
*
|
|
125 |
* @return a String containing servlet description
|
|
126 |
*/
|
|
127 |
@Override
|
|
128 |
public String getServletInfo() {
|
|
129 |
return "Short description";
|
2a7560
|
130 |
}
|
22f5de
|
131 |
|
U |
132 |
}
|