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
.cms
.ui
.UxContext
;
24 import org
.argeo
.eclipse
.ui
.specific
.UiContext
;
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
;
43 private LoginContext loginContext
;
45 private final Repository repository
;
46 private final String workspace
;
47 private final String defaultPath
;
48 private final Map
<String
, String
> factoryProperties
;
51 private Session session
;
55 private Throwable exception
;
58 private final JavaScriptExecutor jsExecutor
;
59 private final BrowserNavigation browserNavigation
;
61 public AbstractCmsEntryPoint(Repository repository
, String workspace
,
62 String defaultPath
, Map
<String
, String
> factoryProperties
) {
63 this.repository
= repository
;
64 this.workspace
= workspace
;
65 this.defaultPath
= defaultPath
;
66 this.factoryProperties
= new HashMap
<String
, String
>(factoryProperties
);
67 subject
= new Subject();
71 loginContext
= new LoginContext(AuthConstants
.LOGIN_CONTEXT_USER
,
72 subject
, new HttpRequestCallbackHandler(
73 UiContext
.getHttpRequest()));
75 } catch (CredentialNotFoundException e
) {
77 loginContext
= new LoginContext(
78 AuthConstants
.LOGIN_CONTEXT_ANONYMOUS
, subject
);
80 } catch (LoginException e1
) {
81 throw new ArgeoException("Cannot log as anonymous", e
);
83 } catch (LoginException e
) {
84 throw new ArgeoException("Cannot initialize subject", e
);
86 authChange(loginContext
);
88 jsExecutor
= RWT
.getClient().getService(JavaScriptExecutor
.class);
89 browserNavigation
= RWT
.getClient().getService(BrowserNavigation
.class);
90 if (browserNavigation
!= null)
92 .addBrowserNavigationListener(new CmsNavigationListener());
96 protected Shell
createShell(Display display
) {
97 Shell shell
= super.createShell(display
);
98 shell
.setData(RWT
.CUSTOM_VARIANT
, CmsStyles
.CMS_SHELL
);
99 display
.disposeExec(new Runnable() {
103 if (log
.isTraceEnabled())
104 log
.trace("Logging out " + session
);
105 JcrUtils
.logoutQuietly(session
);
112 protected final void createContents(final Composite parent
) {
113 UiContext
.setData(CmsView
.KEY
, this);
114 Subject
.doAs(subject
, new PrivilegedAction
<Void
>() {
119 } catch (Exception e
) {
120 throw new CmsException("Cannot create entrypoint contents",
129 protected abstract void initUi(Composite parent
);
131 /** Recreate UI after navigation or auth change */
132 protected abstract void refresh();
135 * The node to return when no node was found (for authenticated users and
138 protected Node
getDefaultNode(Session session
) throws RepositoryException
{
139 if (!session
.hasPermission(defaultPath
, "read")) {
140 if (session
.getUserID().equals(AuthConstants
.ROLE_ANONYMOUS
))
141 // TODO throw a special exception
142 throw new CmsException("Login required");
144 throw new CmsException("Unauthorized");
146 return session
.getNode(defaultPath
);
149 protected String
getBaseTitle() {
150 return factoryProperties
.get(WebClient
.PAGE_TITLE
);
153 public void navigateTo(String state
) {
155 String title
= setState(state
);
157 if (browserNavigation
!= null)
158 browserNavigation
.pushState(state
, title
);
162 public Subject
getSubject() {
167 public void logout() {
168 if (loginContext
== null)
169 throw new CmsException("Login context should not be null");
171 loginContext
.logout();
172 LoginContext anonymousLc
= new LoginContext(
173 AuthConstants
.LOGIN_CONTEXT_ANONYMOUS
, subject
);
175 authChange(anonymousLc
);
176 } catch (LoginException e
) {
177 throw new CmsException("Cannot logout", e
);
182 public void authChange(LoginContext loginContext
) {
183 if (loginContext
== null)
184 throw new CmsException("Login context cannot be null");
185 this.loginContext
= loginContext
;
186 Subject
.doAs(subject
, new PrivilegedAction
<Void
>() {
191 String currentPath
= null;
193 currentPath
= node
.getPath();
194 JcrUtils
.logoutQuietly(session
);
196 session
= repository
.login(workspace
);
197 if (currentPath
!= null)
199 node
= session
.getNode(currentPath
);
200 } catch (Exception e
) {
202 session
= repository
.login(workspace
);
209 } catch (RepositoryException e
) {
210 throw new CmsException("Cannot perform auth change", e
);
220 public void exception(final Throwable e
) {
221 AbstractCmsEntryPoint
.this.exception
= e
;
222 log
.error("Unexpected exception in CMS", e
);
226 protected void doRefresh() {
227 Subject
.doAs(subject
, new PrivilegedAction
<Void
>() {
237 // public Object local(Msg msg) {
238 // String key = msg.getId();
239 // int lastDot = key.lastIndexOf('.');
240 // String className = key.substring(0, lastDot);
241 // String fieldName = key.substring(lastDot + 1);
242 // Locale locale = RWT.getLocale();
243 // ResourceBundle rb = ResourceBundle.getBundle(className, locale,
244 // msg.getClassLoader());
245 // return rb.getString(fieldName);
248 /** Sets the state of the entry point and retrieve the related JCR node. */
249 protected synchronized String
setState(String newState
) {
250 String previousState
= this.state
;
254 this.state
= newState
;
255 if (newState
.equals("~"))
259 int firstSlash
= state
.indexOf('/');
260 if (firstSlash
== 0) {
261 if (session
.nodeExists(state
))
262 node
= session
.getNode(state
);
264 throw new CmsException("Data " + state
+ " does not exist");
266 } else if (firstSlash
> 0) {
267 String prefix
= state
.substring(0, firstSlash
);
268 String path
= state
.substring(firstSlash
);
269 if (session
.nodeExists(path
))
270 node
= session
.getNode(path
);
272 throw new CmsException("Data " + path
+ " does not exist");
275 node
= getDefaultNode(session
);
281 if (node
.isNodeType(NodeType
.MIX_TITLE
)
282 && node
.hasProperty(Property
.JCR_TITLE
))
283 title
= node
.getProperty(Property
.JCR_TITLE
).getString()
284 + " - " + getBaseTitle();
286 title
= getBaseTitle();
287 jsExecutor
.execute("document.title = \"" + title
+ "\"");
289 if (log
.isTraceEnabled())
290 log
.trace("node=" + node
+ ", state=" + state
+ " (page="
291 + page
+ ", title=" + title
+ ")");
294 } catch (Exception e
) {
295 log
.error("Cannot set state '" + state
+ "'", e
);
296 if (previousState
.equals(""))
298 navigateTo(previousState
);
299 throw new CmsException("Unexpected issue when accessing #"
304 protected Node
getNode() {
308 protected String
getState() {
312 protected Throwable
getException() {
316 protected void resetException() {
320 protected Session
getSession() {
324 private class CmsNavigationListener
implements BrowserNavigationListener
{
325 private static final long serialVersionUID
= -3591018803430389270L;
328 public void navigated(BrowserNavigationEvent event
) {
329 setState(event
.getState());