WebBox Klassenbibliothek
ulrich
2017-12-27 05b5c097f8b02185d615703961afcb198ab23652
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;
21 import java.security.Principal;
2a7560 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;
28 import org.apache.catalina.servlets.DefaultServlet;
29
30 /**
7bcebf 31  * Das ViewServlet f&uuml;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&auml;nzt werden
36  * 
37  * TODO: Stylesheets dynamisch einbinden
22f5de 38  */
U 39 public class ViewServlet extends DefaultServlet {
40   
41   private static final Logger logger = Logger.getLogger(ViewServlet.class.getName());
2a7560 42   private static final String HOME_CTX = "/home";
22f5de 43
U 44   /**
45    * Handles the HTTP <code>GET</code> method.
46    *
47    * @param request servlet request
48    * @param response servlet response
49    * @throws ServletException if a servlet-specific error occurs
50    * @throws IOException if an I/O error occurs
51    */
52   @Override
53   protected void doGet(HttpServletRequest request, HttpServletResponse response)
54           throws ServletException, IOException {
2a7560 55     String contextPath = request.getContextPath();
U 56     if(HOME_CTX.equals(contextPath)) {
22f5de 57       String userName = getUserName(request);
2a7560 58       if (userName != null) {
U 59         String urlUser = getUrlUser(request, userName);
60         if(userName.equals(urlUser)) {
61           seiteAusgeben(request, response);
62         } else {
63           logger.fine("Wrong user.");
64         }
65       } else {
66         logger.fine("Missing login.");
67       }
68     } else {
69       seiteAusgeben(request, response);
70     }
22f5de 71   }
U 72   
2a7560 73   private void seiteAusgeben(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
U 74     ServletOutputStream out = response.getOutputStream();
75     printHeader(out);
76     super.doGet(request, response);
77     printFooter(out);
78   }
79   
80   private void printHeader(ServletOutputStream out) throws IOException {
066b66 81     out.print("<!DOCTYPE html><html><head>\r\n");
a123ba 82     out.print("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"/>\r\n");
066b66 83     out.print("<link rel=\"stylesheet\" type=\"text/css\" href=\"/jslib/bootstrap/css/bootstrap.min.css\">\r\n");
U 84     out.print("<link rel=\"stylesheet\" type=\"text/css\" href=\"/jslib/lightbox/lightbox.css\">\r\n");
85     out.print("<link rel=\"stylesheet\" type=\"text/css\" href=\"stile.css\">\r\n");
86     out.print("</head><body class=\"p-3\">\r\n");
2a7560 87   }
U 88   
89   private void printFooter(ServletOutputStream out) throws IOException {
066b66 90     out.print("<script src=\"/jslib/jquery/jquery.min.js\"></script>\r\n");
U 91     out.print("<script src=\"/jslib/lightbox/lightbox.min.js\"></script>\r\n");
2a7560 92     out.print("</body></html>");
U 93   }
94   
95   private String getUrlUser(HttpServletRequest request, String userName) throws IOException {
96     String result = "";
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   
113   private String getUserName(HttpServletRequest hr) {
114     String userName = null;
115     Object p = hr.getUserPrincipal();
116     if (p instanceof Principal) {
117       userName = ((Principal) p).getName();
118     }
119     return userName;
120   }
121
122   /**
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 }