(ConversationRegistry)container.getComponentInstanceOfType(ConversationRegistry.class);
IdentityRegistry identityRegistry =
(IdentityRegistry)container.getComponentInstanceOfType(IdentityRegistry.class);
ConversationState state = null;
String userId = httpRequest.getRemoteUser();
// only if user authenticated, otherwise there is no reason to do anythings
if (userId != null)
{
HttpSession httpSession = httpRequest.getSession();
StateKey stateKey = new HttpSessionStateKey(httpSession);
if (log.isDebugEnabled())
{
log.debug("Looking for Conversation State " + httpSession.getId());
}
state = conversationRegistry.getState(stateKey);
if (state == null)
{
if (log.isDebugEnabled())
{
log.debug("Conversation State not found, try create new one.");
}
Identity identity = identityRegistry.getIdentity(userId);
if (identity != null)
{
state = new ConversationState(identity);
// Keep subject as attribute in ConversationState.
// TODO remove this, do not need it any more.
state.setAttribute(ConversationState.SUBJECT, identity.getSubject());
}
else
{
if (restoreIdentity)
{
if (log.isDebugEnabled())
{
log.debug("Not found identity for " + userId + " try to restore it. ");
}
Authenticator authenticator =
(Authenticator)container.getComponentInstanceOfType(Authenticator.class);
try
{
identity = authenticator.createIdentity(userId);
identityRegistry.register(identity);
}
catch (Exception e)
{
log.error("Unable restore identity. " + e.getMessage(), e);
}
if (identity != null)
{
state = new ConversationState(identity);
}
}
else
{
log.error("Not found identity in IdentityRegistry for user " + userId + ", check Login Module.");
}
}
if (state != null)
{
conversationRegistry.register(stateKey, state);
if (log.isDebugEnabled())
{
log.debug("Register Conversation state " + httpSession.getId());
}
}
}
}
else
{
state = new ConversationState(new Identity(IdentityConstants.ANONIM));
}
return state;
}