]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.cms/src/org/argeo/cms/AbstractCmsEntryPoint.java
Rather take the first found image than the last one.
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / AbstractCmsEntryPoint.java
index 6722d7b86291051c61b39fc286f88d136ebc3a55..0a988a63e99d6087ea70490f977984b3aa1fe407 100644 (file)
@@ -1,65 +1,97 @@
 package org.argeo.cms;
 
-import java.util.Locale;
-import java.util.ResourceBundle;
+import java.security.PrivilegedAction;
+import java.util.HashMap;
+import java.util.Map;
 
 import javax.jcr.Node;
+import javax.jcr.PathNotFoundException;
+import javax.jcr.Property;
 import javax.jcr.Repository;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.nodetype.NodeType;
+import javax.security.auth.Subject;
+import javax.security.auth.login.CredentialNotFoundException;
+import javax.security.auth.login.LoginContext;
+import javax.security.auth.login.LoginException;
+import javax.servlet.http.HttpServletRequest;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.argeo.ArgeoException;
+import org.argeo.cms.auth.AuthConstants;
+import org.argeo.cms.auth.HttpRequestCallbackHandler;
+import org.argeo.eclipse.ui.specific.UiContext;
 import org.argeo.jcr.JcrUtils;
 import org.eclipse.rap.rwt.RWT;
 import org.eclipse.rap.rwt.application.AbstractEntryPoint;
+import org.eclipse.rap.rwt.client.WebClient;
 import org.eclipse.rap.rwt.client.service.BrowserNavigation;
 import org.eclipse.rap.rwt.client.service.BrowserNavigationEvent;
 import org.eclipse.rap.rwt.client.service.BrowserNavigationListener;
+import org.eclipse.rap.rwt.client.service.JavaScriptExecutor;
+import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Shell;
-import org.springframework.security.core.context.SecurityContext;
-import org.springframework.security.core.context.SecurityContextHolder;
 
 /** Manages history and navigation */
 public abstract class AbstractCmsEntryPoint extends AbstractEntryPoint
-               implements CmsSession {
+               implements CmsView {
        private final Log log = LogFactory.getLog(AbstractCmsEntryPoint.class);
-       private static final String SPRING_SECURITY_CONTEXT_KEY = "SPRING_SECURITY_CONTEXT";
 
-       private Repository repository;
-       private String workspace;
-       private Session session;
+       private final Subject subject;
+       private LoginContext loginContext;
+
+       private final Repository repository;
+       private final String workspace;
+       private final String defaultPath;
+       private final Map<String, String> factoryProperties;
 
-       // current state
+       // Current state
+       private Session session;
        private Node node;
+       private String nodePath;// useful when changing auth
        private String state;
        private String page;
        private Throwable exception;
 
-       private BrowserNavigation history;
-
-       public AbstractCmsEntryPoint(Repository repository, String workspace) {
-               if (SecurityContextHolder.getContext().getAuthentication() == null) {
-                       SecurityContext contextFromSessionObject = (SecurityContext) RWT
-                                       .getRequest().getSession()
-                                       .getAttribute(SPRING_SECURITY_CONTEXT_KEY);
-                       if (contextFromSessionObject != null)
-                               SecurityContextHolder.setContext(contextFromSessionObject);
-                       else
-                               logAsAnonymous();
-               }
+       // Client services
+       private final JavaScriptExecutor jsExecutor;
+       private final BrowserNavigation browserNavigation;
 
+       public AbstractCmsEntryPoint(Repository repository, String workspace,
+                       String defaultPath, Map<String, String> factoryProperties) {
                this.repository = repository;
                this.workspace = workspace;
-               authChange();
+               this.defaultPath = defaultPath;
+               this.factoryProperties = new HashMap<String, String>(factoryProperties);
+               subject = new Subject();
 
-               history = RWT.getClient().getService(BrowserNavigation.class);
-               if (history != null)
-                       history.addBrowserNavigationListener(new CmsNavigationListener());
+               // Initial login
+               try {
+                       loginContext = new LoginContext(AuthConstants.LOGIN_CONTEXT_USER,
+                                       subject, new HttpRequestCallbackHandler(
+                                                       UiContext.getHttpRequest()));
+                       loginContext.login();
+               } catch (CredentialNotFoundException e) {
+                       try {
+                               loginContext = new LoginContext(
+                                               AuthConstants.LOGIN_CONTEXT_ANONYMOUS, subject);
+                               loginContext.login();
+                       } catch (LoginException e1) {
+                               throw new ArgeoException("Cannot log as anonymous", e);
+                       }
+               } catch (LoginException e) {
+                       throw new ArgeoException("Cannot initialize subject", e);
+               }
+               authChange(loginContext);
 
-               // RWT.setLocale(Locale.FRANCE);
+               jsExecutor = RWT.getClient().getService(JavaScriptExecutor.class);
+               browserNavigation = RWT.getClient().getService(BrowserNavigation.class);
+               if (browserNavigation != null)
+                       browserNavigation
+                                       .addBrowserNavigationListener(new CmsNavigationListener());
        }
 
        @Override
@@ -78,170 +110,242 @@ public abstract class AbstractCmsEntryPoint extends AbstractEntryPoint
                return shell;
        }
 
-       /** Recreate header UI */
-       protected abstract void refreshHeader();
+       @Override
+       protected final void createContents(final Composite parent) {
+               UiContext.setData(CmsView.KEY, this);
+               Subject.doAs(subject, new PrivilegedAction<Void>() {
+                       @Override
+                       public Void run() {
+                               try {
+                                       initUi(parent);
+                               } catch (Exception e) {
+                                       throw new CmsException("Cannot create entrypoint contents",
+                                                       e);
+                               }
+                               return null;
+                       }
+               });
+       }
 
-       /** Recreate body UI */
-       protected abstract void refreshBody();
+       /** Create UI */
+       protected abstract void initUi(Composite parent);
 
-       /** Log as anonymous */
-       protected abstract void logAsAnonymous();
+       /** Recreate UI after navigation or auth change */
+       protected abstract void refresh();
 
        /**
         * The node to return when no node was found (for authenticated users and
         * anonymous)
         */
-       protected abstract Node getDefaultNode(Session session)
-                       throws RepositoryException;
-
-       /**
-        * Reasonable default since it is a nt:hierarchyNode and is thus compatible
-        * with the obvious default folder type, nt:folder, conceptual equivalent of
-        * an empty text file in an operating system. To be overridden.
-        */
-       protected String getDefaultNewNodeType() {
-               return CmsTypes.CMS_TEXT;
+       protected Node getDefaultNode(Session session) throws RepositoryException {
+               if (!session.hasPermission(defaultPath, "read")) {
+                       if (session.getUserID().equals(AuthConstants.ROLE_ANONYMOUS))
+                               // TODO throw a special exception
+                               throw new CmsException("Login required");
+                       else
+                               throw new CmsException("Unauthorized");
+               }
+               return session.getNode(defaultPath);
        }
 
-       /** Default new folder type (used in mkdirs) is nt:folder. To be overridden. */
-       protected String getDefaultNewFolderType() {
-               return NodeType.NT_FOLDER;
+       protected String getBaseTitle() {
+               return factoryProperties.get(WebClient.PAGE_TITLE);
        }
 
        public void navigateTo(String state) {
                exception = null;
-               setState(state);
-               refreshBody();
-               if (history != null)
-                       history.pushState(state, state);
+               String title = setState(state);
+               doRefresh();
+               if (browserNavigation != null)
+                       browserNavigation.pushState(state, title);
+       }
+
+       @Override
+       public synchronized Subject getSubject() {
+               return subject;
        }
 
        @Override
-       public void authChange() {
+       public synchronized void logout() {
+               if (loginContext == null)
+                       throw new CmsException("Login context should not be null");
                try {
-                       String currentPath = null;
-                       if (node != null)
-                               currentPath = node.getPath();
-                       JcrUtils.logoutQuietly(session);
-
-                       if (SecurityContextHolder.getContext().getAuthentication() == null)
-                               logAsAnonymous();
-                       session = repository.login(workspace);
-                       if (currentPath != null)
-                               node = session.getNode(currentPath);
-
-                       // refresh UI
-                       refreshHeader();
-                       refreshBody();
-               } catch (RepositoryException e) {
-                       throw new CmsException("Cannot perform auth change", e);
+                       loginContext.logout();
+                       LoginContext anonymousLc = new LoginContext(
+                                       AuthConstants.LOGIN_CONTEXT_ANONYMOUS, subject);
+                       anonymousLc.login();
+                       authChange(anonymousLc);
+               } catch (LoginException e) {
+                       throw new CmsException("Cannot logout", e);
                }
+       }
+
+       @Override
+       public synchronized void authChange(LoginContext loginContext) {
+               if (loginContext == null)
+                       throw new CmsException("Login context cannot be null");
+               this.loginContext = loginContext;
+               Subject.doAs(loginContext.getSubject(), new PrivilegedAction<Void>() {
+
+                       @Override
+                       public Void run() {
+                               try {
+                                       JcrUtils.logoutQuietly(session);
+                                       session = repository.login(workspace);
+                                       if (nodePath != null)
+                                               try {
+                                                       node = session.getNode(nodePath);
+                                               } catch (PathNotFoundException e) {
+                                                       // logout();
+                                                       // session = repository.login(workspace);
+                                                       navigateTo("~");
+                                                       throw e;
+                                               }
+
+                                       // refresh UI
+                                       doRefresh();
+                               } catch (RepositoryException e) {
+                                       throw new CmsException("Cannot perform auth change", e);
+                               }
+                               return null;
+                       }
+
+               });
 
        }
 
        @Override
-       public void exception(Throwable e) {
-               this.exception = e;
+       public void exception(final Throwable e) {
+               AbstractCmsEntryPoint.this.exception = e;
                log.error("Unexpected exception in CMS", e);
-               refreshBody();
+               doRefresh();
        }
 
-       @Override
-       public Object local(Msg msg) {
-               String key = msg.getId();
-               int lastDot = key.lastIndexOf('.');
-               String className = key.substring(0, lastDot);
-               String fieldName = key.substring(lastDot + 1);
-               Locale locale = RWT.getLocale();
-               ResourceBundle rb = ResourceBundle.getBundle(className, locale,
-                               msg.getClassLoader());
-               return rb.getString(fieldName);
+       protected synchronized void doRefresh() {
+               Subject.doAs(subject, new PrivilegedAction<Void>() {
+                       @Override
+                       public Void run() {
+                               refresh();
+                               return null;
+                       }
+               });
        }
 
        /** Sets the state of the entry point and retrieve the related JCR node. */
-       protected synchronized void setState(String newState) {
+       protected synchronized String setState(String newState) {
                String previousState = this.state;
 
-               node = null;
+               Node node = null;
                page = null;
                this.state = newState;
+               if (newState.equals("~"))
+                       this.state = "";
 
                try {
                        int firstSlash = state.indexOf('/');
                        if (firstSlash == 0) {
-                               if (!session.nodeExists(state))
-                                       node = addNode(session, state, null);
-                               else
+                               if (session.nodeExists(state))
                                        node = session.getNode(state);
+                               else
+                                       throw new CmsException("Data " + state + " does not exist");
                                page = "";
                        } else if (firstSlash > 0) {
                                String prefix = state.substring(0, firstSlash);
                                String path = state.substring(firstSlash);
-                               if (session.getWorkspace().getNodeTypeManager()
-                                               .hasNodeType(prefix)) {
-                                       String nodeType = prefix;
-                                       if (!session.nodeExists(path))
-                                               node = addNode(session, path, nodeType);
-                                       else {
-                                               node = session.getNode(path);
-                                               if (!node.isNodeType(nodeType))
-                                                       throw new CmsException("Node " + path
-                                                                       + " not of type " + nodeType);
-                                       }
-                               } else if ("delete".equals(prefix)) {
-                                       if (session.itemExists(path)) {
-                                               Node nodeToDelete = session.getNode(path);
-                                               // TODO "Are you sure?"
-                                               nodeToDelete.remove();
-                                               session.save();
-                                               log.debug("Deleted " + path);
-                                               navigateTo(previousState);
-                                       } else
-                                               throw new CmsException("Data " + path
-                                                               + " does not exist");
-                               } else {
-                                       if (session.itemExists(path))
-                                               node = session.getNode(path);
-                                       else
-                                               throw new CmsException("Data " + path
-                                                               + " does not exist");
-                               }
+                               if (session.nodeExists(path))
+                                       node = session.getNode(path);
+                               else
+                                       throw new CmsException("Data " + path + " does not exist");
                                page = prefix;
                        } else {
                                node = getDefaultNode(session);
-                               if (state.equals("~"))
-                                       page = "";
-                               else
-                                       page = state;
+                               page = state;
                        }
+                       setNode(node);
+                       String title = publishMetaData(node);
 
                        if (log.isTraceEnabled())
-                               log.trace("page=" + page + ", node=" + node + ", state="
-                                               + state);
-
-               } catch (RepositoryException e) {
-                       throw new CmsException("Cannot retrieve node", e);
+                               log.trace("node=" + node + ", state=" + state + " (page="
+                                               + page + ")");
+
+                       return title;
+               } catch (Exception e) {
+                       log.error("Cannot set state '" + state + "'", e);
+                       if (previousState.equals(""))
+                               previousState = "~";
+                       navigateTo(previousState);
+                       throw new CmsException("Unexpected issue when accessing #"
+                                       + newState, e);
                }
        }
 
-       protected Node addNode(Session session, String path, String nodeType)
-                       throws RepositoryException {
-               return JcrUtils.mkdirs(session, path, nodeType != null ? nodeType
-                               : getDefaultNewNodeType(), getDefaultNewFolderType(), false);
-               // not saved, so that the UI can discard it later on
+       private String publishMetaData(Node node) throws RepositoryException {
+               // Title
+               String title;
+               if (node.isNodeType(NodeType.MIX_TITLE)
+                               && node.hasProperty(Property.JCR_TITLE))
+                       title = node.getProperty(Property.JCR_TITLE).getString() + " - "
+                                       + getBaseTitle();
+               else
+                       title = getBaseTitle();
+
+               HttpServletRequest request = UiContext.getHttpRequest();
+               if (request == null)
+                       return null;
+               // String url = CmsUtils.getCanonicalUrl(node, request);
+               // String desc = node.hasProperty(JCR_DESCRIPTION) ? node.getProperty(
+               // JCR_DESCRIPTION).getString() : null;
+               // String imgUrl = null;
+               // for (NodeIterator it = node.getNodes(); it.hasNext();) {
+               // Node child = it.nextNode();
+               // if (child.isNodeType(CmsTypes.CMS_IMAGE))
+               // imgUrl = CmsUtils.getDataUrl(child, request);
+               // }
+
+               StringBuilder js = new StringBuilder();
+               js.append("document.title = '" + title + "';");
+               // js.append("var metas = document.getElementsByTagName('meta');");
+               // js.append("for (var i=0; i<metas.length; i++) {");
+               // js.append("  if (metas[i].getAttribute('property'))");
+               // js.append("   if(metas[i].getAttribute('property')=='og:title')");
+               // js.append("    metas[i].setAttribute('content','" + title + "');");
+               // js.append("   else if(metas[i].getAttribute('property')=='og:url')");
+               // js.append("    metas[i].setAttribute('content','" + url + "');");
+               // js.append("   else if(metas[i].getAttribute('property')=='og:type')");
+               // js.append("    metas[i].setAttribute('content','website');");
+               // if (desc != null) {
+               // js.append("   else if(metas[i].getAttribute('property')=='og:decription')");
+               // js.append("    metas[i].setAttribute('content','" + clean(desc)
+               // + "');");
+               // }
+               // if (imgUrl != null) {
+               // js.append("   else if(metas[i].getAttribute('property')=='og:image')");
+               // js.append("    metas[i].setAttribute('content','" + imgUrl + "');");
+               // } else {
+               // // TODO reset default image
+               // }
+               // js.append("  };");
+               jsExecutor.execute(js.toString());
+               return title;
        }
 
-       protected Node getNode() {
+       // Simply remove some illegal character
+       // private String clean(String stringToClean) {
+       // return stringToClean.replaceAll("'", "").replaceAll("\\n", "")
+       // .replaceAll("\\t", "");
+       // }
+
+       protected synchronized Node getNode() {
                return node;
        }
 
-       @Override
-       public String getState() {
-               return state;
+       private synchronized void setNode(Node node) throws RepositoryException {
+               this.node = node;
+               this.nodePath = node == null ? null : node.getPath();
        }
 
-       protected String getPage() {
-               return page;
+       protected String getState() {
+               return state;
        }
 
        protected Throwable getException() {
@@ -262,8 +366,7 @@ public abstract class AbstractCmsEntryPoint extends AbstractEntryPoint
                @Override
                public void navigated(BrowserNavigationEvent event) {
                        setState(event.getState());
-                       refreshBody();
+                       refresh();
                }
        }
-
-}
+}
\ No newline at end of file