HttpSession hsession = request.getSession(false);
WebLogger.WEB_SECURITY_LOGGER.tracef("Begin invoke, caller=" + caller);
boolean createdSecurityContext = false;
SecurityContext sc = SecurityActions.getSecurityContext();
if (sc == null) {
createdSecurityContext = true;
String securityDomain = SecurityUtil.unprefixSecurityDomain(metaData.getSecurityDomain());
if (securityDomain == null)
securityDomain = SecurityConstants.DEFAULT_WEB_APPLICATION_POLICY;
sc = SecurityActions.createSecurityContext(securityDomain);
SecurityActions.setSecurityContextOnAssociation(sc);
}
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) {
WebLogger.WEB_SECURITY_LOGGER.tracef(name + ", runAs: " + identity);
runAsIdentity = new RunAsIdentity(identity.getRoleName(), identity.getPrincipalName(),
identity.getRunAsRoles());
}
SecurityActions.pushRunAsIdentity(runAsIdentity);
}
// 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)) {
// Look to the session for the active caller security context
if (session != null) {
principal = (JBossGenericPrincipal) session.getPrincipal();
}
if (principal == null) {
Session sessionInternal = request.getSessionInternal(false);
if (sessionInternal != null) {
principal = (JBossGenericPrincipal) sessionInternal.getNote(Constants.FORM_PRINCIPAL_NOTE);
}
}
} 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) {
WebLogger.WEB_SECURITY_LOGGER.tracef("Restoring principal info from cache");
if (createdSecurityContext) {
sc.getUtil().createSubjectInfo(principal.getUserPrincipal(), principal.getCredentials(),
principal.getSubject());
}
}
} catch (Throwable e) {
//TODO:decide whether to log this as info or warn