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.components;
018:
019: import org.apache.avalon.framework.context.Context;
020: import org.apache.avalon.framework.context.ContextException;
021: import org.apache.avalon.framework.context.Contextualizable;
022: import org.apache.avalon.framework.logger.AbstractLogEnabled;
023: import org.apache.avalon.framework.service.ServiceException;
024: import org.apache.avalon.framework.service.ServiceManager;
025: import org.apache.avalon.framework.service.Serviceable;
026: import org.apache.avalon.framework.thread.ThreadSafe;
027: import org.apache.cocoon.ProcessingException;
028: import org.apache.cocoon.components.ContextHelper;
029: import org.apache.cocoon.environment.Request;
030: import org.apache.cocoon.webapps.authentication.configuration.HandlerConfiguration;
031: import org.apache.cocoon.webapps.authentication.user.UserHandler;
032: import org.apache.excalibur.source.SourceParameters;
033: import org.apache.excalibur.xml.dom.DOMParser;
034: import org.w3c.dom.Document;
035: import org.w3c.dom.Element;
036: import org.xml.sax.SAXException;
037:
038: /**
039: * Verify if a user can be authenticated.
040: * This is a very simple authenticator that checks if the user is authenticated
041: * using the servlet authentication mechanisms.
042: *
043: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
044: * @deprecated This block is deprecated and will be removed in future versions.
045: * @version CVS $Id: PipelineAuthenticator.java 30932 2004-07-29 17:35:38Z vgritsenko $
046: */
047: public class ServletAuthenticator extends AbstractLogEnabled implements
048: Contextualizable, ThreadSafe, Serviceable, Authenticator {
049:
050: protected Context context;
051: protected ServiceManager manager;
052:
053: /* (non-Javadoc)
054: * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
055: */
056: public void contextualize(Context context) throws ContextException {
057: this .context = context;
058: }
059:
060: /* (non-Javadoc)
061: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
062: */
063: public void service(ServiceManager manager) throws ServiceException {
064: this .manager = manager;
065: }
066:
067: /**
068: * Fill the authentication context.
069: * This method can be overwritten to add any application specific data
070: * to the user.
071: * (Don't forget to call this implementation via super as well as it
072: * adds the ID).
073: *
074: * @param contextDoc The context. This document has already the authentication
075: * root node.
076: */
077: protected void fillContext(Document contextDoc) {
078: final Request req = ContextHelper.getRequest(this .context);
079: final Element root = contextDoc.getDocumentElement();
080:
081: // append the ID
082: final Element id = contextDoc.createElement("ID");
083: id.appendChild(contextDoc.createTextNode(req.getRemoteUser()));
084: root.appendChild(id);
085: }
086:
087: /* (non-Javadoc)
088: * @see org.apache.cocoon.webapps.authentication.components.Authenticator#authenticate(org.apache.cocoon.webapps.authentication.configuration.HandlerConfiguration, org.apache.excalibur.source.SourceParameters)
089: */
090: public AuthenticationResult authenticate(
091: HandlerConfiguration configuration,
092: SourceParameters parameters) throws ProcessingException {
093: if (this .getLogger().isDebugEnabled()) {
094: this .getLogger().debug(
095: "start authenticator using handler "
096: + configuration.getName());
097: }
098:
099: final Request req = ContextHelper.getRequest(this .context);
100: AuthenticationResult result = null;
101: if (req.getRemoteUser() != null) {
102: DOMParser parser = null;
103: try {
104: parser = (DOMParser) this .manager
105: .lookup(DOMParser.ROLE);
106: final Document doc = parser.createDocument();
107: final Element root = doc
108: .createElement("authentication");
109: doc.appendChild(root);
110: this .fillContext(doc);
111:
112: result = new AuthenticationResult(true, doc);
113: } catch (SAXException se) {
114: throw new ProcessingException(
115: "Unable to create document.", se);
116: } catch (ServiceException se) {
117: throw new ProcessingException(
118: "Unable to lookup dom parser.", se);
119: } finally {
120: this .manager.release(parser);
121: }
122: }
123:
124: if (this .getLogger().isDebugEnabled()) {
125: this .getLogger().debug("end authenticator: " + result);
126: }
127:
128: return result;
129: }
130:
131: /* (non-Javadoc)
132: * @see org.apache.cocoon.webapps.authentication.components.Authenticator#logout(UserHandler)
133: */
134: public void logout(UserHandler handler) {
135: if (this .getLogger().isDebugEnabled()) {
136: this .getLogger().debug(
137: "logout using handler " + handler.getHandlerName());
138: }
139: // TODO what can we do here?
140: }
141: }
|