1 package org
.argeo
.cms
.web
;
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
.api
.NodeConstants
;
27 import org
.argeo
.cms
.CmsException
;
28 import org
.argeo
.cms
.auth
.CurrentUser
;
29 import org
.argeo
.cms
.auth
.HttpRequestCallback
;
30 import org
.argeo
.cms
.auth
.HttpRequestCallbackHandler
;
31 import org
.argeo
.cms
.ui
.CmsStyles
;
32 import org
.argeo
.cms
.ui
.CmsView
;
33 import org
.argeo
.eclipse
.ui
.specific
.UiContext
;
34 import org
.argeo
.jcr
.JcrUtils
;
35 import org
.argeo
.naming
.AuthPassword
;
36 import org
.argeo
.naming
.SharedSecret
;
37 import org
.eclipse
.rap
.rwt
.RWT
;
38 import org
.eclipse
.rap
.rwt
.application
.AbstractEntryPoint
;
39 import org
.eclipse
.rap
.rwt
.client
.WebClient
;
40 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigation
;
41 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigationEvent
;
42 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigationListener
;
43 import org
.eclipse
.rap
.rwt
.client
.service
.JavaScriptExecutor
;
44 import org
.eclipse
.swt
.widgets
.Composite
;
45 import org
.eclipse
.swt
.widgets
.Display
;
46 import org
.eclipse
.swt
.widgets
.Shell
;
48 /** Manages history and navigation */
50 public abstract class AbstractCmsEntryPoint
extends AbstractEntryPoint
implements CmsView
{
51 private static final long serialVersionUID
= 906558779562569784L;
53 private final Log log
= LogFactory
.getLog(AbstractCmsEntryPoint
.class);
55 // private final Subject subject;
56 private LoginContext loginContext
;
58 private final Repository repository
;
59 private final String workspace
;
60 private final String defaultPath
;
61 private final Map
<String
, String
> factoryProperties
;
64 private Session session
;
66 private String nodePath
;// useful when changing auth
68 private Throwable exception
;
71 private final JavaScriptExecutor jsExecutor
;
72 private final BrowserNavigation browserNavigation
;
74 public AbstractCmsEntryPoint(Repository repository
, String workspace
, String defaultPath
,
75 Map
<String
, String
> factoryProperties
) {
76 this.repository
= repository
;
77 this.workspace
= workspace
;
78 this.defaultPath
= defaultPath
;
79 this.factoryProperties
= new HashMap
<String
, String
>(factoryProperties
);
80 // subject = new Subject();
85 lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_USER
,
86 new HttpRequestCallbackHandler(UiContext
.getHttpRequest(), UiContext
.getHttpResponse()));
88 } catch (LoginException e
) {
90 lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_ANONYMOUS
);
92 } catch (LoginException e1
) {
93 throw new CmsException("Cannot log in as anonymous", e1
);
98 jsExecutor
= RWT
.getClient().getService(JavaScriptExecutor
.class);
99 browserNavigation
= RWT
.getClient().getService(BrowserNavigation
.class);
100 if (browserNavigation
!= null)
101 browserNavigation
.addBrowserNavigationListener(new CmsNavigationListener());
105 protected Shell
createShell(Display display
) {
106 Shell shell
= super.createShell(display
);
107 shell
.setData(RWT
.CUSTOM_VARIANT
, CmsStyles
.CMS_SHELL
);
108 display
.disposeExec(new Runnable() {
112 if (log
.isTraceEnabled())
113 log
.trace("Logging out " + session
);
114 JcrUtils
.logoutQuietly(session
);
121 protected final void createContents(final Composite parent
) {
122 // UiContext.setData(CmsView.KEY, this);
123 CmsView
.registerCmsView(parent
.getShell(), this);
124 Subject
.doAs(getSubject(), new PrivilegedAction
<Void
>() {
129 } catch (Exception e
) {
130 throw new CmsException("Cannot create entrypoint contents", e
);
138 protected abstract void initUi(Composite parent
);
140 /** Recreate UI after navigation or auth change */
141 protected abstract void refresh();
144 * The node to return when no node was found (for authenticated users and
147 // private Node getDefaultNode(Session session) throws RepositoryException {
148 // if (!session.hasPermission(defaultPath, "read")) {
149 // String userId = session.getUserID();
150 // if (userId.equals(NodeConstants.ROLE_ANONYMOUS))
151 // // TODO throw a special exception
152 // throw new CmsException("Login required");
154 // throw new CmsException("Unauthorized");
156 // return session.getNode(defaultPath);
159 protected String
getBaseTitle() {
160 return factoryProperties
.get(WebClient
.PAGE_TITLE
);
163 public void navigateTo(String state
) {
165 String title
= setState(state
);
167 if (browserNavigation
!= null)
168 browserNavigation
.pushState(state
, title
);
172 // public synchronized Subject getSubject() {
177 // public LoginContext getLoginContext() {
178 // return loginContext;
180 protected Subject
getSubject() {
181 return loginContext
.getSubject();
185 public boolean isAnonymous() {
186 return CurrentUser
.isAnonymous(getSubject());
190 public synchronized void logout() {
191 if (loginContext
== null)
192 throw new CmsException("Login context should not be null");
194 CurrentUser
.logoutCmsSession(loginContext
.getSubject());
195 loginContext
.logout();
196 LoginContext anonymousLc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_ANONYMOUS
);
198 authChange(anonymousLc
);
199 } catch (LoginException e
) {
200 log
.error("Cannot logout", e
);
205 public synchronized void authChange(LoginContext lc
) {
207 throw new CmsException("Login context cannot be null");
208 // logout previous login context
209 if (this.loginContext
!= null)
211 this.loginContext
.logout();
212 } catch (LoginException e1
) {
213 log
.warn("Could not log out: " + e1
);
215 this.loginContext
= lc
;
216 Subject
.doAs(getSubject(), new PrivilegedAction
<Void
>() {
221 JcrUtils
.logoutQuietly(session
);
222 session
= repository
.login(workspace
);
223 if (nodePath
!= null)
225 node
= session
.getNode(nodePath
);
226 } catch (PathNotFoundException e
) {
232 } catch (RepositoryException e
) {
233 throw new CmsException("Cannot perform auth change", e
);
242 public void exception(final Throwable e
) {
243 AbstractCmsEntryPoint
.this.exception
= e
;
244 log
.error("Unexpected exception in CMS", e
);
248 protected synchronized void doRefresh() {
249 Subject
.doAs(getSubject(), new PrivilegedAction
<Void
>() {
258 /** Sets the state of the entry point and retrieve the related JCR node. */
259 protected synchronized String
setState(String newState
) {
260 String previousState
= this.state
;
262 String newNodePath
= null;
263 String prefix
= null;
264 this.state
= newState
;
265 if (newState
.equals("~"))
269 int firstSlash
= state
.indexOf('/');
270 if (firstSlash
== 0) {
273 } else if (firstSlash
> 0) {
274 prefix
= state
.substring(0, firstSlash
);
275 newNodePath
= state
.substring(firstSlash
);
277 newNodePath
= defaultPath
;
283 int colonIndex
= prefix
.indexOf('$');
284 if (colonIndex
> 0) {
285 SharedSecret token
= new SharedSecret(new AuthPassword(X_SHARED_SECRET
+ '$' + prefix
)) {
288 public void handle(Callback
[] callbacks
) throws IOException
, UnsupportedCallbackException
{
289 super.handle(callbacks
);
290 // handle HTTP context
291 for (Callback callback
: callbacks
) {
292 if (callback
instanceof HttpRequestCallback
) {
293 ((HttpRequestCallback
) callback
).setRequest(UiContext
.getHttpRequest());
294 ((HttpRequestCallback
) callback
).setResponse(UiContext
.getHttpResponse());
299 LoginContext lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_USER
, token
);
301 authChange(lc
);// sets the node as well
303 // // TODO check consistency
307 if (session
.nodeExists(newNodePath
))
308 newNode
= session
.getNode(newNodePath
);
310 // throw new CmsException("Data " + newNodePath + " does not exist");
315 String title
= publishMetaData(getNode());
317 if (log
.isTraceEnabled())
318 log
.trace("node=" + newNodePath
+ ", state=" + state
+ " (prefix=" + prefix
+ ")");
321 } catch (Exception e
) {
322 log
.error("Cannot set state '" + state
+ "'", e
);
323 if (state
.equals("") || newState
.equals("~") || newState
.equals(previousState
))
324 return "Unrecoverable exception : " + e
.getClass().getSimpleName();
325 if (previousState
.equals(""))
327 navigateTo(previousState
);
328 throw new CmsException("Unexpected issue when accessing #" + newState
, e
);
332 private String
publishMetaData(Node node
) throws RepositoryException
{
335 if (node
!= null && node
.isNodeType(NodeType
.MIX_TITLE
) && node
.hasProperty(Property
.JCR_TITLE
))
336 title
= node
.getProperty(Property
.JCR_TITLE
).getString() + " - " + getBaseTitle();
338 title
= getBaseTitle();
340 HttpServletRequest request
= UiContext
.getHttpRequest();
344 StringBuilder js
= new StringBuilder();
347 title
= title
.replace("'", "\\'");// sanitize
348 js
.append("document.title = '" + title
+ "';");
349 jsExecutor
.execute(js
.toString());
353 // Simply remove some illegal character
354 // private String clean(String stringToClean) {
355 // return stringToClean.replaceAll("'", "").replaceAll("\\n", "")
356 // .replaceAll("\\t", "");
359 protected synchronized Node
getNode() {
363 private synchronized void setNode(Node node
) throws RepositoryException
{
365 this.nodePath
= node
== null ?
null : node
.getPath();
368 protected String
getState() {
372 protected Throwable
getException() {
376 protected void resetException() {
380 protected Session
getSession() {
384 private class CmsNavigationListener
implements BrowserNavigationListener
{
385 private static final long serialVersionUID
= -3591018803430389270L;
388 public void navigated(BrowserNavigationEvent event
) {
389 setState(event
.getState());