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