]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.security.ui.rap/src/org/argeo/security/ui/rap/AnonymousEntryPoint.java
Improve and simplify OSGi Boot
[lgpl/argeo-commons.git] / org.argeo.security.ui.rap / src / org / argeo / security / ui / rap / AnonymousEntryPoint.java
index ac0007acfa9af3bac041d356c40fefd07d01b901..04b6f0a9d38c08a9b02c854d82750a5a21df2ede 100644 (file)
  */
 package org.argeo.security.ui.rap;
 
+import java.security.PrivilegedAction;
+
+import javax.security.auth.Subject;
+import javax.security.auth.login.LoginContext;
+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.cms.auth.AuthConstants;
 import org.eclipse.rap.rwt.RWT;
 import org.eclipse.rap.rwt.application.EntryPoint;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.ui.PlatformUI;
 
 /**
  * RAP entry point which authenticates the subject as anonymous, for public
  * unauthenticated access.
  */
 public class AnonymousEntryPoint implements EntryPoint {
-       // private final static Log log =
-       // LogFactory.getLog(AnonymousEntryPoint.class);
+       private final static Log log = LogFactory.getLog(AnonymousEntryPoint.class);
 
        /**
         * How many seconds to wait before invalidating the session if the user has
         * not yet logged in.
         */
-       private Integer loginTimeout = 1 * 60;
+       private Integer sessionTimeout = 5 * 60;
 
        @Override
        public int createUI() {
-               // Short login timeout so that the modal dialog login doesn't hang
-               // around too long
-               RWT.getRequest().getSession().setMaxInactiveInterval(loginTimeout);
+               RWT.getRequest().getSession().setMaxInactiveInterval(sessionTimeout);
 
                // if (log.isDebugEnabled())
                // log.debug("Anonymous THREAD=" + Thread.currentThread().getId()
                // + ", sessionStore=" + RWT.getSessionStore().getId());
 
-               // create display
-               // final Display display = PlatformUI.createDisplay();
+               final Display display = PlatformUI.createDisplay();
+               Subject subject = new Subject();
+
+               final LoginContext loginContext;
+               try {
+                       loginContext = new LoginContext(AuthConstants.LOGIN_CONTEXT_ANONYMOUS,
+                                       subject);
+                       loginContext.login();
+               } catch (LoginException e1) {
+                       throw new CmsException("Cannot initialize login context", e1);
+               }
+
+               // identify after successful login
+               if (log.isDebugEnabled())
+                       log.debug("Authenticated " + subject);
+               final String username = subject.getPrincipals().iterator().next()
+                               .getName();
+
+               // Logout callback when the display is disposed
+               display.disposeExec(new Runnable() {
+                       public void run() {
+                               log.debug("Display disposed");
+                               logout(loginContext, username);
+                       }
+               });
 
-               // log in
-               // final ILoginContext loginContext = SecureRapActivator
-               // .createLoginContext(SecureRapActivator.CONTEXT_SPRING_ANONYMOUS);
-               // Subject subject = null;
-               // try {
-               // loginContext.login();
-               // subject = loginContext.getSubject();
-               // } catch (LoginException e) {
-               // throw new ArgeoException(
-               // "Unexpected exception during authentication", e);
-               // }
-               //
-               // // identify after successful login
-               // if (log.isDebugEnabled())
-               // log.debug("Authenticated " + subject);
-               // final String username = subject.getPrincipals().iterator().next()
-               // .getName();
-               //
-               // // Once the user is logged in, she can have a longer session timeout
-               // RWT.getRequest().getSession().setMaxInactiveInterval(sessionTimeout);
                //
-               // // Logout callback when the display is disposed
-               // display.disposeExec(new Runnable() {
-               // public void run() {
-               // log.debug("Display disposed");
-               // logout(loginContext, username);
-               // }
-               // });
+               // RUN THE WORKBENCH
                //
-               // //
-               // // RUN THE WORKBENCH
-               // //
-               // Integer returnCode = null;
-               // try {
-               // returnCode = Subject.doAs(subject, new PrivilegedAction<Integer>() {
-               // public Integer run() {
-               // RapWorkbenchAdvisor workbenchAdvisor = new RapWorkbenchAdvisor(
-               // null);
-               // int result = PlatformUI.createAndRunWorkbench(display,
-               // workbenchAdvisor);
-               // return new Integer(result);
-               // }
-               // });
-               // logout(loginContext, username);
-               // } finally {
-               // display.dispose();
-               // }
+               Integer returnCode = null;
+               try {
+                       returnCode = Subject.doAs(subject, new PrivilegedAction<Integer>() {
+                               public Integer run() {
+                                       RapWorkbenchAdvisor workbenchAdvisor = new RapWorkbenchAdvisor(
+                                                       null);
+                                       int result = PlatformUI.createAndRunWorkbench(display,
+                                                       workbenchAdvisor);
+                                       return new Integer(result);
+                               }
+                       });
+                       logout(loginContext, username);
+                       if (log.isTraceEnabled())
+                               log.trace("Return code " + returnCode);
+               } finally {
+                       display.dispose();
+               }
                return 1;
        }
 
-       // private void logout(ILoginContext secureContext, String username) {
-       // try {
-       // secureContext.logout();
-       // log.info("Logged out " + (username != null ? username : "")
-       // + " (THREAD=" + Thread.currentThread().getId() + ")");
-       // } catch (LoginException e) {
-       // log.error("Erorr when logging out", e);
-       // }
-       // }
+       private void logout(LoginContext loginContext, String username) {
+               try {
+                       loginContext.logout();
+                       log.info("Logged out " + (username != null ? username : "")
+                                       + " (THREAD=" + Thread.currentThread().getId() + ")");
+               } catch (LoginException e) {
+                       log.error("Erorr when logging out", e);
+               }
+       }
 }