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 */
49 public abstract class AbstractCmsEntryPoint
extends AbstractEntryPoint
implements CmsView
{
50 private static final long serialVersionUID
= 906558779562569784L;
52 private final Log log
= LogFactory
.getLog(AbstractCmsEntryPoint
.class);
54 // private final Subject subject;
55 private LoginContext loginContext
;
57 private final Repository repository
;
58 private final String workspace
;
59 private final String defaultPath
;
60 private final Map
<String
, String
> factoryProperties
;
63 private Session session
;
65 private String nodePath
;// useful when changing auth
67 private Throwable exception
;
70 private final JavaScriptExecutor jsExecutor
;
71 private final BrowserNavigation browserNavigation
;
73 public AbstractCmsEntryPoint(Repository repository
, String workspace
, String defaultPath
,
74 Map
<String
, String
> factoryProperties
) {
75 this.repository
= repository
;
76 this.workspace
= workspace
;
77 this.defaultPath
= defaultPath
;
78 this.factoryProperties
= new HashMap
<String
, String
>(factoryProperties
);
79 // subject = new Subject();
84 lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_USER
,
85 new HttpRequestCallbackHandler(UiContext
.getHttpRequest(), UiContext
.getHttpResponse()));
87 } catch (LoginException e
) {
89 lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_ANONYMOUS
);
91 } catch (LoginException e1
) {
92 throw new CmsException("Cannot log in as anonymous", e1
);
97 jsExecutor
= RWT
.getClient().getService(JavaScriptExecutor
.class);
98 browserNavigation
= RWT
.getClient().getService(BrowserNavigation
.class);
99 if (browserNavigation
!= null)
100 browserNavigation
.addBrowserNavigationListener(new CmsNavigationListener());
104 protected Shell
createShell(Display display
) {
105 Shell shell
= super.createShell(display
);
106 shell
.setData(RWT
.CUSTOM_VARIANT
, CmsStyles
.CMS_SHELL
);
107 display
.disposeExec(new Runnable() {
111 if (log
.isTraceEnabled())
112 log
.trace("Logging out " + session
);
113 JcrUtils
.logoutQuietly(session
);
120 protected final void createContents(final Composite parent
) {
121 // UiContext.setData(CmsView.KEY, this);
122 CmsView
.registerCmsView(parent
.getShell(), this);
123 Subject
.doAs(getSubject(), new PrivilegedAction
<Void
>() {
128 } catch (Exception e
) {
129 throw new CmsException("Cannot create entrypoint contents", e
);
137 protected abstract void initUi(Composite parent
);
139 /** Recreate UI after navigation or auth change */
140 protected abstract void refresh();
143 * The node to return when no node was found (for authenticated users and
146 // private Node getDefaultNode(Session session) throws RepositoryException {
147 // if (!session.hasPermission(defaultPath, "read")) {
148 // String userId = session.getUserID();
149 // if (userId.equals(NodeConstants.ROLE_ANONYMOUS))
150 // // TODO throw a special exception
151 // throw new CmsException("Login required");
153 // throw new CmsException("Unauthorized");
155 // return session.getNode(defaultPath);
158 protected String
getBaseTitle() {
159 return factoryProperties
.get(WebClient
.PAGE_TITLE
);
162 public void navigateTo(String state
) {
164 String title
= setState(state
);
166 if (browserNavigation
!= null)
167 browserNavigation
.pushState(state
, title
);
171 // public synchronized Subject getSubject() {
176 // public LoginContext getLoginContext() {
177 // return loginContext;
179 protected Subject
getSubject() {
180 return loginContext
.getSubject();
184 public boolean isAnonymous() {
185 return CurrentUser
.isAnonymous(getSubject());
189 public synchronized void logout() {
190 if (loginContext
== null)
191 throw new CmsException("Login context should not be null");
193 CurrentUser
.logoutCmsSession(loginContext
.getSubject());
194 loginContext
.logout();
195 LoginContext anonymousLc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_ANONYMOUS
);
197 authChange(anonymousLc
);
198 } catch (LoginException e
) {
199 log
.error("Cannot logout", e
);
204 public synchronized void authChange(LoginContext lc
) {
206 throw new CmsException("Login context cannot be null");
207 // logout previous login context
208 if (this.loginContext
!= null)
210 this.loginContext
.logout();
211 } catch (LoginException e1
) {
212 log
.warn("Could not log out: " + e1
);
214 this.loginContext
= lc
;
215 Subject
.doAs(getSubject(), new PrivilegedAction
<Void
>() {
220 JcrUtils
.logoutQuietly(session
);
221 session
= repository
.login(workspace
);
222 if (nodePath
!= null)
224 node
= session
.getNode(nodePath
);
225 } catch (PathNotFoundException e
) {
231 } catch (RepositoryException e
) {
232 throw new CmsException("Cannot perform auth change", e
);
241 public void exception(final Throwable e
) {
242 AbstractCmsEntryPoint
.this.exception
= e
;
243 log
.error("Unexpected exception in CMS", e
);
247 protected synchronized void doRefresh() {
248 Subject
.doAs(getSubject(), new PrivilegedAction
<Void
>() {
257 /** Sets the state of the entry point and retrieve the related JCR node. */
258 protected synchronized String
setState(String newState
) {
259 String previousState
= this.state
;
261 String newNodePath
= null;
262 String prefix
= null;
263 this.state
= newState
;
264 if (newState
.equals("~"))
268 int firstSlash
= state
.indexOf('/');
269 if (firstSlash
== 0) {
272 } else if (firstSlash
> 0) {
273 prefix
= state
.substring(0, firstSlash
);
274 newNodePath
= state
.substring(firstSlash
);
276 newNodePath
= defaultPath
;
282 int colonIndex
= prefix
.indexOf('$');
283 if (colonIndex
> 0) {
284 SharedSecret token
= new SharedSecret(new AuthPassword(X_SHARED_SECRET
+ '$' + prefix
)) {
287 public void handle(Callback
[] callbacks
) throws IOException
, UnsupportedCallbackException
{
288 super.handle(callbacks
);
289 // handle HTTP context
290 for (Callback callback
: callbacks
) {
291 if (callback
instanceof HttpRequestCallback
) {
292 ((HttpRequestCallback
) callback
).setRequest(UiContext
.getHttpRequest());
293 ((HttpRequestCallback
) callback
).setResponse(UiContext
.getHttpResponse());
298 LoginContext lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_USER
, token
);
300 authChange(lc
);// sets the node as well
302 // // TODO check consistency
306 if (session
.nodeExists(newNodePath
))
307 newNode
= session
.getNode(newNodePath
);
309 // throw new CmsException("Data " + newNodePath + " does not exist");
314 String title
= publishMetaData(getNode());
316 if (log
.isTraceEnabled())
317 log
.trace("node=" + newNodePath
+ ", state=" + state
+ " (prefix=" + prefix
+ ")");
320 } catch (Exception e
) {
321 log
.error("Cannot set state '" + state
+ "'", e
);
322 if (state
.equals("") || newState
.equals("~") || newState
.equals(previousState
))
323 return "Unrecoverable exception : " + e
.getClass().getSimpleName();
324 if (previousState
.equals(""))
326 navigateTo(previousState
);
327 throw new CmsException("Unexpected issue when accessing #" + newState
, e
);
331 private String
publishMetaData(Node node
) throws RepositoryException
{
334 if (node
!= null && node
.isNodeType(NodeType
.MIX_TITLE
) && node
.hasProperty(Property
.JCR_TITLE
))
335 title
= node
.getProperty(Property
.JCR_TITLE
).getString() + " - " + getBaseTitle();
337 title
= getBaseTitle();
339 HttpServletRequest request
= UiContext
.getHttpRequest();
343 StringBuilder js
= new StringBuilder();
346 title
= title
.replace("'", "\\'");// sanitize
347 js
.append("document.title = '" + title
+ "';");
348 jsExecutor
.execute(js
.toString());
352 // Simply remove some illegal character
353 // private String clean(String stringToClean) {
354 // return stringToClean.replaceAll("'", "").replaceAll("\\n", "")
355 // .replaceAll("\\t", "");
358 protected synchronized Node
getNode() {
362 private synchronized void setNode(Node node
) throws RepositoryException
{
364 this.nodePath
= node
== null ?
null : node
.getPath();
367 protected String
getState() {
371 protected Throwable
getException() {
375 protected void resetException() {
379 protected Session
getSession() {
383 private class CmsNavigationListener
implements BrowserNavigationListener
{
384 private static final long serialVersionUID
= -3591018803430389270L;
387 public void navigated(BrowserNavigationEvent event
) {
388 setState(event
.getState());