Dateiverwaltung für die WebBox
ulrich
2017-07-07 fafe1b3b2b35a3c032a367d5ed76b243f6d654a9
commit | author | age
b7475d 1 /*
8931b7 2
U 3     Dateiverwaltung - File management in your browser
4     Copyright (C) 2017 Ulrich Hilger, http://uhilger.de
5
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU Affero General Public License as
8     published by the Free Software Foundation, either version 3 of the
9     License, or (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU Affero General Public License for more details.
15
16     You should have received a copy of the GNU Affero General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
b7475d 20
U 21 package de.uhilger.filecms.api;
22
6e70be 23 import de.uhilger.filecms.data.FileRef;
U 24 import de.uhilger.filecms.web.Initialiser;
a62bed 25 import de.uhilger.transit.Access;
6e70be 26 import de.uhilger.transit.web.RequestKontext;
6e1a29 27 import de.uhilger.transit.web.WebKontext;
72e43d 28 import java.io.File;
6e70be 29 import java.security.Principal;
72e43d 30 import java.util.logging.Logger;
6e1a29 31 import javax.servlet.ServletContext;
6e70be 32 import javax.servlet.http.HttpServletRequest;
b7475d 33
U 34 /**
35  *
36  */
6e70be 37 public abstract class Api implements WebKontext, RequestKontext {
6e1a29 38   
U 39   protected ServletContext ctx;  
6e70be 40   
U 41   /** Zeiger zum Request, der zur Ausfuehrung fuehrte */
42   protected HttpServletRequest request;  
b7475d 43   
72e43d 44   private static final Logger logger = Logger.getLogger(Api.class.getName());
U 45
46   public static final String WBX_DATA_PATH = "daten/";
47   public static final String PUB_DIR_PATH = "www/";
48   public static final String HOME_DIR_PATH = "home/";
49   public static final String PUB_DIR_NAME = "Oeffentlich";
50   //public static final String HOME_DIR_NAME = "Persoenlicher Ordner";
51   public static final String HOME_DIR_NAME = "Persoenlich";
52   public static final String WBX_ADMIN_ROLE = "wbxAdmin";
53   
54   public static final String WBX_BASE = "$basis";
55   public static final String WBX_DATA = "$daten";
ae9140 56     
72e43d 57   /**
U 58    * Einen relativen Pfad in einen absoluten Pfad der WebBox 
59    * aufloesen.
60    * 
61    * Nur die absoluten Pfade zu PUB_DIR_NAME, HOME_DIR_NAME 
62    * sowie WBX_BASE und WBX_DATA werden ausgegeben. Letztere 
63    * beiden nur fuer Nutzer mit der Rolle WBX_ADMIN_ROLE.
64    * 
65    * D.h., es werden nur Pfade aufgeloest, die sich innerhalb 
66    * des Ordners der WeBox befinden.
67    * 
68    * @param relPath
69    * @return 
70    */
71   protected File getTargetDir(String relPath) {
72     logger.fine(relPath);
73     File targetDir;
74     String targetPath = null;
75     if(relPath.startsWith(PUB_DIR_NAME)) {
76       targetPath = PUB_DIR_PATH + getUserName() + relPath.substring(PUB_DIR_NAME.length());
77       targetDir = new File(getBase().getAbsolutePath(), targetPath);
78     } else if(relPath.startsWith(HOME_DIR_NAME)) {
79       targetPath = HOME_DIR_PATH + getUserName() + relPath.substring(HOME_DIR_NAME.length());
80       targetDir = new File(getBase().getAbsolutePath(), targetPath);
81     } else if(getRequest().isUserInRole(WBX_ADMIN_ROLE)) {
82       logger.fine("in admin role");
83       if(relPath.startsWith(WBX_BASE)) {
84         logger.fine("is base");
6e1a29 85         targetPath = getCatalinaBase(ctx);
72e43d 86         targetDir = new File(targetPath, relPath.substring(WBX_BASE.length()));
U 87       } else if(relPath.startsWith(WBX_DATA)) {
6e1a29 88         targetPath = getWbxDataDir(ctx);
72e43d 89         logger.fine("is data, combine " + targetPath + ' ' + relPath.substring(WBX_DATA.length()));
U 90         targetDir = new File(targetPath, relPath.substring(WBX_DATA.length()));
91       } else {
92         targetDir = getDefaultDir(relPath);
93       }
94     } else {
95       // kann eigentlich nicht sein..
96       targetDir = getDefaultDir(relPath);
97     }
98     logger.fine("returning targetDir " + targetDir.getAbsolutePath());
99     //File targetDir = new File(getBase().getAbsolutePath(), targetPath);
100     return targetDir;
101   }
102   
103   protected File getDefaultDir(String relPath) {
104     String targetPath = PUB_DIR_PATH + getUserName() + relPath.substring(PUB_DIR_NAME.length());
105     return new File(getBase().getAbsolutePath(), targetPath);
106   }
107   
108   protected FileRef getBase() {
109     FileRef base = null;
6e1a29 110     Object o = getFileBase(ctx);
ae9140 111     if(o instanceof File) {
U 112       File file = (File) o;
72e43d 113       base = new FileRef(file.getAbsolutePath(), file.isDirectory());
U 114     }
115     return base;
b7475d 116   }
6e70be 117   
U 118   /* -------------- Hilfsfunktionen --------------- */
119   
120   /**
121    * Das Datenverzeichnis der WebBox ermitteln
122    * @return Ordner $wbx/daten
123    */
124   protected File getFileBase(ServletContext ctx) {
125     File file = null;
126     Object o = ctx.getAttribute(Initialiser.FILE_BASE);
127     if(o instanceof String) {
128       String baseStr = (String) o;
129       logger.fine(baseStr);
130       file = new File(baseStr);
131     }
132     return file;
133   }
134   
135   /**
136    * Den absoluten Pfad zum Verzeichnis ermitteln das gemaess der 
137    * Tomcat-Doku als CATALINA_BASE der WebBox gilt
138    * @return absoluter Pfad zu $wbx/sys/base
139    */
140   protected String getCatalinaBase(ServletContext ctx) {
141     String path = ctx.getRealPath("/");
142     logger.fine("getRealPath: " + path); // file-cms in webapps
143     File file = new File(path);
144     file = file.getParentFile().getParentFile();
145     return file.getAbsolutePath();
146   }
147   
148   /**
149    * Den absoluten Pfad zum Datenverzeichnis der WebBox ermitteln
150    * @return absoluter Pfad zu $wbx/daten
151    */
152   protected String getWbxDataDir(ServletContext ctx) {
153     return getFileBase(ctx).getAbsolutePath();
154   }
155   
156   /**
157    * Das Verzeichnis ermitteln, in dem die WebBox laeuft
158    * @return der Ordner $wbx
159    */
160   protected File getWbxDir(ServletContext ctx) {
161     String path = ctx.getRealPath("/");
162     logger.fine("getRealPath: " + path);
163     File file = new File(path);
164     file = file.getParentFile().getParentFile().getParentFile().getParentFile();    
165     logger.fine("WebBox: " + file.getAbsolutePath());
166     return file;
167   }
168   
169   /**
170    * den Namen des angemeldeten Benutzers ermitteln
171    * @return Name des angemeldeten Benutzers oder null, wenn keiner angemeldet ist
172    */
173   protected String getUserName() {
174     String userName = null;
175     Object p = getRequest().getUserPrincipal();
176     if(p instanceof Principal) {
177       userName = ((Principal) p).getName();
178     }
179     return userName;
180   }        
6e1a29 181
U 182   
183   /* ------------- Implementierung WebKontext ------------- */
184
185   @Override
a62bed 186   @Access(type = Access.Type.RESTRICT)
6e1a29 187   public ServletContext getServletContext() {
U 188     return ctx;
189   }
190
191   @Override
a62bed 192   @Access(type = Access.Type.RESTRICT)
6e1a29 193   public void setServletContext(ServletContext servletContext) {
U 194     this.ctx = servletContext;
195   }
b7475d 196   
6e70be 197   /* ------------- Implementierung RequestKontext ------------- */
U 198
199   @Override
a62bed 200   @Access(type = Access.Type.RESTRICT)
6e70be 201   public HttpServletRequest getRequest() {
U 202     return request;
203   }
204
205   @Override
a62bed 206   @Access(type = Access.Type.RESTRICT)
6e70be 207   public void setRequest(HttpServletRequest r) {
U 208     this.request = r;
209   }
210   
b7475d 211 }