annotate servlet/src/digilib/auth/XMLAuthOps.java @ 532:6d66b4ed7f07 digilibPDF

clean up
author robcast
date Fri, 05 Feb 2010 20:59:18 +0100
parents 9cedd170b581
children e758a49258e8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
1 /* XMLAuthOps -- Authentication class implementation using XML files
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
2
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
3 Digital Image Library servlet components
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
4
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
5 Copyright (C) 2001, 2002 Robert Casties (robcast@mail.berlios.de)
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
6
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
7 This program is free software; you can redistribute it and/or modify it
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
9 Free Software Foundation; either version 2 of the License, or (at your
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
10 option) any later version.
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
11
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
12 Please read license.txt for the full details. A copy of the GPL
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
13 may be found at http://www.gnu.org/copyleft/lgpl.html
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
14
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
16 along with this program; if not, write to the Free Software
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
18
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
19 */
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
20
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
21 package digilib.auth;
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
22
531
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
23 import java.io.File;
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
24 import java.util.List;
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
25 import java.util.Map;
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
26
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
27 import javax.servlet.http.HttpServletRequest;
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
28
531
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
29 import digilib.io.XMLListLoader;
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
30 import digilib.servlet.DigilibRequest;
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
31
531
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
32 /** Implementation of AuthOps using XML files.
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
33 *
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
34 * The configuration file is read by an XMLListLoader into HashTree objects for
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
35 * authentication paths and IP numbers.
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
36 */
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
37 public class XMLAuthOps extends AuthOpsImpl {
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
38
531
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
39 private File configFile;
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
40 private HashTree authPaths;
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
41 private HashTree authIPs;
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
42
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
43 /** Constructor taking an XML config file.
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
44 *
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
45 * @param u utils object
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
46 * @param confFile Configuration file.
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
47 * @throws AuthOpException Exception thrown on error.
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
48 */
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
49 public XMLAuthOps(File confFile) throws AuthOpException {
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
50 configFile = confFile;
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
51 init();
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
52 }
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
53
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
54 /** Set configuration file.
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
55 *
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
56 * @param confFile XML config file.
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
57 * @throws AuthOpException Exception thrown on error.
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
58 */
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
59 public void setConfig(File confFile) throws AuthOpException {
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
60 configFile = confFile;
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
61 init();
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
62 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
63
531
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
64 /** Initialize.
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
65 *
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
66 * Read configuration files and setup authentication arrays.
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
67 *
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
68 * @throws AuthOpException Exception thrown on error.
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
69 */
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
70 public void init() throws AuthOpException {
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
71 logger.debug("xmlauthops.init (" + configFile + ")");
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
72 Map<String, String> pathList = null;
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
73 Map<String, String> ipList = null;
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
74 try {
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
75 // load authPaths
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
76 XMLListLoader pathLoader =
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
77 new XMLListLoader("digilib-paths", "path", "name", "role");
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
78 pathList = pathLoader.loadURL(configFile.toURL().toString());
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
79 // load authIPs
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
80 XMLListLoader ipLoader =
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
81 new XMLListLoader("digilib-addresses", "address", "ip", "role");
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
82 ipList = ipLoader.loadURL(configFile.toURL().toString());
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
83 } catch (Exception e) {
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
84 throw new AuthOpException(
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
85 "ERROR loading authorization config file: " + e);
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
86 }
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
87 if ((pathList == null) || (ipList == null)) {
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
88 throw new AuthOpException("ERROR unable to load authorization config file!");
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
89 }
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
90 // setup path tree
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
91 authPaths = new HashTree(pathList, "/", ",");
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
92 // setup ip tree
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
93 authIPs = new HashTree(ipList, ".", ",");
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
94 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
95
531
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
96 /** Return authorization roles needed for request.
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
97 *
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
98 * Returns the list of authorization roles that are needed to access the
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
99 * specified path. No list means the path is free.
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
100 *
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
101 * The location information of the request is also considered.
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
102 *
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
103 * @param filepath filepath to be accessed.
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
104 * @param request ServletRequest with address information.
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
105 * @throws AuthOpException Exception thrown on error.
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
106 * @return List of Strings with role names.
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
107 */
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
108 public List<String> rolesForPath(String filepath, HttpServletRequest request)
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
109 throws digilib.auth.AuthOpException {
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
110 logger.debug("rolesForPath ("
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
111 + filepath
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
112 + ") by ["
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
113 + request.getRemoteAddr()
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
114 + "]");
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
115
531
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
116 // check if the requests address provides a role
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
117 List<String> provided = authIPs.match(request.getRemoteAddr());
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
118 if ((provided != null) && (provided.contains("ALL"))) {
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
119 // ALL switches off checking;
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
120 return null;
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
121 }
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
122 // which roles are required?
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
123 List<String> required = authPaths.match(filepath);
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
124 // do any provided roles match?
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
125 if ((provided != null) && (required != null)) {
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
126 for (int i = 0; i < provided.size(); i++) {
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
127 if (required.contains(provided.get(i))) {
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
128 // satisfied
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
129 return null;
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
130 }
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
131 }
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
132 }
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
133 return required;
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
134 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
135
531
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
136 /**
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
137 * @see digilib.auth.AuthOps#rolesForPath(digilib.servlet.DigilibRequest)
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
138 */
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
139 public List<String> rolesForPath(DigilibRequest request) throws AuthOpException {
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
140 logger.debug("rolesForPath ("
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
141 + request.getFilePath()
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
142 + ") by ["
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
143 + request.getServletRequest().getRemoteAddr()
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
144 + "]");
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
145
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
146 // check if the requests address provides a role
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
147 List<String> provided =
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
148 authIPs.match(request.getServletRequest().getRemoteAddr());
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
149 if ((provided != null) && (provided.contains("ALL"))) {
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
150 // ALL switches off checking;
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
151 return null;
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
152 }
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
153 // which roles are required?
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
154 List<String> required = authPaths.match(request.getFilePath());
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
155 // do any provided roles match?
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
156 if ((provided != null) && (required != null)) {
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
157 for (int i = 0; i < provided.size(); i++) {
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
158 if (required.contains(provided.get(i))) {
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
159 // satisfied
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
160 return null;
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
161 }
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
162 }
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
163 }
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
164 return required;
9cedd170b581 * PDF generation works now even with subdirectories
robcast
parents: 1
diff changeset
165 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
166
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
167 }