1 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 ; 34 import org.w3c.dom.Element ; 35 import org.xml.sax.SAXException ; 36 37 45 public class ServletAuthenticator 46 extends AbstractLogEnabled 47 implements Contextualizable, ThreadSafe, Serviceable, Authenticator { 48 49 protected Context context; 50 protected ServiceManager manager; 51 52 55 public void contextualize(Context context) throws ContextException { 56 this.context = context; 57 } 58 59 62 public void service(ServiceManager manager) throws ServiceException { 63 this.manager = manager; 64 } 65 66 76 protected void fillContext(Document contextDoc) { 77 final Request req = ContextHelper.getRequest(this.context); 78 final Element root = contextDoc.getDocumentElement(); 79 80 final Element id = contextDoc.createElement("ID"); 82 id.appendChild(contextDoc.createTextNode(req.getRemoteUser())); 83 root.appendChild(id); 84 } 85 86 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 doc = parser.createDocument(); 103 final Element root = doc.createElement("authentication"); 104 doc.appendChild(root); 105 this.fillContext(doc); 106 107 result = new AuthenticationResult(true, doc); 108 } catch (SAXException 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 128 public void logout(UserHandler handler) { 129 if (this.getLogger().isDebugEnabled() ) { 130 this.getLogger().debug("logout using handler " + handler.getHandlerName()); 131 } 132 } 134 } 135 | Popular Tags |