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
;
7 import java
.util
.Locale
;
11 import javax
.security
.auth
.Subject
;
12 import javax
.security
.auth
.login
.LoginContext
;
13 import javax
.security
.auth
.login
.LoginException
;
15 import org
.argeo
.api
.cms
.CmsApp
;
16 import org
.argeo
.api
.cms
.CmsAuth
;
17 import org
.argeo
.api
.cms
.CmsImageManager
;
18 import org
.argeo
.api
.cms
.CmsSession
;
19 import org
.argeo
.api
.cms
.CmsUi
;
20 import org
.argeo
.api
.cms
.CmsView
;
21 import org
.argeo
.api
.cms
.CmsLog
;
22 import org
.argeo
.api
.cms
.UxContext
;
23 import org
.argeo
.cms
.LocaleUtils
;
24 import org
.argeo
.cms
.auth
.CurrentUser
;
25 import org
.argeo
.cms
.auth
.RemoteAuthCallbackHandler
;
26 import org
.argeo
.cms
.osgi
.CmsOsgiUtils
;
27 import org
.argeo
.cms
.servlet
.ServletHttpRequest
;
28 import org
.argeo
.cms
.servlet
.ServletHttpResponse
;
29 import org
.argeo
.cms
.swt
.CmsSwtUtils
;
30 import org
.argeo
.cms
.swt
.SimpleSwtUxContext
;
31 import org
.argeo
.cms
.swt
.dialogs
.CmsFeedback
;
32 import org
.argeo
.cms
.ui
.util
.DefaultImageManager
;
33 import org
.argeo
.eclipse
.ui
.specific
.UiContext
;
34 import org
.eclipse
.rap
.rwt
.RWT
;
35 import org
.eclipse
.rap
.rwt
.application
.EntryPoint
;
36 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigation
;
37 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigationEvent
;
38 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigationListener
;
39 import org
.eclipse
.rap
.rwt
.internal
.lifecycle
.RWTLifeCycle
;
40 import org
.eclipse
.swt
.SWT
;
41 import org
.eclipse
.swt
.SWTError
;
42 import org
.eclipse
.swt
.widgets
.Composite
;
43 import org
.eclipse
.swt
.widgets
.Display
;
44 import org
.eclipse
.swt
.widgets
.Shell
;
45 import org
.osgi
.service
.event
.Event
;
46 import org
.osgi
.service
.event
.EventAdmin
;
48 /** The {@link CmsView} for a {@link CmsWebApp}. */
49 @SuppressWarnings("restriction")
50 public class CmsWebEntryPoint
implements EntryPoint
, CmsView
, BrowserNavigationListener
{
51 private static final long serialVersionUID
= 7733510691684570402L;
52 private final static CmsLog log
= CmsLog
.getLog(CmsWebEntryPoint
.class);
54 private EventAdmin eventAdmin
;
56 private final CmsWebApp cmsWebApp
;
57 private final String uiName
;
59 private LoginContext loginContext
;
61 private Throwable exception
;
62 private UxContext uxContext
;
63 private CmsImageManager imageManager
;
65 private Display display
;
71 // private final JavaScriptExecutor jsExecutor;
72 private final BrowserNavigation browserNavigation
;
74 /** Experimental OS-like multi windows. */
75 private boolean multipleShells
= false;
77 public CmsWebEntryPoint(CmsWebApp cmsWebApp
, String uiName
) {
78 assert cmsWebApp
!= null;
79 assert uiName
!= null;
80 this.cmsWebApp
= cmsWebApp
;
82 uid
= UUID
.randomUUID().toString();
87 lc
= new LoginContext(CmsAuth
.LOGIN_CONTEXT_USER
,
88 new RemoteAuthCallbackHandler(new ServletHttpRequest(UiContext
.getHttpRequest()),
89 new ServletHttpResponse(UiContext
.getHttpResponse())));
91 } catch (LoginException e
) {
93 lc
= new LoginContext(CmsAuth
.LOGIN_CONTEXT_ANONYMOUS
);
95 } catch (LoginException e1
) {
96 throw new IllegalStateException("Cannot log in as anonymous", e1
);
101 // jsExecutor = RWT.getClient().getService(JavaScriptExecutor.class);
102 browserNavigation
= RWT
.getClient().getService(BrowserNavigation
.class);
103 if (browserNavigation
!= null)
104 browserNavigation
.addBrowserNavigationListener(this);
107 protected void createContents(Composite parent
) {
108 Subject
.doAs(loginContext
.getSubject(), new PrivilegedAction
<Void
>() {
112 uxContext
= new SimpleSwtUxContext();
113 imageManager
= new DefaultImageManager();
114 CmsSession cmsSession
= getCmsSession();
115 if (cmsSession
!= null) {
116 UiContext
.setLocale(cmsSession
.getLocale());
117 LocaleUtils
.setThreadLocale(cmsSession
.getLocale());
119 Locale rwtLocale
= RWT
.getUISession().getLocale();
120 LocaleUtils
.setThreadLocale(rwtLocale
);
122 parent
.setData(CmsApp
.UI_NAME_PROPERTY
, uiName
);
123 display
= parent
.getDisplay();
124 ui
= cmsWebApp
.getCmsApp().initUi(parent
);
125 if (ui
instanceof Composite
)
126 ((Composite
) ui
).setLayoutData(CmsSwtUtils
.fillAll());
127 // we need ui to be set before refresh so that CmsView can store UI context data
129 cmsWebApp
.getCmsApp().refreshUi(ui
, null);
130 } catch (Exception e
) {
131 throw new IllegalStateException("Cannot create entrypoint contents", e
);
138 protected Subject
getSubject() {
139 return loginContext
.getSubject();
142 public <T
> T
doAs(PrivilegedAction
<T
> action
) {
143 return Subject
.doAs(getSubject(), action
);
147 public boolean isAnonymous() {
148 return CurrentUser
.isAnonymous(getSubject());
152 public synchronized void logout() {
153 if (loginContext
== null)
154 throw new IllegalArgumentException("Login context should not be null");
156 CurrentUser
.logoutCmsSession(loginContext
.getSubject());
157 loginContext
.logout();
158 LoginContext anonymousLc
= new LoginContext(CmsAuth
.LOGIN_CONTEXT_ANONYMOUS
);
160 authChange(anonymousLc
);
161 } catch (LoginException e
) {
162 log
.error("Cannot logout", e
);
167 public synchronized void authChange(LoginContext lc
) {
169 throw new IllegalArgumentException("Login context cannot be null");
170 // logout previous login context
171 if (this.loginContext
!= null)
173 this.loginContext
.logout();
174 } catch (LoginException e1
) {
175 log
.warn("Could not log out: " + e1
);
177 this.loginContext
= lc
;
182 public void exception(final Throwable e
) {
183 if (e
instanceof SWTError
) {
184 SWTError swtError
= (SWTError
) e
;
185 if (swtError
.code
== SWT
.ERROR_FUNCTION_DISPOSED
)
188 display
.syncExec(() -> {
189 // CmsFeedback.show("Unexpected exception in CMS", e);
191 // log.error("Unexpected exception in CMS", e);
196 protected synchronized void doRefresh() {
198 Subject
.doAs(getSubject(), new PrivilegedAction
<Void
>() {
201 if (exception
!= null) {
202 // TODO internationalise
203 CmsFeedback
.show("Unexpected exception", exception
);
207 cmsWebApp
.getCmsApp().refreshUi(ui
, state
);
213 /** Sets the state of the entry point and retrieve the related JCR node. */
214 protected String
setState(String newState
) {
215 cmsWebApp
.getCmsApp().setState(ui
, newState
);
221 public UxContext
getUxContext() {
226 public String
getUid() {
231 public void navigateTo(String state
) {
233 String title
= setState(state
);
236 if (browserNavigation
!= null)
237 browserNavigation
.pushState(state
, title
);
240 public CmsImageManager
getImageManager() {
245 public void navigated(BrowserNavigationEvent event
) {
246 setState(event
.getState());
251 public void sendEvent(String topic
, Map
<String
, Object
> properties
) {
252 if (properties
== null)
253 properties
= new HashMap
<>();
254 if (properties
.containsKey(CMS_VIEW_UID_PROPERTY
) && !properties
.get(CMS_VIEW_UID_PROPERTY
).equals(uid
))
255 throw new IllegalArgumentException("Property " + CMS_VIEW_UID_PROPERTY
+ " is set to another CMS view uid ("
256 + properties
.get(CMS_VIEW_UID_PROPERTY
) + ") then " + uid
);
257 properties
.put(CMS_VIEW_UID_PROPERTY
, uid
);
258 eventAdmin
.sendEvent(new Event(topic
, properties
));
262 public void stateChanged(String state
, String title
) {
263 browserNavigation
.pushState(state
, title
);
267 public CmsSession
getCmsSession() {
268 CmsSession cmsSession
= CmsOsgiUtils
.getCmsSession(cmsWebApp
.getBundleContext(), getSubject());
273 public Object
getData(String key
) {
275 return ui
.getData(key
);
277 throw new IllegalStateException("UI is not initialized");
282 public void setData(String key
, Object value
) {
284 ui
.setData(key
, value
);
286 throw new IllegalStateException("UI is not initialized");
291 * EntryPoint IMPLEMENTATION
295 public int createUI() {
296 Display display
= new Display();
297 Shell shell
= createShell(display
);
298 shell
.setLayout(CmsSwtUtils
.noSpaceGridLayout());
299 CmsSwtUtils
.registerCmsView(shell
, this);
300 createContents(shell
);
302 // if (shell.getMaximized()) {
308 if (getApplicationContext().getLifeCycleFactory().getLifeCycle() instanceof RWTLifeCycle
) {
309 eventLoop
: while (!shell
.isDisposed()) {
311 if (!display
.readAndDispatch()) {
314 } catch (Throwable e
) {
315 if (e
instanceof SWTError
) {
316 SWTError swtError
= (SWTError
) e
;
317 if (swtError
.code
== SWT
.ERROR_FUNCTION_DISPOSED
) {
318 log
.error("Unexpected SWT error in event loop, ignoring it. " + e
.getMessage());
321 log
.error("Unexpected SWT error in event loop, shutting down...", e
);
324 } else if (e
instanceof ThreadDeath
) {
325 throw (ThreadDeath
) e
;
326 } else if (e
instanceof Error
) {
327 log
.error("Unexpected error in event loop, shutting down...", e
);
330 log
.error("Unexpected exception in event loop, ignoring it. " + e
.getMessage());
335 if (!display
.isDisposed())
341 protected Shell
createShell(Display display
) {
343 if (!multipleShells
) {
344 shell
= new Shell(display
, SWT
.NO_TRIM
);
345 shell
.setMaximized(true);
347 shell
= new Shell(display
, SWT
.SHELL_TRIM
);
348 shell
.setSize(800, 600);
353 public void setEventAdmin(EventAdmin eventAdmin
) {
354 this.eventAdmin
= eventAdmin
;