1 package org
.argeo
.cms
.ui
;
3 import static org
.argeo
.naming
.SharedSecret
.X_SHARED_SECRET
;
5 import java
.security
.PrivilegedAction
;
6 import java
.util
.HashMap
;
10 import javax
.jcr
.PathNotFoundException
;
11 import javax
.jcr
.Property
;
12 import javax
.jcr
.Repository
;
13 import javax
.jcr
.RepositoryException
;
14 import javax
.jcr
.Session
;
15 import javax
.jcr
.nodetype
.NodeType
;
16 import javax
.security
.auth
.Subject
;
17 import javax
.security
.auth
.login
.LoginContext
;
18 import javax
.security
.auth
.login
.LoginException
;
19 import javax
.servlet
.http
.HttpServletRequest
;
21 import org
.apache
.commons
.logging
.Log
;
22 import org
.apache
.commons
.logging
.LogFactory
;
23 import org
.argeo
.cms
.CmsException
;
24 import org
.argeo
.cms
.auth
.CurrentUser
;
25 import org
.argeo
.cms
.auth
.HttpRequestCallbackHandler
;
26 import org
.argeo
.eclipse
.ui
.specific
.UiContext
;
27 import org
.argeo
.jcr
.JcrUtils
;
28 import org
.argeo
.naming
.AuthPassword
;
29 import org
.argeo
.naming
.SharedSecret
;
30 import org
.argeo
.node
.NodeConstants
;
31 import org
.eclipse
.rap
.rwt
.RWT
;
32 import org
.eclipse
.rap
.rwt
.application
.AbstractEntryPoint
;
33 import org
.eclipse
.rap
.rwt
.client
.WebClient
;
34 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigation
;
35 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigationEvent
;
36 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigationListener
;
37 import org
.eclipse
.rap
.rwt
.client
.service
.JavaScriptExecutor
;
38 import org
.eclipse
.swt
.widgets
.Composite
;
39 import org
.eclipse
.swt
.widgets
.Display
;
40 import org
.eclipse
.swt
.widgets
.Shell
;
42 /** Manages history and navigation */
43 public abstract class AbstractCmsEntryPoint
extends AbstractEntryPoint
implements CmsView
{
44 private static final long serialVersionUID
= 906558779562569784L;
46 private final Log log
= LogFactory
.getLog(AbstractCmsEntryPoint
.class);
48 // private final Subject subject;
49 private LoginContext loginContext
;
51 private final Repository repository
;
52 private final String workspace
;
53 private final String defaultPath
;
54 private final Map
<String
, String
> factoryProperties
;
57 private Session session
;
59 private String nodePath
;// useful when changing auth
61 private Throwable exception
;
64 private final JavaScriptExecutor jsExecutor
;
65 private final BrowserNavigation browserNavigation
;
67 public AbstractCmsEntryPoint(Repository repository
, String workspace
, String defaultPath
,
68 Map
<String
, String
> factoryProperties
) {
69 this.repository
= repository
;
70 this.workspace
= workspace
;
71 this.defaultPath
= defaultPath
;
72 this.factoryProperties
= new HashMap
<String
, String
>(factoryProperties
);
73 // subject = new Subject();
78 lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_USER
,
79 new HttpRequestCallbackHandler(UiContext
.getHttpRequest(), UiContext
.getHttpResponse()));
81 } catch (LoginException e
) {
83 lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_ANONYMOUS
);
85 } catch (LoginException e1
) {
86 throw new CmsException("Cannot log in as anonymous", e1
);
91 jsExecutor
= RWT
.getClient().getService(JavaScriptExecutor
.class);
92 browserNavigation
= RWT
.getClient().getService(BrowserNavigation
.class);
93 if (browserNavigation
!= null)
94 browserNavigation
.addBrowserNavigationListener(new CmsNavigationListener());
98 protected Shell
createShell(Display display
) {
99 Shell shell
= super.createShell(display
);
100 shell
.setData(RWT
.CUSTOM_VARIANT
, CmsStyles
.CMS_SHELL
);
101 display
.disposeExec(new Runnable() {
105 if (log
.isTraceEnabled())
106 log
.trace("Logging out " + session
);
107 JcrUtils
.logoutQuietly(session
);
114 protected final void createContents(final Composite parent
) {
115 UiContext
.setData(CmsView
.KEY
, this);
116 Subject
.doAs(getSubject(), new PrivilegedAction
<Void
>() {
121 } catch (Exception e
) {
122 throw new CmsException("Cannot create entrypoint contents", e
);
130 protected abstract void initUi(Composite parent
);
132 /** Recreate UI after navigation or auth change */
133 protected abstract void refresh();
136 * The node to return when no node was found (for authenticated users and
139 private Node
getDefaultNode(Session session
) throws RepositoryException
{
140 if (!session
.hasPermission(defaultPath
, "read")) {
141 String userId
= session
.getUserID();
142 if (userId
.equals(NodeConstants
.ROLE_ANONYMOUS
))
143 // TODO throw a special exception
144 throw new CmsException("Login required");
146 throw new CmsException("Unauthorized");
148 return session
.getNode(defaultPath
);
151 protected String
getBaseTitle() {
152 return factoryProperties
.get(WebClient
.PAGE_TITLE
);
155 public void navigateTo(String state
) {
157 String title
= setState(state
);
159 if (browserNavigation
!= null)
160 browserNavigation
.pushState(state
, title
);
164 // public synchronized Subject getSubject() {
169 // public LoginContext getLoginContext() {
170 // return loginContext;
172 protected Subject
getSubject() {
173 return loginContext
.getSubject();
177 public boolean isAnonymous() {
178 return CurrentUser
.isAnonymous(getSubject());
182 public synchronized void logout() {
183 if (loginContext
== null)
184 throw new CmsException("Login context should not be null");
186 CurrentUser
.logoutCmsSession(loginContext
.getSubject());
187 loginContext
.logout();
188 LoginContext anonymousLc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_ANONYMOUS
);
190 authChange(anonymousLc
);
191 } catch (LoginException e
) {
192 log
.error("Cannot logout", e
);
197 public synchronized void authChange(LoginContext lc
) {
199 throw new CmsException("Login context cannot be null");
200 // logout previous login context
201 if (this.loginContext
!= null)
203 this.loginContext
.logout();
204 } catch (LoginException e1
) {
205 log
.warn("Could not log out: " + e1
);
207 this.loginContext
= lc
;
208 Subject
.doAs(getSubject(), new PrivilegedAction
<Void
>() {
213 JcrUtils
.logoutQuietly(session
);
214 session
= repository
.login(workspace
);
215 if (nodePath
!= null)
217 node
= session
.getNode(nodePath
);
218 } catch (PathNotFoundException e
) {
224 } catch (RepositoryException e
) {
225 throw new CmsException("Cannot perform auth change", e
);
234 public void exception(final Throwable e
) {
235 AbstractCmsEntryPoint
.this.exception
= e
;
236 log
.error("Unexpected exception in CMS", e
);
240 protected synchronized void doRefresh() {
241 Subject
.doAs(getSubject(), new PrivilegedAction
<Void
>() {
250 /** Sets the state of the entry point and retrieve the related JCR node. */
251 protected synchronized String
setState(String newState
) {
252 String previousState
= this.state
;
254 String newNodePath
= null;
255 String prefix
= null;
256 this.state
= newState
;
257 if (newState
.equals("~"))
261 int firstSlash
= state
.indexOf('/');
262 if (firstSlash
== 0) {
265 } else if (firstSlash
> 0) {
266 prefix
= state
.substring(0, firstSlash
);
267 newNodePath
= state
.substring(firstSlash
);
269 newNodePath
= defaultPath
;
275 int colonIndex
= prefix
.indexOf('$');
276 if (colonIndex
> 0) {
277 // String user = prefix.substring(0, colonIndex);
278 // // if (isAnonymous()) {
279 // String token = prefix.substring(colonIndex + 1);
280 // LoginContext lc = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER, new
281 // CallbackHandler() {
284 // public void handle(Callback[] callbacks) throws IOException,
285 // UnsupportedCallbackException {
286 // for (Callback callback : callbacks) {
287 // if (callback instanceof NameCallback)
288 // ((NameCallback) callback).setName(user);
289 // else if (callback instanceof PasswordCallback)
290 // ((PasswordCallback) callback).setPassword(token.toCharArray());
295 SharedSecret token
= new SharedSecret(new AuthPassword(X_SHARED_SECRET
+ '$' + prefix
));
296 LoginContext lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_USER
, token
);
298 authChange(lc
);// sets the node as well
300 // // TODO check consistency
304 if (session
.nodeExists(newNodePath
))
305 newNode
= session
.getNode(newNodePath
);
307 throw new CmsException("Data " + newNodePath
+ " does not exist");
310 String title
= publishMetaData(getNode());
312 if (log
.isTraceEnabled())
313 log
.trace("node=" + newNodePath
+ ", state=" + state
+ " (prefix=" + prefix
+ ")");
316 } catch (Exception e
) {
317 log
.error("Cannot set state '" + state
+ "'", e
);
318 if (state
.equals("") || newState
.equals("~") || newState
.equals(previousState
))
319 return "Unrecoverable exception : " + e
.getClass().getSimpleName();
320 if (previousState
.equals(""))
322 navigateTo(previousState
);
323 throw new CmsException("Unexpected issue when accessing #" + newState
, e
);
327 private String
publishMetaData(Node node
) throws RepositoryException
{
330 if (node
.isNodeType(NodeType
.MIX_TITLE
) && node
.hasProperty(Property
.JCR_TITLE
))
331 title
= node
.getProperty(Property
.JCR_TITLE
).getString() + " - " + getBaseTitle();
333 title
= getBaseTitle();
335 HttpServletRequest request
= UiContext
.getHttpRequest();
339 StringBuilder js
= new StringBuilder();
340 title
= title
.replace("'", "\\'");// sanitize
341 js
.append("document.title = '" + title
+ "';");
342 jsExecutor
.execute(js
.toString());
346 // Simply remove some illegal character
347 // private String clean(String stringToClean) {
348 // return stringToClean.replaceAll("'", "").replaceAll("\\n", "")
349 // .replaceAll("\\t", "");
352 protected synchronized Node
getNode() {
356 private synchronized void setNode(Node node
) throws RepositoryException
{
358 this.nodePath
= node
== null ?
null : node
.getPath();
361 protected String
getState() {
365 protected Throwable
getException() {
369 protected void resetException() {
373 protected Session
getSession() {
377 private class CmsNavigationListener
implements BrowserNavigationListener
{
378 private static final long serialVersionUID
= -3591018803430389270L;
381 public void navigated(BrowserNavigationEvent event
) {
382 setState(event
.getState());