WebBox Klassenbibliothek
ulrich
2017-03-29 592772978cac6558dc894763e21ff568a58a4490
commit | author | age
592772 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  */
18
19 package de.uhilger.wbx.api;
20
21 import de.uhilger.transit.web.RequestKontext;
22 import de.uhilger.transit.web.WebKontext;
23 import de.uhilger.wbx.web.Initialiser;
24 import java.io.File;
25 import java.security.Principal;
26 import java.util.logging.Logger;
27 import javax.servlet.ServletContext;
28 import javax.servlet.http.HttpServletRequest;
29
30 /**
31  *
32  */
33 public abstract class ApiBase implements WebKontext, RequestKontext {
34
35   private static final Logger logger = Logger.getLogger(ApiBase.class.getName());
36   
37   /** Zeiger zum Servlet-Kontext dieser Anwendung */
38   protected ServletContext ctx;
39   
40   /** Zeiger zum Request, der zur Ausfuehrung fuehrte */
41   protected HttpServletRequest request;  
42   
43   
44   /**
45    * Das Datenverzeichnis der WebBox ermitteln
46    * @return Ordner $wbx/daten
47    */
48   protected File getFileBase() {
49     File file = null;
50     Object o = getServletContext().getAttribute(Initialiser.FILE_BASE);
51     if(o instanceof String) {
52       String baseStr = (String) o;
53       logger.fine(baseStr);
54       file = new File(baseStr);
55     }
56     return file;
57   }
58   
59   /**
60    * Den absoluten Pfad zum Verzeichnis ermitteln das gemaess der 
61    * Tomcat-Doku als CATALINA_BASE der WebBox gilt
62    * @return absoluter Pfad zu $wbx/sys/base
63    */
64   protected String getCatalinaBase() {
65     String path = getServletContext().getRealPath("/");
66     logger.fine("getRealPath: " + path); // file-cms in webapps
67     File file = new File(path);
68     file = file.getParentFile().getParentFile();
69     return file.getAbsolutePath();
70   }
71   
72   /**
73    * Den absoluten Pfad zum Datenverzeichnis der WebBox ermitteln
74    * @return absoluter Pfad zu $wbx/daten
75    */
76   protected String getWbxDataDir() {
77     return getFileBase().getAbsolutePath();
78   }
79   
80   /**
81    * Das Verzeichnis ermitteln, in dem die WebBox laeuft
82    * @return der Ordner $wbx
83    */
84   protected File getWbxDir() {
85     String path = getServletContext().getRealPath("/");
86     logger.fine("getRealPath: " + path);
87     File file = new File(path);
88     file = file.getParentFile().getParentFile().getParentFile().getParentFile();    
89     logger.fine("WebBox: " + file.getAbsolutePath());
90     return file;
91   }
92   
93   /**
94    * den Namen des angemeldeten Benutzers ermitteln
95    * @return Name des angemeldeten Benutzers oder null, wenn keiner angemeldet ist
96    */
97   protected String getUserName() {
98     String userName = null;
99     Object p = getRequest().getUserPrincipal();
100     if(p instanceof Principal) {
101       userName = ((Principal) p).getName();
102     }
103     return userName;
104   }      
105
106   /* ------------- Implementierung WebKontext ------------- */
107
108   @Override
109   public ServletContext getServletContext() {
110     return ctx;
111   }
112
113   @Override
114   public void setServletContext(ServletContext servletContext) {
115     this.ctx = servletContext;
116   }
117   
118   /* ------------- Implementierung RequestKontext ------------- */
119
120   @Override
121   public HttpServletRequest getRequest() {
122     return request;
123   }
124
125   @Override
126   public void setRequest(HttpServletRequest r) {
127     this.request = r;
128   }
129   
130   
131   
132 }