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
.api
.cms
.CmsView
;
28 import org
.argeo
.cms
.CmsException
;
29 import org
.argeo
.cms
.auth
.CurrentUser
;
30 import org
.argeo
.cms
.auth
.HttpRequestCallback
;
31 import org
.argeo
.cms
.auth
.HttpRequestCallbackHandler
;
32 import org
.argeo
.cms
.servlet
.ServletHttpRequest
;
33 import org
.argeo
.cms
.servlet
.ServletHttpResponse
;
34 import org
.argeo
.cms
.swt
.CmsStyles
;
35 import org
.argeo
.cms
.swt
.CmsSwtUtils
;
36 import org
.argeo
.eclipse
.ui
.specific
.UiContext
;
37 import org
.argeo
.jcr
.JcrUtils
;
38 import org
.argeo
.naming
.AuthPassword
;
39 import org
.argeo
.naming
.SharedSecret
;
40 import org
.eclipse
.rap
.rwt
.RWT
;
41 import org
.eclipse
.rap
.rwt
.application
.AbstractEntryPoint
;
42 import org
.eclipse
.rap
.rwt
.client
.WebClient
;
43 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigation
;
44 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigationEvent
;
45 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigationListener
;
46 import org
.eclipse
.rap
.rwt
.client
.service
.JavaScriptExecutor
;
47 import org
.eclipse
.swt
.widgets
.Composite
;
48 import org
.eclipse
.swt
.widgets
.Display
;
49 import org
.eclipse
.swt
.widgets
.Shell
;
51 /** Manages history and navigation */
53 public abstract class AbstractCmsEntryPoint
extends AbstractEntryPoint
implements CmsView
{
54 private static final long serialVersionUID
= 906558779562569784L;
56 private final Log log
= LogFactory
.getLog(AbstractCmsEntryPoint
.class);
58 // private final Subject subject;
59 private LoginContext loginContext
;
61 private final Repository repository
;
62 private final String workspace
;
63 private final String defaultPath
;
64 private final Map
<String
, String
> factoryProperties
;
67 private Session session
;
69 private String nodePath
;// useful when changing auth
71 private Throwable exception
;
74 private final JavaScriptExecutor jsExecutor
;
75 private final BrowserNavigation browserNavigation
;
77 public AbstractCmsEntryPoint(Repository repository
, String workspace
, String defaultPath
,
78 Map
<String
, String
> factoryProperties
) {
79 this.repository
= repository
;
80 this.workspace
= workspace
;
81 this.defaultPath
= defaultPath
;
82 this.factoryProperties
= new HashMap
<String
, String
>(factoryProperties
);
83 // subject = new Subject();
88 lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_USER
,
89 new HttpRequestCallbackHandler(new ServletHttpRequest(UiContext
.getHttpRequest()),
90 new ServletHttpResponse(UiContext
.getHttpResponse())));
92 } catch (LoginException e
) {
94 lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_ANONYMOUS
);
96 } catch (LoginException e1
) {
97 throw new CmsException("Cannot log in as anonymous", e1
);
102 jsExecutor
= RWT
.getClient().getService(JavaScriptExecutor
.class);
103 browserNavigation
= RWT
.getClient().getService(BrowserNavigation
.class);
104 if (browserNavigation
!= null)
105 browserNavigation
.addBrowserNavigationListener(new CmsNavigationListener());
109 protected Shell
createShell(Display display
) {
110 Shell shell
= super.createShell(display
);
111 shell
.setData(RWT
.CUSTOM_VARIANT
, CmsStyles
.CMS_SHELL
);
112 display
.disposeExec(new Runnable() {
116 if (log
.isTraceEnabled())
117 log
.trace("Logging out " + session
);
118 JcrUtils
.logoutQuietly(session
);
125 protected final void createContents(final Composite parent
) {
126 // UiContext.setData(CmsView.KEY, this);
127 CmsSwtUtils
.registerCmsView(parent
.getShell(), this);
128 Subject
.doAs(getSubject(), new PrivilegedAction
<Void
>() {
133 } catch (Exception e
) {
134 throw new CmsException("Cannot create entrypoint contents", e
);
142 protected abstract void initUi(Composite parent
);
144 /** Recreate UI after navigation or auth change */
145 protected abstract void refresh();
148 * The node to return when no node was found (for authenticated users and
151 // private Node getDefaultNode(Session session) throws RepositoryException {
152 // if (!session.hasPermission(defaultPath, "read")) {
153 // String userId = session.getUserID();
154 // if (userId.equals(NodeConstants.ROLE_ANONYMOUS))
155 // // TODO throw a special exception
156 // throw new CmsException("Login required");
158 // throw new CmsException("Unauthorized");
160 // return session.getNode(defaultPath);
163 protected String
getBaseTitle() {
164 return factoryProperties
.get(WebClient
.PAGE_TITLE
);
167 public void navigateTo(String state
) {
169 String title
= setState(state
);
171 if (browserNavigation
!= null)
172 browserNavigation
.pushState(state
, title
);
176 // public synchronized Subject getSubject() {
181 // public LoginContext getLoginContext() {
182 // return loginContext;
184 protected Subject
getSubject() {
185 return loginContext
.getSubject();
189 public boolean isAnonymous() {
190 return CurrentUser
.isAnonymous(getSubject());
194 public synchronized void logout() {
195 if (loginContext
== null)
196 throw new CmsException("Login context should not be null");
198 CurrentUser
.logoutCmsSession(loginContext
.getSubject());
199 loginContext
.logout();
200 LoginContext anonymousLc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_ANONYMOUS
);
202 authChange(anonymousLc
);
203 } catch (LoginException e
) {
204 log
.error("Cannot logout", e
);
209 public synchronized void authChange(LoginContext lc
) {
211 throw new CmsException("Login context cannot be null");
212 // logout previous login context
213 if (this.loginContext
!= null)
215 this.loginContext
.logout();
216 } catch (LoginException e1
) {
217 log
.warn("Could not log out: " + e1
);
219 this.loginContext
= lc
;
220 Subject
.doAs(getSubject(), new PrivilegedAction
<Void
>() {
225 JcrUtils
.logoutQuietly(session
);
226 session
= repository
.login(workspace
);
227 if (nodePath
!= null)
229 node
= session
.getNode(nodePath
);
230 } catch (PathNotFoundException e
) {
236 } catch (RepositoryException e
) {
237 throw new CmsException("Cannot perform auth change", e
);
246 public void exception(final Throwable e
) {
247 AbstractCmsEntryPoint
.this.exception
= e
;
248 log
.error("Unexpected exception in CMS", e
);
252 protected synchronized void doRefresh() {
253 Subject
.doAs(getSubject(), new PrivilegedAction
<Void
>() {
262 /** Sets the state of the entry point and retrieve the related JCR node. */
263 protected synchronized String
setState(String newState
) {
264 String previousState
= this.state
;
266 String newNodePath
= null;
267 String prefix
= null;
268 this.state
= newState
;
269 if (newState
.equals("~"))
273 int firstSlash
= state
.indexOf('/');
274 if (firstSlash
== 0) {
277 } else if (firstSlash
> 0) {
278 prefix
= state
.substring(0, firstSlash
);
279 newNodePath
= state
.substring(firstSlash
);
281 newNodePath
= defaultPath
;
287 int colonIndex
= prefix
.indexOf('$');
288 if (colonIndex
> 0) {
289 SharedSecret token
= new SharedSecret(new AuthPassword(X_SHARED_SECRET
+ '$' + prefix
)) {
292 public void handle(Callback
[] callbacks
) throws IOException
, UnsupportedCallbackException
{
293 super.handle(callbacks
);
294 // handle HTTP context
295 for (Callback callback
: callbacks
) {
296 if (callback
instanceof HttpRequestCallback
) {
297 ((HttpRequestCallback
) callback
)
298 .setRequest(new ServletHttpRequest(UiContext
.getHttpRequest()));
299 ((HttpRequestCallback
) callback
)
300 .setResponse(new ServletHttpResponse(UiContext
.getHttpResponse()));
305 LoginContext lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_USER
, token
);
307 authChange(lc
);// sets the node as well
309 // // TODO check consistency
313 if (session
.nodeExists(newNodePath
))
314 newNode
= session
.getNode(newNodePath
);
316 // throw new CmsException("Data " + newNodePath + " does not exist");
321 String title
= publishMetaData(getNode());
323 if (log
.isTraceEnabled())
324 log
.trace("node=" + newNodePath
+ ", state=" + state
+ " (prefix=" + prefix
+ ")");
327 } catch (Exception e
) {
328 log
.error("Cannot set state '" + state
+ "'", e
);
329 if (state
.equals("") || newState
.equals("~") || newState
.equals(previousState
))
330 return "Unrecoverable exception : " + e
.getClass().getSimpleName();
331 if (previousState
.equals(""))
333 navigateTo(previousState
);
334 throw new CmsException("Unexpected issue when accessing #" + newState
, e
);
338 private String
publishMetaData(Node node
) throws RepositoryException
{
341 if (node
!= null && node
.isNodeType(NodeType
.MIX_TITLE
) && node
.hasProperty(Property
.JCR_TITLE
))
342 title
= node
.getProperty(Property
.JCR_TITLE
).getString() + " - " + getBaseTitle();
344 title
= getBaseTitle();
346 HttpServletRequest request
= UiContext
.getHttpRequest();
350 StringBuilder js
= new StringBuilder();
353 title
= title
.replace("'", "\\'");// sanitize
354 js
.append("document.title = '" + title
+ "';");
355 jsExecutor
.execute(js
.toString());
359 // Simply remove some illegal character
360 // private String clean(String stringToClean) {
361 // return stringToClean.replaceAll("'", "").replaceAll("\\n", "")
362 // .replaceAll("\\t", "");
365 protected synchronized Node
getNode() {
369 private synchronized void setNode(Node node
) throws RepositoryException
{
371 this.nodePath
= node
== null ?
null : node
.getPath();
374 protected String
getState() {
378 protected Throwable
getException() {
382 protected void resetException() {
386 protected Session
getSession() {
390 private class CmsNavigationListener
implements BrowserNavigationListener
{
391 private static final long serialVersionUID
= -3591018803430389270L;
394 public void navigated(BrowserNavigationEvent event
) {
395 setState(event
.getState());