Change default workspace from 'main' to 'sys'.
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / auth / CmsSessionImpl.java
index 9b667717beaf80a60463d7f5068b87c99979798a..8761480fe29f8ebdc57a4d7336ab4c748ab4ffd1 100644 (file)
@@ -3,6 +3,7 @@ package org.argeo.cms.internal.auth;
 import java.security.AccessControlContext;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
+import java.security.PrivilegedActionException;
 import java.security.PrivilegedExceptionAction;
 import java.time.ZonedDateTime;
 import java.util.Collection;
@@ -15,6 +16,7 @@ import java.util.Map;
 import java.util.Set;
 import java.util.UUID;
 
+import javax.crypto.SecretKey;
 import javax.jcr.Repository;
 import javax.jcr.Session;
 import javax.naming.InvalidNameException;
@@ -25,11 +27,10 @@ import javax.security.auth.login.LoginException;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.argeo.cms.CmsException;
+import org.argeo.api.NodeConstants;
+import org.argeo.api.security.NodeSecurityUtils;
 import org.argeo.cms.auth.CmsSession;
 import org.argeo.jcr.JcrUtils;
-import org.argeo.node.NodeConstants;
-import org.argeo.node.security.NodeSecurityUtils;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.FrameworkUtil;
 import org.osgi.framework.InvalidSyntaxException;
@@ -78,7 +79,7 @@ public class CmsSessionImpl implements CmsSession {
                                this.userDn = new LdapName(authorization.getName());
                                this.anonymous = false;
                        } catch (InvalidNameException e) {
-                               throw new CmsException("Invalid user name " + authorization.getName(), e);
+                               throw new IllegalArgumentException("Invalid user name " + authorization.getName(), e);
                        }
                else {
                        this.userDn = NodeSecurityUtils.ROLE_ANONYMOUS_NAME;
@@ -123,10 +124,18 @@ public class CmsSessionImpl implements CmsSession {
                return Subject.getSubject(initialContext);
        }
 
+       public Set<SecretKey> getSecretKeys() {
+               return getSubject().getPrivateCredentials(SecretKey.class);
+       }
+
+       public Session newDataSession(String cn, String workspace, Repository repository) {
+               return login(repository, workspace);
+       }
+
        public synchronized Session getDataSession(String cn, String workspace, Repository repository) {
                // FIXME make it more robust
                if (workspace == null)
-                       workspace = "main";
+                       workspace = NodeConstants.SYS_WORKSPACE;
                String path = cn + '/' + workspace;
                if (dataSessionsInUse.contains(path)) {
                        try {
@@ -164,8 +173,8 @@ public class CmsSessionImpl implements CmsSession {
                                        return repository.login(workspace);
                                }
                        });
-               } catch (Exception e) {
-                       throw new CmsException("Cannot log in " + userDn + " to JCR", e);
+               } catch (PrivilegedActionException e) {
+                       throw new IllegalStateException("Cannot log in " + userDn + " to JCR", e);
                }
        }
 
@@ -173,6 +182,8 @@ public class CmsSessionImpl implements CmsSession {
                if (additionalDataSessions.contains(session)) {
                        JcrUtils.logoutQuietly(session);
                        additionalDataSessions.remove(session);
+                       if (log.isTraceEnabled())
+                               log.trace("Remove additional data session " + session);
                        return;
                }
                String path = cn + '/' + session.getWorkspace().getName();
@@ -182,6 +193,8 @@ public class CmsSessionImpl implements CmsSession {
                Session registeredSession = dataSessions.get(path);
                if (session != registeredSession)
                        log.warn("Data session " + path + " not consistent for " + userDn);
+               if (log.isTraceEnabled())
+                       log.trace("Released data session " + session + " for " + path);
                notifyAll();
        }
 
@@ -243,7 +256,7 @@ public class CmsSessionImpl implements CmsSession {
                try {
                        sr = bc.getServiceReferences(CmsSession.class, "(" + CmsSession.SESSION_LOCAL_ID + "=" + localId + ")");
                } catch (InvalidSyntaxException e) {
-                       throw new CmsException("Cannot get CMS session for id " + localId, e);
+                       throw new IllegalArgumentException("Cannot get CMS session for id " + localId, e);
                }
                ServiceReference<CmsSession> cmsSessionRef;
                if (sr.size() == 1) {
@@ -252,7 +265,7 @@ public class CmsSessionImpl implements CmsSession {
                } else if (sr.size() == 0) {
                        return null;
                } else
-                       throw new CmsException(sr.size() + " CMS sessions registered for " + localId);
+                       throw new IllegalStateException(sr.size() + " CMS sessions registered for " + localId);
 
        }
 
@@ -261,7 +274,7 @@ public class CmsSessionImpl implements CmsSession {
                try {
                        sr = bc.getServiceReferences(CmsSession.class, "(" + CmsSession.SESSION_UUID + "=" + uuid + ")");
                } catch (InvalidSyntaxException e) {
-                       throw new CmsException("Cannot get CMS session for uuid " + uuid, e);
+                       throw new IllegalArgumentException("Cannot get CMS session for uuid " + uuid, e);
                }
                ServiceReference<CmsSession> cmsSessionRef;
                if (sr.size() == 1) {
@@ -270,7 +283,7 @@ public class CmsSessionImpl implements CmsSession {
                } else if (sr.size() == 0) {
                        return null;
                } else
-                       throw new CmsException(sr.size() + " CMS sessions registered for " + uuid);
+                       throw new IllegalStateException(sr.size() + " CMS sessions registered for " + uuid);
 
        }
 
@@ -287,7 +300,7 @@ public class CmsSessionImpl implements CmsSession {
                                }
                        }
                } catch (InvalidSyntaxException e) {
-                       throw new CmsException("Cannot get CMS sessions", e);
+                       throw new IllegalArgumentException("Cannot get CMS sessions", e);
                }
        }
 }