commit | author | age
|
22f5de
|
1 |
package de.uhilger.wbx.web;
|
U |
2 |
|
|
3 |
import java.io.IOException;
|
|
4 |
import java.security.Principal;
|
2a7560
|
5 |
import java.util.logging.Level;
|
22f5de
|
6 |
import java.util.logging.Logger;
|
U |
7 |
import javax.servlet.ServletException;
|
|
8 |
import javax.servlet.ServletOutputStream;
|
|
9 |
import javax.servlet.http.HttpServletRequest;
|
|
10 |
import javax.servlet.http.HttpServletResponse;
|
|
11 |
import org.apache.catalina.servlets.DefaultServlet;
|
|
12 |
|
|
13 |
/**
|
|
14 |
*
|
|
15 |
*/
|
|
16 |
public class ViewServlet extends DefaultServlet {
|
|
17 |
|
|
18 |
private static final Logger logger = Logger.getLogger(ViewServlet.class.getName());
|
2a7560
|
19 |
private static final String HOME_CTX = "/home";
|
22f5de
|
20 |
|
U |
21 |
/**
|
|
22 |
* Handles the HTTP <code>GET</code> method.
|
|
23 |
*
|
|
24 |
* @param request servlet request
|
|
25 |
* @param response servlet response
|
|
26 |
* @throws ServletException if a servlet-specific error occurs
|
|
27 |
* @throws IOException if an I/O error occurs
|
|
28 |
*/
|
|
29 |
@Override
|
|
30 |
protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
|
31 |
throws ServletException, IOException {
|
2a7560
|
32 |
String contextPath = request.getContextPath();
|
U |
33 |
if(HOME_CTX.equals(contextPath)) {
|
22f5de
|
34 |
String userName = getUserName(request);
|
2a7560
|
35 |
if (userName != null) {
|
U |
36 |
String urlUser = getUrlUser(request, userName);
|
|
37 |
if(userName.equals(urlUser)) {
|
|
38 |
seiteAusgeben(request, response);
|
|
39 |
} else {
|
|
40 |
logger.fine("Wrong user.");
|
|
41 |
}
|
|
42 |
} else {
|
|
43 |
logger.fine("Missing login.");
|
|
44 |
}
|
|
45 |
} else {
|
|
46 |
seiteAusgeben(request, response);
|
|
47 |
}
|
22f5de
|
48 |
}
|
U |
49 |
|
2a7560
|
50 |
private void seiteAusgeben(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
|
U |
51 |
ServletOutputStream out = response.getOutputStream();
|
|
52 |
printHeader(out);
|
|
53 |
super.doGet(request, response);
|
|
54 |
printFooter(out);
|
|
55 |
}
|
|
56 |
|
|
57 |
private void printHeader(ServletOutputStream out) throws IOException {
|
|
58 |
out.print("<!DOCTYPE html><html><head>");
|
|
59 |
out.print("<link rel=\"stylesheet\" type=\"text/css\" href=\"/jslib/bootstrap/css/bootstrap.min.css\">");
|
|
60 |
out.print("</head><body>");
|
|
61 |
}
|
|
62 |
|
|
63 |
private void printFooter(ServletOutputStream out) throws IOException {
|
|
64 |
out.print("</body></html>");
|
|
65 |
}
|
|
66 |
|
|
67 |
private String getUrlUser(HttpServletRequest request, String userName) throws IOException {
|
|
68 |
String result = "";
|
|
69 |
String requestUrlStr = request.getRequestURL().toString();
|
|
70 |
String contextPath = request.getContextPath();
|
22f5de
|
71 |
if(contextPath != null && requestUrlStr != null && userName != null) {
|
U |
72 |
int start = requestUrlStr.indexOf(contextPath);
|
|
73 |
start += contextPath.length();
|
|
74 |
start++;
|
|
75 |
int end = start + userName.length();
|
|
76 |
try {
|
|
77 |
result = requestUrlStr.substring(start, end);
|
|
78 |
} catch(Exception ex) {
|
2a7560
|
79 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
|
22f5de
|
80 |
}
|
U |
81 |
}
|
|
82 |
return result;
|
|
83 |
}
|
|
84 |
|
|
85 |
private String getUserName(HttpServletRequest hr) {
|
|
86 |
String userName = null;
|
|
87 |
Object p = hr.getUserPrincipal();
|
|
88 |
if (p instanceof Principal) {
|
|
89 |
userName = ((Principal) p).getName();
|
|
90 |
}
|
|
91 |
return userName;
|
|
92 |
}
|
|
93 |
|
|
94 |
/**
|
|
95 |
* Returns a short description of the servlet.
|
|
96 |
*
|
|
97 |
* @return a String containing servlet description
|
|
98 |
*/
|
|
99 |
@Override
|
|
100 |
public String getServletInfo() {
|
|
101 |
return "Short description";
|
2a7560
|
102 |
}
|
22f5de
|
103 |
|
U |
104 |
}
|