3 import java
.security
.AccessControlContext
;
4 import java
.security
.PrivilegedAction
;
5 import java
.util
.HashMap
;
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
.security
.auth
.x500
.X500Principal
;
18 import javax
.servlet
.http
.HttpServletRequest
;
19 import javax
.servlet
.http
.HttpSession
;
21 import org
.apache
.commons
.logging
.Log
;
22 import org
.apache
.commons
.logging
.LogFactory
;
23 import org
.argeo
.ArgeoException
;
24 import org
.argeo
.cms
.auth
.AuthConstants
;
25 import org
.argeo
.jcr
.JcrUtils
;
26 import org
.eclipse
.rap
.rwt
.RWT
;
27 import org
.eclipse
.rap
.rwt
.application
.AbstractEntryPoint
;
28 import org
.eclipse
.rap
.rwt
.client
.WebClient
;
29 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigation
;
30 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigationEvent
;
31 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigationListener
;
32 import org
.eclipse
.rap
.rwt
.client
.service
.JavaScriptExecutor
;
33 import org
.eclipse
.swt
.widgets
.Composite
;
34 import org
.eclipse
.swt
.widgets
.Display
;
35 import org
.eclipse
.swt
.widgets
.Shell
;
37 /** Manages history and navigation */
38 public abstract class AbstractCmsEntryPoint
extends AbstractEntryPoint
40 private final Log log
= LogFactory
.getLog(AbstractCmsEntryPoint
.class);
42 private final Subject subject
;
44 private final Repository repository
;
45 private final String workspace
;
46 private final String defaultPath
;
47 private final Map
<String
, String
> factoryProperties
;
50 private Session session
;
54 private Throwable exception
;
57 private final JavaScriptExecutor jsExecutor
;
58 private final BrowserNavigation browserNavigation
;
60 public AbstractCmsEntryPoint(Repository repository
, String workspace
,
61 String defaultPath
, Map
<String
, String
> factoryProperties
) {
62 this.repository
= repository
;
63 this.workspace
= workspace
;
64 this.defaultPath
= defaultPath
;
65 this.factoryProperties
= new HashMap
<String
, String
>(factoryProperties
);
67 // load context from session
68 HttpServletRequest httpRequest
= RWT
.getRequest();
69 final HttpSession httpSession
= httpRequest
.getSession();
70 AccessControlContext acc
= (AccessControlContext
) httpSession
71 .getAttribute(AuthConstants
.ACCESS_CONTROL_CONTEXT
);
73 && Subject
.getSubject(acc
).getPrincipals(X500Principal
.class)
75 subject
= Subject
.getSubject(acc
);
77 subject
= new Subject();
81 new LoginContext(AuthConstants
.LOGIN_CONTEXT_USER
, subject
)
83 } catch (LoginException e
) {
84 // if (log.isTraceEnabled())
85 // log.trace("Cannot authenticate user", e);
87 new LoginContext(AuthConstants
.LOGIN_CONTEXT_ANONYMOUS
,
89 } catch (LoginException eAnonymous
) {
90 throw new ArgeoException("Cannot initialize subject",
97 jsExecutor
= RWT
.getClient().getService(JavaScriptExecutor
.class);
98 browserNavigation
= RWT
.getClient().getService(BrowserNavigation
.class);
99 if (browserNavigation
!= null)
101 .addBrowserNavigationListener(new CmsNavigationListener());
105 protected Shell
createShell(Display display
) {
106 Shell shell
= super.createShell(display
);
107 shell
.setData(RWT
.CUSTOM_VARIANT
, CmsStyles
.CMS_SHELL
);
108 display
.disposeExec(new Runnable() {
112 if (log
.isTraceEnabled())
113 log
.trace("Logging out " + session
);
114 JcrUtils
.logoutQuietly(session
);
121 protected final void createContents(final Composite parent
) {
122 getShell().getDisplay().setData(CmsView
.KEY
, this);
123 Subject
.doAs(subject
, new PrivilegedAction
<Void
>() {
128 } catch (Exception e
) {
129 throw new CmsException("Cannot create entrypoint contents",
138 protected abstract void createUi(Composite parent
);
140 /** Recreate UI after navigation or auth change */
141 protected abstract void refresh();
144 * The node to return when no node was found (for authenticated users and
147 protected Node
getDefaultNode(Session session
) throws RepositoryException
{
148 if (!session
.hasPermission(defaultPath
, "read")) {
149 if (session
.getUserID().equals(AuthConstants
.ROLE_ANONYMOUS
))
150 // TODO throw a special exception
151 throw new CmsException("Login required");
153 throw new CmsException("Unauthorized");
155 return session
.getNode(defaultPath
);
158 protected String
getBaseTitle() {
159 return factoryProperties
.get(WebClient
.PAGE_TITLE
);
162 public void navigateTo(String state
) {
164 String title
= setState(state
);
166 if (browserNavigation
!= null)
167 browserNavigation
.pushState(state
, title
);
171 public Subject
getSubject() {
176 public void authChange() {
177 Subject
.doAs(subject
, new PrivilegedAction
<Void
>() {
182 String currentPath
= null;
184 currentPath
= node
.getPath();
185 JcrUtils
.logoutQuietly(session
);
187 session
= repository
.login(workspace
);
188 if (currentPath
!= null)
190 node
= session
.getNode(currentPath
);
191 } catch (Exception e
) {
193 // TODO find a less hacky way to log out
195 AuthConstants
.LOGIN_CONTEXT_ANONYMOUS
,
198 AuthConstants
.LOGIN_CONTEXT_ANONYMOUS
,
200 } catch (LoginException eAnonymous
) {
201 throw new ArgeoException(
202 "Cannot reset to anonymous", eAnonymous
);
204 JcrUtils
.logoutQuietly(session
);
205 session
= repository
.login(workspace
);
212 } catch (RepositoryException e
) {
213 throw new CmsException("Cannot perform auth change", e
);
223 public void exception(final Throwable e
) {
224 AbstractCmsEntryPoint
.this.exception
= e
;
225 log
.error("Unexpected exception in CMS", e
);
229 protected void doRefresh() {
230 Subject
.doAs(subject
, new PrivilegedAction
<Void
>() {
240 // public Object local(Msg msg) {
241 // String key = msg.getId();
242 // int lastDot = key.lastIndexOf('.');
243 // String className = key.substring(0, lastDot);
244 // String fieldName = key.substring(lastDot + 1);
245 // Locale locale = RWT.getLocale();
246 // ResourceBundle rb = ResourceBundle.getBundle(className, locale,
247 // msg.getClassLoader());
248 // return rb.getString(fieldName);
251 /** Sets the state of the entry point and retrieve the related JCR node. */
252 protected synchronized String
setState(String newState
) {
253 String previousState
= this.state
;
257 this.state
= newState
;
258 if (newState
.equals("~"))
262 int firstSlash
= state
.indexOf('/');
263 if (firstSlash
== 0) {
264 if (session
.nodeExists(state
))
265 node
= session
.getNode(state
);
267 throw new CmsException("Data " + state
+ " does not exist");
269 } else if (firstSlash
> 0) {
270 String prefix
= state
.substring(0, firstSlash
);
271 String path
= state
.substring(firstSlash
);
272 if (session
.nodeExists(path
))
273 node
= session
.getNode(path
);
275 throw new CmsException("Data " + path
+ " does not exist");
278 node
= getDefaultNode(session
);
284 if (node
.isNodeType(NodeType
.MIX_TITLE
)
285 && node
.hasProperty(Property
.JCR_TITLE
))
286 title
= node
.getProperty(Property
.JCR_TITLE
).getString()
287 + " - " + getBaseTitle();
289 title
= getBaseTitle();
290 jsExecutor
.execute("document.title = \"" + title
+ "\"");
292 if (log
.isTraceEnabled())
293 log
.trace("node=" + node
+ ", state=" + state
+ " (page="
294 + page
+ ", title=" + title
+ ")");
297 } catch (Exception e
) {
298 log
.error("Cannot set state '" + state
+ "'", e
);
299 if (previousState
.equals(""))
301 navigateTo(previousState
);
302 throw new CmsException("Unexpected issue when accessing #"
307 protected Node
getNode() {
311 protected String
getState() {
315 protected Throwable
getException() {
319 protected void resetException() {
323 protected Session
getSession() {
327 private class CmsNavigationListener
implements BrowserNavigationListener
{
328 private static final long serialVersionUID
= -3591018803430389270L;
331 public void navigated(BrowserNavigationEvent event
) {
332 setState(event
.getState());