1 package org
.argeo
.cms
.ui
;
3 import java
.io
.IOException
;
4 import java
.security
.PrivilegedAction
;
5 import java
.util
.HashMap
;
9 import javax
.jcr
.PathNotFoundException
;
10 import javax
.jcr
.Property
;
11 import javax
.jcr
.Repository
;
12 import javax
.jcr
.RepositoryException
;
13 import javax
.jcr
.Session
;
14 import javax
.jcr
.nodetype
.NodeType
;
15 import javax
.security
.auth
.Subject
;
16 import javax
.security
.auth
.callback
.Callback
;
17 import javax
.security
.auth
.callback
.CallbackHandler
;
18 import javax
.security
.auth
.callback
.NameCallback
;
19 import javax
.security
.auth
.callback
.PasswordCallback
;
20 import javax
.security
.auth
.callback
.UnsupportedCallbackException
;
21 import javax
.security
.auth
.login
.LoginContext
;
22 import javax
.security
.auth
.login
.LoginException
;
23 import javax
.servlet
.http
.HttpServletRequest
;
25 import org
.apache
.commons
.logging
.Log
;
26 import org
.apache
.commons
.logging
.LogFactory
;
27 import org
.argeo
.cms
.CmsException
;
28 import org
.argeo
.cms
.auth
.CurrentUser
;
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
.node
.NodeConstants
;
33 import org
.eclipse
.rap
.rwt
.RWT
;
34 import org
.eclipse
.rap
.rwt
.application
.AbstractEntryPoint
;
35 import org
.eclipse
.rap
.rwt
.client
.WebClient
;
36 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigation
;
37 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigationEvent
;
38 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigationListener
;
39 import org
.eclipse
.rap
.rwt
.client
.service
.JavaScriptExecutor
;
40 import org
.eclipse
.swt
.widgets
.Composite
;
41 import org
.eclipse
.swt
.widgets
.Display
;
42 import org
.eclipse
.swt
.widgets
.Shell
;
44 /** Manages history and navigation */
45 public abstract class AbstractCmsEntryPoint
extends AbstractEntryPoint
implements CmsView
{
46 private static final long serialVersionUID
= 906558779562569784L;
48 private final Log log
= LogFactory
.getLog(AbstractCmsEntryPoint
.class);
50 // private final Subject subject;
51 private LoginContext loginContext
;
53 private final Repository repository
;
54 private final String workspace
;
55 private final String defaultPath
;
56 private final Map
<String
, String
> factoryProperties
;
59 private Session session
;
61 private String nodePath
;// useful when changing auth
63 private Throwable exception
;
66 private final JavaScriptExecutor jsExecutor
;
67 private final BrowserNavigation browserNavigation
;
69 public AbstractCmsEntryPoint(Repository repository
, String workspace
, String defaultPath
,
70 Map
<String
, String
> factoryProperties
) {
71 this.repository
= repository
;
72 this.workspace
= workspace
;
73 this.defaultPath
= defaultPath
;
74 this.factoryProperties
= new HashMap
<String
, String
>(factoryProperties
);
75 // subject = new Subject();
80 lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_USER
,
81 new HttpRequestCallbackHandler(UiContext
.getHttpRequest(), UiContext
.getHttpResponse()));
83 } catch (LoginException e
) {
85 lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_ANONYMOUS
);
87 } catch (LoginException e1
) {
88 throw new CmsException("Cannot log in as anonymous", e1
);
93 jsExecutor
= RWT
.getClient().getService(JavaScriptExecutor
.class);
94 browserNavigation
= RWT
.getClient().getService(BrowserNavigation
.class);
95 if (browserNavigation
!= null)
96 browserNavigation
.addBrowserNavigationListener(new CmsNavigationListener());
100 protected Shell
createShell(Display display
) {
101 Shell shell
= super.createShell(display
);
102 shell
.setData(RWT
.CUSTOM_VARIANT
, CmsStyles
.CMS_SHELL
);
103 display
.disposeExec(new Runnable() {
107 if (log
.isTraceEnabled())
108 log
.trace("Logging out " + session
);
109 JcrUtils
.logoutQuietly(session
);
116 protected final void createContents(final Composite parent
) {
117 UiContext
.setData(CmsView
.KEY
, this);
118 Subject
.doAs(getSubject(), new PrivilegedAction
<Void
>() {
123 } catch (Exception e
) {
124 throw new CmsException("Cannot create entrypoint contents", e
);
132 protected abstract void initUi(Composite parent
);
134 /** Recreate UI after navigation or auth change */
135 protected abstract void refresh();
138 * The node to return when no node was found (for authenticated users and
141 private Node
getDefaultNode(Session session
) throws RepositoryException
{
142 if (!session
.hasPermission(defaultPath
, "read")) {
143 String userId
= session
.getUserID();
144 if (userId
.equals(NodeConstants
.ROLE_ANONYMOUS
))
145 // TODO throw a special exception
146 throw new CmsException("Login required");
148 throw new CmsException("Unauthorized");
150 return session
.getNode(defaultPath
);
153 protected String
getBaseTitle() {
154 return factoryProperties
.get(WebClient
.PAGE_TITLE
);
157 public void navigateTo(String state
) {
159 String title
= setState(state
);
161 if (browserNavigation
!= null)
162 browserNavigation
.pushState(state
, title
);
166 // public synchronized Subject getSubject() {
171 // public LoginContext getLoginContext() {
172 // return loginContext;
174 protected Subject
getSubject() {
175 return loginContext
.getSubject();
179 public boolean isAnonymous() {
180 return CurrentUser
.isAnonymous(getSubject());
184 public synchronized void logout() {
185 if (loginContext
== null)
186 throw new CmsException("Login context should not be null");
188 CurrentUser
.logoutCmsSession(loginContext
.getSubject());
189 loginContext
.logout();
190 LoginContext anonymousLc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_ANONYMOUS
);
192 authChange(anonymousLc
);
193 } catch (LoginException e
) {
194 log
.error("Cannot logout", e
);
199 public synchronized void authChange(LoginContext lc
) {
201 throw new CmsException("Login context cannot be null");
202 // logout previous login context
203 if (this.loginContext
!= null)
205 this.loginContext
.logout();
206 } catch (LoginException e1
) {
207 log
.warn("Could not log out: " + e1
);
209 this.loginContext
= lc
;
210 Subject
.doAs(getSubject(), new PrivilegedAction
<Void
>() {
215 JcrUtils
.logoutQuietly(session
);
216 session
= repository
.login(workspace
);
217 if (nodePath
!= null)
219 node
= session
.getNode(nodePath
);
220 } catch (PathNotFoundException e
) {
226 } catch (RepositoryException e
) {
227 throw new CmsException("Cannot perform auth change", e
);
236 public void exception(final Throwable e
) {
237 AbstractCmsEntryPoint
.this.exception
= e
;
238 log
.error("Unexpected exception in CMS", e
);
242 protected synchronized void doRefresh() {
243 Subject
.doAs(getSubject(), new PrivilegedAction
<Void
>() {
252 /** Sets the state of the entry point and retrieve the related JCR node. */
253 protected synchronized String
setState(String newState
) {
254 String previousState
= this.state
;
256 String newNodePath
= null;
257 String prefix
= null;
258 this.state
= newState
;
259 if (newState
.equals("~"))
263 int firstSlash
= state
.indexOf('/');
264 if (firstSlash
== 0) {
267 } else if (firstSlash
> 0) {
268 prefix
= state
.substring(0, firstSlash
);
269 newNodePath
= state
.substring(firstSlash
);
271 newNodePath
= defaultPath
;
277 int colonIndex
= prefix
.indexOf(':');
278 if (colonIndex
> 0) {
279 String user
= prefix
.substring(0, colonIndex
);
280 // if (isAnonymous()) {
281 String token
= prefix
.substring(colonIndex
+ 1);
282 LoginContext lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_USER
, new CallbackHandler() {
285 public void handle(Callback
[] callbacks
) throws IOException
, 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());
296 authChange(lc
);// sets the node as well
298 // // TODO check consistency
302 if (session
.nodeExists(newNodePath
))
303 newNode
= session
.getNode(newNodePath
);
305 throw new CmsException("Data " + newNodePath
+ " does not exist");
308 String title
= publishMetaData(getNode());
310 if (log
.isTraceEnabled())
311 log
.trace("node=" + newNodePath
+ ", state=" + state
+ " (prefix=" + prefix
+ ")");
314 } catch (Exception e
) {
315 log
.error("Cannot set state '" + state
+ "'", e
);
316 if (state
.equals("") || newState
.equals("~") || newState
.equals(previousState
))
317 return "Unrecoverable exception : " + e
.getClass().getSimpleName();
318 if (previousState
.equals(""))
320 navigateTo(previousState
);
321 throw new CmsException("Unexpected issue when accessing #" + newState
, e
);
325 private String
publishMetaData(Node node
) throws RepositoryException
{
328 if (node
.isNodeType(NodeType
.MIX_TITLE
) && node
.hasProperty(Property
.JCR_TITLE
))
329 title
= node
.getProperty(Property
.JCR_TITLE
).getString() + " - " + getBaseTitle();
331 title
= getBaseTitle();
333 HttpServletRequest request
= UiContext
.getHttpRequest();
337 StringBuilder js
= new StringBuilder();
338 title
= title
.replace("'", "\\'");// sanitize
339 js
.append("document.title = '" + title
+ "';");
340 jsExecutor
.execute(js
.toString());
344 // Simply remove some illegal character
345 // private String clean(String stringToClean) {
346 // return stringToClean.replaceAll("'", "").replaceAll("\\n", "")
347 // .replaceAll("\\t", "");
350 protected synchronized Node
getNode() {
354 private synchronized void setNode(Node node
) throws RepositoryException
{
356 this.nodePath
= node
== null ?
null : node
.getPath();
359 protected String
getState() {
363 protected Throwable
getException() {
367 protected void resetException() {
371 protected Session
getSession() {
375 private class CmsNavigationListener
implements BrowserNavigationListener
{
376 private static final long serialVersionUID
= -3591018803430389270L;
379 public void navigated(BrowserNavigationEvent event
) {
380 setState(event
.getState());