WebBox Klassenbibliothek
ulrich
2017-03-04 fcc098bcea5f82a518d903105c9e777a15d8b1c2
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\">");
7d6a51 60     out.print("<link rel=\"stylesheet\" type=\"text/css\" href=\"stile.css\">");
U 61     out.print("</head><body class=\"p-3\">");
2a7560 62   }
U 63   
64   private void printFooter(ServletOutputStream out) throws IOException {
65     out.print("</body></html>");
66   }
67   
68   private String getUrlUser(HttpServletRequest request, String userName) throws IOException {
69     String result = "";
70     String requestUrlStr = request.getRequestURL().toString();
71     String contextPath = request.getContextPath();
22f5de 72     if(contextPath != null && requestUrlStr != null && userName != null) {
U 73       int start = requestUrlStr.indexOf(contextPath);
74       start += contextPath.length();
75       start++;
76       int end = start + userName.length();
77       try {
78         result = requestUrlStr.substring(start, end);
79       } catch(Exception ex) {
2a7560 80         logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
22f5de 81       }
U 82     }
83     return result;
84   }
85   
86   private String getUserName(HttpServletRequest hr) {
87     String userName = null;
88     Object p = hr.getUserPrincipal();
89     if (p instanceof Principal) {
90       userName = ((Principal) p).getName();
91     }
92     return userName;
93   }
94
95   /**
96    * Returns a short description of the servlet.
97    *
98    * @return a String containing servlet description
99    */
100   @Override
101   public String getServletInfo() {
102     return "Short description";
2a7560 103   }
22f5de 104
U 105 }