WebBox Klassenbibliothek
ulrich
2017-03-31 b86c7798e88d6070a1493c6e941a3076c73ce47b
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 /**
b86c77 31  * Die Klasse ApiBase enthaelt Methoden, die Klassen gemeinsam 
U 32  * haben, die als Programmschnittstelle fuer Apps einer WebBox 
33  * dienen sollen. Sie ist als abstrakte Basisklasse fuer solche 
34  * Klassen vorgesehen.
592772 35  */
595841 36 public abstract class ApiBase implements RequestKontext {
592772 37
U 38   private static final Logger logger = Logger.getLogger(ApiBase.class.getName());
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    */
595841 48   protected File getFileBase(ServletContext ctx) {
592772 49     File file = null;
595841 50     Object o = ctx.getAttribute(Initialiser.FILE_BASE);
592772 51     if(o instanceof String) {
U 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    */
595841 64   protected String getCatalinaBase(ServletContext ctx) {
U 65     String path = ctx.getRealPath("/");
592772 66     logger.fine("getRealPath: " + path); // file-cms in webapps
U 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    */
595841 76   protected String getWbxDataDir(ServletContext ctx) {
U 77     return getFileBase(ctx).getAbsolutePath();
592772 78   }
U 79   
80   /**
81    * Das Verzeichnis ermitteln, in dem die WebBox laeuft
82    * @return der Ordner $wbx
83    */
595841 84   protected File getWbxDir(ServletContext ctx) {
U 85     String path = ctx.getRealPath("/");
592772 86     logger.fine("getRealPath: " + path);
U 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 RequestKontext ------------- */
107
108   @Override
109   public HttpServletRequest getRequest() {
110     return request;
111   }
112
113   @Override
114   public void setRequest(HttpServletRequest r) {
115     this.request = r;
116   }
117   
118   
119   
120 }