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