3 import static javax
.jcr
.Property
.JCR_DESCRIPTION
;
5 import java
.security
.PrivilegedAction
;
6 import java
.util
.HashMap
;
10 import javax
.jcr
.NodeIterator
;
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
.CredentialNotFoundException
;
18 import javax
.security
.auth
.login
.LoginContext
;
19 import javax
.security
.auth
.login
.LoginException
;
20 import javax
.servlet
.http
.HttpServletRequest
;
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
.AuthConstants
;
26 import org
.argeo
.cms
.auth
.HttpRequestCallbackHandler
;
27 import org
.argeo
.cms
.util
.CmsUtils
;
28 import org
.argeo
.eclipse
.ui
.specific
.UiContext
;
29 import org
.argeo
.jcr
.JcrUtils
;
30 import org
.eclipse
.rap
.rwt
.RWT
;
31 import org
.eclipse
.rap
.rwt
.application
.AbstractEntryPoint
;
32 import org
.eclipse
.rap
.rwt
.client
.WebClient
;
33 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigation
;
34 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigationEvent
;
35 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigationListener
;
36 import org
.eclipse
.rap
.rwt
.client
.service
.JavaScriptExecutor
;
37 import org
.eclipse
.swt
.widgets
.Composite
;
38 import org
.eclipse
.swt
.widgets
.Display
;
39 import org
.eclipse
.swt
.widgets
.Shell
;
41 /** Manages history and navigation */
42 public abstract class AbstractCmsEntryPoint
extends AbstractEntryPoint
44 private final Log log
= LogFactory
.getLog(AbstractCmsEntryPoint
.class);
46 private final Subject subject
;
47 private LoginContext loginContext
;
49 private final Repository repository
;
50 private final String workspace
;
51 private final String defaultPath
;
52 private final Map
<String
, String
> factoryProperties
;
55 private Session session
;
59 private Throwable exception
;
62 private final JavaScriptExecutor jsExecutor
;
63 private final BrowserNavigation browserNavigation
;
65 public AbstractCmsEntryPoint(Repository repository
, String workspace
,
66 String defaultPath
, Map
<String
, String
> factoryProperties
) {
67 this.repository
= repository
;
68 this.workspace
= workspace
;
69 this.defaultPath
= defaultPath
;
70 this.factoryProperties
= new HashMap
<String
, String
>(factoryProperties
);
71 subject
= new Subject();
75 loginContext
= new LoginContext(AuthConstants
.LOGIN_CONTEXT_USER
,
76 subject
, new HttpRequestCallbackHandler(
77 UiContext
.getHttpRequest()));
79 } catch (CredentialNotFoundException e
) {
81 loginContext
= new LoginContext(
82 AuthConstants
.LOGIN_CONTEXT_ANONYMOUS
, subject
);
84 } catch (LoginException e1
) {
85 throw new ArgeoException("Cannot log as anonymous", e
);
87 } catch (LoginException e
) {
88 throw new ArgeoException("Cannot initialize subject", e
);
90 authChange(loginContext
);
92 jsExecutor
= RWT
.getClient().getService(JavaScriptExecutor
.class);
93 browserNavigation
= RWT
.getClient().getService(BrowserNavigation
.class);
94 if (browserNavigation
!= null)
96 .addBrowserNavigationListener(new CmsNavigationListener());
100 protected Shell
createShell(Display display
) {
101 Shell shell
= super.createShell(display
);
102 shell
.setData(RWT
.CUSTOM_VARIANT
, CmsStyles
.CMS_SHELL
);
103 display
.disposeExec(new Runnable() {
107 if (log
.isTraceEnabled())
108 log
.trace("Logging out " + session
);
109 JcrUtils
.logoutQuietly(session
);
116 protected final void createContents(final Composite parent
) {
117 UiContext
.setData(CmsView
.KEY
, this);
118 Subject
.doAs(subject
, new PrivilegedAction
<Void
>() {
123 } catch (Exception e
) {
124 throw new CmsException("Cannot create entrypoint contents",
133 protected abstract void initUi(Composite parent
);
135 /** Recreate UI after navigation or auth change */
136 protected abstract void refresh();
139 * The node to return when no node was found (for authenticated users and
142 protected Node
getDefaultNode(Session session
) throws RepositoryException
{
143 if (!session
.hasPermission(defaultPath
, "read")) {
144 if (session
.getUserID().equals(AuthConstants
.ROLE_ANONYMOUS
))
145 // TODO throw a special exception
146 throw new CmsException("Login required");
148 throw new CmsException("Unauthorized");
150 return session
.getNode(defaultPath
);
153 protected String
getBaseTitle() {
154 return factoryProperties
.get(WebClient
.PAGE_TITLE
);
157 public void navigateTo(String state
) {
159 String title
= setState(state
);
161 if (browserNavigation
!= null)
162 browserNavigation
.pushState(state
, title
);
166 public Subject
getSubject() {
171 public void logout() {
172 if (loginContext
== null)
173 throw new CmsException("Login context should not be null");
175 loginContext
.logout();
176 LoginContext anonymousLc
= new LoginContext(
177 AuthConstants
.LOGIN_CONTEXT_ANONYMOUS
, subject
);
179 authChange(anonymousLc
);
180 } catch (LoginException e
) {
181 throw new CmsException("Cannot logout", e
);
186 public void authChange(LoginContext loginContext
) {
187 if (loginContext
== null)
188 throw new CmsException("Login context cannot be null");
189 this.loginContext
= loginContext
;
190 Subject
.doAs(subject
, new PrivilegedAction
<Void
>() {
195 String currentPath
= null;
197 currentPath
= node
.getPath();
198 JcrUtils
.logoutQuietly(session
);
200 session
= repository
.login(workspace
);
201 if (currentPath
!= null)
203 node
= session
.getNode(currentPath
);
204 } catch (Exception e
) {
206 session
= repository
.login(workspace
);
213 } catch (RepositoryException e
) {
214 throw new CmsException("Cannot perform auth change", e
);
224 public void exception(final Throwable e
) {
225 AbstractCmsEntryPoint
.this.exception
= e
;
226 log
.error("Unexpected exception in CMS", e
);
230 protected void doRefresh() {
231 Subject
.doAs(subject
, new PrivilegedAction
<Void
>() {
240 /** Sets the state of the entry point and retrieve the related JCR node. */
241 protected synchronized String
setState(String newState
) {
242 String previousState
= this.state
;
246 this.state
= newState
;
247 if (newState
.equals("~"))
251 int firstSlash
= state
.indexOf('/');
252 if (firstSlash
== 0) {
253 if (session
.nodeExists(state
))
254 node
= session
.getNode(state
);
256 throw new CmsException("Data " + state
+ " does not exist");
258 } else if (firstSlash
> 0) {
259 String prefix
= state
.substring(0, firstSlash
);
260 String path
= state
.substring(firstSlash
);
261 if (session
.nodeExists(path
))
262 node
= session
.getNode(path
);
264 throw new CmsException("Data " + path
+ " does not exist");
267 node
= getDefaultNode(session
);
273 if (node
.isNodeType(NodeType
.MIX_TITLE
)
274 && node
.hasProperty(Property
.JCR_TITLE
))
275 title
= node
.getProperty(Property
.JCR_TITLE
).getString()
276 + " - " + getBaseTitle();
278 title
= getBaseTitle();
280 publishMetaData(title
);
282 if (log
.isTraceEnabled())
283 log
.trace("node=" + node
+ ", state=" + state
+ " (page="
284 + page
+ ", title=" + title
+ ")");
287 } catch (Exception e
) {
288 log
.error("Cannot set state '" + state
+ "'", e
);
289 if (previousState
.equals(""))
291 navigateTo(previousState
);
292 throw new CmsException("Unexpected issue when accessing #"
297 private void publishMetaData(String title
) throws RepositoryException
{
298 HttpServletRequest request
= UiContext
.getHttpRequest();
301 String url
= CmsUtils
.getCanonicalUrl(node
, request
);
302 String desc
= node
.hasProperty(JCR_DESCRIPTION
) ? node
.getProperty(
303 JCR_DESCRIPTION
).getString() : null;
304 String imgUrl
= null;
305 for (NodeIterator it
= node
.getNodes(); it
.hasNext();) {
306 Node child
= it
.nextNode();
307 if (child
.isNodeType(CmsTypes
.CMS_IMAGE
))
308 imgUrl
= CmsUtils
.getDataUrl(child
, request
);
311 StringBuilder js
= new StringBuilder();
312 js
.append("document.title = '" + title
+ "';");
313 // js.append("var metas = document.getElementsByTagName('meta');");
314 // js.append("for (var i=0; i<metas.length; i++) {");
315 // js.append(" if (metas[i].getAttribute('property'))");
316 // js.append(" if(metas[i].getAttribute('property')=='og:title')");
317 // js.append(" metas[i].setAttribute('content','" + title + "');");
318 // js.append(" else if(metas[i].getAttribute('property')=='og:url')");
319 // js.append(" metas[i].setAttribute('content','" + url + "');");
320 // js.append(" else if(metas[i].getAttribute('property')=='og:type')");
321 // js.append(" metas[i].setAttribute('content','website');");
322 // if (desc != null) {
323 // js.append(" else if(metas[i].getAttribute('property')=='og:decription')");
324 // js.append(" metas[i].setAttribute('content','" + clean(desc)
327 // if (imgUrl != null) {
328 // js.append(" else if(metas[i].getAttribute('property')=='og:image')");
329 // js.append(" metas[i].setAttribute('content','" + imgUrl + "');");
331 // // TODO reset default image
334 jsExecutor
.execute(js
.toString());
337 // Simply remove some illegal character
338 // private String clean(String stringToClean) {
339 // return stringToClean.replaceAll("'", "").replaceAll("\\n", "")
340 // .replaceAll("\\t", "");
343 protected Node
getNode() {
347 protected String
getState() {
351 protected Throwable
getException() {
355 protected void resetException() {
359 protected Session
getSession() {
363 private class CmsNavigationListener
implements BrowserNavigationListener
{
364 private static final long serialVersionUID
= -3591018803430389270L;
367 public void navigated(BrowserNavigationEvent event
) {
368 setState(event
.getState());