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