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
.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
.ArgeoLoginContext
;
25 import org
.argeo
.cms
.auth
.LoginRequiredException
;
26 import org
.argeo
.cms
.i18n
.Msg
;
27 import org
.argeo
.jcr
.JcrUtils
;
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
41 implements CmsSession
{
42 private final Log log
= LogFactory
.getLog(AbstractCmsEntryPoint
.class);
44 private final Subject subject
;
46 private final Repository repository
;
47 private final String workspace
;
48 private final String defaultPath
;
49 private final Map
<String
, String
> factoryProperties
;
52 private Session session
;
56 private Throwable exception
;
59 private final JavaScriptExecutor jsExecutor
;
60 private final BrowserNavigation browserNavigation
;
62 public AbstractCmsEntryPoint(Repository repository
, String workspace
,
63 String defaultPath
, Map
<String
, String
> factoryProperties
) {
64 this.repository
= repository
;
65 this.workspace
= workspace
;
66 this.defaultPath
= defaultPath
;
67 this.factoryProperties
= new HashMap
<String
, String
>(factoryProperties
);
69 // load context from session
70 HttpServletRequest httpRequest
= RWT
.getRequest();
71 final HttpSession httpSession
= httpRequest
.getSession();
72 AccessControlContext acc
= (AccessControlContext
) httpSession
73 .getAttribute(KernelHeader
.ACCESS_CONTROL_CONTEXT
);
75 subject
= Subject
.getSubject(acc
);
77 subject
= new Subject();
81 new ArgeoLoginContext(KernelHeader
.LOGIN_CONTEXT_USER
, subject
)
83 } catch (LoginException e
) {
84 // if (log.isTraceEnabled())
85 // log.trace("Cannot authenticate user", e);
87 new ArgeoLoginContext(KernelHeader
.LOGIN_CONTEXT_ANONYMOUS
,
89 } catch (LoginException eAnonymous
) {
90 throw new ArgeoException("Cannot initialize subject",
96 jsExecutor
= RWT
.getClient().getService(JavaScriptExecutor
.class);
97 browserNavigation
= RWT
.getClient().getService(BrowserNavigation
.class);
98 if (browserNavigation
!= null)
100 .addBrowserNavigationListener(new CmsNavigationListener());
104 protected Shell
createShell(Display display
) {
105 Shell shell
= super.createShell(display
);
106 shell
.setData(RWT
.CUSTOM_VARIANT
, CmsStyles
.CMS_SHELL
);
107 display
.disposeExec(new Runnable() {
111 if (log
.isTraceEnabled())
112 log
.trace("Logging out " + session
);
113 JcrUtils
.logoutQuietly(session
);
120 protected final void createContents(final Composite parent
) {
121 getShell().getDisplay().setData(CmsSession
.KEY
, this);
122 Subject
.doAs(subject
, new PrivilegedAction
<Void
>() {
127 } catch (Exception e
) {
128 throw new CmsException("Cannot create entrypoint contents",
137 protected abstract void createUi(Composite parent
);
139 /** Recreate UI after navigation or auth change */
140 protected abstract void refresh();
143 * The node to return when no node was found (for authenticated users and
146 protected Node
getDefaultNode(Session session
) throws RepositoryException
{
147 if (!session
.hasPermission(defaultPath
, "read")) {
148 if (session
.getUserID().equals("anonymous"))
149 throw new LoginRequiredException();
151 throw new CmsException("Unauthorized");
153 return session
.getNode(defaultPath
);
156 protected String
getBaseTitle() {
157 return factoryProperties
.get(WebClient
.PAGE_TITLE
);
160 public void navigateTo(String state
) {
162 String title
= setState(state
);
164 if (browserNavigation
!= null)
165 browserNavigation
.pushState(state
, title
);
169 public Subject
getSubject() {
174 public void authChange() {
175 Subject
.doAs(subject
, new PrivilegedAction
<Void
>() {
180 String currentPath
= null;
182 currentPath
= node
.getPath();
183 JcrUtils
.logoutQuietly(session
);
185 session
= repository
.login(workspace
);
186 if (currentPath
!= null)
188 node
= session
.getNode(currentPath
);
189 } catch (Exception e
) {
191 // TODO find a less hacky way to log out
192 new ArgeoLoginContext(
193 KernelHeader
.LOGIN_CONTEXT_ANONYMOUS
,
195 new ArgeoLoginContext(
196 KernelHeader
.LOGIN_CONTEXT_ANONYMOUS
,
198 } catch (LoginException eAnonymous
) {
199 throw new ArgeoException(
200 "Cannot reset to anonymous", eAnonymous
);
202 JcrUtils
.logoutQuietly(session
);
203 session
= repository
.login(workspace
);
210 } catch (RepositoryException e
) {
211 throw new CmsException("Cannot perform auth change", e
);
221 public void exception(final Throwable e
) {
222 AbstractCmsEntryPoint
.this.exception
= e
;
223 log
.error("Unexpected exception in CMS", e
);
227 protected void doRefresh() {
228 Subject
.doAs(subject
, new PrivilegedAction
<Void
>() {
238 public Object
local(Msg msg
) {
239 String key
= msg
.getId();
240 int lastDot
= key
.lastIndexOf('.');
241 String className
= key
.substring(0, lastDot
);
242 String fieldName
= key
.substring(lastDot
+ 1);
243 Locale locale
= RWT
.getLocale();
244 ResourceBundle rb
= ResourceBundle
.getBundle(className
, locale
,
245 msg
.getClassLoader());
246 return rb
.getString(fieldName
);
249 /** Sets the state of the entry point and retrieve the related JCR node. */
250 protected synchronized String
setState(String newState
) {
251 String previousState
= this.state
;
255 this.state
= newState
;
256 if (newState
.equals("~"))
260 int firstSlash
= state
.indexOf('/');
261 if (firstSlash
== 0) {
262 if (session
.nodeExists(state
))
263 node
= session
.getNode(state
);
265 throw new CmsException("Data " + state
+ " does not exist");
267 } else if (firstSlash
> 0) {
268 String prefix
= state
.substring(0, firstSlash
);
269 String path
= state
.substring(firstSlash
);
270 if (session
.nodeExists(path
))
271 node
= session
.getNode(path
);
273 throw new CmsException("Data " + path
+ " does not exist");
276 node
= getDefaultNode(session
);
282 if (node
.isNodeType(NodeType
.MIX_TITLE
)
283 && node
.hasProperty(Property
.JCR_TITLE
))
284 title
= node
.getProperty(Property
.JCR_TITLE
).getString()
285 + " - " + getBaseTitle();
287 title
= getBaseTitle();
288 jsExecutor
.execute("document.title = \"" + title
+ "\"");
290 if (log
.isTraceEnabled())
291 log
.trace("node=" + node
+ ", state=" + state
+ " (page="
292 + page
+ ", title=" + title
+ ")");
295 } catch (Exception e
) {
296 log
.error("Cannot set state '" + state
+ "'", e
);
297 if (previousState
.equals(""))
299 navigateTo(previousState
);
300 throw new CmsException("Unexpected issue when accessing #"
305 protected Node
getNode() {
309 protected String
getState() {
313 protected Throwable
getException() {
317 protected void resetException() {
321 protected Session
getSession() {
325 private class CmsNavigationListener
implements BrowserNavigationListener
{
326 private static final long serialVersionUID
= -3591018803430389270L;
329 public void navigated(BrowserNavigationEvent event
) {
330 setState(event
.getState());