WebBox Klassenbibliothek
ulrich
2021-01-28 96dfd62fbfe771616045a253bbeb1415538e815f
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&uuml;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&auml;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");
1c2f00 73     //out.print("<link rel=\"stylesheet\" type=\"text/css\" href=\"/jslib/bootstrap/css/bootstrap.min.css\">\r\n");
066b66 74     out.print("<link rel=\"stylesheet\" type=\"text/css\" href=\"/jslib/lightbox/lightbox.css\">\r\n");
96dfd6 75     out.print("<link rel=\"stylesheet\" type=\"text/css\" href=\"/wbx-stile.css\">\r\n");
066b66 76     out.print("<link rel=\"stylesheet\" type=\"text/css\" href=\"stile.css\">\r\n");
4c53ff 77     String title = getTitle();
U 78     if(title != null) {
79       out.print("<title>");
80       out.print(title);
81       out.print(" ");
82       out.print(request.getRequestURI());
83       out.print("</title>\r\n");
84     }
066b66 85     out.print("</head><body class=\"p-3\">\r\n");
2a7560 86   }
U 87   
88   private void printFooter(ServletOutputStream out) throws IOException {
96dfd6 89     out.print("<script src=\"/jslib/jquery/jquery.min.js\"></script>\r\n");
066b66 90     out.print("<script src=\"/jslib/lightbox/lightbox.min.js\"></script>\r\n");
2a7560 91     out.print("</body></html>");
U 92   }
93   
bc9f6a 94   /**
U 95    * Handles the HTTP <code>GET</code> method.
96    *
97    * @param request servlet request
98    * @param response servlet response
99    * @throws ServletException if a servlet-specific error occurs
100    * @throws IOException if an I/O error occurs
101    */
102   @Override
103   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException  {
104     String contextPath = request.getContextPath();
105     if(HOME_CTX.equals(contextPath)) {
106       String userName = getUserName(request);
107       if (userName != null) {
108         String urlUser = getUrlUser(request, userName);
109         if(userName.equals(urlUser)) {
110           seiteAusgeben(request, response);
111         } else {
112           logger.fine("Wrong user.");
113         }
114       } else {
115         logger.fine("Missing login.");
116       }
117     } else {
118       seiteAusgeben(request, response);
119     }
120   }
121   
122   protected String getUrlUser(HttpServletRequest request, String userName) {
2a7560 123     String result = "";
U 124     String requestUrlStr = request.getRequestURL().toString();
125     String contextPath = request.getContextPath();
22f5de 126     if(contextPath != null && requestUrlStr != null && userName != null) {
U 127       int start = requestUrlStr.indexOf(contextPath);
128       start += contextPath.length();
129       start++;
130       int end = start + userName.length();
131       try {
132         result = requestUrlStr.substring(start, end);
133       } catch(Exception ex) {
2a7560 134         logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
22f5de 135       }
U 136     }
137     return result;
138   }
139   
bc9f6a 140   protected String getUserName(HttpServletRequest hr) {
22f5de 141     String userName = null;
U 142     Object p = hr.getUserPrincipal();
143     if (p instanceof Principal) {
144       userName = ((Principal) p).getName();
145     }
146     return userName;
147   }
bc9f6a 148   
22f5de 149   /**
U 150    * Returns a short description of the servlet.
151    *
152    * @return a String containing servlet description
153    */
154   @Override
155   public String getServletInfo() {
156     return "Short description";
2a7560 157   }
22f5de 158
U 159 }