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
.LoginContext
;
16 import javax
.security
.auth
.login
.LoginException
;
17 import javax
.servlet
.http
.HttpServletRequest
;
19 import org
.apache
.commons
.logging
.Log
;
20 import org
.apache
.commons
.logging
.LogFactory
;
21 import org
.argeo
.cms
.CmsException
;
22 import org
.argeo
.cms
.auth
.HttpRequestCallbackHandler
;
23 import org
.argeo
.eclipse
.ui
.specific
.UiContext
;
24 import org
.argeo
.jcr
.JcrUtils
;
25 import org
.argeo
.node
.NodeConstants
;
26 import org
.argeo
.node
.security
.NodeAuthenticated
;
27 import org
.eclipse
.rap
.rwt
.RWT
;
28 import org
.eclipse
.rap
.rwt
.application
.AbstractEntryPoint
;
29 import org
.eclipse
.rap
.rwt
.client
.WebClient
;
30 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigation
;
31 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigationEvent
;
32 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigationListener
;
33 import org
.eclipse
.rap
.rwt
.client
.service
.JavaScriptExecutor
;
34 import org
.eclipse
.swt
.widgets
.Composite
;
35 import org
.eclipse
.swt
.widgets
.Display
;
36 import org
.eclipse
.swt
.widgets
.Shell
;
38 /** Manages history and navigation */
39 public abstract class AbstractCmsEntryPoint
extends AbstractEntryPoint
implements CmsView
{
40 private static final long serialVersionUID
= 906558779562569784L;
42 private final Log log
= LogFactory
.getLog(AbstractCmsEntryPoint
.class);
44 // private final Subject subject;
45 private LoginContext loginContext
;
47 private final Repository repository
;
48 private final String workspace
;
49 private final String defaultPath
;
50 private final Map
<String
, String
> factoryProperties
;
53 private Session session
;
55 private String nodePath
;// useful when changing auth
58 private Throwable exception
;
61 private final JavaScriptExecutor jsExecutor
;
62 private final BrowserNavigation browserNavigation
;
64 public AbstractCmsEntryPoint(Repository repository
, String workspace
, String defaultPath
,
65 Map
<String
, String
> factoryProperties
) {
66 this.repository
= repository
;
67 this.workspace
= workspace
;
68 this.defaultPath
= defaultPath
;
69 this.factoryProperties
= new HashMap
<String
, String
>(factoryProperties
);
70 // subject = new Subject();
74 loginContext
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_USER
,
75 new HttpRequestCallbackHandler(UiContext
.getHttpRequest()));
77 } catch (LoginException e
) {
79 loginContext
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_USER
);
81 } catch (LoginException e1
) {
82 throw new CmsException("Cannot log in as anonymous", e1
);
85 authChange(loginContext
);
87 jsExecutor
= RWT
.getClient().getService(JavaScriptExecutor
.class);
88 browserNavigation
= RWT
.getClient().getService(BrowserNavigation
.class);
89 if (browserNavigation
!= null)
90 browserNavigation
.addBrowserNavigationListener(new CmsNavigationListener());
94 protected Shell
createShell(Display display
) {
95 Shell shell
= super.createShell(display
);
96 shell
.setData(RWT
.CUSTOM_VARIANT
, CmsStyles
.CMS_SHELL
);
97 display
.disposeExec(new Runnable() {
101 if (log
.isTraceEnabled())
102 log
.trace("Logging out " + session
);
103 JcrUtils
.logoutQuietly(session
);
110 protected final void createContents(final Composite parent
) {
111 UiContext
.setData(NodeAuthenticated
.KEY
, this);
112 Subject
.doAs(loginContext
.getSubject(), new PrivilegedAction
<Void
>() {
117 } catch (Exception e
) {
118 throw new CmsException("Cannot create entrypoint contents", e
);
126 protected abstract void initUi(Composite parent
);
128 /** Recreate UI after navigation or auth change */
129 protected abstract void refresh();
132 * The node to return when no node was found (for authenticated users and
135 protected Node
getDefaultNode(Session session
) throws RepositoryException
{
136 if (!session
.hasPermission(defaultPath
, "read")) {
137 String userId
= session
.getUserID();
138 if (userId
.equals(NodeConstants
.ROLE_ANONYMOUS
))
139 // TODO throw a special exception
140 throw new CmsException("Login required");
142 throw new CmsException("Unauthorized");
144 return session
.getNode(defaultPath
);
147 protected String
getBaseTitle() {
148 return factoryProperties
.get(WebClient
.PAGE_TITLE
);
151 public void navigateTo(String state
) {
153 String title
= setState(state
);
155 if (browserNavigation
!= null)
156 browserNavigation
.pushState(state
, title
);
160 // public synchronized Subject getSubject() {
165 public LoginContext
getLoginContext() {
170 public synchronized void logout() {
171 if (loginContext
== null)
172 throw new CmsException("Login context should not be null");
174 loginContext
.logout();
175 LoginContext anonymousLc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_USER
);
177 authChange(anonymousLc
);
178 } catch (LoginException e
) {
179 log
.error("Cannot logout", e
);
184 public synchronized void authChange(LoginContext loginContext
) {
185 if (loginContext
== null)
186 throw new CmsException("Login context cannot be null");
187 this.loginContext
= loginContext
;
188 Subject
.doAs(loginContext
.getSubject(), new PrivilegedAction
<Void
>() {
193 JcrUtils
.logoutQuietly(session
);
194 session
= repository
.login(workspace
);
195 if (nodePath
!= null)
197 node
= session
.getNode(nodePath
);
198 } catch (PathNotFoundException e
) {
204 } catch (RepositoryException e
) {
205 throw new CmsException("Cannot perform auth change", e
);
215 public void exception(final Throwable e
) {
216 AbstractCmsEntryPoint
.this.exception
= e
;
217 log
.error("Unexpected exception in CMS", e
);
221 protected synchronized void doRefresh() {
222 Subject
.doAs(loginContext
.getSubject(), new PrivilegedAction
<Void
>() {
231 /** Sets the state of the entry point and retrieve the related JCR node. */
232 protected synchronized String
setState(String newState
) {
233 String previousState
= this.state
;
237 this.state
= newState
;
238 if (newState
.equals("~"))
242 int firstSlash
= state
.indexOf('/');
243 if (firstSlash
== 0) {
244 node
= session
.getNode(state
);
246 } else if (firstSlash
> 0) {
247 String prefix
= state
.substring(0, firstSlash
);
248 String path
= state
.substring(firstSlash
);
249 if (session
.nodeExists(path
))
250 node
= session
.getNode(path
);
252 throw new CmsException("Data " + path
+ " does not exist");
255 node
= getDefaultNode(session
);
259 String title
= publishMetaData(node
);
261 if (log
.isTraceEnabled())
262 log
.trace("node=" + node
+ ", state=" + state
+ " (page=" + page
+ ")");
265 } catch (Exception e
) {
266 log
.error("Cannot set state '" + state
+ "'", e
);
267 if (state
.equals("") || newState
.equals("~") || newState
.equals(previousState
))
268 return "Unrecoverable exception : " + e
.getClass().getSimpleName();
269 if (previousState
.equals(""))
271 navigateTo(previousState
);
272 throw new CmsException("Unexpected issue when accessing #" + newState
, e
);
276 private String
publishMetaData(Node node
) throws RepositoryException
{
279 if (node
.isNodeType(NodeType
.MIX_TITLE
) && node
.hasProperty(Property
.JCR_TITLE
))
280 title
= node
.getProperty(Property
.JCR_TITLE
).getString() + " - " + getBaseTitle();
282 title
= getBaseTitle();
284 HttpServletRequest request
= UiContext
.getHttpRequest();
288 StringBuilder js
= new StringBuilder();
289 title
= title
.replace("'", "\\'");// sanitize
290 js
.append("document.title = '" + title
+ "';");
291 jsExecutor
.execute(js
.toString());
295 // Simply remove some illegal character
296 // private String clean(String stringToClean) {
297 // return stringToClean.replaceAll("'", "").replaceAll("\\n", "")
298 // .replaceAll("\\t", "");
301 protected synchronized Node
getNode() {
305 private synchronized void setNode(Node node
) throws RepositoryException
{
307 this.nodePath
= node
== null ?
null : node
.getPath();
310 protected String
getState() {
314 protected Throwable
getException() {
318 protected void resetException() {
322 protected Session
getSession() {
326 private class CmsNavigationListener
implements BrowserNavigationListener
{
327 private static final long serialVersionUID
= -3591018803430389270L;
330 public void navigated(BrowserNavigationEvent event
) {
331 setState(event
.getState());