cziegeler 2003/10/24 01:26:35
Modified:
src/blocks/authentication-fw/java/org/apache/cocoon/webapps/authentication/components
DefaultAuthenticationManager.java
Authenticator.java PipelineAuthenticator.java
Log:
Authenticator now only does what it should do - everything else has moved to
the AM
Revision Changes Path
1.19 +43 -2
cocoon-2.1/src/blocks/authentication-fw/java/org/apache/cocoon/webapps/authentication/components/DefaultAuthenticationManager.java
Index: DefaultAuthenticationManager.java
===================================================================
RCS file:
/home/cvs/cocoon-2.1/src/blocks/authentication-fw/java/org/apache/cocoon/webapps/authentication/components/DefaultAuthenticationManager.java,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -r1.18 -r1.19
--- DefaultAuthenticationManager.java 23 Oct 2003 11:29:36 -0000 1.18
+++ DefaultAuthenticationManager.java 24 Oct 2003 08:26:35 -0000 1.19
@@ -80,15 +80,19 @@
import org.apache.cocoon.webapps.authentication.AuthenticationManager;
import
org.apache.cocoon.webapps.authentication.configuration.ApplicationConfiguration;
import
org.apache.cocoon.webapps.authentication.configuration.HandlerConfiguration;
+import
org.apache.cocoon.webapps.authentication.context.AuthenticationContext;
import org.apache.cocoon.webapps.authentication.user.RequestState;
import org.apache.cocoon.webapps.authentication.user.UserHandler;
import org.apache.cocoon.webapps.authentication.user.UserState;
import org.apache.cocoon.webapps.session.ContextManager;
+import org.apache.cocoon.webapps.session.SessionConstants;
import org.apache.cocoon.webapps.session.SessionManager;
import org.apache.cocoon.webapps.session.context.SessionContext;
import org.apache.excalibur.source.SourceParameters;
import org.apache.excalibur.source.SourceResolver;
import org.apache.excalibur.source.SourceUtil;
+import org.w3c.dom.DocumentFragment;
+import org.w3c.dom.Node;
import org.xml.sax.SAXException;
/**
@@ -236,7 +240,44 @@
Authenticator authenticator = this.lookupAuthenticator( config );
try {
- handler = authenticator.authenticate( config, parameters );
+ Authenticator.AuthenticationResult result =
authenticator.authenticate( config, parameters );
+ if ( result != null && result.valid ) {
+ AuthenticationContext authContext = new
AuthenticationContext(this.context);
+ handler = new UserHandler(config, authContext);
+ // store the authentication data in the context
+ authContext.init(result.result);
+ } else if ( result != null ) {
+ // now set the failure information in the temporary context
+ ContextManager contextManager = null;
+ try {
+ contextManager = (ContextManager) this.manager.lookup(
ContextManager.ROLE );
+ SessionContext temp = contextManager.getContext(
SessionConstants.TEMPORARY_CONTEXT );
+
+ final DocumentFragment fragment =
result.result.createDocumentFragment();
+ final Node root = result.result.getDocumentElement();
+ root.normalize();
+ Node child;
+ boolean appendedNode = false;
+ while (root.hasChildNodes() ) {
+ child = root.getFirstChild();
+ root.removeChild(child);
+ // Leave out empty text nodes before any other node
+ if (appendedNode
+ || child.getNodeType() != Node.TEXT_NODE
+ || child.getNodeValue().trim().length() > 0) {
+ fragment.appendChild(child);
+ appendedNode = true;
+ }
+ }
+ temp.appendXML("/", fragment);
+ } catch ( ServiceException se ) {
+ throw new ProcessingException("Unable to lookup session
manager.", se);
+ } finally {
+ this.manager.release( contextManager );
+ }
+
+ }
+
} finally {
this.releaseAuthenticator( authenticator, config );
}
1.9 +39 -6
cocoon-2.1/src/blocks/authentication-fw/java/org/apache/cocoon/webapps/authentication/components/Authenticator.java
Index: Authenticator.java
===================================================================
RCS file:
/home/cvs/cocoon-2.1/src/blocks/authentication-fw/java/org/apache/cocoon/webapps/authentication/components/Authenticator.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- Authenticator.java 12 Jul 2003 18:39:49 -0000 1.8
+++ Authenticator.java 24 Oct 2003 08:26:35 -0000 1.9
@@ -52,8 +52,8 @@
import org.apache.cocoon.ProcessingException;
import
org.apache.cocoon.webapps.authentication.configuration.HandlerConfiguration;
-import org.apache.cocoon.webapps.authentication.user.UserHandler;
import org.apache.excalibur.source.SourceParameters;
+import org.w3c.dom.Document;
/**
* Verify if a user can be authenticated.
@@ -67,12 +67,45 @@
public interface Authenticator {
/**
+ * This object describes the success or the failure of an attempt
+ * to authenticate a user.
+ * The boolean flag valid specifies a success (valid) or a failure
+ * (not valid).
+ * The document result contains in the case of a success the
+ * authentication xml that is store in the session.
+ * In the case of a failure, the result can contain information
+ * about the failure (or the document can be null).
+ * If in the case of a failure the result contains information,
+ * the xml must follow this format:
+ * <root>
+ * <failed/>
+ * if data is available data is included, otherwise:
+ * <data>No information</data>
+ * If exception message contains info, it is included into failed
+ * </root>
+ * The root element is removed and the contained elements are stored
+ * into the temporary context.
+ */
+ public static class AuthenticationResult {
+
+ public final boolean valid;
+ public final Document result;
+
+ public AuthenticationResult(final boolean valid,
+ final Document result) {
+ this.valid = valid;
+ this.result = result;
+ }
+
+ }
+
+ /**
* Try to authenticate the user.
- * @return A new [EMAIL PROTECTED] UserHandler} if authentication was
successful,
- * otherwise null is returned.
+ * @return A AuthenticationResult that is either valid (authentication
+ * successful) or invalid (authentication failed.
* @throws ProcessingException Only if an error occurs
*/
- public abstract UserHandler authenticate(HandlerConfiguration
configuration,
- SourceParameters parameters)
+ public AuthenticationResult authenticate(HandlerConfiguration
configuration,
+ SourceParameters parameters)
throws ProcessingException;
}
1.6 +23 -44
cocoon-2.1/src/blocks/authentication-fw/java/org/apache/cocoon/webapps/authentication/components/PipelineAuthenticator.java
Index: PipelineAuthenticator.java
===================================================================
RCS file:
/home/cvs/cocoon-2.1/src/blocks/authentication-fw/java/org/apache/cocoon/webapps/authentication/components/PipelineAuthenticator.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- PipelineAuthenticator.java 23 Oct 2003 11:29:36 -0000 1.5
+++ PipelineAuthenticator.java 24 Oct 2003 08:26:35 -0000 1.6
@@ -63,12 +63,7 @@
import org.apache.avalon.framework.thread.ThreadSafe;
import org.apache.cocoon.ProcessingException;
import
org.apache.cocoon.webapps.authentication.configuration.HandlerConfiguration;
-import
org.apache.cocoon.webapps.authentication.context.AuthenticationContext;
-import org.apache.cocoon.webapps.authentication.user.UserHandler;
-import org.apache.cocoon.webapps.session.ContextManager;
import org.apache.cocoon.webapps.session.MediaManager;
-import org.apache.cocoon.webapps.session.SessionConstants;
-import org.apache.cocoon.webapps.session.context.SessionContext;
import org.apache.cocoon.xml.XMLUtils;
import org.apache.cocoon.xml.dom.DOMUtil;
import org.apache.excalibur.source.Source;
@@ -76,7 +71,6 @@
import org.apache.excalibur.source.SourceParameters;
import org.apache.excalibur.source.SourceResolver;
import org.w3c.dom.Document;
-import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
@@ -160,13 +154,11 @@
return isValid;
}
- /**
- * Try to authenticate the user.
- * @return A new [EMAIL PROTECTED] UserHandler} if authentication was
successful
- * @throws ProcessingException
+ /* (non-Javadoc)
+ * @see
org.apache.cocoon.webapps.authentication.components.Authenticator#authenticate(org.apache.cocoon.webapps.authentication.configuration.HandlerConfiguration,
org.apache.excalibur.source.SourceParameters)
*/
- public UserHandler authenticate( HandlerConfiguration configuration,
- SourceParameters parameters)
+ public AuthenticationResult authenticate( HandlerConfiguration
configuration,
+ SourceParameters
parameters)
throws ProcessingException {
if (this.getLogger().isDebugEnabled() ) {
this.getLogger().debug("start authenticator using handler " +
configuration.getName());
@@ -210,7 +202,7 @@
// test if authentication was successful
boolean isValid = false;
- UserHandler handler = null;
+ AuthenticationResult result = null;
if (doc != null) {
isValid = this.isValidAuthenticationFragment( doc );
@@ -219,9 +211,6 @@
this.getLogger().info("Authenticator: User authenticated
using handler '" + configuration.getName()+"'");
}
- AuthenticationContext authContext = new
AuthenticationContext(this.context);
- handler = new UserHandler(configuration, authContext);
-
MediaManager mediaManager = null;
String mediaType;
try {
@@ -232,7 +221,7 @@
} finally {
this.manager.release( mediaManager );
}
- synchronized(authContext) {
+ synchronized (configuration) {
// add special nodes to the authentication block:
// useragent, type and media
Element specialElement;
@@ -251,8 +240,7 @@
specialElement.appendChild(specialValue);
authNode.appendChild(specialElement);
- // store the authentication data in the context
- authContext.init(doc);
+ result = new AuthenticationResult(true, doc);
} // end sync
}
@@ -267,19 +255,20 @@
if (doc != null) {
data = DOMUtil.getFirstNodeFromPath(doc, new String[]
{"authentication","data"}, false);
- } else {
- doc = DOMUtil.createDocument();
}
+ doc = DOMUtil.createDocument();
// now create the following xml:
- // <failed/>
- // if data is available data is included, otherwise:
- // <data>No information</data>
- // If exception message contains info, it is included into failed
- DocumentFragment authenticationFragment =
doc.createDocumentFragment();
-
+ // <root>
+ // <failed/>
+ // if data is available data is included, otherwise:
+ // <data>No information</data>
+ // If exception message contains info, it is included into
failed
+ // </root>
+ final Element root = doc.createElementNS(null, "root");
+ doc.appendChild(root);
Element element = doc.createElementNS(null, "failed");
- authenticationFragment.appendChild(element);
+ root.appendChild(element);
if (exceptionMsg != null) {
Text text = doc.createTextNode(exceptionMsg);
@@ -288,31 +277,21 @@
if (data == null) {
element = doc.createElementNS(null, "data");
- authenticationFragment.appendChild(element);
- Text text = doc.createTextNode("No information");
+ root.appendChild(element);
+ Text text = doc.createTextNode("No information available");
element.appendChild(text);
} else {
- authenticationFragment.appendChild(doc.importNode(data,
true));
+ root.appendChild(doc.importNode(data, true));
}
- // now set this information in the temporary context
- ContextManager sessionManager = null;
- try {
- sessionManager = (ContextManager) this.manager.lookup(
ContextManager.ROLE );
- SessionContext temp = sessionManager.getContext(
SessionConstants.TEMPORARY_CONTEXT );
- temp.appendXML("/", authenticationFragment);
- } catch ( ServiceException se ) {
- throw new ProcessingException("Unable to lookup session
manager.", se);
- } finally {
- this.manager.release( sessionManager );
- }
+ result = new AuthenticationResult(false, doc);
}
if (this.getLogger().isDebugEnabled() ) {
this.getLogger().debug("end authenticator");
}
- return handler;
+ return result;
}