Dateiverwaltung für die WebBox
ulrich
2017-03-19 72e43ddf5a01b28d57a41bdbd4b77a0519d92912
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
72e43d 23 import de.uhilger.filecms.data.FileRef;
U 24 import de.uhilger.filecms.web.Initialiser;
b7475d 25 import de.uhilger.transit.web.RequestKontext;
U 26 import de.uhilger.transit.web.WebKontext;
72e43d 27 import java.io.File;
U 28 import java.security.Principal;
29 import java.util.logging.Logger;
b7475d 30 import javax.servlet.ServletContext;
U 31 import javax.servlet.http.HttpServletRequest;
32
33 /**
34  *
35  */
36 public abstract class Api implements WebKontext, RequestKontext {
37   
72e43d 38   private static final Logger logger = Logger.getLogger(Api.class.getName());
U 39
40   public static final String WBX_DATA_PATH = "daten/";
41   public static final String PUB_DIR_PATH = "www/";
42   public static final String HOME_DIR_PATH = "home/";
43   public static final String PUB_DIR_NAME = "Oeffentlich";
44   //public static final String HOME_DIR_NAME = "Persoenlicher Ordner";
45   public static final String HOME_DIR_NAME = "Persoenlich";
46   public static final String WBX_ADMIN_ROLE = "wbxAdmin";
47   
48   public static final String WBX_BASE = "$basis";
49   public static final String WBX_DATA = "$daten";
50   
b7475d 51   /** Zeiger zum Servlet-Kontext dieser Anwendung */
U 52   private ServletContext ctx;
53   
8931b7 54   /** Zeiger zum Request, der zur Ausfuehrung fuehrte */
b7475d 55   private HttpServletRequest request;  
U 56   
57   
72e43d 58   /**
U 59    * Einen relativen Pfad in einen absoluten Pfad der WebBox 
60    * aufloesen.
61    * 
62    * Nur die absoluten Pfade zu PUB_DIR_NAME, HOME_DIR_NAME 
63    * sowie WBX_BASE und WBX_DATA werden ausgegeben. Letztere 
64    * beiden nur fuer Nutzer mit der Rolle WBX_ADMIN_ROLE.
65    * 
66    * D.h., es werden nur Pfade aufgeloest, die sich innerhalb 
67    * des Ordners der WeBox befinden.
68    * 
69    * @param relPath
70    * @return 
71    */
72   protected File getTargetDir(String relPath) {
73     logger.fine(relPath);
74     File targetDir;
75     String targetPath = null;
76     if(relPath.startsWith(PUB_DIR_NAME)) {
77       targetPath = PUB_DIR_PATH + getUserName() + relPath.substring(PUB_DIR_NAME.length());
78       targetDir = new File(getBase().getAbsolutePath(), targetPath);
79     } else if(relPath.startsWith(HOME_DIR_NAME)) {
80       targetPath = HOME_DIR_PATH + getUserName() + relPath.substring(HOME_DIR_NAME.length());
81       targetDir = new File(getBase().getAbsolutePath(), targetPath);
82     } else if(getRequest().isUserInRole(WBX_ADMIN_ROLE)) {
83       logger.fine("in admin role");
84       if(relPath.startsWith(WBX_BASE)) {
85         logger.fine("is base");
86         targetPath = getCatalinaBase();
87         targetDir = new File(targetPath, relPath.substring(WBX_BASE.length()));
88       } else if(relPath.startsWith(WBX_DATA)) {
89         targetPath = getWbxDataDir();
90         logger.fine("is data, combine " + targetPath + ' ' + relPath.substring(WBX_DATA.length()));
91         targetDir = new File(targetPath, relPath.substring(WBX_DATA.length()));
92       } else {
93         targetDir = getDefaultDir(relPath);
94       }
95     } else {
96       // kann eigentlich nicht sein..
97       targetDir = getDefaultDir(relPath);
98     }
99     logger.fine("returning targetDir " + targetDir.getAbsolutePath());
100     //File targetDir = new File(getBase().getAbsolutePath(), targetPath);
101     return targetDir;
102   }
103   
104   protected File getDefaultDir(String relPath) {
105     String targetPath = PUB_DIR_PATH + getUserName() + relPath.substring(PUB_DIR_NAME.length());
106     return new File(getBase().getAbsolutePath(), targetPath);
107   }
108   
109   protected FileRef getBase() {
110     FileRef base = null;
111     Object o = getServletContext().getAttribute(Initialiser.FILE_BASE);
112     if(o instanceof String) {
113       String baseStr = (String) o;
114       logger.fine(baseStr);
115       File file = new File(baseStr);
116       base = new FileRef(file.getAbsolutePath(), file.isDirectory());
117     }
118     return base;
119   }
120   
121   protected String getUserName() {
122     String userName = null;
123     Object p = getRequest().getUserPrincipal();
124     if(p instanceof Principal) {
125       userName = ((Principal) p).getName();
126     }
127     return userName;
128   }    
129   
130   protected String getCatalinaBase() {
131     String path = getServletContext().getRealPath("/");
132     logger.fine("getRealPath: " + path); // file-cms in webapps
133     File file = new File(path);
134     file = file.getParentFile().getParentFile();
135     return file.getAbsolutePath();
136   }
137   
138   protected String getWbxDataDir() {
139     String wbxBase = getBase().getAbsolutePath();
140     File file = new File(wbxBase);
141     return file.getAbsolutePath();
142   }
b7475d 143   /* ------------- Implementierung WebKontext ------------- */
U 144
145   @Override
146   public ServletContext getServletContext() {
147     return ctx;
148   }
149
150   @Override
151   public void setServletContext(ServletContext servletContext) {
152     this.ctx = servletContext;
153   }
154   
155   /* ------------- Implementierung RequestKontext ------------- */
156
157   @Override
158   public HttpServletRequest getRequest() {
159     return request;
160   }
161
162   @Override
163   public void setRequest(HttpServletRequest r) {
164     this.request = r;
165   }
166   
167 }