001: /**
002: */package clime.messadmin.providers.user;
003:
004: import java.lang.reflect.Method;
005:
006: import javax.servlet.ServletContext;
007: import javax.servlet.http.HttpServletRequest;
008: import javax.servlet.http.HttpServletResponse;
009:
010: import clime.messadmin.model.ISessionInfo;
011: import clime.messadmin.model.Server;
012: import clime.messadmin.model.Session;
013: import clime.messadmin.model.SessionInfo;
014: import clime.messadmin.providers.spi.RequestLifeCycleProvider;
015:
016: /**
017: * Fetch the current user name from Acegi, if available.
018: *
019: * Implementation note: Acegi stores its data in a ThreadLocal object, so we need to sniff it at request time.
020: * Copy (cache) the information as RemoteUser at request time. It will be picked by the HttpRequestRemoteUser plugin.
021: *
022: * @author Cédrik LIME
023: * @since 4.1
024: */
025: public class AcegiUserName implements RequestLifeCycleProvider {
026: private Class securityContextHolderClass;
027: private Method securityContextHolder_getContext;
028: private Class securityContextClass;
029: private Method securityContext_getAuthentication;
030: private Class authenticationClass;
031: private Method authentication_getPrincipal;
032: private Class userDetailsClass;
033: private Method userDetails_getUsername;
034: private boolean acegiAvailable = false;
035:
036: /**
037: */
038: public AcegiUserName() {
039: super ();
040: try {
041: securityContextHolderClass = Class
042: .forName("org.acegisecurity.context.SecurityContextHolder");//$NON-NLS-1$
043: securityContextClass = Class
044: .forName("org.acegisecurity.context.SecurityContext");//$NON-NLS-1$
045: authenticationClass = Class
046: .forName("org.acegisecurity.Authentication");//$NON-NLS-1$
047: userDetailsClass = Class
048: .forName("org.acegisecurity.userdetails.UserDetails");//$NON-NLS-1$
049:
050: securityContextHolder_getContext = securityContextHolderClass
051: .getMethod("getContext", null);//$NON-NLS-1$
052: securityContext_getAuthentication = securityContextClass
053: .getMethod("getAuthentication", null);//$NON-NLS-1$
054: authentication_getPrincipal = authenticationClass
055: .getMethod("getPrincipal", null);//$NON-NLS-1$
056: userDetails_getUsername = userDetailsClass.getMethod(
057: "getUsername", null);//$NON-NLS-1$
058:
059: acegiAvailable = securityContextHolderClass != null
060: && securityContextClass != null
061: && authenticationClass != null
062: && userDetailsClass != null
063: && securityContextHolder_getContext != null
064: && securityContext_getAuthentication != null
065: && authentication_getPrincipal != null
066: && userDetails_getUsername != null;
067: } catch (Exception e) {
068: // do nothing, Acegi not available
069: acegiAvailable = false;
070: }
071: }
072:
073: /**
074: * @see clime.messadmin.providers.spi.BaseProvider#getPriority()
075: */
076: public int getPriority() {
077: return 50;
078: }
079:
080: /**
081: * {@inheritDoc}
082: */
083: public void requestInitialized(HttpServletRequest request,
084: HttpServletResponse response, ServletContext servletContext) {
085: /*
086: Object obj = org.acegisecurity.context.SecurityContextHolder.getContext().getAuthentication().getPrincipal();
087: if (obj instanceof org.acegisecurity.userdetails.UserDetails) {
088: String username = ((org.acegisecurity.userdetails.UserDetails) obj).getUsername();
089: } else {
090: String username = obj.toString();
091: }
092: */
093: if (acegiAvailable) {
094: Session session = Server.getInstance().getSession(
095: request.getSession(false));
096: if (session != null) {
097: ISessionInfo sessionInfo = session.getSessionInfo();
098: if (sessionInfo != null
099: && sessionInfo.getRemoteUser() == null) {
100: try {
101: String userName = null;
102: Object securityContext = securityContextHolder_getContext
103: .invoke(null, null);
104: Object authentication = securityContext_getAuthentication
105: .invoke(securityContext, null);
106: Object obj = authentication_getPrincipal
107: .invoke(authentication, null);
108: if (userDetailsClass.isInstance(obj)) { // obj instanceof UserDetails
109: userName = (String) userDetails_getUsername
110: .invoke(obj, null);
111: } else {
112: userName = (obj == null) ? null : obj
113: .toString();
114: }
115: if (userName != null) {
116: ((SessionInfo) sessionInfo)
117: .setRemoteUser(userName);
118: }
119: } catch (Exception e) {
120: // shouldn't happen; nothing we can do anyway...
121: }
122: }
123: }
124: }
125: }
126:
127: /**
128: * {@inheritDoc}
129: */
130: public void requestDestroyed(HttpServletRequest request,
131: HttpServletResponse response, ServletContext servletContext) {
132: // do nothing
133: }
134: }
|