1 package org
.argeo
.cms
.ui
;
3 import java
.security
.PrivilegedAction
;
4 import java
.util
.HashMap
;
8 import javax
.jcr
.PathNotFoundException
;
9 import javax
.jcr
.Property
;
10 import javax
.jcr
.Repository
;
11 import javax
.jcr
.RepositoryException
;
12 import javax
.jcr
.Session
;
13 import javax
.jcr
.nodetype
.NodeType
;
14 import javax
.security
.auth
.Subject
;
15 import javax
.security
.auth
.login
.LoginContext
;
16 import javax
.security
.auth
.login
.LoginException
;
17 import javax
.servlet
.http
.HttpServletRequest
;
19 import org
.apache
.commons
.logging
.Log
;
20 import org
.apache
.commons
.logging
.LogFactory
;
21 import org
.argeo
.cms
.CmsException
;
22 import org
.argeo
.cms
.auth
.CurrentUser
;
23 import org
.argeo
.cms
.auth
.HttpRequestCallbackHandler
;
24 import org
.argeo
.cms
.auth
.CmsAuthenticated
;
25 import org
.argeo
.eclipse
.ui
.specific
.UiContext
;
26 import org
.argeo
.jcr
.JcrUtils
;
27 import org
.argeo
.node
.NodeConstants
;
28 import org
.eclipse
.rap
.rwt
.RWT
;
29 import org
.eclipse
.rap
.rwt
.application
.AbstractEntryPoint
;
30 import org
.eclipse
.rap
.rwt
.client
.WebClient
;
31 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigation
;
32 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigationEvent
;
33 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigationListener
;
34 import org
.eclipse
.rap
.rwt
.client
.service
.JavaScriptExecutor
;
35 import org
.eclipse
.swt
.widgets
.Composite
;
36 import org
.eclipse
.swt
.widgets
.Display
;
37 import org
.eclipse
.swt
.widgets
.Shell
;
39 /** Manages history and navigation */
40 public abstract class AbstractCmsEntryPoint
extends AbstractEntryPoint
implements CmsView
{
41 private static final long serialVersionUID
= 906558779562569784L;
43 private final Log log
= LogFactory
.getLog(AbstractCmsEntryPoint
.class);
45 // private final Subject subject;
46 private LoginContext loginContext
;
48 private final Repository repository
;
49 private final String workspace
;
50 private final String defaultPath
;
51 private final Map
<String
, String
> factoryProperties
;
54 private Session session
;
56 private String nodePath
;// useful when changing auth
59 private Throwable exception
;
62 private final JavaScriptExecutor jsExecutor
;
63 private final BrowserNavigation browserNavigation
;
65 public AbstractCmsEntryPoint(Repository repository
, String workspace
, String defaultPath
,
66 Map
<String
, String
> factoryProperties
) {
67 this.repository
= repository
;
68 this.workspace
= workspace
;
69 this.defaultPath
= defaultPath
;
70 this.factoryProperties
= new HashMap
<String
, String
>(factoryProperties
);
71 // subject = new Subject();
76 lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_USER
,
77 new HttpRequestCallbackHandler(UiContext
.getHttpRequest(), UiContext
.getHttpResponse()));
79 } catch (LoginException e
) {
81 lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_ANONYMOUS
);
83 } catch (LoginException e1
) {
84 throw new CmsException("Cannot log in as anonymous", e1
);
89 jsExecutor
= RWT
.getClient().getService(JavaScriptExecutor
.class);
90 browserNavigation
= RWT
.getClient().getService(BrowserNavigation
.class);
91 if (browserNavigation
!= null)
92 browserNavigation
.addBrowserNavigationListener(new CmsNavigationListener());
96 protected Shell
createShell(Display display
) {
97 Shell shell
= super.createShell(display
);
98 shell
.setData(RWT
.CUSTOM_VARIANT
, CmsStyles
.CMS_SHELL
);
99 display
.disposeExec(new Runnable() {
103 if (log
.isTraceEnabled())
104 log
.trace("Logging out " + session
);
105 JcrUtils
.logoutQuietly(session
);
112 protected final void createContents(final Composite parent
) {
113 UiContext
.setData(CmsAuthenticated
.KEY
, this);
114 Subject
.doAs(getSubject(), new PrivilegedAction
<Void
>() {
119 } catch (Exception e
) {
120 throw new CmsException("Cannot create entrypoint contents", e
);
128 protected abstract void initUi(Composite parent
);
130 /** Recreate UI after navigation or auth change */
131 protected abstract void refresh();
134 * The node to return when no node was found (for authenticated users and
137 protected Node
getDefaultNode(Session session
) throws RepositoryException
{
138 if (!session
.hasPermission(defaultPath
, "read")) {
139 String userId
= session
.getUserID();
140 if (userId
.equals(NodeConstants
.ROLE_ANONYMOUS
))
141 // TODO throw a special exception
142 throw new CmsException("Login required");
144 throw new CmsException("Unauthorized");
146 return session
.getNode(defaultPath
);
149 protected String
getBaseTitle() {
150 return factoryProperties
.get(WebClient
.PAGE_TITLE
);
153 public void navigateTo(String state
) {
155 String title
= setState(state
);
157 if (browserNavigation
!= null)
158 browserNavigation
.pushState(state
, title
);
162 // public synchronized Subject getSubject() {
167 // public LoginContext getLoginContext() {
168 // return loginContext;
170 public Subject
getSubject() {
171 return loginContext
.getSubject();
175 public synchronized void logout() {
176 if (loginContext
== null)
177 throw new CmsException("Login context should not be null");
179 CurrentUser
.logoutCmsSession(loginContext
.getSubject());
180 loginContext
.logout();
181 LoginContext anonymousLc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_ANONYMOUS
);
183 authChange(anonymousLc
);
184 } catch (LoginException e
) {
185 log
.error("Cannot logout", e
);
190 public synchronized void authChange(LoginContext lc
) {
192 throw new CmsException("Login context cannot be null");
193 // logout previous login context
194 if (this.loginContext
!= null)
196 this.loginContext
.logout();
197 } catch (LoginException e1
) {
198 log
.warn("Could not log out: " + e1
);
200 this.loginContext
= lc
;
201 Subject
.doAs(getSubject(), new PrivilegedAction
<Void
>() {
206 JcrUtils
.logoutQuietly(session
);
207 session
= repository
.login(workspace
);
208 if (nodePath
!= null)
210 node
= session
.getNode(nodePath
);
211 } catch (PathNotFoundException e
) {
217 } catch (RepositoryException e
) {
218 throw new CmsException("Cannot perform auth change", e
);
227 public void exception(final Throwable e
) {
228 AbstractCmsEntryPoint
.this.exception
= e
;
229 log
.error("Unexpected exception in CMS", e
);
233 protected synchronized void doRefresh() {
234 Subject
.doAs(getSubject(), new PrivilegedAction
<Void
>() {
243 /** Sets the state of the entry point and retrieve the related JCR node. */
244 protected synchronized String
setState(String newState
) {
245 String previousState
= this.state
;
249 this.state
= newState
;
250 if (newState
.equals("~"))
254 int firstSlash
= state
.indexOf('/');
255 if (firstSlash
== 0) {
256 node
= session
.getNode(state
);
258 } else if (firstSlash
> 0) {
259 String prefix
= state
.substring(0, firstSlash
);
260 String path
= state
.substring(firstSlash
);
261 if (session
.nodeExists(path
))
262 node
= session
.getNode(path
);
264 throw new CmsException("Data " + path
+ " does not exist");
267 node
= getDefaultNode(session
);
271 String title
= publishMetaData(node
);
273 if (log
.isTraceEnabled())
274 log
.trace("node=" + node
+ ", state=" + state
+ " (page=" + page
+ ")");
277 } catch (Exception e
) {
278 log
.error("Cannot set state '" + state
+ "'", e
);
279 if (state
.equals("") || newState
.equals("~") || newState
.equals(previousState
))
280 return "Unrecoverable exception : " + e
.getClass().getSimpleName();
281 if (previousState
.equals(""))
283 navigateTo(previousState
);
284 throw new CmsException("Unexpected issue when accessing #" + newState
, e
);
288 private String
publishMetaData(Node node
) throws RepositoryException
{
291 if (node
.isNodeType(NodeType
.MIX_TITLE
) && node
.hasProperty(Property
.JCR_TITLE
))
292 title
= node
.getProperty(Property
.JCR_TITLE
).getString() + " - " + getBaseTitle();
294 title
= getBaseTitle();
296 HttpServletRequest request
= UiContext
.getHttpRequest();
300 StringBuilder js
= new StringBuilder();
301 title
= title
.replace("'", "\\'");// sanitize
302 js
.append("document.title = '" + title
+ "';");
303 jsExecutor
.execute(js
.toString());
307 // Simply remove some illegal character
308 // private String clean(String stringToClean) {
309 // return stringToClean.replaceAll("'", "").replaceAll("\\n", "")
310 // .replaceAll("\\t", "");
313 protected synchronized Node
getNode() {
317 private synchronized void setNode(Node node
) throws RepositoryException
{
319 this.nodePath
= node
== null ?
null : node
.getPath();
322 protected String
getState() {
326 protected Throwable
getException() {
330 protected void resetException() {
334 protected Session
getSession() {
338 private class CmsNavigationListener
implements BrowserNavigationListener
{
339 private static final long serialVersionUID
= -3591018803430389270L;
342 public void navigated(BrowserNavigationEvent event
) {
343 setState(event
.getState());