3 import java
.security
.PrivilegedAction
;
4 import java
.util
.HashMap
;
8 import javax
.jcr
.NodeIterator
;
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
.CredentialNotFoundException
;
16 import javax
.security
.auth
.login
.LoginContext
;
17 import javax
.security
.auth
.login
.LoginException
;
18 import javax
.servlet
.http
.HttpServletRequest
;
20 import org
.apache
.commons
.logging
.Log
;
21 import org
.apache
.commons
.logging
.LogFactory
;
22 import org
.argeo
.ArgeoException
;
23 import org
.argeo
.cms
.auth
.AuthConstants
;
24 import org
.argeo
.cms
.auth
.HttpRequestCallbackHandler
;
25 import org
.argeo
.cms
.util
.CmsUtils
;
26 import org
.argeo
.eclipse
.ui
.specific
.UiContext
;
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
42 private final Log log
= LogFactory
.getLog(AbstractCmsEntryPoint
.class);
44 private final Subject subject
;
45 private LoginContext loginContext
;
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
);
69 subject
= new Subject();
73 loginContext
= new LoginContext(AuthConstants
.LOGIN_CONTEXT_USER
,
74 subject
, new HttpRequestCallbackHandler(
75 UiContext
.getHttpRequest()));
77 } catch (CredentialNotFoundException e
) {
79 loginContext
= new LoginContext(
80 AuthConstants
.LOGIN_CONTEXT_ANONYMOUS
, subject
);
82 } catch (LoginException e1
) {
83 throw new ArgeoException("Cannot log as anonymous", e
);
85 } catch (LoginException e
) {
86 throw new ArgeoException("Cannot initialize subject", e
);
88 authChange(loginContext
);
90 jsExecutor
= RWT
.getClient().getService(JavaScriptExecutor
.class);
91 browserNavigation
= RWT
.getClient().getService(BrowserNavigation
.class);
92 if (browserNavigation
!= null)
94 .addBrowserNavigationListener(new CmsNavigationListener());
98 protected Shell
createShell(Display display
) {
99 Shell shell
= super.createShell(display
);
100 shell
.setData(RWT
.CUSTOM_VARIANT
, CmsStyles
.CMS_SHELL
);
101 display
.disposeExec(new Runnable() {
105 if (log
.isTraceEnabled())
106 log
.trace("Logging out " + session
);
107 JcrUtils
.logoutQuietly(session
);
114 protected final void createContents(final Composite parent
) {
115 UiContext
.setData(CmsView
.KEY
, this);
116 Subject
.doAs(subject
, new PrivilegedAction
<Void
>() {
121 } catch (Exception e
) {
122 throw new CmsException("Cannot create entrypoint contents",
131 protected abstract void initUi(Composite parent
);
133 /** Recreate UI after navigation or auth change */
134 protected abstract void refresh();
137 * The node to return when no node was found (for authenticated users and
140 protected Node
getDefaultNode(Session session
) throws RepositoryException
{
141 if (!session
.hasPermission(defaultPath
, "read")) {
142 if (session
.getUserID().equals(AuthConstants
.ROLE_ANONYMOUS
))
143 // TODO throw a special exception
144 throw new CmsException("Login required");
146 throw new CmsException("Unauthorized");
148 return session
.getNode(defaultPath
);
151 protected String
getBaseTitle() {
152 return factoryProperties
.get(WebClient
.PAGE_TITLE
);
155 public void navigateTo(String state
) {
157 String title
= setState(state
);
159 if (browserNavigation
!= null)
160 browserNavigation
.pushState(state
, title
);
164 public Subject
getSubject() {
169 public void logout() {
170 if (loginContext
== null)
171 throw new CmsException("Login context should not be null");
173 loginContext
.logout();
174 LoginContext anonymousLc
= new LoginContext(
175 AuthConstants
.LOGIN_CONTEXT_ANONYMOUS
, subject
);
177 authChange(anonymousLc
);
178 } catch (LoginException e
) {
179 throw new CmsException("Cannot logout", e
);
184 public void authChange(LoginContext loginContext
) {
185 if (loginContext
== null)
186 throw new CmsException("Login context cannot be null");
187 this.loginContext
= loginContext
;
188 Subject
.doAs(subject
, new PrivilegedAction
<Void
>() {
193 String currentPath
= null;
195 currentPath
= node
.getPath();
196 JcrUtils
.logoutQuietly(session
);
198 session
= repository
.login(workspace
);
199 if (currentPath
!= null)
201 node
= session
.getNode(currentPath
);
202 } catch (Exception e
) {
204 session
= repository
.login(workspace
);
211 } catch (RepositoryException e
) {
212 throw new CmsException("Cannot perform auth change", e
);
222 public void exception(final Throwable e
) {
223 AbstractCmsEntryPoint
.this.exception
= e
;
224 log
.error("Unexpected exception in CMS", e
);
228 protected void doRefresh() {
229 Subject
.doAs(subject
, new PrivilegedAction
<Void
>() {
238 /** Sets the state of the entry point and retrieve the related JCR node. */
239 protected synchronized String
setState(String newState
) {
240 String previousState
= this.state
;
244 this.state
= newState
;
245 if (newState
.equals("~"))
249 int firstSlash
= state
.indexOf('/');
250 if (firstSlash
== 0) {
251 if (session
.nodeExists(state
))
252 node
= session
.getNode(state
);
254 throw new CmsException("Data " + state
+ " does not exist");
256 } else if (firstSlash
> 0) {
257 String prefix
= state
.substring(0, firstSlash
);
258 String path
= state
.substring(firstSlash
);
259 if (session
.nodeExists(path
))
260 node
= session
.getNode(path
);
262 throw new CmsException("Data " + path
+ " does not exist");
265 node
= getDefaultNode(session
);
271 if (node
.isNodeType(NodeType
.MIX_TITLE
)
272 && node
.hasProperty(Property
.JCR_TITLE
))
273 title
= node
.getProperty(Property
.JCR_TITLE
).getString()
274 + " - " + getBaseTitle();
276 title
= getBaseTitle();
278 publishMetaData(title
);
280 if (log
.isTraceEnabled())
281 log
.trace("node=" + node
+ ", state=" + state
+ " (page="
282 + page
+ ", title=" + title
+ ")");
285 } catch (Exception e
) {
286 log
.error("Cannot set state '" + state
+ "'", e
);
287 if (previousState
.equals(""))
289 navigateTo(previousState
);
290 throw new CmsException("Unexpected issue when accessing #"
295 private void publishMetaData(String title
) throws RepositoryException
{
296 HttpServletRequest request
= UiContext
.getHttpRequest();
299 String url
= CmsUtils
.getCanonicalUrl(node
, request
);
300 String imgUrl
= null;
301 for (NodeIterator it
= node
.getNodes(); it
.hasNext();) {
302 Node child
= it
.nextNode();
303 if (child
.isNodeType(CmsTypes
.CMS_IMAGE
))
304 imgUrl
= CmsUtils
.getDataUrl(child
, request
);
307 StringBuilder js
= new StringBuilder();
308 js
.append("document.title = '" + title
+ "';");
309 js
.append("var metas = document.getElementsByTagName('meta');");
310 js
.append("for (var i=0; i<metas.length; i++) {");
311 js
.append(" if (metas[i].getAttribute('property'))");
312 js
.append(" if(metas[i].getAttribute('property')=='og:title')");
313 js
.append(" metas[i].setAttribute('content','" + title
+ "');");
314 js
.append(" else if(metas[i].getAttribute('property')=='og:url')");
315 js
.append(" metas[i].setAttribute('content','" + url
+ "');");
316 if (imgUrl
!= null) {
317 js
.append(" else if(metas[i].getAttribute('property')=='og:image')");
318 js
.append(" metas[i].setAttribute('content','" + imgUrl
+ "');");
320 // TODO reset default image
323 jsExecutor
.execute(js
.toString());
326 protected Node
getNode() {
330 protected String
getState() {
334 protected Throwable
getException() {
338 protected void resetException() {
342 protected Session
getSession() {
346 private class CmsNavigationListener
implements BrowserNavigationListener
{
347 private static final long serialVersionUID
= -3591018803430389270L;
350 public void navigated(BrowserNavigationEvent event
) {
351 setState(event
.getState());