KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > webapps > authentication > configuration > HandlerConfiguration


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.webapps.authentication.configuration;
17
18 import java.io.Serializable JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Hashtable JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.apache.avalon.framework.configuration.Configuration;
24 import org.apache.avalon.framework.configuration.ConfigurationException;
25 import org.apache.cocoon.ProcessingException;
26 import org.apache.cocoon.environment.Request;
27 import org.apache.cocoon.webapps.authentication.components.PipelineAuthenticator;
28 import org.apache.excalibur.source.SourceParameters;
29
30 /**
31  * The authentication Handler.
32  *
33  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
34  * @version CVS $Id: HandlerConfiguration.java 125257 2005-01-15 07:59:07Z antonio $
35 */

36 public final class HandlerConfiguration implements Serializable JavaDoc {
37
38     /** The unique name of the handler */
39     private final String JavaDoc name;
40
41     /** The redirect-to URI */
42     private String JavaDoc redirectURI;
43
44     /** The redirect parameters */
45     private SourceParameters redirectParameters;
46
47     /** The authentication resource */
48     private String JavaDoc authenticationResource;
49     
50     /** The logout resource */
51     private String JavaDoc logoutResource;
52     
53     /** The class name of the authenticator to use */
54     private String JavaDoc authenticatorClass;
55
56     /** The authentication resource parameters */
57     private SourceParameters authenticationResourceParameters;
58
59     /** The load resource (optional) */
60     private String JavaDoc loadResource;
61
62     /** The load resource (optional) parameters */
63     private SourceParameters loadResourceParameters;
64
65     /** The save resource (optional) */
66     private String JavaDoc saveResource;
67
68     /** The save resource (optional) parameters */
69     private SourceParameters saveResourceParameters;
70
71     /** The Application Configurations */
72     private Map JavaDoc applications = new Hashtable JavaDoc(3, 2);
73
74     /** The configuration fragments */
75     private Map JavaDoc configurations;
76
77     /** Save the context on logout */
78     private boolean saveOnLogout = false;
79     
80     /**
81      * Create a new handler object.
82      */

83     public HandlerConfiguration(String JavaDoc name) {
84         this.name = name;
85         this.configurations = new HashMap JavaDoc(3, 2);
86     }
87
88     /**
89      * Configure
90      */

91     public void configure(Request request,
92                           Configuration conf)
93     throws ProcessingException, ConfigurationException {
94         // get login (required)
95
Configuration child = conf.getChild("redirect-to", false);
96         if (child == null)
97             throw new ConfigurationException("Handler '"+this.name+"' needs a redirect-to URI.");
98         this.redirectURI = child.getAttribute("uri");
99         if ( this.redirectURI.startsWith("cocoon:") ) {
100             final int pos = this.redirectURI.indexOf('/');
101             if ( pos != -1 && this.redirectURI.length() > pos) {
102                 if (this.redirectURI.charAt(pos+1) == '/') {
103                     this.redirectURI = this.redirectURI.substring(pos+2).trim();
104                     this.redirectURI = request.getContextPath()+"/"+this.redirectURI;
105                 } else {
106                     this.redirectURI = this.redirectURI.substring(pos+1).trim();
107                 }
108             }
109         }
110
111         this.redirectParameters = SourceParameters.create(child);
112
113         // get load resource (required)
114
child = conf.getChild("authentication", false);
115         if (child == null) {
116             throw new ConfigurationException("Handler '"+this.name+"' needs authentication configuration");
117         }
118         this.authenticatorClass = child.getAttribute("authenticator", PipelineAuthenticator.class.getName());
119         if ( PipelineAuthenticator.class.getName().equals(authenticatorClass)) {
120             this.authenticationResource = child.getAttribute("uri");
121         } else {
122             // the uri attribute is optional for other authenticators
123
this.authenticationResource = child.getAttribute("uri", null);
124         }
125         // optinal logout resource
126
this.logoutResource = child.getAttribute("logout-uri", null);
127         this.authenticationResourceParameters = SourceParameters.create(child);
128
129         // get load resource (optional)
130
child = conf.getChild("load", false);
131         if (child != null) {
132             this.loadResource = child.getAttribute("uri");
133             this.loadResourceParameters = SourceParameters.create(child);
134         }
135
136         // get save resource (optional)
137
child = conf.getChild("save", false);
138         if (child != null) {
139             this.saveResource = child.getAttribute("uri");
140             this.saveResourceParameters = SourceParameters.create(child);
141             this.saveOnLogout = child.getAttributeAsBoolean("saveOnLogout", false);
142         }
143
144         // And now: Applications
145
child = conf.getChild("applications", false);
146         if (child != null) {
147             Configuration[] appConfs = child.getChildren("application");
148             Configuration appconf;
149
150             if (appConfs != null) {
151                 for(int i = 0; i < appConfs.length; i++) {
152                     appconf = appConfs[i];
153
154                     // get name
155
String JavaDoc appName = appconf.getAttribute("name");
156
157                     // test if handler is unique
158
if (this.applications.get(appName) != null) {
159                         throw new ConfigurationException("Application names must be unique: " + appName);
160                     }
161
162                     // create handler
163
ApplicationConfiguration apphandler = new ApplicationConfiguration(this, appName);
164
165                     // store handler
166
this.applications.put(appName, apphandler);
167
168                     // configure
169
apphandler.configure(appconf);
170                 }
171             }
172         }
173
174         // get configurations (optional)
175
Configuration[] configurations = conf.getChildren("configuration");
176         if (configurations != null) {
177             for(int i = 0; i < configurations.length; i++) {
178                 child = configurations[i];
179                 String JavaDoc value = child.getAttribute("name");
180                 if (this.getConfiguration(value) != null) {
181                     throw new ConfigurationException("Configuration names must be unique for application " + this.name + ": " + value);
182                 }
183                 this.configurations.put(value, child);
184             }
185         }
186
187     }
188
189
190     /**
191      * Get the handler name.
192      */

193     public String JavaDoc getName() { return name; }
194
195     /**
196      * Get the redirect URI
197      */

198     public String JavaDoc getRedirectURI() {
199         return this.redirectURI;
200     }
201
202     /**
203      * Get the redirect parameters
204      */

205     public SourceParameters getRedirectParameters() {
206         return this.redirectParameters;
207     }
208
209     /**
210      * Get the authentication resource
211      */

212     public String JavaDoc getAuthenticationResource() {
213         return this.authenticationResource;
214     }
215
216     /**
217      * Get the authentication resource
218      */

219     public SourceParameters getAuthenticationResourceParameters() {
220         return this.authenticationResourceParameters;
221     }
222
223     /**
224      * Get the logout resource
225      */

226     public String JavaDoc getLogoutResource() {
227         return this.logoutResource;
228     }
229     
230     /** Get the save resource */
231     public String JavaDoc getSaveResource() {
232         return this.saveResource; }
233     
234
235     /** Get the load resource */
236     public String JavaDoc getLoadResource() {
237         return this.loadResource;
238     }
239
240     /** Should we save on logout? */
241     public boolean saveOnLogout() {
242         return this.saveOnLogout;
243     }
244     
245     /** Get the save resource */
246     public SourceParameters getSaveResourceParameters() {
247         return this.saveResourceParameters;
248     }
249
250     /** Get the load resource parameters */
251     public SourceParameters getLoadResourceParameters() {
252         return this.loadResourceParameters;
253     }
254
255     /**
256      * Get the applications map
257      */

258     public Map JavaDoc getApplications() {
259         return applications;
260     }
261
262     /**
263      * Get the configuration
264      */

265     public Configuration getConfiguration(String JavaDoc name) {
266         return (Configuration)this.configurations.get(name);
267     }
268
269     /**
270      * toString()
271      */

272     public String JavaDoc toString() {
273         return "authentication handler '" + this.name + "' (" + super.toString() + ')';
274     }
275     
276     /**
277      * Return the authenticator class
278      */

279     public String JavaDoc getAuthenticatorClassName() {
280         return this.authenticatorClass;
281     }
282 }
283
Popular Tags