KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > webapps > authentication > components > ServletAuthenticator


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.components;
17
18 import org.apache.avalon.framework.context.Context;
19 import org.apache.avalon.framework.context.ContextException;
20 import org.apache.avalon.framework.context.Contextualizable;
21 import org.apache.avalon.framework.logger.AbstractLogEnabled;
22 import org.apache.avalon.framework.service.ServiceException;
23 import org.apache.avalon.framework.service.ServiceManager;
24 import org.apache.avalon.framework.service.Serviceable;
25 import org.apache.avalon.framework.thread.ThreadSafe;
26 import org.apache.cocoon.ProcessingException;
27 import org.apache.cocoon.components.ContextHelper;
28 import org.apache.cocoon.environment.Request;
29 import org.apache.cocoon.webapps.authentication.configuration.HandlerConfiguration;
30 import org.apache.cocoon.webapps.authentication.user.UserHandler;
31 import org.apache.excalibur.source.SourceParameters;
32 import org.apache.excalibur.xml.dom.DOMParser;
33 import org.w3c.dom.Document JavaDoc;
34 import org.w3c.dom.Element JavaDoc;
35 import org.xml.sax.SAXException JavaDoc;
36
37 /**
38  * Verify if a user can be authenticated.
39  * This is a very simple authenticator that checks if the user is authenticated
40  * using the servlet authentication mechanisms.
41  *
42  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
43  * @version CVS $Id: PipelineAuthenticator.java 30932 2004-07-29 17:35:38Z vgritsenko $
44 */

45 public class ServletAuthenticator
46     extends AbstractLogEnabled
47     implements Contextualizable, ThreadSafe, Serviceable, Authenticator {
48
49     protected Context context;
50     protected ServiceManager manager;
51
52     /* (non-Javadoc)
53      * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
54      */

55     public void contextualize(Context context) throws ContextException {
56         this.context = context;
57     }
58
59     /* (non-Javadoc)
60      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
61      */

62     public void service(ServiceManager manager) throws ServiceException {
63         this.manager = manager;
64     }
65
66     /**
67      * Fill the authentication context.
68      * This method can be overwritten to add any application specific data
69      * to the user.
70      * (Don't forget to call this implementation via super as well as it
71      * adds the ID).
72      *
73      * @param contextDoc The context. This document has already the authentication
74      * root node.
75      */

76     protected void fillContext(Document JavaDoc contextDoc) {
77         final Request req = ContextHelper.getRequest(this.context);
78         final Element JavaDoc root = contextDoc.getDocumentElement();
79
80         // append the ID
81
final Element JavaDoc id = contextDoc.createElement("ID");
82         id.appendChild(contextDoc.createTextNode(req.getRemoteUser()));
83         root.appendChild(id);
84     }
85
86     /* (non-Javadoc)
87      * @see org.apache.cocoon.webapps.authentication.components.Authenticator#authenticate(org.apache.cocoon.webapps.authentication.configuration.HandlerConfiguration, org.apache.excalibur.source.SourceParameters)
88      */

89     public AuthenticationResult authenticate(HandlerConfiguration configuration,
90                                              SourceParameters parameters)
91     throws ProcessingException {
92         if (this.getLogger().isDebugEnabled() ) {
93             this.getLogger().debug("start authenticator using handler " + configuration.getName());
94         }
95
96         final Request req = ContextHelper.getRequest(this.context);
97         AuthenticationResult result = null;
98         if ( req.getRemoteUser() != null ) {
99             DOMParser parser = null;
100             try {
101                 parser = (DOMParser)this.manager.lookup(DOMParser.ROLE);
102                 final Document JavaDoc doc = parser.createDocument();
103                 final Element JavaDoc root = doc.createElement("authentication");
104                 doc.appendChild(root);
105                 this.fillContext(doc);
106
107                 result = new AuthenticationResult(true, doc);
108             } catch (SAXException JavaDoc se) {
109                 throw new ProcessingException("Unable to create document.", se);
110             } catch (ServiceException se) {
111                 throw new ProcessingException("Unable to lookup dom parser.", se);
112             } finally {
113                 this.manager.release(parser);
114             }
115         }
116
117         if (this.getLogger().isDebugEnabled() ) {
118             this.getLogger().debug("end authenticator: " + result);
119         }
120
121         return result;
122     }
123
124
125     /* (non-Javadoc)
126      * @see org.apache.cocoon.webapps.authentication.components.Authenticator#logout(UserHandler)
127      */

128     public void logout(UserHandler handler) {
129         if (this.getLogger().isDebugEnabled() ) {
130             this.getLogger().debug("logout using handler " + handler.getHandlerName());
131         }
132         // TODO what can we do here?
133
}
134 }
135
Popular Tags