001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.webapps.authentication.configuration;
018:
019: import java.io.Serializable;
020: import java.util.HashMap;
021: import java.util.Hashtable;
022: import java.util.Map;
023:
024: import org.apache.avalon.framework.configuration.Configuration;
025: import org.apache.avalon.framework.configuration.ConfigurationException;
026: import org.apache.cocoon.ProcessingException;
027: import org.apache.cocoon.environment.Request;
028: import org.apache.cocoon.webapps.authentication.components.PipelineAuthenticator;
029: import org.apache.excalibur.source.SourceParameters;
030:
031: /**
032: * The authentication Handler.
033: *
034: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
035: * @deprecated This block is deprecated and will be removed in future versions.
036: * @version CVS $Id: HandlerConfiguration.java 433543 2006-08-22 06:22:54Z crossley $
037: */
038: public final class HandlerConfiguration implements Serializable {
039:
040: /** The unique name of the handler */
041: private final String name;
042:
043: /** The redirect-to URI */
044: private String redirectURI;
045:
046: /** The redirect parameters */
047: private SourceParameters redirectParameters;
048:
049: /** The authentication resource */
050: private String authenticationResource;
051:
052: /** The logout resource */
053: private String logoutResource;
054:
055: /** The class name of the authenticator to use */
056: private String authenticatorClass;
057:
058: /** The authentication resource parameters */
059: private SourceParameters authenticationResourceParameters;
060:
061: /** The load resource (optional) */
062: private String loadResource;
063:
064: /** The load resource (optional) parameters */
065: private SourceParameters loadResourceParameters;
066:
067: /** The save resource (optional) */
068: private String saveResource;
069:
070: /** The save resource (optional) parameters */
071: private SourceParameters saveResourceParameters;
072:
073: /** The Application Configurations */
074: private Map applications = new Hashtable(3, 2);
075:
076: /** The configuration fragments */
077: private Map configurations;
078:
079: /** Save the context on logout */
080: private boolean saveOnLogout = false;
081:
082: /**
083: * Create a new handler object.
084: */
085: public HandlerConfiguration(String name) {
086: this .name = name;
087: this .configurations = new HashMap(3, 2);
088: }
089:
090: /**
091: * Configure
092: */
093: public void configure(Request request, Configuration conf)
094: throws ProcessingException, ConfigurationException {
095: // get login (required)
096: Configuration child = conf.getChild("redirect-to", false);
097: if (child == null)
098: throw new ConfigurationException("Handler '" + this .name
099: + "' needs a redirect-to URI.");
100: this .redirectURI = child.getAttribute("uri");
101: if (this .redirectURI.startsWith("cocoon:")) {
102: final int pos = this .redirectURI.indexOf('/');
103: if (pos != -1 && this .redirectURI.length() > pos) {
104: if (this .redirectURI.charAt(pos + 1) == '/') {
105: this .redirectURI = this .redirectURI.substring(
106: pos + 2).trim();
107: this .redirectURI = request.getContextPath() + "/"
108: + this .redirectURI;
109: } else {
110: this .redirectURI = this .redirectURI.substring(
111: pos + 1).trim();
112: }
113: }
114: }
115:
116: this .redirectParameters = SourceParameters.create(child);
117:
118: // get load resource (required)
119: child = conf.getChild("authentication", false);
120: if (child == null) {
121: throw new ConfigurationException("Handler '" + this .name
122: + "' needs authentication configuration");
123: }
124: this .authenticatorClass = child.getAttribute("authenticator",
125: PipelineAuthenticator.class.getName());
126: if (PipelineAuthenticator.class.getName().equals(
127: authenticatorClass)) {
128: this .authenticationResource = child.getAttribute("uri");
129: } else {
130: // the uri attribute is optional for other authenticators
131: this .authenticationResource = child.getAttribute("uri",
132: null);
133: }
134: // optinal logout resource
135: this .logoutResource = child.getAttribute("logout-uri", null);
136: this .authenticationResourceParameters = SourceParameters
137: .create(child);
138:
139: // get load resource (optional)
140: child = conf.getChild("load", false);
141: if (child != null) {
142: this .loadResource = child.getAttribute("uri");
143: this .loadResourceParameters = SourceParameters
144: .create(child);
145: }
146:
147: // get save resource (optional)
148: child = conf.getChild("save", false);
149: if (child != null) {
150: this .saveResource = child.getAttribute("uri");
151: this .saveResourceParameters = SourceParameters
152: .create(child);
153: this .saveOnLogout = child.getAttributeAsBoolean(
154: "saveOnLogout", false);
155: }
156:
157: // And now: Applications
158: child = conf.getChild("applications", false);
159: if (child != null) {
160: Configuration[] appConfs = child.getChildren("application");
161: Configuration appconf;
162:
163: if (appConfs != null) {
164: for (int i = 0; i < appConfs.length; i++) {
165: appconf = appConfs[i];
166:
167: // get name
168: String appName = appconf.getAttribute("name");
169:
170: // test if handler is unique
171: if (this .applications.get(appName) != null) {
172: throw new ConfigurationException(
173: "Application names must be unique: "
174: + appName);
175: }
176:
177: // create handler
178: ApplicationConfiguration apphandler = new ApplicationConfiguration(
179: this , appName);
180:
181: // store handler
182: this .applications.put(appName, apphandler);
183:
184: // configure
185: apphandler.configure(appconf);
186: }
187: }
188: }
189:
190: // get configurations (optional)
191: Configuration[] configurations = conf
192: .getChildren("configuration");
193: if (configurations != null) {
194: for (int i = 0; i < configurations.length; i++) {
195: child = configurations[i];
196: String value = child.getAttribute("name");
197: if (this .getConfiguration(value) != null) {
198: throw new ConfigurationException(
199: "Configuration names must be unique for application "
200: + this .name + ": " + value);
201: }
202: this .configurations.put(value, child);
203: }
204: }
205:
206: }
207:
208: /**
209: * Get the handler name.
210: */
211: public String getName() {
212: return name;
213: }
214:
215: /**
216: * Get the redirect URI
217: */
218: public String getRedirectURI() {
219: return this .redirectURI;
220: }
221:
222: /**
223: * Get the redirect parameters
224: */
225: public SourceParameters getRedirectParameters() {
226: return this .redirectParameters;
227: }
228:
229: /**
230: * Get the authentication resource
231: */
232: public String getAuthenticationResource() {
233: return this .authenticationResource;
234: }
235:
236: /**
237: * Get the authentication resource
238: */
239: public SourceParameters getAuthenticationResourceParameters() {
240: return this .authenticationResourceParameters;
241: }
242:
243: /**
244: * Get the logout resource
245: */
246: public String getLogoutResource() {
247: return this .logoutResource;
248: }
249:
250: /** Get the save resource */
251: public String getSaveResource() {
252: return this .saveResource;
253: }
254:
255: /** Get the load resource */
256: public String getLoadResource() {
257: return this .loadResource;
258: }
259:
260: /** Should we save on logout? */
261: public boolean saveOnLogout() {
262: return this .saveOnLogout;
263: }
264:
265: /** Get the save resource */
266: public SourceParameters getSaveResourceParameters() {
267: return this .saveResourceParameters;
268: }
269:
270: /** Get the load resource parameters */
271: public SourceParameters getLoadResourceParameters() {
272: return this .loadResourceParameters;
273: }
274:
275: /**
276: * Get the applications map
277: */
278: public Map getApplications() {
279: return applications;
280: }
281:
282: /**
283: * Get the configuration
284: */
285: public Configuration getConfiguration(String name) {
286: return (Configuration) this .configurations.get(name);
287: }
288:
289: /**
290: * toString()
291: */
292: public String toString() {
293: return "authentication handler '" + this .name + "' ("
294: + super .toString() + ')';
295: }
296:
297: /**
298: * Return the authenticator class
299: */
300: public String getAuthenticatorClassName() {
301: return this.authenticatorClass;
302: }
303: }
|