1 package org
.argeo
.cms
.ui
;
3 import java
.security
.PrivilegedAction
;
4 import java
.util
.HashMap
;
8 import javax
.jcr
.PathNotFoundException
;
9 import javax
.jcr
.Property
;
10 import javax
.jcr
.Repository
;
11 import javax
.jcr
.RepositoryException
;
12 import javax
.jcr
.Session
;
13 import javax
.jcr
.nodetype
.NodeType
;
14 import javax
.security
.auth
.Subject
;
15 import javax
.security
.auth
.login
.CredentialNotFoundException
;
16 import javax
.security
.auth
.login
.LoginContext
;
17 import javax
.security
.auth
.login
.LoginException
;
18 import javax
.servlet
.http
.HttpServletRequest
;
20 import org
.apache
.commons
.logging
.Log
;
21 import org
.apache
.commons
.logging
.LogFactory
;
22 import org
.argeo
.cms
.CmsException
;
23 import org
.argeo
.cms
.auth
.HttpRequestCallbackHandler
;
24 import org
.argeo
.eclipse
.ui
.specific
.UiContext
;
25 import org
.argeo
.jcr
.JcrUtils
;
26 import org
.argeo
.node
.NodeConstants
;
27 import org
.argeo
.node
.security
.NodeAuthenticated
;
28 import org
.eclipse
.rap
.rwt
.RWT
;
29 import org
.eclipse
.rap
.rwt
.application
.AbstractEntryPoint
;
30 import org
.eclipse
.rap
.rwt
.client
.WebClient
;
31 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigation
;
32 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigationEvent
;
33 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigationListener
;
34 import org
.eclipse
.rap
.rwt
.client
.service
.JavaScriptExecutor
;
35 import org
.eclipse
.swt
.widgets
.Composite
;
36 import org
.eclipse
.swt
.widgets
.Display
;
37 import org
.eclipse
.swt
.widgets
.Shell
;
39 /** Manages history and navigation */
40 public abstract class AbstractCmsEntryPoint
extends AbstractEntryPoint
implements CmsView
{
41 private static final long serialVersionUID
= 906558779562569784L;
43 private final Log log
= LogFactory
.getLog(AbstractCmsEntryPoint
.class);
45 // private final Subject subject;
46 private LoginContext loginContext
;
48 private final Repository repository
;
49 private final String workspace
;
50 private final String defaultPath
;
51 private final Map
<String
, String
> factoryProperties
;
54 private Session session
;
56 private String nodePath
;// useful when changing auth
59 private Throwable exception
;
62 private final JavaScriptExecutor jsExecutor
;
63 private final BrowserNavigation browserNavigation
;
65 public AbstractCmsEntryPoint(Repository repository
, String workspace
, String defaultPath
,
66 Map
<String
, String
> factoryProperties
) {
67 this.repository
= repository
;
68 this.workspace
= workspace
;
69 this.defaultPath
= defaultPath
;
70 this.factoryProperties
= new HashMap
<String
, String
>(factoryProperties
);
71 // subject = new Subject();
75 loginContext
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_USER
,
76 new HttpRequestCallbackHandler(UiContext
.getHttpRequest()));
78 } catch (CredentialNotFoundException e
) {
80 loginContext
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_USER
);
82 } catch (LoginException e1
) {
83 throw new CmsException("Cannot log in as anonymous", e1
);
85 } catch (LoginException e
) {
86 throw new CmsException("Cannot initialize subject", e
);
88 authChange(loginContext
);
90 jsExecutor
= RWT
.getClient().getService(JavaScriptExecutor
.class);
91 browserNavigation
= RWT
.getClient().getService(BrowserNavigation
.class);
92 if (browserNavigation
!= null)
93 browserNavigation
.addBrowserNavigationListener(new CmsNavigationListener());
97 protected Shell
createShell(Display display
) {
98 Shell shell
= super.createShell(display
);
99 shell
.setData(RWT
.CUSTOM_VARIANT
, CmsStyles
.CMS_SHELL
);
100 display
.disposeExec(new Runnable() {
104 if (log
.isTraceEnabled())
105 log
.trace("Logging out " + session
);
106 JcrUtils
.logoutQuietly(session
);
113 protected final void createContents(final Composite parent
) {
114 UiContext
.setData(NodeAuthenticated
.KEY
, this);
115 Subject
.doAs(loginContext
.getSubject(), new PrivilegedAction
<Void
>() {
120 } catch (Exception e
) {
121 throw new CmsException("Cannot create entrypoint contents", e
);
129 protected abstract void initUi(Composite parent
);
131 /** Recreate UI after navigation or auth change */
132 protected abstract void refresh();
135 * The node to return when no node was found (for authenticated users and
138 protected Node
getDefaultNode(Session session
) throws RepositoryException
{
139 if (!session
.hasPermission(defaultPath
, "read")) {
140 String userId
= session
.getUserID();
141 if (userId
.equals(NodeConstants
.ROLE_ANONYMOUS
))
142 // TODO throw a special exception
143 throw new CmsException("Login required");
145 throw new CmsException("Unauthorized");
147 return session
.getNode(defaultPath
);
150 protected String
getBaseTitle() {
151 return factoryProperties
.get(WebClient
.PAGE_TITLE
);
154 public void navigateTo(String state
) {
156 String title
= setState(state
);
158 if (browserNavigation
!= null)
159 browserNavigation
.pushState(state
, title
);
163 // public synchronized Subject getSubject() {
168 public LoginContext
getLoginContext() {
173 public synchronized void logout() {
174 if (loginContext
== null)
175 throw new CmsException("Login context should not be null");
177 loginContext
.logout();
178 LoginContext anonymousLc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_USER
);
180 authChange(anonymousLc
);
181 } catch (LoginException e
) {
182 throw new CmsException("Cannot logout", e
);
187 public synchronized void authChange(LoginContext loginContext
) {
188 if (loginContext
== null)
189 throw new CmsException("Login context cannot be null");
190 this.loginContext
= loginContext
;
191 Subject
.doAs(loginContext
.getSubject(), new PrivilegedAction
<Void
>() {
196 JcrUtils
.logoutQuietly(session
);
197 session
= repository
.login(workspace
);
198 if (nodePath
!= null)
200 node
= session
.getNode(nodePath
);
201 } catch (PathNotFoundException e
) {
207 } catch (RepositoryException e
) {
208 throw new CmsException("Cannot perform auth change", e
);
218 public void exception(final Throwable e
) {
219 AbstractCmsEntryPoint
.this.exception
= e
;
220 log
.error("Unexpected exception in CMS", e
);
224 protected synchronized void doRefresh() {
225 Subject
.doAs(loginContext
.getSubject(), new PrivilegedAction
<Void
>() {
234 /** Sets the state of the entry point and retrieve the related JCR node. */
235 protected synchronized String
setState(String newState
) {
236 String previousState
= this.state
;
240 this.state
= newState
;
241 if (newState
.equals("~"))
245 int firstSlash
= state
.indexOf('/');
246 if (firstSlash
== 0) {
247 node
= session
.getNode(state
);
249 } else if (firstSlash
> 0) {
250 String prefix
= state
.substring(0, firstSlash
);
251 String path
= state
.substring(firstSlash
);
252 if (session
.nodeExists(path
))
253 node
= session
.getNode(path
);
255 throw new CmsException("Data " + path
+ " does not exist");
258 node
= getDefaultNode(session
);
262 String title
= publishMetaData(node
);
264 if (log
.isTraceEnabled())
265 log
.trace("node=" + node
+ ", state=" + state
+ " (page=" + page
+ ")");
268 } catch (Exception e
) {
269 log
.error("Cannot set state '" + state
+ "'", e
);
270 if (state
.equals("") || newState
.equals("~") || newState
.equals(previousState
))
271 return "Unrecoverable exception : " + e
.getClass().getSimpleName();
272 if (previousState
.equals(""))
274 navigateTo(previousState
);
275 throw new CmsException("Unexpected issue when accessing #" + newState
, e
);
279 private String
publishMetaData(Node node
) throws RepositoryException
{
282 if (node
.isNodeType(NodeType
.MIX_TITLE
) && node
.hasProperty(Property
.JCR_TITLE
))
283 title
= node
.getProperty(Property
.JCR_TITLE
).getString() + " - " + getBaseTitle();
285 title
= getBaseTitle();
287 HttpServletRequest request
= UiContext
.getHttpRequest();
291 StringBuilder js
= new StringBuilder();
292 title
= title
.replace("'", "\\'");// sanitize
293 js
.append("document.title = '" + title
+ "';");
294 jsExecutor
.execute(js
.toString());
298 // Simply remove some illegal character
299 // private String clean(String stringToClean) {
300 // return stringToClean.replaceAll("'", "").replaceAll("\\n", "")
301 // .replaceAll("\\t", "");
304 protected synchronized Node
getNode() {
308 private synchronized void setNode(Node node
) throws RepositoryException
{
310 this.nodePath
= node
== null ?
null : node
.getPath();
313 protected String
getState() {
317 protected Throwable
getException() {
321 protected void resetException() {
325 protected Session
getSession() {
329 private class CmsNavigationListener
implements BrowserNavigationListener
{
330 private static final long serialVersionUID
= -3591018803430389270L;
333 public void navigated(BrowserNavigationEvent event
) {
334 setState(event
.getState());