2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.security
.ui
.rap
;
18 import java
.security
.PrivilegedAction
;
20 import javax
.security
.auth
.Subject
;
21 import javax
.security
.auth
.login
.LoginContext
;
22 import javax
.security
.auth
.login
.LoginException
;
24 import org
.apache
.commons
.logging
.Log
;
25 import org
.apache
.commons
.logging
.LogFactory
;
26 import org
.argeo
.ArgeoException
;
27 import org
.argeo
.cms
.KernelHeader
;
28 import org
.argeo
.cms
.auth
.ArgeoLoginContext
;
29 import org
.eclipse
.rap
.rwt
.RWT
;
30 import org
.eclipse
.rap
.rwt
.application
.EntryPoint
;
31 import org
.eclipse
.swt
.widgets
.Display
;
32 import org
.eclipse
.ui
.PlatformUI
;
35 * RAP entry point which authenticates the subject as anonymous, for public
36 * unauthenticated access.
38 public class AnonymousEntryPoint
implements EntryPoint
{
39 private final static Log log
= LogFactory
.getLog(AnonymousEntryPoint
.class);
42 * How many seconds to wait before invalidating the session if the user has
45 private Integer sessionTimeout
= 5 * 60;
48 public int createUI() {
49 RWT
.getRequest().getSession().setMaxInactiveInterval(sessionTimeout
);
51 // if (log.isDebugEnabled())
52 // log.debug("Anonymous THREAD=" + Thread.currentThread().getId()
53 // + ", sessionStore=" + RWT.getSessionStore().getId());
55 final Display display
= PlatformUI
.createDisplay();
56 Subject subject
= new Subject();
58 final LoginContext loginContext
;
60 loginContext
= new ArgeoLoginContext(
61 KernelHeader
.LOGIN_CONTEXT_ANONYMOUS
, subject
);
63 } catch (LoginException e1
) {
64 throw new ArgeoException("Cannot initialize login context", e1
);
67 // identify after successful login
68 if (log
.isDebugEnabled())
69 log
.debug("Authenticated " + subject
);
70 final String username
= subject
.getPrincipals().iterator().next()
73 // Logout callback when the display is disposed
74 display
.disposeExec(new Runnable() {
76 log
.debug("Display disposed");
77 logout(loginContext
, username
);
84 Integer returnCode
= null;
86 returnCode
= Subject
.doAs(subject
, new PrivilegedAction
<Integer
>() {
87 public Integer
run() {
88 RapWorkbenchAdvisor workbenchAdvisor
= new RapWorkbenchAdvisor(
90 int result
= PlatformUI
.createAndRunWorkbench(display
,
92 return new Integer(result
);
95 logout(loginContext
, username
);
96 if (log
.isTraceEnabled())
97 log
.trace("Return code " + returnCode
);
104 private void logout(LoginContext loginContext
, String username
) {
106 loginContext
.logout();
107 log
.info("Logged out " + (username
!= null ? username
: "")
108 + " (THREAD=" + Thread
.currentThread().getId() + ")");
109 } catch (LoginException e
) {
110 log
.error("Erorr when logging out", e
);