if (this.getLogger().isDebugEnabled() == true) {
this.getLogger().debug("BEGIN authenticate handler=" + loginHandlerName +
", parameters="+parameters);
}
DocumentFragment authenticationFragment = null;
boolean isValid = false;
Handler myHandler = this.getHandler(loginHandlerName);
if (this.getLogger().isInfoEnabled() == true) {
this.getLogger().info("AuthenticationManager: Trying to authenticate using handler '" + loginHandlerName +"'");
}
if (myHandler != null) {
String exceptionMsg = null;
if (this.getLogger().isDebugEnabled() == true) {
this.getLogger().debug("start authentication");
}
final String authenticationResourceName = myHandler.getAuthenticationResource();
final SourceParameters authenticationParameters = myHandler.getAuthenticationResourceParameters();
if (parameters != null) {
parameters.add(authenticationParameters);
} else {
parameters = authenticationParameters;
}
try {
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("start invoking auth resource");
}
Source source = null;
try {
source = org.apache.cocoon.components.source.SourceUtil.getSource(authenticationResourceName,
null,
parameters,
this.resolver);
Document doc = org.apache.cocoon.components.source.SourceUtil.toDOM(source);
authenticationFragment = doc.createDocumentFragment();
authenticationFragment.appendChild(doc.getDocumentElement());
} catch (SAXException se) {
throw new ProcessingException(se);
} catch (SourceException se) {
throw org.apache.cocoon.components.source.SourceUtil.handle(se);
} finally {
this.resolver.release(source);
}
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("end invoking auth resource");
}
} catch (ProcessingException local) {
this.getLogger().error("authenticate", local);
exceptionMsg = local.getMessage();
}
// test if authentication was successful
if (authenticationFragment != null) {
isValid = this.isValidAuthenticationFragment(authenticationFragment);
if (isValid == true) {
if (this.getLogger().isInfoEnabled() == true) {
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) {
this.getLogger().debug("creating session");
}
SessionContext context = this.getAuthenticationSessionContext(true);
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
Element specialElement;
Text specialValue;
Element authNode;
authNode = (Element)authenticationFragment.getFirstChild();
specialElement = authenticationFragment.getOwnerDocument().createElementNS(null, "useragent");
specialValue = authenticationFragment.getOwnerDocument().createTextNode(request.getHeader("User-Agent"));
specialElement.appendChild(specialValue);
authNode.appendChild(specialElement);
specialElement = authenticationFragment.getOwnerDocument().createElementNS(null, "type");
specialValue = authenticationFragment.getOwnerDocument().createTextNode("cocoon.authentication");
specialElement.appendChild(specialValue);
authNode.appendChild(specialElement);
specialElement = authenticationFragment.getOwnerDocument().createElementNS(null, "media");
specialValue = authenticationFragment.getOwnerDocument().createTextNode(this.mediaType);
specialElement.appendChild(specialValue);
authNode.appendChild(specialElement);
// store the authentication data in the context
context.setXML("/" + myHandler.getName(), authenticationFragment);
// Now create the return value for this method:
// <code>null</code>
authenticationFragment = null;
// And now load applications
boolean loaded = true;
Iterator applications = myHandler.getApplications().values().iterator();
ApplicationHandler appHandler;
while (applications.hasNext() == true) {
appHandler = (ApplicationHandler)applications.next();
if (appHandler.getLoadOnDemand() == false) {
this.loadApplicationXML((SessionContextImpl)this.getSessionManager().getContext(AuthenticationConstants.SESSION_CONTEXT_NAME),
appHandler, "/");
} else {
loaded = appHandler.getIsLoaded();
}
}
myHandler.setApplicationsLoaded(loaded);
} // end sync
}
}
if (isValid == false) {
if (this.getLogger().isInfoEnabled() == true) {
this.getLogger().info("AuthenticationManager: Failed authentication using handler '" + myHandler.getName()+"'");
}
// get the /authentication/data Node if available
Node data = null;
if (authenticationFragment != null) {
data = DOMUtil.getFirstNodeFromPath(authenticationFragment, new String[] {"authentication","data"}, false);
}
// 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
Document doc = DOMUtil.createDocument();
authenticationFragment = doc.createDocumentFragment();
Element element = doc.createElementNS(null, "failed");
authenticationFragment.appendChild(element);
if (exceptionMsg != null) {
Text text = doc.createTextNode(exceptionMsg);
element.appendChild(text);
}
if (data == null) {
element = doc.createElementNS(null, "data");
authenticationFragment.appendChild(element);
Text text = doc.createTextNode("No information");
element.appendChild(text);
} else {
authenticationFragment.appendChild(doc.importNode(data, true));
}
}
if (this.getLogger().isDebugEnabled() == true) {
this.getLogger().debug("end authentication");