X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Finternal%2Fauth%2FCmsSessionImpl.java;h=164d319f197ae92074b2c5965233deb6f7c1be5c;hb=da9d144b6b241e1526a3bd255dff905a7969a5bc;hp=4e9b4e07293188b62e31d4fcdf383e71f90ee613;hpb=b7683883512d924a039a43c2e1102290aa49f64d;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms/src/org/argeo/cms/internal/auth/CmsSessionImpl.java b/org.argeo.cms/src/org/argeo/cms/internal/auth/CmsSessionImpl.java index 4e9b4e072..164d319f1 100644 --- a/org.argeo.cms/src/org/argeo/cms/internal/auth/CmsSessionImpl.java +++ b/org.argeo.cms/src/org/argeo/cms/internal/auth/CmsSessionImpl.java @@ -5,13 +5,16 @@ import java.security.AccessControlContext; import java.security.AccessController; import java.security.PrivilegedAction; import java.time.ZonedDateTime; -import java.util.Collection; +import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; -import java.util.Hashtable; +import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.UUID; +import java.util.function.Consumer; import javax.crypto.SecretKey; import javax.naming.InvalidNameException; @@ -21,23 +24,19 @@ import javax.security.auth.login.LoginContext; import javax.security.auth.login.LoginException; import javax.security.auth.x500.X500Principal; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.argeo.api.NodeConstants; +import org.argeo.api.cms.CmsAuth; +import org.argeo.api.cms.CmsLog; import org.argeo.api.cms.CmsSession; -import org.argeo.api.security.NodeSecurityUtils; -import org.osgi.framework.BundleContext; -import org.osgi.framework.FrameworkUtil; -import org.osgi.framework.InvalidSyntaxException; -import org.osgi.framework.ServiceReference; +import org.argeo.cms.internal.runtime.CmsContextImpl; +import org.argeo.cms.security.NodeSecurityUtils; import org.osgi.framework.ServiceRegistration; import org.osgi.service.useradmin.Authorization; /** Default CMS session implementation. */ public class CmsSessionImpl implements CmsSession, Serializable { private static final long serialVersionUID = 1867719354246307225L; - private final static BundleContext bc = FrameworkUtil.getBundle(CmsSessionImpl.class).getBundleContext(); - private final static Log log = LogFactory.getLog(CmsSessionImpl.class); +// private final static BundleContext bc = FrameworkUtil.getBundle(CmsSessionImpl.class).getBundleContext(); + private final static CmsLog log = CmsLog.getLog(CmsSessionImpl.class); // private final Subject initialSubject; private transient AccessControlContext accessControlContext; @@ -55,7 +54,12 @@ public class CmsSessionImpl implements CmsSession, Serializable { private Map views = new HashMap<>(); - public CmsSessionImpl(Subject initialSubject, Authorization authorization, Locale locale, String localSessionId) { + private List> onCloseCallbacks = Collections.synchronizedList(new ArrayList<>()); + + public CmsSessionImpl(UUID uuid, Subject initialSubject, Authorization authorization, Locale locale, + String localSessionId) { + Objects.requireNonNull(uuid); + this.creationTime = ZonedDateTime.now(); this.locale = locale; this.accessControlContext = Subject.doAs(initialSubject, new PrivilegedAction() { @@ -80,25 +84,24 @@ public class CmsSessionImpl implements CmsSession, Serializable { this.userDn = NodeSecurityUtils.ROLE_ANONYMOUS_NAME; this.anonymous = true; } - this.uuid = UUID.randomUUID(); - // register as service - Hashtable props = new Hashtable<>(); - props.put(CmsSession.USER_DN, userDn.toString()); - props.put(CmsSession.SESSION_UUID, uuid.toString()); - props.put(CmsSession.SESSION_LOCAL_ID, localSessionId); - serviceRegistration = bc.registerService(CmsSession.class, this, props); + this.uuid = uuid; } public void close() { end = ZonedDateTime.now(); - serviceRegistration.unregister(); + CmsContextImpl.getCmsContext().unregisterCmsSession(this); +// serviceRegistration.unregister(); + + for (Consumer onClose : onCloseCallbacks) { + onClose.accept(this); + } try { LoginContext lc; if (isAnonymous()) { - lc = new LoginContext(NodeConstants.LOGIN_CONTEXT_ANONYMOUS, getSubject()); + lc = new LoginContext(CmsAuth.LOGIN_CONTEXT_ANONYMOUS, getSubject()); } else { - lc = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER, getSubject()); + lc = new LoginContext(CmsAuth.LOGIN_CONTEXT_USER, getSubject()); } lc.logout(); } catch (LoginException e) { @@ -109,6 +112,11 @@ public class CmsSessionImpl implements CmsSession, Serializable { log.debug("Closed " + this); } + @Override + public void addOnCloseCallback(Consumer onClose) { + onCloseCallbacks.add(onClose); + } + public Subject getSubject() { return Subject.getSubject(accessControlContext); } @@ -191,59 +199,6 @@ public class CmsSessionImpl implements CmsSession, Serializable { } public String toString() { - return "CMS Session " + userDn + " local=" + localSessionId + ", uuid=" + uuid; - } - - public static CmsSessionImpl getByLocalId(String localId) { - Collection> sr; - try { - sr = bc.getServiceReferences(CmsSession.class, "(" + CmsSession.SESSION_LOCAL_ID + "=" + localId + ")"); - } catch (InvalidSyntaxException e) { - throw new IllegalArgumentException("Cannot get CMS session for id " + localId, e); - } - ServiceReference cmsSessionRef; - if (sr.size() == 1) { - cmsSessionRef = sr.iterator().next(); - return (CmsSessionImpl) bc.getService(cmsSessionRef); - } else if (sr.size() == 0) { - return null; - } else - throw new IllegalStateException(sr.size() + " CMS sessions registered for " + localId); - - } - - public static CmsSessionImpl getByUuid(Object uuid) { - Collection> sr; - try { - sr = bc.getServiceReferences(CmsSession.class, "(" + CmsSession.SESSION_UUID + "=" + uuid + ")"); - } catch (InvalidSyntaxException e) { - throw new IllegalArgumentException("Cannot get CMS session for uuid " + uuid, e); - } - ServiceReference cmsSessionRef; - if (sr.size() == 1) { - cmsSessionRef = sr.iterator().next(); - return (CmsSessionImpl) bc.getService(cmsSessionRef); - } else if (sr.size() == 0) { - return null; - } else - throw new IllegalStateException(sr.size() + " CMS sessions registered for " + uuid); - - } - - public static void closeInvalidSessions() { - Collection> srs; - try { - srs = bc.getServiceReferences(CmsSession.class, null); - for (ServiceReference sr : srs) { - CmsSession cmsSession = bc.getService(sr); - if (!cmsSession.isValid()) { - ((CmsSessionImpl) cmsSession).close(); - if (log.isDebugEnabled()) - log.debug("Closed expired CMS session " + cmsSession); - } - } - } catch (InvalidSyntaxException e) { - throw new IllegalArgumentException("Cannot get CMS sessions", e); - } + return "CMS Session " + userDn + " localId=" + localSessionId + ", uuid=" + uuid; } }