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
.apache
.commons
.logging
.Log
;
16 import org
.apache
.commons
.logging
.LogFactory
;
17 import org
.argeo
.api
.NodeConstants
;
18 import org
.argeo
.api
.cms
.CmsApp
;
19 import org
.argeo
.api
.cms
.CmsImageManager
;
20 import org
.argeo
.api
.cms
.CmsSession
;
21 import org
.argeo
.api
.cms
.CmsUi
;
22 import org
.argeo
.api
.cms
.CmsView
;
23 import org
.argeo
.api
.cms
.UxContext
;
24 import org
.argeo
.cms
.LocaleUtils
;
25 import org
.argeo
.cms
.auth
.CurrentUser
;
26 import org
.argeo
.cms
.auth
.HttpRequestCallbackHandler
;
27 import org
.argeo
.cms
.osgi
.CmsOsgiUtils
;
28 import org
.argeo
.cms
.swt
.CmsSwtUtils
;
29 import org
.argeo
.cms
.swt
.SimpleSwtUxContext
;
30 import org
.argeo
.cms
.swt
.dialogs
.CmsFeedback
;
31 import org
.argeo
.cms
.ui
.util
.DefaultImageManager
;
32 import org
.argeo
.eclipse
.ui
.specific
.UiContext
;
33 import org
.eclipse
.rap
.rwt
.RWT
;
34 import org
.eclipse
.rap
.rwt
.application
.EntryPoint
;
35 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigation
;
36 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigationEvent
;
37 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigationListener
;
38 import org
.eclipse
.rap
.rwt
.internal
.lifecycle
.RWTLifeCycle
;
39 import org
.eclipse
.swt
.SWT
;
40 import org
.eclipse
.swt
.SWTError
;
41 import org
.eclipse
.swt
.widgets
.Composite
;
42 import org
.eclipse
.swt
.widgets
.Display
;
43 import org
.eclipse
.swt
.widgets
.Shell
;
44 import org
.osgi
.service
.event
.Event
;
45 import org
.osgi
.service
.event
.EventAdmin
;
47 /** The {@link CmsView} for a {@link CmsWebApp}. */
48 @SuppressWarnings("restriction")
49 public class CmsWebEntryPoint
implements EntryPoint
, CmsView
, BrowserNavigationListener
{
50 private static final long serialVersionUID
= 7733510691684570402L;
51 private final static Log log
= LogFactory
.getLog(CmsWebEntryPoint
.class);
53 private EventAdmin eventAdmin
;
55 private final CmsWebApp cmsWebApp
;
56 private final String uiName
;
58 private LoginContext loginContext
;
60 private Throwable exception
;
61 private UxContext uxContext
;
62 private CmsImageManager imageManager
;
64 private Display display
;
70 // private final JavaScriptExecutor jsExecutor;
71 private final BrowserNavigation browserNavigation
;
73 /** Experimental OS-like multi windows. */
74 private boolean multipleShells
= false;
76 public CmsWebEntryPoint(CmsWebApp cmsWebApp
, String uiName
) {
77 assert cmsWebApp
!= null;
78 assert uiName
!= null;
79 this.cmsWebApp
= cmsWebApp
;
81 uid
= UUID
.randomUUID().toString();
86 lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_USER
,
87 new HttpRequestCallbackHandler(UiContext
.getHttpRequest(), UiContext
.getHttpResponse()));
89 } catch (LoginException e
) {
91 lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_ANONYMOUS
);
93 } catch (LoginException e1
) {
94 throw new IllegalStateException("Cannot log in as anonymous", e1
);
99 // jsExecutor = RWT.getClient().getService(JavaScriptExecutor.class);
100 browserNavigation
= RWT
.getClient().getService(BrowserNavigation
.class);
101 if (browserNavigation
!= null)
102 browserNavigation
.addBrowserNavigationListener(this);
105 protected void createContents(Composite parent
) {
106 Subject
.doAs(loginContext
.getSubject(), new PrivilegedAction
<Void
>() {
110 uxContext
= new SimpleSwtUxContext();
111 imageManager
= new DefaultImageManager();
112 CmsSession cmsSession
= getCmsSession();
113 if (cmsSession
!= null) {
114 UiContext
.setLocale(cmsSession
.getLocale());
115 LocaleUtils
.setThreadLocale(cmsSession
.getLocale());
117 Locale rwtLocale
= RWT
.getUISession().getLocale();
118 LocaleUtils
.setThreadLocale(rwtLocale
);
120 parent
.setData(CmsApp
.UI_NAME_PROPERTY
, uiName
);
121 display
= parent
.getDisplay();
122 ui
= cmsWebApp
.getCmsApp().initUi(parent
);
123 if (ui
instanceof Composite
)
124 ((Composite
) ui
).setLayoutData(CmsSwtUtils
.fillAll());
125 // we need ui to be set before refresh so that CmsView can store UI context data
127 cmsWebApp
.getCmsApp().refreshUi(ui
, null);
128 } catch (Exception e
) {
129 throw new IllegalStateException("Cannot create entrypoint contents", e
);
136 protected Subject
getSubject() {
137 return loginContext
.getSubject();
140 public <T
> T
doAs(PrivilegedAction
<T
> action
) {
141 return Subject
.doAs(getSubject(), action
);
145 public boolean isAnonymous() {
146 return CurrentUser
.isAnonymous(getSubject());
150 public synchronized void logout() {
151 if (loginContext
== null)
152 throw new IllegalArgumentException("Login context should not be null");
154 CurrentUser
.logoutCmsSession(loginContext
.getSubject());
155 loginContext
.logout();
156 LoginContext anonymousLc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_ANONYMOUS
);
158 authChange(anonymousLc
);
159 } catch (LoginException e
) {
160 log
.error("Cannot logout", e
);
165 public synchronized void authChange(LoginContext lc
) {
167 throw new IllegalArgumentException("Login context cannot be null");
168 // logout previous login context
169 if (this.loginContext
!= null)
171 this.loginContext
.logout();
172 } catch (LoginException e1
) {
173 log
.warn("Could not log out: " + e1
);
175 this.loginContext
= lc
;
180 public void exception(final Throwable e
) {
181 if (e
instanceof SWTError
) {
182 SWTError swtError
= (SWTError
) e
;
183 if (swtError
.code
== SWT
.ERROR_FUNCTION_DISPOSED
)
186 display
.syncExec(() -> {
187 // CmsFeedback.show("Unexpected exception in CMS", e);
189 // log.error("Unexpected exception in CMS", e);
194 protected synchronized void doRefresh() {
196 Subject
.doAs(getSubject(), new PrivilegedAction
<Void
>() {
199 if (exception
!= null) {
200 // TODO internationalise
201 CmsFeedback
.show("Unexpected exception", exception
);
205 cmsWebApp
.getCmsApp().refreshUi(ui
, state
);
211 /** Sets the state of the entry point and retrieve the related JCR node. */
212 protected String
setState(String newState
) {
213 cmsWebApp
.getCmsApp().setState(ui
, newState
);
219 public UxContext
getUxContext() {
224 public String
getUid() {
229 public void navigateTo(String state
) {
231 String title
= setState(state
);
234 if (browserNavigation
!= null)
235 browserNavigation
.pushState(state
, title
);
238 public CmsImageManager
getImageManager() {
243 public void navigated(BrowserNavigationEvent event
) {
244 setState(event
.getState());
249 public void sendEvent(String topic
, Map
<String
, Object
> properties
) {
250 if (properties
== null)
251 properties
= new HashMap
<>();
252 if (properties
.containsKey(CMS_VIEW_UID_PROPERTY
) && !properties
.get(CMS_VIEW_UID_PROPERTY
).equals(uid
))
253 throw new IllegalArgumentException("Property " + CMS_VIEW_UID_PROPERTY
+ " is set to another CMS view uid ("
254 + properties
.get(CMS_VIEW_UID_PROPERTY
) + ") then " + uid
);
255 properties
.put(CMS_VIEW_UID_PROPERTY
, uid
);
256 eventAdmin
.sendEvent(new Event(topic
, properties
));
260 public void stateChanged(String state
, String title
) {
261 browserNavigation
.pushState(state
, title
);
265 public CmsSession
getCmsSession() {
266 CmsSession cmsSession
= CmsOsgiUtils
.getCmsSession(cmsWebApp
.getBundleContext(), getSubject());
271 public Object
getData(String key
) {
273 return ui
.getData(key
);
275 throw new IllegalStateException("UI is not initialized");
280 public void setData(String key
, Object value
) {
282 ui
.setData(key
, value
);
284 throw new IllegalStateException("UI is not initialized");
289 * EntryPoint IMPLEMENTATION
293 public int createUI() {
294 Display display
= new Display();
295 Shell shell
= createShell(display
);
296 shell
.setLayout(CmsSwtUtils
.noSpaceGridLayout());
297 CmsSwtUtils
.registerCmsView(shell
, this);
298 createContents(shell
);
300 // if (shell.getMaximized()) {
306 if (getApplicationContext().getLifeCycleFactory().getLifeCycle() instanceof RWTLifeCycle
) {
307 eventLoop
: while (!shell
.isDisposed()) {
309 if (!display
.readAndDispatch()) {
312 } catch (Throwable e
) {
313 if (e
instanceof SWTError
) {
314 SWTError swtError
= (SWTError
) e
;
315 if (swtError
.code
== SWT
.ERROR_FUNCTION_DISPOSED
) {
316 log
.error("Unexpected SWT error in event loop, ignoring it. " + e
.getMessage());
319 log
.error("Unexpected SWT error in event loop, shutting down...", e
);
322 } else if (e
instanceof ThreadDeath
) {
323 throw (ThreadDeath
) e
;
324 } else if (e
instanceof Error
) {
325 log
.error("Unexpected error in event loop, shutting down...", e
);
328 log
.error("Unexpected exception in event loop, ignoring it. " + e
.getMessage());
333 if (!display
.isDisposed())
339 protected Shell
createShell(Display display
) {
341 if (!multipleShells
) {
342 shell
= new Shell(display
, SWT
.NO_TRIM
);
343 shell
.setMaximized(true);
345 shell
= new Shell(display
, SWT
.SHELL_TRIM
);
346 shell
.setSize(800, 600);
351 public void setEventAdmin(EventAdmin eventAdmin
) {
352 this.eventAdmin
= eventAdmin
;