public void event(Request request, Response response, HttpEvent event)
throws IOException, ServletException
{
Session session = null;
// Get the request caller which could be set due to SSO
Principal caller = request.getPrincipal();
// The cached web container principal
JBossGenericPrincipal principal = null;
HttpSession hsession = request.getSession(false);
if( trace )
log.trace("Begin invoke, caller="+caller);
try
{
Wrapper servlet = null;
try
{
servlet = request.getWrapper();
if (servlet != null)
{
String name = servlet.getName();
RunAsIdentityMetaData identity = metaData.getRunAsIdentity(name);
RunAsIdentity runAsIdentity = null;
if(identity != null)
{
if (trace)
log.trace(name + ", runAs: " + identity);
runAsIdentity = new RunAsIdentity(identity.getRoleName(),
identity.getPrincipalName(), identity.getRunAsRoles());
}
SecurityAssociationActions.pushRunAsIdentity(runAsIdentity);
}
userPrincipal.set(caller);
// If there is a session, get the tomcat session for the principal
Manager manager = container.getManager();
if (manager != null && hsession != null)
{
try
{
session = manager.findSession(hsession.getId());
}
catch (IOException ignore)
{
}
}
if (caller == null || (caller instanceof JBossGenericPrincipal) == false)
{
// Look to the session for the active caller security context
if (session != null)
{
principal =
(JBossGenericPrincipal) session.getPrincipal();
}
}
else
{
// Use the request principal as the caller identity
principal = (JBossGenericPrincipal) caller;
}
// If there is a caller use this as the identity to propagate
if (principal != null)
{
if (trace)
log.trace("Restoring principal info from cache");
SecurityAssociationActions.setPrincipalInfo(principal.getAuthPrincipal(),
principal.getCredentials(), principal.getSubject());
}
// Put the authenticated subject in the session if requested
if (subjectAttributeName != null)
{
javax.naming.Context securityNamingCtx = getSecurityNamingContext();
if (securityNamingCtx != null)
{
// Get the JBoss security manager from the ENC context
AuthenticationManager securityMgr = (AuthenticationManager) securityNamingCtx.lookup("securityMgr");
Subject subject = securityMgr.getActiveSubject();
request.getRequest().setAttribute(subjectAttributeName, subject);
}
}
}
catch (Throwable e)
{
log.debug("Failed to determine servlet", e);
}
// Perform the request
getNext().event(request, response, event);
if(servlet != null)
{
SecurityAssociationActions.popRunAsIdentity();
}
/* If the security domain cache is to be kept in synch with the
session then flush the cache if the session has been invalidated.
*/
if( secMgrService != null &&
session != null && session.isValid() == false &&
metaData.isFlushOnSessionInvalidation() == true )
{
if( principal != null )
{
String securityDomain = metaData.getSecurityDomain();
if (trace)
{
log.trace("Session is invalid, security domain: "+securityDomain
+", user="+principal);
}
try
{
Principal authPrincipal = principal.getAuthPrincipal();
secMgrService.flushAuthenticationCache(securityDomain, authPrincipal);
}
catch(Exception e)
{
log.debug("Failed to flush auth cache", e);