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