Dateiverwaltung für die WebBox
ulrich
2018-03-03 eb2a2d78152c7ca689e3aac116c107229a896a66
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/";
968b07 49   public static final String DAV_DIR_PATH = "dav/";
U 50
72e43d 51   public static final String PUB_DIR_NAME = "Oeffentlich";
U 52   //public static final String HOME_DIR_NAME = "Persoenlicher Ordner";
53   public static final String HOME_DIR_NAME = "Persoenlich";
968b07 54   public static final String DAV_DIR_NAME = "Austausch";
U 55
72e43d 56   public static final String WBX_ADMIN_ROLE = "wbxAdmin";
U 57   
58   public static final String WBX_BASE = "$basis";
59   public static final String WBX_DATA = "$daten";
968b07 60   public static final String DAV_DATA = "$dav";
ae9140 61     
72e43d 62   /**
U 63    * Einen relativen Pfad in einen absoluten Pfad der WebBox 
64    * aufloesen.
65    * 
66    * Nur die absoluten Pfade zu PUB_DIR_NAME, HOME_DIR_NAME 
67    * sowie WBX_BASE und WBX_DATA werden ausgegeben. Letztere 
68    * beiden nur fuer Nutzer mit der Rolle WBX_ADMIN_ROLE.
69    * 
70    * D.h., es werden nur Pfade aufgeloest, die sich innerhalb 
71    * des Ordners der WeBox befinden.
72    * 
73    * @param relPath
74    * @return 
75    */
76   protected File getTargetDir(String relPath) {
77     logger.fine(relPath);
78     File targetDir;
79     String targetPath = null;
80     if(relPath.startsWith(PUB_DIR_NAME)) {
81       targetPath = PUB_DIR_PATH + getUserName() + relPath.substring(PUB_DIR_NAME.length());
82       targetDir = new File(getBase().getAbsolutePath(), targetPath);
83     } else if(relPath.startsWith(HOME_DIR_NAME)) {
84       targetPath = HOME_DIR_PATH + getUserName() + relPath.substring(HOME_DIR_NAME.length());
85       targetDir = new File(getBase().getAbsolutePath(), targetPath);
968b07 86     } else if(relPath.startsWith(DAV_DIR_NAME)) {
U 87       targetPath = DAV_DIR_PATH + /* getUserName() +*/ relPath.substring(DAV_DIR_NAME.length());
88       targetDir = new File(getBase().getAbsolutePath(), targetPath);
72e43d 89     } else if(getRequest().isUserInRole(WBX_ADMIN_ROLE)) {
U 90       logger.fine("in admin role");
91       if(relPath.startsWith(WBX_BASE)) {
92         logger.fine("is base");
6e1a29 93         targetPath = getCatalinaBase(ctx);
72e43d 94         targetDir = new File(targetPath, relPath.substring(WBX_BASE.length()));
U 95       } else if(relPath.startsWith(WBX_DATA)) {
6e1a29 96         targetPath = getWbxDataDir(ctx);
72e43d 97         logger.fine("is data, combine " + targetPath + ' ' + relPath.substring(WBX_DATA.length()));
U 98         targetDir = new File(targetPath, relPath.substring(WBX_DATA.length()));
99       } else {
100         targetDir = getDefaultDir(relPath);
101       }
102     } else {
103       // kann eigentlich nicht sein..
104       targetDir = getDefaultDir(relPath);
105     }
106     logger.fine("returning targetDir " + targetDir.getAbsolutePath());
107     //File targetDir = new File(getBase().getAbsolutePath(), targetPath);
108     return targetDir;
109   }
110   
111   protected File getDefaultDir(String relPath) {
112     String targetPath = PUB_DIR_PATH + getUserName() + relPath.substring(PUB_DIR_NAME.length());
113     return new File(getBase().getAbsolutePath(), targetPath);
114   }
115   
116   protected FileRef getBase() {
117     FileRef base = null;
6e1a29 118     Object o = getFileBase(ctx);
ae9140 119     if(o instanceof File) {
U 120       File file = (File) o;
72e43d 121       base = new FileRef(file.getAbsolutePath(), file.isDirectory());
U 122     }
123     return base;
b7475d 124   }
6e70be 125   
U 126   /* -------------- Hilfsfunktionen --------------- */
127   
128   /**
129    * Das Datenverzeichnis der WebBox ermitteln
130    * @return Ordner $wbx/daten
131    */
132   protected File getFileBase(ServletContext ctx) {
133     File file = null;
134     Object o = ctx.getAttribute(Initialiser.FILE_BASE);
135     if(o instanceof String) {
136       String baseStr = (String) o;
137       logger.fine(baseStr);
138       file = new File(baseStr);
139     }
140     return file;
141   }
142   
143   /**
144    * Den absoluten Pfad zum Verzeichnis ermitteln das gemaess der 
145    * Tomcat-Doku als CATALINA_BASE der WebBox gilt
146    * @return absoluter Pfad zu $wbx/sys/base
147    */
148   protected String getCatalinaBase(ServletContext ctx) {
149     String path = ctx.getRealPath("/");
150     logger.fine("getRealPath: " + path); // file-cms in webapps
151     File file = new File(path);
152     file = file.getParentFile().getParentFile();
153     return file.getAbsolutePath();
154   }
155   
156   /**
157    * Den absoluten Pfad zum Datenverzeichnis der WebBox ermitteln
158    * @return absoluter Pfad zu $wbx/daten
159    */
160   protected String getWbxDataDir(ServletContext ctx) {
161     return getFileBase(ctx).getAbsolutePath();
162   }
163   
164   /**
165    * Das Verzeichnis ermitteln, in dem die WebBox laeuft
166    * @return der Ordner $wbx
167    */
168   protected File getWbxDir(ServletContext ctx) {
169     String path = ctx.getRealPath("/");
170     logger.fine("getRealPath: " + path);
171     File file = new File(path);
172     file = file.getParentFile().getParentFile().getParentFile().getParentFile();    
173     logger.fine("WebBox: " + file.getAbsolutePath());
174     return file;
175   }
176   
177   /**
178    * den Namen des angemeldeten Benutzers ermitteln
179    * @return Name des angemeldeten Benutzers oder null, wenn keiner angemeldet ist
180    */
181   protected String getUserName() {
182     String userName = null;
183     Object p = getRequest().getUserPrincipal();
184     if(p instanceof Principal) {
185       userName = ((Principal) p).getName();
186     }
187     return userName;
188   }        
6e1a29 189
U 190   
191   /* ------------- Implementierung WebKontext ------------- */
192
193   @Override
a62bed 194   @Access(type = Access.Type.RESTRICT)
6e1a29 195   public ServletContext getServletContext() {
U 196     return ctx;
197   }
198
199   @Override
a62bed 200   @Access(type = Access.Type.RESTRICT)
6e1a29 201   public void setServletContext(ServletContext servletContext) {
U 202     this.ctx = servletContext;
203   }
b7475d 204   
6e70be 205   /* ------------- Implementierung RequestKontext ------------- */
U 206
207   @Override
a62bed 208   @Access(type = Access.Type.RESTRICT)
6e70be 209   public HttpServletRequest getRequest() {
U 210     return request;
211   }
212
213   @Override
a62bed 214   @Access(type = Access.Type.RESTRICT)
6e70be 215   public void setRequest(HttpServletRequest r) {
U 216     this.request = r;
217   }
218   
b7475d 219 }