1 package org
.argeo
.cms
.ui
;
3 import static org
.argeo
.naming
.SharedSecret
.X_SHARED_SECRET
;
5 import java
.io
.IOException
;
6 import java
.security
.PrivilegedAction
;
7 import java
.util
.HashMap
;
10 import javax
.jcr
.Node
;
11 import javax
.jcr
.PathNotFoundException
;
12 import javax
.jcr
.Property
;
13 import javax
.jcr
.Repository
;
14 import javax
.jcr
.RepositoryException
;
15 import javax
.jcr
.Session
;
16 import javax
.jcr
.nodetype
.NodeType
;
17 import javax
.security
.auth
.Subject
;
18 import javax
.security
.auth
.callback
.Callback
;
19 import javax
.security
.auth
.callback
.UnsupportedCallbackException
;
20 import javax
.security
.auth
.login
.LoginContext
;
21 import javax
.security
.auth
.login
.LoginException
;
22 import javax
.servlet
.http
.HttpServletRequest
;
24 import org
.apache
.commons
.logging
.Log
;
25 import org
.apache
.commons
.logging
.LogFactory
;
26 import org
.argeo
.cms
.CmsException
;
27 import org
.argeo
.cms
.auth
.CurrentUser
;
28 import org
.argeo
.cms
.auth
.HttpRequestCallback
;
29 import org
.argeo
.cms
.auth
.HttpRequestCallbackHandler
;
30 import org
.argeo
.eclipse
.ui
.specific
.UiContext
;
31 import org
.argeo
.jcr
.JcrUtils
;
32 import org
.argeo
.naming
.AuthPassword
;
33 import org
.argeo
.naming
.SharedSecret
;
34 import org
.argeo
.node
.NodeConstants
;
35 import org
.eclipse
.rap
.rwt
.RWT
;
36 import org
.eclipse
.rap
.rwt
.application
.AbstractEntryPoint
;
37 import org
.eclipse
.rap
.rwt
.client
.WebClient
;
38 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigation
;
39 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigationEvent
;
40 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigationListener
;
41 import org
.eclipse
.rap
.rwt
.client
.service
.JavaScriptExecutor
;
42 import org
.eclipse
.swt
.widgets
.Composite
;
43 import org
.eclipse
.swt
.widgets
.Display
;
44 import org
.eclipse
.swt
.widgets
.Shell
;
46 /** Manages history and navigation */
47 public abstract class AbstractCmsEntryPoint
extends AbstractEntryPoint
implements CmsView
{
48 private static final long serialVersionUID
= 906558779562569784L;
50 private final Log log
= LogFactory
.getLog(AbstractCmsEntryPoint
.class);
52 // private final Subject subject;
53 private LoginContext loginContext
;
55 private final Repository repository
;
56 private final String workspace
;
57 private final String defaultPath
;
58 private final Map
<String
, String
> factoryProperties
;
61 private Session session
;
63 private String nodePath
;// useful when changing auth
65 private Throwable exception
;
68 private final JavaScriptExecutor jsExecutor
;
69 private final BrowserNavigation browserNavigation
;
71 public AbstractCmsEntryPoint(Repository repository
, String workspace
, String defaultPath
,
72 Map
<String
, String
> factoryProperties
) {
73 this.repository
= repository
;
74 this.workspace
= workspace
;
75 this.defaultPath
= defaultPath
;
76 this.factoryProperties
= new HashMap
<String
, String
>(factoryProperties
);
77 // subject = new Subject();
82 lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_USER
,
83 new HttpRequestCallbackHandler(UiContext
.getHttpRequest(), UiContext
.getHttpResponse()));
85 } catch (LoginException e
) {
87 lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_ANONYMOUS
);
89 } catch (LoginException e1
) {
90 throw new CmsException("Cannot log in as anonymous", e1
);
95 jsExecutor
= RWT
.getClient().getService(JavaScriptExecutor
.class);
96 browserNavigation
= RWT
.getClient().getService(BrowserNavigation
.class);
97 if (browserNavigation
!= null)
98 browserNavigation
.addBrowserNavigationListener(new CmsNavigationListener());
102 protected Shell
createShell(Display display
) {
103 Shell shell
= super.createShell(display
);
104 shell
.setData(RWT
.CUSTOM_VARIANT
, CmsStyles
.CMS_SHELL
);
105 display
.disposeExec(new Runnable() {
109 if (log
.isTraceEnabled())
110 log
.trace("Logging out " + session
);
111 JcrUtils
.logoutQuietly(session
);
118 protected final void createContents(final Composite parent
) {
119 UiContext
.setData(CmsView
.KEY
, this);
120 Subject
.doAs(getSubject(), new PrivilegedAction
<Void
>() {
125 } catch (Exception e
) {
126 throw new CmsException("Cannot create entrypoint contents", e
);
134 protected abstract void initUi(Composite parent
);
136 /** Recreate UI after navigation or auth change */
137 protected abstract void refresh();
140 * The node to return when no node was found (for authenticated users and
143 // private Node getDefaultNode(Session session) throws RepositoryException {
144 // if (!session.hasPermission(defaultPath, "read")) {
145 // String userId = session.getUserID();
146 // if (userId.equals(NodeConstants.ROLE_ANONYMOUS))
147 // // TODO throw a special exception
148 // throw new CmsException("Login required");
150 // throw new CmsException("Unauthorized");
152 // return session.getNode(defaultPath);
155 protected String
getBaseTitle() {
156 return factoryProperties
.get(WebClient
.PAGE_TITLE
);
159 public void navigateTo(String state
) {
161 String title
= setState(state
);
163 if (browserNavigation
!= null)
164 browserNavigation
.pushState(state
, title
);
168 // public synchronized Subject getSubject() {
173 // public LoginContext getLoginContext() {
174 // return loginContext;
176 protected Subject
getSubject() {
177 return loginContext
.getSubject();
181 public boolean isAnonymous() {
182 return CurrentUser
.isAnonymous(getSubject());
186 public synchronized void logout() {
187 if (loginContext
== null)
188 throw new CmsException("Login context should not be null");
190 CurrentUser
.logoutCmsSession(loginContext
.getSubject());
191 loginContext
.logout();
192 LoginContext anonymousLc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_ANONYMOUS
);
194 authChange(anonymousLc
);
195 } catch (LoginException e
) {
196 log
.error("Cannot logout", e
);
201 public synchronized void authChange(LoginContext lc
) {
203 throw new CmsException("Login context cannot be null");
204 // logout previous login context
205 if (this.loginContext
!= null)
207 this.loginContext
.logout();
208 } catch (LoginException e1
) {
209 log
.warn("Could not log out: " + e1
);
211 this.loginContext
= lc
;
212 Subject
.doAs(getSubject(), new PrivilegedAction
<Void
>() {
217 JcrUtils
.logoutQuietly(session
);
218 session
= repository
.login(workspace
);
219 if (nodePath
!= null)
221 node
= session
.getNode(nodePath
);
222 } catch (PathNotFoundException e
) {
228 } catch (RepositoryException e
) {
229 throw new CmsException("Cannot perform auth change", e
);
238 public void exception(final Throwable e
) {
239 AbstractCmsEntryPoint
.this.exception
= e
;
240 log
.error("Unexpected exception in CMS", e
);
244 protected synchronized void doRefresh() {
245 Subject
.doAs(getSubject(), new PrivilegedAction
<Void
>() {
254 /** Sets the state of the entry point and retrieve the related JCR node. */
255 protected synchronized String
setState(String newState
) {
256 String previousState
= this.state
;
258 String newNodePath
= null;
259 String prefix
= null;
260 this.state
= newState
;
261 if (newState
.equals("~"))
265 int firstSlash
= state
.indexOf('/');
266 if (firstSlash
== 0) {
269 } else if (firstSlash
> 0) {
270 prefix
= state
.substring(0, firstSlash
);
271 newNodePath
= state
.substring(firstSlash
);
273 newNodePath
= defaultPath
;
279 int colonIndex
= prefix
.indexOf('$');
280 if (colonIndex
> 0) {
281 SharedSecret token
= new SharedSecret(new AuthPassword(X_SHARED_SECRET
+ '$' + prefix
)) {
284 public void handle(Callback
[] callbacks
) throws IOException
, UnsupportedCallbackException
{
285 super.handle(callbacks
);
286 // handle HTTP context
287 for (Callback callback
: callbacks
) {
288 if (callback
instanceof HttpRequestCallback
) {
289 ((HttpRequestCallback
) callback
).setRequest(UiContext
.getHttpRequest());
290 ((HttpRequestCallback
) callback
).setResponse(UiContext
.getHttpResponse());
295 LoginContext lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_USER
, token
);
297 authChange(lc
);// sets the node as well
299 // // TODO check consistency
303 if (session
.nodeExists(newNodePath
))
304 newNode
= session
.getNode(newNodePath
);
306 // throw new CmsException("Data " + newNodePath + " does not exist");
311 String title
= publishMetaData(getNode());
313 if (log
.isTraceEnabled())
314 log
.trace("node=" + newNodePath
+ ", state=" + state
+ " (prefix=" + prefix
+ ")");
317 } catch (Exception e
) {
318 log
.error("Cannot set state '" + state
+ "'", e
);
319 if (state
.equals("") || newState
.equals("~") || newState
.equals(previousState
))
320 return "Unrecoverable exception : " + e
.getClass().getSimpleName();
321 if (previousState
.equals(""))
323 navigateTo(previousState
);
324 throw new CmsException("Unexpected issue when accessing #" + newState
, e
);
328 private String
publishMetaData(Node node
) throws RepositoryException
{
331 if (node
!= null && node
.isNodeType(NodeType
.MIX_TITLE
) && node
.hasProperty(Property
.JCR_TITLE
))
332 title
= node
.getProperty(Property
.JCR_TITLE
).getString() + " - " + getBaseTitle();
334 title
= getBaseTitle();
336 HttpServletRequest request
= UiContext
.getHttpRequest();
340 StringBuilder js
= new StringBuilder();
343 title
= title
.replace("'", "\\'");// sanitize
344 js
.append("document.title = '" + title
+ "';");
345 jsExecutor
.execute(js
.toString());
349 // Simply remove some illegal character
350 // private String clean(String stringToClean) {
351 // return stringToClean.replaceAll("'", "").replaceAll("\\n", "")
352 // .replaceAll("\\t", "");
355 protected synchronized Node
getNode() {
359 private synchronized void setNode(Node node
) throws RepositoryException
{
361 this.nodePath
= node
== null ?
null : node
.getPath();
364 protected String
getState() {
368 protected Throwable
getException() {
372 protected void resetException() {
376 protected Session
getSession() {
380 private class CmsNavigationListener
implements BrowserNavigationListener
{
381 private static final long serialVersionUID
= -3591018803430389270L;
384 public void navigated(BrowserNavigationEvent event
) {
385 setState(event
.getState());