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