]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.cms/src/org/argeo/cms/CmsApplication.java
- Window title based on JCR
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / CmsApplication.java
index 90df56a61a967d65c2c46ab4ba1ee8bb8252b608..2f2697d5a0e8a2d465f64f8a4643fb19f08f14b8 100644 (file)
@@ -2,8 +2,6 @@ package org.argeo.cms;
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.PrintWriter;
-import java.io.StringWriter;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
@@ -18,10 +16,13 @@ import javax.jcr.Session;
 import javax.jcr.security.Privilege;
 import javax.jcr.version.VersionManager;
 
-import org.apache.commons.io.IOUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.argeo.cms.auth.LoginRequiredException;
 import org.argeo.cms.internal.ImageManagerImpl;
+import org.argeo.cms.util.BundleResourceLoader;
+import org.argeo.cms.util.CmsUtils;
+import org.argeo.cms.util.SystemNotifications;
 import org.argeo.jcr.JcrUtils;
 import org.eclipse.gemini.blueprint.context.BundleContextAware;
 import org.eclipse.rap.rwt.RWT;
@@ -38,23 +39,18 @@ import org.eclipse.swt.layout.FillLayout;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
-import org.eclipse.swt.widgets.Label;
-import org.eclipse.swt.widgets.Text;
 import org.osgi.framework.BundleContext;
 
 /** Configures an Argeo CMS RWT application. */
 public class CmsApplication implements CmsConstants, ApplicationConfiguration,
                BundleContextAware {
-       final static Log log = LogFactory.getLog(CmsApplication.class);
+       private final static Log log = LogFactory.getLog(CmsApplication.class);
 
-       // private Map<String, EntryPointFactory> entryPoints = new HashMap<String,
-       // EntryPointFactory>();
        private Map<String, Map<String, String>> branding = new HashMap<String, Map<String, String>>();
        private Map<String, List<String>> styleSheets = new HashMap<String, List<String>>();
 
        private List<String> resources = new ArrayList<String>();
 
-       // private Bundle clientScriptingBundle;
        private BundleContext bundleContext;
 
        private Repository repository;
@@ -63,8 +59,6 @@ public class CmsApplication implements CmsConstants, ApplicationConfiguration,
        private List<String> roPrincipals = Arrays.asList("anonymous", "everyone");
        private List<String> rwPrincipals = Arrays.asList("everyone");
 
-       private CmsLogin cmsLogin;
-
        private CmsUiProvider header;
        private Map<String, CmsUiProvider> pages = new LinkedHashMap<String, CmsUiProvider>();
 
@@ -124,8 +118,12 @@ public class CmsApplication implements CmsConstants, ApplicationConfiguration,
                                if (!properties.containsKey(WebClient.BODY_HTML))
                                        properties.put(WebClient.BODY_HTML, DEFAULT_LOADING_BODY);
 
+                               //
+                               // ADD ENTRY POINT
+                               //
                                application.addEntryPoint("/" + page, new CmsEntryPointFactory(
-                                               page), properties);
+                                               pages.get(page), repository, workspace, properties),
+                                               properties);
                                log.info("Registered entry point /" + page);
                        }
 
@@ -147,13 +145,6 @@ public class CmsApplication implements CmsConstants, ApplicationConfiguration,
        }
 
        public void init() throws RepositoryException {
-               if (workspace == null)
-                       throw new CmsException(
-                                       "Workspace must be set when calling initialization."
-                                                       + " Please make sure that read-only and read-write roles"
-                                                       + " have been properly configured:"
-                                                       + " the defaults are open.");
-
                Session session = null;
                try {
                        session = JcrUtils.loginOrCreateWorkspace(repository, workspace);
@@ -209,8 +200,10 @@ public class CmsApplication implements CmsConstants, ApplicationConfiguration,
                this.workspace = workspace;
        }
 
-       public void setCmsLogin(CmsLogin cmsLogin) {
-               this.cmsLogin = cmsLogin;
+       public void setCmsLogin(@SuppressWarnings("deprecation") CmsLogin cmsLogin) {
+               // this.cmsLogin = cmsLogin;
+               log.warn("cmsLogin"
+                               + " is deprecated and will be removed soon. Adapt your configuration ASAP.");
        }
 
        public void setHeader(CmsUiProvider header) {
@@ -237,11 +230,6 @@ public class CmsApplication implements CmsConstants, ApplicationConfiguration,
                this.headerHeight = headerHeight;
        }
 
-       // public void setEntryPoints(
-       // Map<String, EntryPointFactory> entryPointFactories) {
-       // this.entryPoints = entryPointFactories;
-       // }
-
        public void setBranding(Map<String, Map<String, String>> branding) {
                this.branding = branding;
        }
@@ -268,16 +256,23 @@ public class CmsApplication implements CmsConstants, ApplicationConfiguration,
        }
 
        private class CmsEntryPointFactory implements EntryPointFactory {
-               private final String page;
+               private final CmsUiProvider page;
+               private final Repository repository;
+               private final String workspace;
+               private final Map<String, String> properties;
 
-               public CmsEntryPointFactory(String page) {
+               public CmsEntryPointFactory(CmsUiProvider page, Repository repository,
+                               String workspace, Map<String, String> properties) {
                        this.page = page;
+                       this.repository = repository;
+                       this.workspace = workspace;
+                       this.properties = properties;
                }
 
                @Override
                public EntryPoint create() {
                        CmsEntryPoint entryPoint = new CmsEntryPoint(repository, workspace,
-                                       pages.get(page));
+                                       page, properties);
                        entryPoint.setState("");
                        CmsSession.current.set(entryPoint);
                        return entryPoint;
@@ -291,8 +286,8 @@ public class CmsApplication implements CmsConstants, ApplicationConfiguration,
                private final CmsUiProvider uiProvider;
 
                public CmsEntryPoint(Repository repository, String workspace,
-                               CmsUiProvider uiProvider) {
-                       super(repository, workspace);
+                               CmsUiProvider uiProvider, Map<String, String> factoryProperties) {
+                       super(repository, workspace, factoryProperties);
                        this.uiProvider = uiProvider;
                }
 
@@ -317,7 +312,9 @@ public class CmsApplication implements CmsConstants, ApplicationConfiguration,
                                bodyArea.setData(RWT.CUSTOM_VARIANT, CmsStyles.CMS_BODY);
                                bodyArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
                                                true));
-                               bodyArea.setBackgroundMode(SWT.INHERIT_DEFAULT);
+                               // Should not be set here: it then prevent all children
+                               // composite to define a background color via CSS
+                               // bodyArea.setBackgroundMode(SWT.INHERIT_DEFAULT);
                                bodyArea.setLayout(CmsUtils.noSpaceGridLayout());
                                refreshBody();
                        } catch (Exception e) {
@@ -343,60 +340,47 @@ public class CmsApplication implements CmsConstants, ApplicationConfiguration,
                protected void refreshBody() {
                        if (bodyArea == null)
                                return;
-                       // clear
-                       for (Control child : bodyArea.getChildren())
-                               child.dispose();
-                       bodyArea.setLayout(CmsUtils.noSpaceGridLayout());
-
                        // Exception
                        Throwable exception = getException();
                        if (exception != null) {
-                               new Label(bodyArea, SWT.NONE).setText("Unreachable state : "
-                                               + getState());
-                               if (getNode() != null)
-                                       new Label(bodyArea, SWT.NONE).setText("Context : "
-                                                       + getNode());
-
-                               Text errorText = new Text(bodyArea, SWT.MULTI | SWT.H_SCROLL
-                                               | SWT.V_SCROLL);
-                               errorText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
-                                               true));
-                               StringWriter sw = new StringWriter();
-                               exception.printStackTrace(new PrintWriter(sw));
-                               errorText.setText(sw.toString());
-                               IOUtils.closeQuietly(sw);
+                               // new Label(bodyArea, SWT.NONE).setText("Unreachable state : "
+                               // + getState());
+                               // if (getNode() != null)
+                               // new Label(bodyArea, SWT.NONE).setText("Context : "
+                               // + getNode());
+                               //
+                               // Text errorText = new Text(bodyArea, SWT.MULTI | SWT.H_SCROLL
+                               // | SWT.V_SCROLL);
+                               // errorText.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
+                               // true,
+                               // true));
+                               // StringWriter sw = new StringWriter();
+                               // exception.printStackTrace(new PrintWriter(sw));
+                               // errorText.setText(sw.toString());
+                               // IOUtils.closeQuietly(sw);
+                               SystemNotifications systemNotifications = new SystemNotifications(
+                                               bodyArea);
+                               systemNotifications.notifyException(exception);
                                resetException();
+                               return;
                                // TODO report
-                       } else {
-                               String state = getState();
-                               try {
-                                       if (state == null)
-                                               throw new CmsException("State cannot be null");
-                                       uiProvider.createUi(bodyArea, getNode());
-                                       // if (page == null)
-                                       // throw new CmsException("Page cannot be null");
-                                       // // else if (state.length() == 0)
-                                       // // log.debug("empty state");
-                                       // else if (pages.containsKey(page))
-                                       // pages.get(page).createUi(bodyArea, getNode());
-                                       // else {
-                                       // // try {
-                                       // // RWT.getResponse().sendError(404);
-                                       // // } catch (IOException e) {
-                                       // // log.error("Cannot send 404 code", e);
-                                       // // }
-                                       // throw new CmsException("Unsupported state " + state);
-                                       // }
-                               } catch (RepositoryException e) {
-                                       throw new CmsException("Cannot refresh body", e);
-                               }
                        }
-                       bodyArea.layout(true, true);
-               }
 
-               @Override
-               protected void logAsAnonymous() {
-                       cmsLogin.logInAsAnonymous();
+                       // clear
+                       for (Control child : bodyArea.getChildren())
+                               child.dispose();
+                       bodyArea.setLayout(CmsUtils.noSpaceGridLayout());
+
+                       String state = getState();
+                       try {
+                               if (state == null)
+                                       throw new CmsException("State cannot be null");
+                               uiProvider.createUi(bodyArea, getNode());
+                       } catch (RepositoryException e) {
+                               throw new CmsException("Cannot refresh body", e);
+                       }
+
+                       bodyArea.layout(true, true);
                }
 
                @Override
@@ -404,7 +388,7 @@ public class CmsApplication implements CmsConstants, ApplicationConfiguration,
                                throws RepositoryException {
                        if (!session.hasPermission(basePath, "read")) {
                                if (session.getUserID().equals("anonymous"))
-                                       throw new CmsLoginRequiredException();
+                                       throw new LoginRequiredException();
                                else
                                        throw new CmsException("Unauthorized");
                        }