3 import java
.security
.AccessControlContext
;
4 import java
.security
.PrivilegedAction
;
5 import java
.util
.HashMap
;
6 import java
.util
.Locale
;
8 import java
.util
.ResourceBundle
;
10 import javax
.jcr
.Node
;
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
.LoginException
;
18 import javax
.security
.auth
.x500
.X500Principal
;
19 import javax
.servlet
.http
.HttpServletRequest
;
20 import javax
.servlet
.http
.HttpSession
;
22 import org
.apache
.commons
.logging
.Log
;
23 import org
.apache
.commons
.logging
.LogFactory
;
24 import org
.argeo
.ArgeoException
;
25 import org
.argeo
.cms
.auth
.ArgeoLoginContext
;
26 import org
.argeo
.cms
.auth
.LoginRequiredException
;
27 import org
.argeo
.cms
.i18n
.Msg
;
28 import org
.argeo
.jcr
.JcrUtils
;
29 import org
.eclipse
.rap
.rwt
.RWT
;
30 import org
.eclipse
.rap
.rwt
.application
.AbstractEntryPoint
;
31 import org
.eclipse
.rap
.rwt
.client
.WebClient
;
32 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigation
;
33 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigationEvent
;
34 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigationListener
;
35 import org
.eclipse
.rap
.rwt
.client
.service
.JavaScriptExecutor
;
36 import org
.eclipse
.swt
.widgets
.Composite
;
37 import org
.eclipse
.swt
.widgets
.Display
;
38 import org
.eclipse
.swt
.widgets
.Shell
;
40 /** Manages history and navigation */
41 public abstract class AbstractCmsEntryPoint
extends AbstractEntryPoint
43 private final Log log
= LogFactory
.getLog(AbstractCmsEntryPoint
.class);
45 private final Subject subject
;
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
;
57 private Throwable exception
;
60 private final JavaScriptExecutor jsExecutor
;
61 private final BrowserNavigation browserNavigation
;
63 public AbstractCmsEntryPoint(Repository repository
, String workspace
,
64 String defaultPath
, Map
<String
, String
> factoryProperties
) {
65 this.repository
= repository
;
66 this.workspace
= workspace
;
67 this.defaultPath
= defaultPath
;
68 this.factoryProperties
= new HashMap
<String
, String
>(factoryProperties
);
70 // load context from session
71 HttpServletRequest httpRequest
= RWT
.getRequest();
72 final HttpSession httpSession
= httpRequest
.getSession();
73 AccessControlContext acc
= (AccessControlContext
) httpSession
74 .getAttribute(KernelHeader
.ACCESS_CONTROL_CONTEXT
);
76 && Subject
.getSubject(acc
).getPrincipals(X500Principal
.class)
78 subject
= Subject
.getSubject(acc
);
80 subject
= new Subject();
84 new ArgeoLoginContext(KernelHeader
.LOGIN_CONTEXT_USER
, subject
)
86 } catch (LoginException e
) {
87 // if (log.isTraceEnabled())
88 // log.trace("Cannot authenticate user", e);
90 new ArgeoLoginContext(KernelHeader
.LOGIN_CONTEXT_ANONYMOUS
,
92 } catch (LoginException eAnonymous
) {
93 throw new ArgeoException("Cannot initialize subject",
100 jsExecutor
= RWT
.getClient().getService(JavaScriptExecutor
.class);
101 browserNavigation
= RWT
.getClient().getService(BrowserNavigation
.class);
102 if (browserNavigation
!= null)
104 .addBrowserNavigationListener(new CmsNavigationListener());
108 protected Shell
createShell(Display display
) {
109 Shell shell
= super.createShell(display
);
110 shell
.setData(RWT
.CUSTOM_VARIANT
, CmsStyles
.CMS_SHELL
);
111 display
.disposeExec(new Runnable() {
115 if (log
.isTraceEnabled())
116 log
.trace("Logging out " + session
);
117 JcrUtils
.logoutQuietly(session
);
124 protected final void createContents(final Composite parent
) {
125 getShell().getDisplay().setData(CmsView
.KEY
, this);
126 Subject
.doAs(subject
, new PrivilegedAction
<Void
>() {
131 } catch (Exception e
) {
132 throw new CmsException("Cannot create entrypoint contents",
141 protected abstract void createUi(Composite parent
);
143 /** Recreate UI after navigation or auth change */
144 protected abstract void refresh();
147 * The node to return when no node was found (for authenticated users and
150 protected Node
getDefaultNode(Session session
) throws RepositoryException
{
151 if (!session
.hasPermission(defaultPath
, "read")) {
152 if (session
.getUserID().equals("anonymous"))
153 throw new LoginRequiredException();
155 throw new CmsException("Unauthorized");
157 return session
.getNode(defaultPath
);
160 protected String
getBaseTitle() {
161 return factoryProperties
.get(WebClient
.PAGE_TITLE
);
164 public void navigateTo(String state
) {
166 String title
= setState(state
);
168 if (browserNavigation
!= null)
169 browserNavigation
.pushState(state
, title
);
173 public Subject
getSubject() {
178 public void authChange() {
179 Subject
.doAs(subject
, new PrivilegedAction
<Void
>() {
184 String currentPath
= null;
186 currentPath
= node
.getPath();
187 JcrUtils
.logoutQuietly(session
);
189 session
= repository
.login(workspace
);
190 if (currentPath
!= null)
192 node
= session
.getNode(currentPath
);
193 } catch (Exception e
) {
195 // TODO find a less hacky way to log out
196 new ArgeoLoginContext(
197 KernelHeader
.LOGIN_CONTEXT_ANONYMOUS
,
199 new ArgeoLoginContext(
200 KernelHeader
.LOGIN_CONTEXT_ANONYMOUS
,
202 } catch (LoginException eAnonymous
) {
203 throw new ArgeoException(
204 "Cannot reset to anonymous", eAnonymous
);
206 JcrUtils
.logoutQuietly(session
);
207 session
= repository
.login(workspace
);
214 } catch (RepositoryException e
) {
215 throw new CmsException("Cannot perform auth change", e
);
225 public void exception(final Throwable e
) {
226 AbstractCmsEntryPoint
.this.exception
= e
;
227 log
.error("Unexpected exception in CMS", e
);
231 protected void doRefresh() {
232 Subject
.doAs(subject
, new PrivilegedAction
<Void
>() {
242 // public Object local(Msg msg) {
243 // String key = msg.getId();
244 // int lastDot = key.lastIndexOf('.');
245 // String className = key.substring(0, lastDot);
246 // String fieldName = key.substring(lastDot + 1);
247 // Locale locale = RWT.getLocale();
248 // ResourceBundle rb = ResourceBundle.getBundle(className, locale,
249 // msg.getClassLoader());
250 // return rb.getString(fieldName);
253 /** Sets the state of the entry point and retrieve the related JCR node. */
254 protected synchronized String
setState(String newState
) {
255 String previousState
= this.state
;
259 this.state
= newState
;
260 if (newState
.equals("~"))
264 int firstSlash
= state
.indexOf('/');
265 if (firstSlash
== 0) {
266 if (session
.nodeExists(state
))
267 node
= session
.getNode(state
);
269 throw new CmsException("Data " + state
+ " does not exist");
271 } else if (firstSlash
> 0) {
272 String prefix
= state
.substring(0, firstSlash
);
273 String path
= state
.substring(firstSlash
);
274 if (session
.nodeExists(path
))
275 node
= session
.getNode(path
);
277 throw new CmsException("Data " + path
+ " does not exist");
280 node
= getDefaultNode(session
);
286 if (node
.isNodeType(NodeType
.MIX_TITLE
)
287 && node
.hasProperty(Property
.JCR_TITLE
))
288 title
= node
.getProperty(Property
.JCR_TITLE
).getString()
289 + " - " + getBaseTitle();
291 title
= getBaseTitle();
292 jsExecutor
.execute("document.title = \"" + title
+ "\"");
294 if (log
.isTraceEnabled())
295 log
.trace("node=" + node
+ ", state=" + state
+ " (page="
296 + page
+ ", title=" + title
+ ")");
299 } catch (Exception e
) {
300 log
.error("Cannot set state '" + state
+ "'", e
);
301 if (previousState
.equals(""))
303 navigateTo(previousState
);
304 throw new CmsException("Unexpected issue when accessing #"
309 protected Node
getNode() {
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());