cziegeler 2002/06/03 03:02:01 Modified: src/java/org/apache/cocoon/components/treeprocessor TreeProcessor.java src/java/org/apache/cocoon/webapps/authentication/components ApplicationHandler.java AuthenticationManager.java Handler.java HandlerManager.java Log: Rewrote authentication handler code Revision Changes Path 1.13 +9 -7 xml-cocoon2/src/java/org/apache/cocoon/components/treeprocessor/TreeProcessor.java Index: TreeProcessor.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/treeprocessor/TreeProcessor.java,v retrieving revision 1.12 retrieving revision 1.13 diff -u -r1.12 -r1.13 --- TreeProcessor.java 29 May 2002 12:50:21 -0000 1.12 +++ TreeProcessor.java 3 Jun 2002 10:02:00 -0000 1.13 @@ -94,7 +94,7 @@ * Interpreted tree-traversal implementation of a pipeline assembly language. * * @author <a href="mailto:[EMAIL PROTECTED]">Sylvain Wallez</a> - * @version CVS $Id: TreeProcessor.java,v 1.12 2002/05/29 12:50:21 cziegeler Exp $ + * @version CVS $Id: TreeProcessor.java,v 1.13 2002/06/03 10:02:00 cziegeler Exp $ */ public class TreeProcessor @@ -313,15 +313,17 @@ } protected boolean process(Environment environment, InvokeContext context) - throws Exception { + throws Exception { - CocoonComponentManager.enterEnvironment(environment, environment.getObjectModel(), this); + // first, check for sitemap changes + if (this.rootNode == null || + (this.checkReload && this.source.getLastModified() > this.lastModified)) { + setupRootNode(environment); + } + // and now process + CocoonComponentManager.enterEnvironment(environment, environment.getObjectModel(), this); try { - if (this.rootNode == null || - (this.checkReload && this.source.getLastModified() > this.lastModified)) { - setupRootNode(environment); - } return this.rootNode.invoke(environment, context); } finally { CocoonComponentManager.leaveEnvironment(); 1.4 +20 -15 xml-cocoon2/src/java/org/apache/cocoon/webapps/authentication/components/ApplicationHandler.java Index: ApplicationHandler.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/webapps/authentication/components/ApplicationHandler.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- ApplicationHandler.java 29 May 2002 16:05:42 -0000 1.3 +++ ApplicationHandler.java 3 Jun 2002 10:02:01 -0000 1.4 @@ -67,7 +67,7 @@ * inside a handler configuration. * * @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a> - * @version CVS $Id: ApplicationHandler.java,v 1.3 2002/05/29 16:05:42 cziegeler Exp $ + * @version CVS $Id: ApplicationHandler.java,v 1.4 2002/06/03 10:02:01 cziegeler Exp $ */ public final class ApplicationHandler implements java.io.Serializable { @@ -84,15 +84,15 @@ /** Is the application loaded on demand */ private boolean loadOnDemand = false; + /** Is the application loaded ? */ + private boolean isLoaded = false; + /** The corresponding handler */ private Handler handler; /** The configuration fragments */ private Map configurations; - /** The name of the context attribute, where isLoaded is stored */ - private String attributeName; - /** * Construct a new application handler */ @@ -106,14 +106,23 @@ throw new ProcessingException("application name must not contain one of the characters ':','_' or '/'."); } this.configurations = new HashMap(3, 2); - this.attributeName = "authentication_AH_" + handler.getName() + "_" + this.name; } /** * Make a copy */ - public ApplicationHandler copy() { - return null; + public ApplicationHandler copy(Handler handler) { + try { + final ApplicationHandler ah = new ApplicationHandler(handler, this.name); + ah.loadOnDemand = this.loadOnDemand; + ah.loadResource = this.loadResource; + ah.saveResource = this.saveResource; + ah.configurations = this.configurations; + return ah; + } catch (ProcessingException pe) { + // ignore it and keep the compiler happy + return null; + } } /** @@ -181,22 +190,18 @@ } - public boolean getIsLoaded(SessionContext context) + public boolean getIsLoaded() throws ProcessingException { if (this.loadResource == null) { return true; } else { - boolean result = false; - Boolean bool = (Boolean)context.getAttribute(this.attributeName); - if (bool != null) result = bool.booleanValue(); - return result; + return this.isLoaded; } } - public void setIsLoaded(SessionContext context, boolean value) + public void setIsLoaded(boolean value) throws ProcessingException { - Boolean bool = new Boolean(value); - context.setAttribute(this.attributeName, bool); + this.isLoaded = value; } public boolean getLoadOnDemand() { return loadOnDemand; } 1.9 +32 -155 xml-cocoon2/src/java/org/apache/cocoon/webapps/authentication/components/AuthenticationManager.java Index: AuthenticationManager.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/webapps/authentication/components/AuthenticationManager.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- AuthenticationManager.java 29 May 2002 15:58:21 -0000 1.8 +++ AuthenticationManager.java 3 Jun 2002 10:02:01 -0000 1.9 @@ -98,7 +98,7 @@ * This is the basis authentication component. * * @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a> - * @version CVS $Id: AuthenticationManager.java,v 1.8 2002/05/29 15:58:21 cziegeler Exp $ + * @version CVS $Id: AuthenticationManager.java,v 1.9 2002/06/03 10:02:01 cziegeler Exp $ */ public final class AuthenticationManager extends AbstractSessionComponent @@ -118,15 +118,6 @@ /** The handler manager */ private HandlerManager handlerManager = new HandlerManager(); - /** All Handlers hashed by their name */ - private Map configuredHandlers; - - /** All handlers of the current user */ - private Map userHandlers; - - /** The handler configuration */ - private Configuration configuration; - /** The application name of the current resource */ private String applicationName; @@ -172,7 +163,6 @@ this.application = null; this.handler = null; this.handlerName = null; - this.userHandlers = null; // clear handlers this.handlerManager.clearAvailableHandlers(); @@ -183,15 +173,8 @@ */ public void setSitemapConfiguration(Configuration config) throws ConfigurationException { - try { - // synchronized - this.configureHandlers(config); - } catch (ProcessingException pe) { - throw new ConfigurationException("Exception during configuration of handlers."); - } - - //this.handlerManager.addConfiguration( config, this.resolver, this.request ); - //this.handlerManager.addAvailableHandlers( config ); + this.handlerManager.addConfiguration( config, this.resolver, this.request ); + this.handlerManager.addAvailableHandlers( config ); } /** @@ -230,8 +213,12 @@ throws ConfigurationException { // no sync required Configuration mediaConf = myConfiguration.getChild("mediatypes", false); - if (mediaConf == null) throw new ConfigurationException("AuthenticationManager needs mediatypes configuration"); - this.defaultMediaType = mediaConf.getAttribute("default"); + if (mediaConf == null) { + // default configuration + this.defaultMediaType = "html"; + } else { + this.defaultMediaType = mediaConf.getAttribute("default", "html"); + } this.mediaTypeNames = new String[1]; this.mediaTypeNames[0] = this.defaultMediaType; boolean found; @@ -265,74 +252,6 @@ } } this.allMediaTypes = array; - this.configuredHandlers = null; - } - - /** - * Configure the handler - */ - private void configureHandlers(Configuration configuration) - throws ProcessingException { - if (this.configuredHandlers == null - || configuration.equals(this.configuration) == false) { - this.configuration = configuration; // store the configuration - try { - this.configuredHandlers = new Hashtable(3, 2); - - Configuration handlersConfiguration = configuration.getChild("handlers", false); - Configuration currentHandlerConf; - - if (handlersConfiguration != null) { - Configuration[] handlerConfs = handlersConfiguration.getChildren("handler"); - if (handlerConfs != null) { - for(int i = 0; i < handlerConfs.length; i++) { - - currentHandlerConf = handlerConfs[i]; - String value; - - // get name - String name = currentHandlerConf.getAttribute("name"); - - // test if handler is unique - if (this.configuredHandlers.get(name) != null) { - throw new ConfigurationException("Handler names must be unique: " + name); - } - - // create handler - Handler currentHandler = new Handler(name); - - // store handler - this.configuredHandlers.put(name, currentHandler); - - currentHandler.configure(this.resolver, this.request, currentHandlerConf); - } - } - } - } catch (IOException local) { - throw new ProcessingException("IOException: " + local, local); - } catch (SAXException local) { - throw new ProcessingException("SAXException: " + local, local); - } catch (ConfigurationException local) { - throw new ProcessingException("ConfigurationException: " + local, local); - } - } - - } - - /** - * Store the handlers in the session of the user - */ - private void storeHandlers() - throws ProcessingException { - final Request req = ObjectModelHelper.getRequest( this.objectModel ); - final Session session = req.getSession(); - Map myHandlers = (Map)session.getAttribute(HandlerManager.SESSION_ATTRIBUTE_HANDLERS); - if (myHandlers == null) { - this.userHandlers = this.configuredHandlers; - session.setAttribute(HandlerManager.SESSION_ATTRIBUTE_HANDLERS, this.userHandlers); - this.configuredHandlers = null; - this.configureHandlers(this.configuration); - } } /** @@ -683,33 +602,7 @@ // if no handler: authenticated if (name != null) { - isAuthenticated = false; - - // get the session context - // if no session context: not authenticated - SessionContext context = this.getAuthenticationSessionContext(false); - if (context != null) { - synchronized(context) { - - try { - // is result in cache (= context attribute) - if (context.getAttribute("COCOON_ISAUTHENTICATED:" + name) != null) { - isAuthenticated = true; - } else { - DocumentFragment id = context.getXML("/" + name + "/authentication/ID"); - isAuthenticated = (id != null); - if (isAuthenticated == true) { - // cache result - context.setAttribute("COCOON_ISAUTHENTICATED:" + name, "YES"); - } - } - } catch (ProcessingException local) { - // ignore this for now - this.getLogger().error("isAuthenticated"); - isAuthenticated = false; - } - } - } + isAuthenticated = this.handlerManager.hasUserHandler( name, this.request ); } if (this.getLogger().isDebugEnabled() == true) { @@ -729,13 +622,6 @@ } boolean isAuthenticated = true; - // get user handlers (if available) - final Request req = ObjectModelHelper.getRequest( this.objectModel ); - final Session session = req.getSession(false); - if ( null != session ) { - this.userHandlers = (Map)session.getAttribute(HandlerManager.SESSION_ATTRIBUTE_HANDLERS); - } - // set the configuration for the handler final String newHandlerName = (String)this.request.getAttribute(AuthenticationConstants.REQUEST_ATTRIBUTE_HANDLER_NAME); final String newAppName = (String)this.request.getAttribute(AuthenticationConstants.REQUEST_ATTRIBUTE_APPLICATION_NAME); @@ -801,12 +687,7 @@ */ private Handler getHandler(String name) { // synchronized - if ( null == name ) return null; - if ( null != this.userHandlers ) { - return (Handler)this.userHandlers.get( name ); - } else { - return (Handler)this.configuredHandlers.get( name ); - } + return this.handlerManager.getHandler( name, this.request); } /** @@ -827,7 +708,7 @@ DocumentFragment authenticationFragment = null; boolean isValid = false; - final Handler myHandler = this.getHandler(loginHandlerName); + Handler myHandler = this.getHandler(loginHandlerName); if (this.getLogger().isInfoEnabled() == true) { this.getLogger().info("AuthenticationManager: Trying to authenticate using handler '" + loginHandlerName +"'"); } @@ -867,10 +748,8 @@ isValid = this.isValidAuthenticationFragment(authenticationFragment); if (isValid == true) { - this.storeHandlers(); - if (this.getLogger().isInfoEnabled() == true) { - this.getLogger().info("AuthenticationManager: Success authenticated using handler '" + myHandler.getName()+"'"); + this.getLogger().info("AuthenticationManager: User authenticated using handler '" + myHandler.getName()+"'"); } // create session object if necessary, context etc and get it if (this.getLogger().isDebugEnabled() == true) { @@ -880,6 +759,10 @@ if (this.getLogger().isDebugEnabled() == true) { this.getLogger().debug("session created"); } + + myHandler = this.handlerManager.storeUserHandler(myHandler, + this.request); + synchronized(context) { // add special nodes to the authentication block: // useragent, type and media @@ -921,10 +804,10 @@ this.loadApplicationXML((SessionContextImpl)this.getSessionManager().getContext(AuthenticationConstants.SESSION_CONTEXT_NAME), appHandler, "/"); } else { - loaded = appHandler.getIsLoaded(context); + loaded = appHandler.getIsLoaded(); } } - myHandler.setApplicationsLoaded(context, loaded); + myHandler.setApplicationsLoaded(loaded); } // end sync } @@ -1078,10 +961,6 @@ if (context != null && logoutHandlerName != null) { - // cached? - if (context.getAttribute("COCOON_ISAUTHENTICATED:" + logoutHandlerName) != null) { - context.setAttribute("COCOON_ISAUTHENTICATED:" + logoutHandlerName, null); - } // remove context context.removeXML(logoutHandlerName); // FIXME (CZ): The sessionContextImpl should not be null, but @@ -1090,20 +969,11 @@ (this.getSessionManager().getContext(AuthenticationConstants.SESSION_CONTEXT_NAME)); if (sessionContextImpl != null) { sessionContextImpl.cleanParametersCache(logoutHandlerName); - } else if (this.getLogger().isDebugEnabled()) { - this.getLogger().debug("AuthenticationManager:logout() - sessionContextImpl is null"); + } else if (this.getLogger().isWarnEnabled()) { + this.getLogger().warn("AuthenticationManager:logout() - sessionContextImpl is null"); } Handler logoutHandler = (Handler)this.getHandler(logoutHandlerName); - // reset application load status - logoutHandler.setApplicationsLoaded(context, false); - Iterator apps = logoutHandler.getApplications().values().iterator(); - ApplicationHandler current; - while (apps.hasNext() == true) { - current = (ApplicationHandler)apps.next(); - current.setIsLoaded(context, false); - } - final List handlerContexts = logoutHandler.getHandlerContexts(); final Iterator iter = handlerContexts.iterator(); while ( iter.hasNext() ) { @@ -1111,6 +981,13 @@ this.getSessionManager().deleteContext( deleteContext.getName() ); } logoutHandler.clearHandlerContexts(); + this.handlerManager.removeUserHandler( logoutHandler, this.request ); + if (logoutHandlerName.equals(this.handlerName)) { + this.handlerName = null; + this.handler = null; + this.applicationName = null; + this.application = null; + } } if ( mode != null && mode.equalsIgnoreCase("terminateSession") ) { @@ -1447,7 +1324,7 @@ Object o = this.getSessionManager().getSession(true); synchronized(o) { - if (appHandler.getIsLoaded(context) == false) { + if (appHandler.getIsLoaded() == false) { final Resource loadResource = appHandler.getLoadResource(); final String loadResourceName = loadResource.getResourceIdentifier(); @@ -1463,7 +1340,7 @@ fragment = this.getResourceConnector().loadXML(loadResourceType, null, loadResourceName, parameters); - appHandler.setIsLoaded(context, true); + appHandler.setIsLoaded(true); context.setApplicationXML(appHandler.getHandler().getName(), appHandler.getName(), @@ -1474,9 +1351,9 @@ Iterator applications = appHandler.getHandler().getApplications().values().iterator(); boolean allLoaded = true; while (allLoaded == true && applications.hasNext() == true) { - allLoaded = ((ApplicationHandler)applications.next()).getIsLoaded(context); + allLoaded = ((ApplicationHandler)applications.next()).getIsLoaded(); } - appHandler.getHandler().setApplicationsLoaded(context, allLoaded); + appHandler.getHandler().setApplicationsLoaded(allLoaded); } } // end synchronized 1.5 +10 -15 xml-cocoon2/src/java/org/apache/cocoon/webapps/authentication/components/Handler.java Index: Handler.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/webapps/authentication/components/Handler.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- Handler.java 29 May 2002 16:05:42 -0000 1.4 +++ Handler.java 3 Jun 2002 10:02:01 -0000 1.5 @@ -68,7 +68,7 @@ * The authentication Handler. * * @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a> - * @version CVS $Id: Handler.java,v 1.4 2002/05/29 16:05:42 cziegeler Exp $ + * @version CVS $Id: Handler.java,v 1.5 2002/06/03 10:02:01 cziegeler Exp $ */ public final class Handler implements java.io.Serializable { @@ -115,25 +115,24 @@ /** The change-user resource */ private Resource changeUserResource; - /** The name of the context attribute, where isLoaded is stored */ - private final String attributeName; - /** The handler contexts */ private List handlerContexts = new ArrayList(2); + /** Are all apps loaded? */ + private boolean appsLoaded = false; + /** * Create a new handler object. */ public Handler(String name) { this.name = name; - this.attributeName = "authentication_H_" + this.name; } /** * Make a copy */ public Handler copy() { - Handler copy = new Handler(this.name); + final Handler copy = new Handler(this.name); copy.redirectURI = this.redirectURI; copy.redirectParameters = this.redirectParameters; copy.authenticationResource = this.authenticationResource; @@ -149,7 +148,7 @@ Iterator iter = this.applications.keySet().iterator(); while (iter.hasNext()) { final String name = (String)iter.next(); - copy.applications.put(name, ((ApplicationHandler)this.applications.get(name)).copy()); + copy.applications.put(name, ((ApplicationHandler)this.applications.get(name)).copy(copy)); } return copy; } @@ -344,21 +343,17 @@ */ public Map getApplications() { return applications; } - public void setApplicationsLoaded(SessionContext context, boolean value) + public void setApplicationsLoaded(boolean value) throws ProcessingException { - Boolean bool = new Boolean(value); - context.setAttribute(this.attributeName, bool); + this.appsLoaded = value; } - public boolean getApplicationsLoaded(SessionContext context) + public boolean getApplicationsLoaded() throws ProcessingException { if (this.applications.isEmpty() == true) { return true; } else { - boolean result = false; - Boolean bool = (Boolean)context.getAttribute(this.attributeName); - if (bool != null) result = bool.booleanValue(); - return result; + return this.appsLoaded; } } 1.5 +34 -2 xml-cocoon2/src/java/org/apache/cocoon/webapps/authentication/components/HandlerManager.java Index: HandlerManager.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/webapps/authentication/components/HandlerManager.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- HandlerManager.java 29 May 2002 15:58:21 -0000 1.4 +++ HandlerManager.java 3 Jun 2002 10:02:01 -0000 1.5 @@ -64,7 +64,7 @@ * This is a utility class managing the authentication handlers * * @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a> - * @version CVS $Id: HandlerManager.java,v 1.4 2002/05/29 15:58:21 cziegeler Exp $ + * @version CVS $Id: HandlerManager.java,v 1.5 2002/06/03 10:02:01 cziegeler Exp $ */ public final class HandlerManager { @@ -187,6 +187,7 @@ */ public Handler getHandler(String handlerName, Request request) { + if ( null == handlerName) return null; if ( null == this.userHandlers) { final Session session = request.getSession(false); if ( null != session) { @@ -215,11 +216,42 @@ if ( null == this.userHandlers ) { this.userHandlers = new HashMap(3); } - // FIXME (CZ) - clone handler + handler = handler.copy(); this.userHandlers.put(handler.getName(), handler); // value did change, update attributes session.setAttribute(SESSION_ATTRIBUTE_HANDLERS, this.userHandlers); return handler; + } + + /** + * Remove from user handler + */ + public void removeUserHandler(Handler handler, Request request) { + final Session session = request.getSession(); + if ( null == this.userHandlers) { + this.userHandlers = (Map)session.getAttribute(SESSION_ATTRIBUTE_HANDLERS); + } + if ( null != this.userHandlers) { + this.userHandlers.remove( handler.getName() ); + // value did change, update attributes + session.setAttribute(SESSION_ATTRIBUTE_HANDLERS, this.userHandlers); + } + } + + /** + * Check, if a user handler is available (= is authenticated) + */ + public boolean hasUserHandler(String name, Request request) { + if ( null == this.userHandlers) { + final Session session = request.getSession(false); + if ( null != session) { + this.userHandlers = (Map)session.getAttribute(SESSION_ATTRIBUTE_HANDLERS); + } + } + if ( null != this.userHandlers) { + return this.userHandlers.containsKey( name ); + } + return false; } }
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]