3 import java
.security
.PrivilegedAction
;
4 import java
.util
.HashMap
;
8 import javax
.jcr
.Property
;
9 import javax
.jcr
.Repository
;
10 import javax
.jcr
.RepositoryException
;
11 import javax
.jcr
.Session
;
12 import javax
.jcr
.nodetype
.NodeType
;
13 import javax
.security
.auth
.Subject
;
14 import javax
.security
.auth
.login
.CredentialNotFoundException
;
15 import javax
.security
.auth
.login
.LoginContext
;
16 import javax
.security
.auth
.login
.LoginException
;
18 import org
.apache
.commons
.logging
.Log
;
19 import org
.apache
.commons
.logging
.LogFactory
;
20 import org
.argeo
.ArgeoException
;
21 import org
.argeo
.cms
.auth
.AuthConstants
;
22 import org
.argeo
.cms
.auth
.HttpRequestCallbackHandler
;
23 import org
.argeo
.eclipse
.ui
.specific
.UiContext
;
24 import org
.argeo
.jcr
.JcrUtils
;
25 import org
.eclipse
.rap
.rwt
.RWT
;
26 import org
.eclipse
.rap
.rwt
.application
.AbstractEntryPoint
;
27 import org
.eclipse
.rap
.rwt
.client
.WebClient
;
28 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigation
;
29 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigationEvent
;
30 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigationListener
;
31 import org
.eclipse
.rap
.rwt
.client
.service
.JavaScriptExecutor
;
32 import org
.eclipse
.swt
.widgets
.Composite
;
33 import org
.eclipse
.swt
.widgets
.Display
;
34 import org
.eclipse
.swt
.widgets
.Shell
;
36 /** Manages history and navigation */
37 public abstract class AbstractCmsEntryPoint
extends AbstractEntryPoint
39 private final Log log
= LogFactory
.getLog(AbstractCmsEntryPoint
.class);
41 private final Subject subject
;
42 private LoginContext loginContext
;
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
);
66 subject
= new Subject();
70 loginContext
= new LoginContext(AuthConstants
.LOGIN_CONTEXT_USER
,
71 subject
, new HttpRequestCallbackHandler(
72 UiContext
.getHttpRequest()));
74 } catch (CredentialNotFoundException e
) {
76 loginContext
= new LoginContext(
77 AuthConstants
.LOGIN_CONTEXT_ANONYMOUS
, subject
);
79 } catch (LoginException e1
) {
80 throw new ArgeoException("Cannot log as anonymous", e
);
82 } catch (LoginException e
) {
83 throw new ArgeoException("Cannot initialize subject", e
);
85 authChange(loginContext
);
87 jsExecutor
= RWT
.getClient().getService(JavaScriptExecutor
.class);
88 browserNavigation
= RWT
.getClient().getService(BrowserNavigation
.class);
89 if (browserNavigation
!= null)
91 .addBrowserNavigationListener(new CmsNavigationListener());
95 protected Shell
createShell(Display display
) {
96 Shell shell
= super.createShell(display
);
97 shell
.setData(RWT
.CUSTOM_VARIANT
, CmsStyles
.CMS_SHELL
);
98 display
.disposeExec(new Runnable() {
102 if (log
.isTraceEnabled())
103 log
.trace("Logging out " + session
);
104 JcrUtils
.logoutQuietly(session
);
111 protected final void createContents(final Composite parent
) {
112 UiContext
.setData(CmsView
.KEY
, this);
113 Subject
.doAs(subject
, new PrivilegedAction
<Void
>() {
118 } catch (Exception e
) {
119 throw new CmsException("Cannot create entrypoint contents",
128 protected abstract void initUi(Composite parent
);
130 /** Recreate UI after navigation or auth change */
131 protected abstract void refresh();
134 * The node to return when no node was found (for authenticated users and
137 protected Node
getDefaultNode(Session session
) throws RepositoryException
{
138 if (!session
.hasPermission(defaultPath
, "read")) {
139 if (session
.getUserID().equals(AuthConstants
.ROLE_ANONYMOUS
))
140 // TODO throw a special exception
141 throw new CmsException("Login required");
143 throw new CmsException("Unauthorized");
145 return session
.getNode(defaultPath
);
148 protected String
getBaseTitle() {
149 return factoryProperties
.get(WebClient
.PAGE_TITLE
);
152 public void navigateTo(String state
) {
154 String title
= setState(state
);
156 if (browserNavigation
!= null)
157 browserNavigation
.pushState(state
, title
);
161 public Subject
getSubject() {
166 public void logout() {
167 if (loginContext
== null)
168 throw new CmsException("Login context should not be null");
170 loginContext
.logout();
171 LoginContext anonymousLc
= new LoginContext(
172 AuthConstants
.LOGIN_CONTEXT_ANONYMOUS
, subject
);
174 authChange(anonymousLc
);
175 } catch (LoginException e
) {
176 throw new CmsException("Cannot logout", e
);
181 public void authChange(LoginContext loginContext
) {
182 if (loginContext
== null)
183 throw new CmsException("Login context cannot be null");
184 this.loginContext
= loginContext
;
185 Subject
.doAs(subject
, new PrivilegedAction
<Void
>() {
190 String currentPath
= null;
192 currentPath
= node
.getPath();
193 JcrUtils
.logoutQuietly(session
);
195 session
= repository
.login(workspace
);
196 if (currentPath
!= null)
198 node
= session
.getNode(currentPath
);
199 } catch (Exception e
) {
201 session
= repository
.login(workspace
);
208 } catch (RepositoryException e
) {
209 throw new CmsException("Cannot perform auth change", e
);
219 public void exception(final Throwable e
) {
220 AbstractCmsEntryPoint
.this.exception
= e
;
221 log
.error("Unexpected exception in CMS", e
);
225 protected void doRefresh() {
226 Subject
.doAs(subject
, new PrivilegedAction
<Void
>() {
236 // public Object local(Msg msg) {
237 // String key = msg.getId();
238 // int lastDot = key.lastIndexOf('.');
239 // String className = key.substring(0, lastDot);
240 // String fieldName = key.substring(lastDot + 1);
241 // Locale locale = RWT.getLocale();
242 // ResourceBundle rb = ResourceBundle.getBundle(className, locale,
243 // msg.getClassLoader());
244 // return rb.getString(fieldName);
247 /** Sets the state of the entry point and retrieve the related JCR node. */
248 protected synchronized String
setState(String newState
) {
249 String previousState
= this.state
;
253 this.state
= newState
;
254 if (newState
.equals("~"))
258 int firstSlash
= state
.indexOf('/');
259 if (firstSlash
== 0) {
260 if (session
.nodeExists(state
))
261 node
= session
.getNode(state
);
263 throw new CmsException("Data " + state
+ " does not exist");
265 } else if (firstSlash
> 0) {
266 String prefix
= state
.substring(0, firstSlash
);
267 String path
= state
.substring(firstSlash
);
268 if (session
.nodeExists(path
))
269 node
= session
.getNode(path
);
271 throw new CmsException("Data " + path
+ " does not exist");
274 node
= getDefaultNode(session
);
280 if (node
.isNodeType(NodeType
.MIX_TITLE
)
281 && node
.hasProperty(Property
.JCR_TITLE
))
282 title
= node
.getProperty(Property
.JCR_TITLE
).getString()
283 + " - " + getBaseTitle();
285 title
= getBaseTitle();
286 jsExecutor
.execute("document.title = \"" + title
+ "\"");
288 if (log
.isTraceEnabled())
289 log
.trace("node=" + node
+ ", state=" + state
+ " (page="
290 + page
+ ", title=" + title
+ ")");
293 } catch (Exception e
) {
294 log
.error("Cannot set state '" + state
+ "'", e
);
295 if (previousState
.equals(""))
297 navigateTo(previousState
);
298 throw new CmsException("Unexpected issue when accessing #"
303 protected Node
getNode() {
307 protected String
getState() {
311 protected Throwable
getException() {
315 protected void resetException() {
319 protected Session
getSession() {
323 private class CmsNavigationListener
implements BrowserNavigationListener
{
324 private static final long serialVersionUID
= -3591018803430389270L;
327 public void navigated(BrowserNavigationEvent event
) {
328 setState(event
.getState());