]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/argeo-commons/org.argeo.cms.ui.workbench.rap/src/org/argeo/cms/ui/workbench/rap/AnonymousEntryPoint.java
Massive Argeo APIs refactoring
[gpl/argeo-slc.git] / legacy / argeo-commons / org.argeo.cms.ui.workbench.rap / src / org / argeo / cms / ui / workbench / rap / AnonymousEntryPoint.java
1 package org.argeo.cms.ui.workbench.rap;
2
3 import java.security.PrivilegedAction;
4
5 import javax.security.auth.Subject;
6 import javax.security.auth.login.LoginContext;
7 import javax.security.auth.login.LoginException;
8
9 import org.argeo.api.cms.CmsAuth;
10 import org.argeo.api.cms.CmsLog;
11 import org.argeo.cms.CmsException;
12 import org.eclipse.rap.rwt.RWT;
13 import org.eclipse.rap.rwt.application.EntryPoint;
14 import org.eclipse.swt.widgets.Display;
15 import org.eclipse.ui.PlatformUI;
16
17 /**
18 * RAP entry point which authenticates the subject as anonymous, for public
19 * unauthenticated access.
20 */
21 public class AnonymousEntryPoint implements EntryPoint {
22 private final static CmsLog log = CmsLog.getLog(AnonymousEntryPoint.class);
23
24 /**
25 * How many seconds to wait before invalidating the session if the user has
26 * not yet logged in.
27 */
28 private Integer sessionTimeout = 5 * 60;
29
30 @Override
31 public int createUI() {
32 RWT.getRequest().getSession().setMaxInactiveInterval(sessionTimeout);
33
34 // if (log.isDebugEnabled())
35 // log.debug("Anonymous THREAD=" + Thread.currentThread().getId()
36 // + ", sessionStore=" + RWT.getSessionStore().getId());
37
38 final Display display = PlatformUI.createDisplay();
39 Subject subject = new Subject();
40
41 final LoginContext loginContext;
42 try {
43 loginContext = new LoginContext(CmsAuth.LOGIN_CONTEXT_ANONYMOUS,
44 subject);
45 loginContext.login();
46 } catch (LoginException e1) {
47 throw new CmsException("Cannot initialize login context", e1);
48 }
49
50 // identify after successful login
51 if (log.isDebugEnabled())
52 log.debug("Authenticated " + subject);
53 final String username = subject.getPrincipals().iterator().next()
54 .getName();
55
56 // Logout callback when the display is disposed
57 display.disposeExec(new Runnable() {
58 public void run() {
59 log.debug("Display disposed");
60 logout(loginContext, username);
61 }
62 });
63
64 //
65 // RUN THE WORKBENCH
66 //
67 Integer returnCode = null;
68 try {
69 returnCode = Subject.doAs(subject, new PrivilegedAction<Integer>() {
70 public Integer run() {
71 RapWorkbenchAdvisor workbenchAdvisor = new RapWorkbenchAdvisor(
72 null);
73 int result = PlatformUI.createAndRunWorkbench(display,
74 workbenchAdvisor);
75 return new Integer(result);
76 }
77 });
78 logout(loginContext, username);
79 if (log.isTraceEnabled())
80 log.trace("Return code " + returnCode);
81 } finally {
82 display.dispose();
83 }
84 return 1;
85 }
86
87 private void logout(LoginContext loginContext, String username) {
88 try {
89 loginContext.logout();
90 log.info("Logged out " + (username != null ? username : "")
91 + " (THREAD=" + Thread.currentThread().getId() + ")");
92 } catch (LoginException e) {
93 log.error("Erorr when logging out", e);
94 }
95 }
96 }