WebBox Klassenbibliothek
ulrich
2017-03-29 59584163af6e9d4e6ba668e74c596a08bf5bd3fd
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  */
595841 33 public abstract class ApiBase implements RequestKontext {
592772 34
U 35   private static final Logger logger = Logger.getLogger(ApiBase.class.getName());
36   
37   /** Zeiger zum Request, der zur Ausfuehrung fuehrte */
38   protected HttpServletRequest request;  
39   
40   
41   /**
42    * Das Datenverzeichnis der WebBox ermitteln
43    * @return Ordner $wbx/daten
44    */
595841 45   protected File getFileBase(ServletContext ctx) {
592772 46     File file = null;
595841 47     Object o = ctx.getAttribute(Initialiser.FILE_BASE);
592772 48     if(o instanceof String) {
U 49       String baseStr = (String) o;
50       logger.fine(baseStr);
51       file = new File(baseStr);
52     }
53     return file;
54   }
55   
56   /**
57    * Den absoluten Pfad zum Verzeichnis ermitteln das gemaess der 
58    * Tomcat-Doku als CATALINA_BASE der WebBox gilt
59    * @return absoluter Pfad zu $wbx/sys/base
60    */
595841 61   protected String getCatalinaBase(ServletContext ctx) {
U 62     String path = ctx.getRealPath("/");
592772 63     logger.fine("getRealPath: " + path); // file-cms in webapps
U 64     File file = new File(path);
65     file = file.getParentFile().getParentFile();
66     return file.getAbsolutePath();
67   }
68   
69   /**
70    * Den absoluten Pfad zum Datenverzeichnis der WebBox ermitteln
71    * @return absoluter Pfad zu $wbx/daten
72    */
595841 73   protected String getWbxDataDir(ServletContext ctx) {
U 74     return getFileBase(ctx).getAbsolutePath();
592772 75   }
U 76   
77   /**
78    * Das Verzeichnis ermitteln, in dem die WebBox laeuft
79    * @return der Ordner $wbx
80    */
595841 81   protected File getWbxDir(ServletContext ctx) {
U 82     String path = ctx.getRealPath("/");
592772 83     logger.fine("getRealPath: " + path);
U 84     File file = new File(path);
85     file = file.getParentFile().getParentFile().getParentFile().getParentFile();    
86     logger.fine("WebBox: " + file.getAbsolutePath());
87     return file;
88   }
89   
90   /**
91    * den Namen des angemeldeten Benutzers ermitteln
92    * @return Name des angemeldeten Benutzers oder null, wenn keiner angemeldet ist
93    */
94   protected String getUserName() {
95     String userName = null;
96     Object p = getRequest().getUserPrincipal();
97     if(p instanceof Principal) {
98       userName = ((Principal) p).getName();
99     }
100     return userName;
101   }      
102
103   /* ------------- Implementierung RequestKontext ------------- */
104
105   @Override
106   public HttpServletRequest getRequest() {
107     return request;
108   }
109
110   @Override
111   public void setRequest(HttpServletRequest r) {
112     this.request = r;
113   }
114   
115   
116   
117 }