1 package org
.argeo
.cms
.web
;
3 import static org
.eclipse
.rap
.rwt
.internal
.service
.ContextProvider
.getApplicationContext
;
5 import java
.security
.PrivilegedAction
;
6 import java
.util
.HashMap
;
10 import javax
.security
.auth
.Subject
;
11 import javax
.security
.auth
.login
.LoginContext
;
12 import javax
.security
.auth
.login
.LoginException
;
14 import org
.apache
.commons
.logging
.Log
;
15 import org
.apache
.commons
.logging
.LogFactory
;
16 import org
.argeo
.api
.NodeConstants
;
17 import org
.argeo
.cms
.auth
.CurrentUser
;
18 import org
.argeo
.cms
.auth
.HttpRequestCallbackHandler
;
19 import org
.argeo
.cms
.ui
.CmsApp
;
20 import org
.argeo
.cms
.ui
.CmsImageManager
;
21 import org
.argeo
.cms
.ui
.CmsView
;
22 import org
.argeo
.cms
.ui
.UxContext
;
23 import org
.argeo
.cms
.ui
.dialogs
.CmsFeedback
;
24 import org
.argeo
.cms
.ui
.util
.CmsUiUtils
;
25 import org
.argeo
.cms
.ui
.util
.DefaultImageManager
;
26 import org
.argeo
.cms
.ui
.util
.SimpleUxContext
;
27 import org
.argeo
.eclipse
.ui
.specific
.UiContext
;
28 import org
.eclipse
.rap
.rwt
.RWT
;
29 import org
.eclipse
.rap
.rwt
.application
.EntryPoint
;
30 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigation
;
31 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigationEvent
;
32 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigationListener
;
33 import org
.eclipse
.rap
.rwt
.internal
.lifecycle
.RWTLifeCycle
;
34 import org
.eclipse
.swt
.SWT
;
35 import org
.eclipse
.swt
.widgets
.Composite
;
36 import org
.eclipse
.swt
.widgets
.Display
;
37 import org
.eclipse
.swt
.widgets
.Shell
;
38 import org
.osgi
.service
.event
.Event
;
39 import org
.osgi
.service
.event
.EventAdmin
;
41 /** The {@link CmsView} for a {@link CmsWebApp}. */
42 @SuppressWarnings("restriction")
43 public class CmsWebEntryPoint
implements EntryPoint
, CmsView
, BrowserNavigationListener
{
44 private static final long serialVersionUID
= 7733510691684570402L;
45 private final static Log log
= LogFactory
.getLog(CmsWebEntryPoint
.class);
47 private EventAdmin eventAdmin
;
49 private final CmsWebApp cmsWebApp
;
50 private final String uiName
;
52 private LoginContext loginContext
;
54 private Throwable exception
;
55 private UxContext uxContext
;
56 private CmsImageManager imageManager
;
63 // private final JavaScriptExecutor jsExecutor;
64 private final BrowserNavigation browserNavigation
;
66 /** Experimental OS-like multi windows. */
67 private boolean multipleShells
= false;
69 public CmsWebEntryPoint(CmsWebApp cmsWebApp
, String uiName
) {
70 assert cmsWebApp
!= null;
71 assert uiName
!= null;
72 this.cmsWebApp
= cmsWebApp
;
74 uid
= UUID
.randomUUID().toString();
79 lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_USER
,
80 new HttpRequestCallbackHandler(UiContext
.getHttpRequest(), UiContext
.getHttpResponse()));
82 } catch (LoginException e
) {
84 lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_ANONYMOUS
);
86 } catch (LoginException e1
) {
87 throw new IllegalStateException("Cannot log in as anonymous", e1
);
92 // jsExecutor = RWT.getClient().getService(JavaScriptExecutor.class);
93 browserNavigation
= RWT
.getClient().getService(BrowserNavigation
.class);
94 if (browserNavigation
!= null)
95 browserNavigation
.addBrowserNavigationListener(this);
98 protected void createContents(Composite parent
) {
99 Subject
.doAs(loginContext
.getSubject(), new PrivilegedAction
<Void
>() {
103 uxContext
= new SimpleUxContext();
104 imageManager
= new DefaultImageManager();
105 ui
= cmsWebApp
.getCmsApp().initUi(parent
);
106 ui
.setData(CmsApp
.UI_NAME_PROPERTY
, uiName
);
107 ui
.setLayoutData(CmsUiUtils
.fillAll());
108 } catch (Exception e
) {
109 throw new IllegalStateException("Cannot create entrypoint contents", e
);
116 protected Subject
getSubject() {
117 return loginContext
.getSubject();
120 public <T
> T
doAs(PrivilegedAction
<T
> action
) {
121 return Subject
.doAs(getSubject(), action
);
125 public boolean isAnonymous() {
126 return CurrentUser
.isAnonymous(getSubject());
130 public synchronized void logout() {
131 if (loginContext
== null)
132 throw new IllegalArgumentException("Login context should not be null");
134 CurrentUser
.logoutCmsSession(loginContext
.getSubject());
135 loginContext
.logout();
136 LoginContext anonymousLc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_ANONYMOUS
);
138 authChange(anonymousLc
);
139 } catch (LoginException e
) {
140 log
.error("Cannot logout", e
);
145 public synchronized void authChange(LoginContext lc
) {
147 throw new IllegalArgumentException("Login context cannot be null");
148 // logout previous login context
149 if (this.loginContext
!= null)
151 this.loginContext
.logout();
152 } catch (LoginException e1
) {
153 log
.warn("Could not log out: " + e1
);
155 this.loginContext
= lc
;
160 public void exception(final Throwable e
) {
161 ui
.getDisplay().syncExec(() -> {
162 CmsFeedback
.show("Unexpected exception in CMS", e
);
164 // log.error("Unexpected exception in CMS", e);
169 protected synchronized void doRefresh() {
171 Subject
.doAs(getSubject(), new PrivilegedAction
<Void
>() {
174 if (exception
!= null) {
175 // TODO internationalise
176 CmsFeedback
.show("Unexpected exception", exception
);
180 cmsWebApp
.getCmsApp().refreshUi(ui
, state
);
186 /** Sets the state of the entry point and retrieve the related JCR node. */
187 protected String
setState(String newState
) {
188 cmsWebApp
.getCmsApp().setState(ui
, newState
);
194 public UxContext
getUxContext() {
199 public String
getUid() {
204 public void navigateTo(String state
) {
206 String title
= setState(state
);
208 if (browserNavigation
!= null)
209 browserNavigation
.pushState(state
, title
);
213 public CmsImageManager
getImageManager() {
218 public void navigated(BrowserNavigationEvent event
) {
219 setState(event
.getState());
224 public void sendEvent(String topic
, Map
<String
, Object
> properties
) {
225 if (properties
== null)
226 properties
= new HashMap
<>();
227 if (properties
.containsKey(CMS_VIEW_UID_PROPERTY
) && !properties
.get(CMS_VIEW_UID_PROPERTY
).equals(uid
))
228 throw new IllegalArgumentException("Property " + CMS_VIEW_UID_PROPERTY
+ " is set to another CMS view uid ("
229 + properties
.get(CMS_VIEW_UID_PROPERTY
) + ") then " + uid
);
230 properties
.put(CMS_VIEW_UID_PROPERTY
, uid
);
231 eventAdmin
.sendEvent(new Event(topic
, properties
));
235 * EntryPoint IMPLEMENTATION
239 public int createUI() {
240 Display display
= new Display();
241 Shell shell
= createShell(display
);
242 shell
.setLayout(CmsUiUtils
.noSpaceGridLayout());
243 CmsView
.registerCmsView(shell
, this);
244 createContents(shell
);
246 // if (shell.getMaximized()) {
252 if (getApplicationContext().getLifeCycleFactory().getLifeCycle() instanceof RWTLifeCycle
) {
253 while (!shell
.isDisposed()) {
254 if (!display
.readAndDispatch()) {
263 protected Shell
createShell(Display display
) {
265 if (!multipleShells
) {
266 shell
= new Shell(display
, SWT
.NO_TRIM
);
267 shell
.setMaximized(true);
269 shell
= new Shell(display
, SWT
.SHELL_TRIM
);
270 shell
.setSize(800, 600);
275 public void setEventAdmin(EventAdmin eventAdmin
) {
276 this.eventAdmin
= eventAdmin
;