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
.eclipse
.ui
.specific
.UiContext
;
25 import org
.argeo
.jcr
.JcrUtils
;
26 import org
.argeo
.node
.NodeConstants
;
27 import org
.eclipse
.rap
.rwt
.RWT
;
28 import org
.eclipse
.rap
.rwt
.application
.AbstractEntryPoint
;
29 import org
.eclipse
.rap
.rwt
.client
.WebClient
;
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
.client
.service
.JavaScriptExecutor
;
34 import org
.eclipse
.swt
.widgets
.Composite
;
35 import org
.eclipse
.swt
.widgets
.Display
;
36 import org
.eclipse
.swt
.widgets
.Shell
;
38 /** Manages history and navigation */
39 public abstract class AbstractCmsEntryPoint
extends AbstractEntryPoint
implements CmsView
{
40 private static final long serialVersionUID
= 906558779562569784L;
42 private final Log log
= LogFactory
.getLog(AbstractCmsEntryPoint
.class);
44 // private final Subject subject;
45 private LoginContext loginContext
;
47 private final Repository repository
;
48 private final String workspace
;
49 private final String defaultPath
;
50 private final Map
<String
, String
> factoryProperties
;
53 private Session session
;
55 private String nodePath
;// useful when changing auth
58 private Throwable exception
;
61 private final JavaScriptExecutor jsExecutor
;
62 private final BrowserNavigation browserNavigation
;
64 public AbstractCmsEntryPoint(Repository repository
, String workspace
, String defaultPath
,
65 Map
<String
, String
> factoryProperties
) {
66 this.repository
= repository
;
67 this.workspace
= workspace
;
68 this.defaultPath
= defaultPath
;
69 this.factoryProperties
= new HashMap
<String
, String
>(factoryProperties
);
70 // subject = new Subject();
75 lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_USER
,
76 new HttpRequestCallbackHandler(UiContext
.getHttpRequest(), UiContext
.getHttpResponse()));
78 } catch (LoginException e
) {
80 lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_ANONYMOUS
);
82 } catch (LoginException e1
) {
83 throw new CmsException("Cannot log in as anonymous", e1
);
88 jsExecutor
= RWT
.getClient().getService(JavaScriptExecutor
.class);
89 browserNavigation
= RWT
.getClient().getService(BrowserNavigation
.class);
90 if (browserNavigation
!= null)
91 browserNavigation
.addBrowserNavigationListener(new CmsNavigationListener());
95 protected Shell
createShell(Display display
) {
96 Shell shell
= super.createShell(display
);
97 shell
.setData(RWT
.CUSTOM_VARIANT
, CmsStyles
.CMS_SHELL
);
98 display
.disposeExec(new Runnable() {
102 if (log
.isTraceEnabled())
103 log
.trace("Logging out " + session
);
104 JcrUtils
.logoutQuietly(session
);
111 protected final void createContents(final Composite parent
) {
112 UiContext
.setData(CmsView
.KEY
, this);
113 Subject
.doAs(getSubject(), new PrivilegedAction
<Void
>() {
118 } catch (Exception e
) {
119 throw new CmsException("Cannot create entrypoint contents", e
);
127 protected abstract void initUi(Composite parent
);
129 /** Recreate UI after navigation or auth change */
130 protected abstract void refresh();
133 * The node to return when no node was found (for authenticated users and
136 protected Node
getDefaultNode(Session session
) throws RepositoryException
{
137 if (!session
.hasPermission(defaultPath
, "read")) {
138 String userId
= session
.getUserID();
139 if (userId
.equals(NodeConstants
.ROLE_ANONYMOUS
))
140 // TODO throw a special exception
141 throw new CmsException("Login required");
143 throw new CmsException("Unauthorized");
145 return session
.getNode(defaultPath
);
148 protected String
getBaseTitle() {
149 return factoryProperties
.get(WebClient
.PAGE_TITLE
);
152 public void navigateTo(String state
) {
154 String title
= setState(state
);
156 if (browserNavigation
!= null)
157 browserNavigation
.pushState(state
, title
);
161 // public synchronized Subject getSubject() {
166 // public LoginContext getLoginContext() {
167 // return loginContext;
169 protected Subject
getSubject() {
170 return loginContext
.getSubject();
174 public boolean isAnonymous() {
175 return CurrentUser
.isAnonymous(getSubject());
179 public synchronized void logout() {
180 if (loginContext
== null)
181 throw new CmsException("Login context should not be null");
183 CurrentUser
.logoutCmsSession(loginContext
.getSubject());
184 loginContext
.logout();
185 LoginContext anonymousLc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_ANONYMOUS
);
187 authChange(anonymousLc
);
188 } catch (LoginException e
) {
189 log
.error("Cannot logout", e
);
194 public synchronized void authChange(LoginContext lc
) {
196 throw new CmsException("Login context cannot be null");
197 // logout previous login context
198 if (this.loginContext
!= null)
200 this.loginContext
.logout();
201 } catch (LoginException e1
) {
202 log
.warn("Could not log out: " + e1
);
204 this.loginContext
= lc
;
205 Subject
.doAs(getSubject(), new PrivilegedAction
<Void
>() {
210 JcrUtils
.logoutQuietly(session
);
211 session
= repository
.login(workspace
);
212 if (nodePath
!= null)
214 node
= session
.getNode(nodePath
);
215 } catch (PathNotFoundException e
) {
221 } catch (RepositoryException e
) {
222 throw new CmsException("Cannot perform auth change", e
);
231 public void exception(final Throwable e
) {
232 AbstractCmsEntryPoint
.this.exception
= e
;
233 log
.error("Unexpected exception in CMS", e
);
237 protected synchronized void doRefresh() {
238 Subject
.doAs(getSubject(), new PrivilegedAction
<Void
>() {
247 /** Sets the state of the entry point and retrieve the related JCR node. */
248 protected synchronized String
setState(String newState
) {
249 String previousState
= this.state
;
253 this.state
= newState
;
254 if (newState
.equals("~"))
258 int firstSlash
= state
.indexOf('/');
259 if (firstSlash
== 0) {
260 node
= session
.getNode(state
);
262 } else if (firstSlash
> 0) {
263 String prefix
= state
.substring(0, firstSlash
);
264 String path
= state
.substring(firstSlash
);
265 if (session
.nodeExists(path
))
266 node
= session
.getNode(path
);
268 throw new CmsException("Data " + path
+ " does not exist");
271 node
= getDefaultNode(session
);
275 String title
= publishMetaData(node
);
277 if (log
.isTraceEnabled())
278 log
.trace("node=" + node
+ ", state=" + state
+ " (page=" + page
+ ")");
281 } catch (Exception e
) {
282 log
.error("Cannot set state '" + state
+ "'", e
);
283 if (state
.equals("") || newState
.equals("~") || newState
.equals(previousState
))
284 return "Unrecoverable exception : " + e
.getClass().getSimpleName();
285 if (previousState
.equals(""))
287 navigateTo(previousState
);
288 throw new CmsException("Unexpected issue when accessing #" + newState
, e
);
292 private String
publishMetaData(Node node
) throws RepositoryException
{
295 if (node
.isNodeType(NodeType
.MIX_TITLE
) && node
.hasProperty(Property
.JCR_TITLE
))
296 title
= node
.getProperty(Property
.JCR_TITLE
).getString() + " - " + getBaseTitle();
298 title
= getBaseTitle();
300 HttpServletRequest request
= UiContext
.getHttpRequest();
304 StringBuilder js
= new StringBuilder();
305 title
= title
.replace("'", "\\'");// sanitize
306 js
.append("document.title = '" + title
+ "';");
307 jsExecutor
.execute(js
.toString());
311 // Simply remove some illegal character
312 // private String clean(String stringToClean) {
313 // return stringToClean.replaceAll("'", "").replaceAll("\\n", "")
314 // .replaceAll("\\t", "");
317 protected synchronized Node
getNode() {
321 private synchronized void setNode(Node node
) throws RepositoryException
{
323 this.nodePath
= node
== null ?
null : node
.getPath();
326 protected String
getState() {
330 protected Throwable
getException() {
334 protected void resetException() {
338 protected Session
getSession() {
342 private class CmsNavigationListener
implements BrowserNavigationListener
{
343 private static final long serialVersionUID
= -3591018803430389270L;
346 public void navigated(BrowserNavigationEvent event
) {
347 setState(event
.getState());