]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.cms/src/org/argeo/cms/AbstractCmsEntryPoint.java
Remove unused directory
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / AbstractCmsEntryPoint.java
index 6e30d8e31d7b16b3b9cf4938faf954e0721839b5..9f3fee857636101bcdc844fa104df5fea408524a 100644 (file)
@@ -3,9 +3,7 @@ package org.argeo.cms;
 import java.security.AccessControlContext;
 import java.security.PrivilegedAction;
 import java.util.HashMap;
-import java.util.Locale;
 import java.util.Map;
-import java.util.ResourceBundle;
 
 import javax.jcr.Node;
 import javax.jcr.Property;
@@ -14,16 +12,16 @@ import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.nodetype.NodeType;
 import javax.security.auth.Subject;
+import javax.security.auth.login.LoginContext;
 import javax.security.auth.login.LoginException;
+import javax.security.auth.x500.X500Principal;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpSession;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.ArgeoException;
-import org.argeo.cms.auth.ArgeoLoginContext;
-import org.argeo.cms.auth.LoginRequiredException;
-import org.argeo.cms.i18n.Msg;
+import org.argeo.cms.auth.AuthConstants;
 import org.argeo.jcr.JcrUtils;
 import org.eclipse.rap.rwt.RWT;
 import org.eclipse.rap.rwt.application.AbstractEntryPoint;
@@ -38,7 +36,7 @@ import org.eclipse.swt.widgets.Shell;
 
 /** Manages history and navigation */
 public abstract class AbstractCmsEntryPoint extends AbstractEntryPoint
-               implements CmsSession {
+               implements CmsView {
        private final Log log = LogFactory.getLog(AbstractCmsEntryPoint.class);
 
        private final Subject subject;
@@ -70,25 +68,28 @@ public abstract class AbstractCmsEntryPoint extends AbstractEntryPoint
                HttpServletRequest httpRequest = RWT.getRequest();
                final HttpSession httpSession = httpRequest.getSession();
                AccessControlContext acc = (AccessControlContext) httpSession
-                               .getAttribute(KernelHeader.ACCESS_CONTROL_CONTEXT);
-               if (acc != null)
+                               .getAttribute(AuthConstants.ACCESS_CONTROL_CONTEXT);
+               if (acc != null
+                               && Subject.getSubject(acc).getPrincipals(X500Principal.class)
+                                               .size() == 1) {
                        subject = Subject.getSubject(acc);
-               else
+               } else {
                        subject = new Subject();
 
-               // Initial login
-               try {
-                       new ArgeoLoginContext(KernelHeader.LOGIN_CONTEXT_USER, subject)
-                                       .login();
-               } catch (LoginException e) {
-                       // if (log.isTraceEnabled())
-                       // log.trace("Cannot authenticate user", e);
+                       // Initial login
                        try {
-                               new ArgeoLoginContext(KernelHeader.LOGIN_CONTEXT_ANONYMOUS,
-                                               subject).login();
-                       } catch (LoginException eAnonymous) {
-                               throw new ArgeoException("Cannot initialize subject",
-                                               eAnonymous);
+                               new LoginContext(AuthConstants.LOGIN_CONTEXT_USER, subject)
+                                               .login();
+                       } catch (LoginException e) {
+                               // if (log.isTraceEnabled())
+                               // log.trace("Cannot authenticate user", e);
+                               try {
+                                       new LoginContext(AuthConstants.LOGIN_CONTEXT_ANONYMOUS,
+                                                       subject).login();
+                               } catch (LoginException eAnonymous) {
+                                       throw new ArgeoException("Cannot initialize subject",
+                                                       eAnonymous);
+                               }
                        }
                }
                authChange();
@@ -118,7 +119,7 @@ public abstract class AbstractCmsEntryPoint extends AbstractEntryPoint
 
        @Override
        protected final void createContents(final Composite parent) {
-               getShell().getDisplay().setData(CmsSession.KEY, this);
+               getShell().getDisplay().setData(CmsView.KEY, this);
                Subject.doAs(subject, new PrivilegedAction<Void>() {
                        @Override
                        public Void run() {
@@ -145,8 +146,9 @@ public abstract class AbstractCmsEntryPoint extends AbstractEntryPoint
         */
        protected Node getDefaultNode(Session session) throws RepositoryException {
                if (!session.hasPermission(defaultPath, "read")) {
-                       if (session.getUserID().equals("anonymous"))
-                               throw new LoginRequiredException();
+                       if (session.getUserID().equals(AuthConstants.ROLE_ANONYMOUS))
+                               // TODO throw a special exception
+                               throw new CmsException("Login required");
                        else
                                throw new CmsException("Unauthorized");
                }
@@ -189,11 +191,11 @@ public abstract class AbstractCmsEntryPoint extends AbstractEntryPoint
                                                } catch (Exception e) {
                                                        try {
                                                                // TODO find a less hacky way to log out
-                                                               new ArgeoLoginContext(
-                                                                               KernelHeader.LOGIN_CONTEXT_ANONYMOUS,
+                                                               new LoginContext(
+                                                                               AuthConstants.LOGIN_CONTEXT_ANONYMOUS,
                                                                                subject).logout();
-                                                               new ArgeoLoginContext(
-                                                                               KernelHeader.LOGIN_CONTEXT_ANONYMOUS,
+                                                               new LoginContext(
+                                                                               AuthConstants.LOGIN_CONTEXT_ANONYMOUS,
                                                                                subject).login();
                                                        } catch (LoginException eAnonymous) {
                                                                throw new ArgeoException(
@@ -234,17 +236,17 @@ public abstract class AbstractCmsEntryPoint extends AbstractEntryPoint
                });
        }
 
-       @Override
-       public Object local(Msg msg) {
-               String key = msg.getId();
-               int lastDot = key.lastIndexOf('.');
-               String className = key.substring(0, lastDot);
-               String fieldName = key.substring(lastDot + 1);
-               Locale locale = RWT.getLocale();
-               ResourceBundle rb = ResourceBundle.getBundle(className, locale,
-                               msg.getClassLoader());
-               return rb.getString(fieldName);
-       }
+       // @Override
+       // public Object local(Msg msg) {
+       // String key = msg.getId();
+       // int lastDot = key.lastIndexOf('.');
+       // String className = key.substring(0, lastDot);
+       // String fieldName = key.substring(lastDot + 1);
+       // Locale locale = RWT.getLocale();
+       // ResourceBundle rb = ResourceBundle.getBundle(className, locale,
+       // msg.getClassLoader());
+       // return rb.getString(fieldName);
+       // }
 
        /** Sets the state of the entry point and retrieve the related JCR node. */
        protected synchronized String setState(String newState) {