commit | author | age
|
1da1ca
|
1 |
/* |
42c48e
|
2 |
Transit - Remote procedure calls made simple |
U |
3 |
Copyright (c) 2012 Ulrich Hilger |
|
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 published by |
|
7 |
the Free Software Foundation, either version 3 of the License, or |
|
8 |
(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/>. |
1da1ca
|
17 |
*/ |
U |
18 |
package de.uhilger.transit.web; |
|
19 |
|
|
20 |
import java.security.Principal; |
|
21 |
import javax.servlet.ServletConfig; |
|
22 |
import javax.servlet.ServletException; |
|
23 |
import javax.servlet.http.HttpServlet; |
|
24 |
import javax.servlet.http.HttpServletRequest; |
|
25 |
|
|
26 |
/** |
42c48e
|
27 |
* Basisklasse mit gemeinsamem Code fuer die TransitServlets |
U |
28 |
* |
|
29 |
* @author Copyright (c) Ulrich Hilger, http://uhilger.de |
|
30 |
* @author Published under the terms and conditions of the |
|
31 |
* <a href="http://www.gnu.org/licenses/agpl-3.0" target="_blank">GNU Affero |
|
32 |
* General Public License</a> |
1da1ca
|
33 |
* |
42c48e
|
34 |
* @version 1, September 16, 2012 |
1da1ca
|
35 |
*/ |
U |
36 |
public abstract class AbstractServlet extends HttpServlet { |
|
37 |
|
|
38 |
public static final String MIME_JSON = "application/json"; |
|
39 |
public static final String MIME_XML = "text/xml"; |
|
40 |
|
|
41 |
public static final String KLASSEN_TRENNER = ";"; |
|
42 |
|
|
43 |
private static final String[] specialChars = {new String("\u00c4"), new String("\u00d6"), |
|
44 |
new String("\u00dc"), new String("\u00e4"), new String("\u00f6"), new String("\u00fc"), new String("\u00df")}; |
|
45 |
|
|
46 |
/** Name, der als Benutzername verwendet wird, wenn kein Benutzer angemeldet ist */ |
|
47 |
public static final String ANONYMER_NUTZER = "anonymous"; |
|
48 |
|
|
49 |
protected String[] klassen; |
|
50 |
|
|
51 |
@Override |
|
52 |
public void init(ServletConfig servletConfig) throws ServletException{ |
|
53 |
this.klassen = servletConfig.getInitParameter("klassen").split(KLASSEN_TRENNER); |
|
54 |
super.init(servletConfig); |
|
55 |
} |
|
56 |
|
|
57 |
/** |
|
58 |
* Den Namen des angemeldeten Benutzers ermitteln. |
|
59 |
* |
|
60 |
* @param req die Anfrage, deren Benutzer ermittelt werden soll |
|
61 |
* @return Name des Benutzers oder <code>anonymous</code>, wenn |
|
62 |
* kein Benutzer angemeldet ist |
|
63 |
*/ |
|
64 |
protected String getUserName(HttpServletRequest req) { |
|
65 |
String userName = null; |
|
66 |
Principal p = req.getUserPrincipal(); |
|
67 |
if(p != null) { |
|
68 |
userName = p.getName(); |
|
69 |
} |
|
70 |
if(userName == null) { |
|
71 |
userName = ANONYMER_NUTZER; |
|
72 |
} |
|
73 |
return userName; |
|
74 |
} |
|
75 |
|
|
76 |
public String escapeHtml(String text) { |
|
77 |
text = text.replace(specialChars[0], "Ä"); |
|
78 |
text = text.replace(specialChars[1], "Ö"); |
|
79 |
text = text.replace(specialChars[2], "Ü"); |
|
80 |
text = text.replace(specialChars[3], "ä"); |
|
81 |
text = text.replace(specialChars[4], "ö"); |
|
82 |
text = text.replace(specialChars[5], "ü"); |
|
83 |
text = text.replace(specialChars[6], "ß"); |
|
84 |
return text; |
|
85 |
} |
|
86 |
|
|
87 |
/** |
|
88 |
* Hier wird geprueft, ob der Name einer Klasse der Liste der erlaubten |
|
89 |
* Klassen bzw. Packages entspricht. Im Deployment Descriptor wird ein |
|
90 |
* Ausdruck wie z.B. |
|
91 |
* |
|
92 |
* de.uhilger.test.web;de.uhilger.test.api;de.uhilger.test.db.KlassenName |
|
93 |
* |
|
94 |
* erwartet. Diese Methode spaltet diesen Ausdruck an den ';' auf und prueft |
|
95 |
* fuer jeden Ausdruck, ob der KlassenName mit diesem Ausdruck beginnt. |
|
96 |
* Beginnt der Klassenname mit einem der Ausdruecke, wird der Zugriff erlaubt |
|
97 |
* |
|
98 |
* @param klassenName Name der Klasse, fuer die der Zugriff geprueft |
|
99 |
* werden soll |
|
100 |
* @return true, wenn die Klasse laut dem Eintrag im Deployment Desccriptor |
|
101 |
* aufgerufen werden darf, false wenn nicht |
|
102 |
*/ |
|
103 |
protected boolean istErlaubt(String klassenName) { |
|
104 |
boolean erlaubt = false; |
|
105 |
for(int i = 0; i < klassen.length && !erlaubt; i++) { |
|
106 |
erlaubt = klassenName.startsWith(klassen[i]); |
|
107 |
} |
|
108 |
return erlaubt; |
|
109 |
} |
|
110 |
|
|
111 |
} |