From c63feb33bc17eb7e4fc6f2b47f31695ab6dfe1f8 Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Wed, 30 Jun 2021 11:14:36 +0200 Subject: [PATCH] Make web publishing easier to configure. --- .../src/org/argeo/suite/ui/SuiteApp.java | 9 +++- .../OSGI-INF/wwwLayer.xml | 9 ++++ publishing/org.argeo.publishing.ui/bnd.bnd | 1 + .../config/wwwLayer.properties | 4 ++ .../argeo/publishing/servlet/DbkServlet.java | 18 ++++++-- .../publishing/ui/DocumentUiProvider.java | 2 +- .../argeo/publishing/ui/PublishingApp.java | 43 ++++++++++++------- 7 files changed, 65 insertions(+), 21 deletions(-) create mode 100644 publishing/org.argeo.publishing.ui/OSGI-INF/wwwLayer.xml create mode 100644 publishing/org.argeo.publishing.ui/config/wwwLayer.properties diff --git a/core/org.argeo.suite.ui/src/org/argeo/suite/ui/SuiteApp.java b/core/org.argeo.suite.ui/src/org/argeo/suite/ui/SuiteApp.java index 32b6da8..bdcf30c 100644 --- a/core/org.argeo.suite.ui/src/org/argeo/suite/ui/SuiteApp.java +++ b/core/org.argeo.suite.ui/src/org/argeo/suite/ui/SuiteApp.java @@ -55,6 +55,7 @@ public class SuiteApp extends AbstractCmsApp implements EventHandler { public final static String PUBLIC_BASE_PATH_PROPERTY = "publicBasePath"; public final static String DEFAULT_UI_NAME_PROPERTY = "defaultUiName"; public final static String DEFAULT_THEME_ID_PROPERTY = "defaultThemeId"; + public final static String DEFAULT_LAYER_PROPERTY = "defaultLayer"; private final static String LOGIN = "login"; private String publicBasePath = null; @@ -88,6 +89,8 @@ public class SuiteApp extends AbstractCmsApp implements EventHandler { defaultUiName = LangUtils.get(properties, DEFAULT_UI_NAME_PROPERTY); if (properties.containsKey(DEFAULT_THEME_ID_PROPERTY)) defaultThemeId = LangUtils.get(properties, DEFAULT_THEME_ID_PROPERTY); + if (properties.containsKey(DEFAULT_LAYER_PROPERTY)) + defaultLayerPid = LangUtils.get(properties, DEFAULT_LAYER_PROPERTY); publicBasePath = LangUtils.get(properties, PUBLIC_BASE_PATH_PROPERTY); if (properties.containsKey(Constants.SERVICE_PID)) { @@ -155,6 +158,8 @@ public class SuiteApp extends AbstractCmsApp implements EventHandler { CmsView cmsView = CmsView.getCmsView(parent); CmsUiProvider headerUiProvider = findUiProvider(headerPid); CmsUiProvider footerUiProvider = findUiProvider(footerPid); + CmsUiProvider leadPaneUiProvider = findUiProvider(leadPanePid); + Localized appTitle = null; if (headerUiProvider instanceof DefaultHeader) { appTitle = ((DefaultHeader) headerUiProvider).getTitle(); @@ -203,7 +208,9 @@ public class SuiteApp extends AbstractCmsApp implements EventHandler { SuiteLayer layer = layersByPid.get(key).get(); ui.addLayer(key, layer); } - refreshPart(findUiProvider(leadPanePid), ui.getLeadPane(), context); + + if (leadPaneUiProvider != null) + refreshPart(leadPaneUiProvider, ui.getLeadPane(), context); if (footerUiProvider != null) refreshPart(footerUiProvider, ui.getFooter(), context); ui.layout(true, true); diff --git a/publishing/org.argeo.publishing.ui/OSGI-INF/wwwLayer.xml b/publishing/org.argeo.publishing.ui/OSGI-INF/wwwLayer.xml new file mode 100644 index 0000000..497d652 --- /dev/null +++ b/publishing/org.argeo.publishing.ui/OSGI-INF/wwwLayer.xml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/publishing/org.argeo.publishing.ui/bnd.bnd b/publishing/org.argeo.publishing.ui/bnd.bnd index 3829f16..67c0dd9 100644 --- a/publishing/org.argeo.publishing.ui/bnd.bnd +++ b/publishing/org.argeo.publishing.ui/bnd.bnd @@ -19,4 +19,5 @@ OSGI-INF/fontsServlet.xml,\ OSGI-INF/htmlServletContext.xml,\ OSGI-INF/dbkServlet.xml,\ OSGI-INF/documentUiProvider.xml,\ +OSGI-INF/wwwLayer.xml,\ OSGI-INF/dbk4Converter.xml diff --git a/publishing/org.argeo.publishing.ui/config/wwwLayer.properties b/publishing/org.argeo.publishing.ui/config/wwwLayer.properties new file mode 100644 index 0000000..d29fa5b --- /dev/null +++ b/publishing/org.argeo.publishing.ui/config/wwwLayer.properties @@ -0,0 +1,4 @@ +service.pid=argeo.publishing.ui.wwwLayer + +title=Web +icon=map \ No newline at end of file diff --git a/publishing/org.argeo.publishing.ui/src/org/argeo/publishing/servlet/DbkServlet.java b/publishing/org.argeo.publishing.ui/src/org/argeo/publishing/servlet/DbkServlet.java index a05bb47..cb3dffa 100644 --- a/publishing/org.argeo.publishing.ui/src/org/argeo/publishing/servlet/DbkServlet.java +++ b/publishing/org.argeo.publishing.ui/src/org/argeo/publishing/servlet/DbkServlet.java @@ -37,6 +37,7 @@ import org.apache.commons.io.IOUtils; import org.apache.xalan.processor.TransformerFactoryImpl; import org.argeo.cms.servlet.ServletAuthUtils; import org.argeo.cms.ui.CmsTheme; +import org.argeo.docbook.DbkType; import org.argeo.docbook.DbkUtils; import org.argeo.jcr.Jcr; import org.argeo.jcr.JcrException; @@ -81,7 +82,13 @@ public class DbkServlet extends HttpServlet { Session session = null; try { session = ServletAuthUtils.doAs(() -> Jcr.login(repository, null), req); - Node node = session.getNode(path); + Node documentNode = session.getNode(path); + Node node; + if (documentNode.hasNode(DbkType.article.get())) + node = documentNode.getNode(DbkType.article.get()); + else { + throw new IllegalArgumentException("Unsupported node " + documentNode); + } if (DbkUtils.isDbk(node)) { CmsTheme cmsTheme = null; String themeId = req.getParameter("themeId"); @@ -98,7 +105,7 @@ public class DbkServlet extends HttpServlet { // TODO optimise with pipes, SAX, etc. ? byte[] arr; try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { - session.exportDocumentView(path, out, true, false); + session.exportDocumentView(node.getPath(), out, true, false); arr = out.toByteArray(); // System.out.println(new String(arr, StandardCharsets.UTF_8)); } catch (RepositoryException e) { @@ -126,8 +133,11 @@ public class DbkServlet extends HttpServlet { sb.append(req.getContextPath()).append(req.getServletPath()).append('/'); sb.append(themeId).append('/').append(cssPath).append(' '); } - sb.append("https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap").append(' '); - sb.append("https://fonts.googleapis.com/css2?family=Montserrat:ital,wght@0,300;0,400;0,600;1,400&display=swap").append(' '); + sb.append("https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap") + .append(' '); + sb.append( + "https://fonts.googleapis.com/css2?family=Montserrat:ital,wght@0,300;0,400;0,600;1,400&display=swap") + .append(' '); if (sb.length() > 0) transformer.setParameter("html.stylesheet", sb.toString()); } diff --git a/publishing/org.argeo.publishing.ui/src/org/argeo/publishing/ui/DocumentUiProvider.java b/publishing/org.argeo.publishing.ui/src/org/argeo/publishing/ui/DocumentUiProvider.java index 1170765..2b82f5d 100644 --- a/publishing/org.argeo.publishing.ui/src/org/argeo/publishing/ui/DocumentUiProvider.java +++ b/publishing/org.argeo.publishing.ui/src/org/argeo/publishing/ui/DocumentUiProvider.java @@ -28,7 +28,7 @@ public class DocumentUiProvider implements CmsUiProvider { // Title parent.setLayout(CmsUiUtils.noSpaceGridLayout()); - CmsLink toHtml = new CmsLink("To HTML", "/html/dbk" + textNode.getPath()); + CmsLink toHtml = new CmsLink("To HTML", "/html/dbk" + context.getPath()); toHtml.createUiPart(parent, context); ScrolledPage page = new ScrolledPage(parent, SWT.NONE); diff --git a/publishing/org.argeo.publishing.ui/src/org/argeo/publishing/ui/PublishingApp.java b/publishing/org.argeo.publishing.ui/src/org/argeo/publishing/ui/PublishingApp.java index f527d5b..705eb96 100644 --- a/publishing/org.argeo.publishing.ui/src/org/argeo/publishing/ui/PublishingApp.java +++ b/publishing/org.argeo.publishing.ui/src/org/argeo/publishing/ui/PublishingApp.java @@ -8,43 +8,46 @@ import java.util.Map; import java.util.Set; import javax.jcr.Node; -import javax.jcr.RepositoryException; import javax.jcr.Session; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.argeo.api.NodeUtils; import org.argeo.cms.ui.AbstractCmsApp; import org.argeo.cms.ui.CmsApp; import org.argeo.cms.ui.CmsUiProvider; -import org.argeo.docbook.DbkType; import org.argeo.docbook.ui.DocumentPage; import org.argeo.jcr.Jcr; -import org.argeo.jcr.JcrUtils; +import org.argeo.suite.ui.SuiteApp; import org.argeo.util.LangUtils; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.osgi.framework.Constants; +import org.osgi.service.event.Event; +import org.osgi.service.event.EventHandler; /** * A {@link CmsApp} dedicated to publishing, typically a public or internal web * site. */ -public class PublishingApp extends AbstractCmsApp { +public class PublishingApp extends AbstractCmsApp implements EventHandler { private final static Log log = LogFactory.getLog(PublishingApp.class); private String pid; private String defaultThemeId; private String defaultUiName = ""; + private String publicBasePath = null; + private CmsUiProvider landingPage; + private CmsUiProvider defaultProvider = new DocumentUiProvider(); public void init(Map properties) { if (properties.containsKey(DEFAULT_UI_NAME_PROPERTY)) defaultUiName = LangUtils.get(properties, DEFAULT_UI_NAME_PROPERTY); if (properties.containsKey(DEFAULT_THEME_ID_PROPERTY)) defaultThemeId = LangUtils.get(properties, DEFAULT_THEME_ID_PROPERTY); + publicBasePath = LangUtils.get(properties, SuiteApp.PUBLIC_BASE_PATH_PROPERTY); pid = properties.get(Constants.SERVICE_PID); if (log.isDebugEnabled()) @@ -66,21 +69,22 @@ public class PublishingApp extends AbstractCmsApp { @Override public Composite initUi(Composite parent) { - Session adminSession = NodeUtils.openDataAdminSession(getRepository(), null); +// Session adminSession = NodeUtils.openDataAdminSession(getRepository(), null); + Session session = Jcr.login(getRepository(), null); parent.setLayout(new GridLayout()); - Node indexNode; - try { - indexNode = JcrUtils.getOrAdd(Jcr.getRootNode(adminSession), DocumentPage.WWW, DbkType.article.get()); - adminSession.save(); - } catch (RepositoryException e) { - throw new IllegalStateException(e); - } + Node indexNode = Jcr.getNode(session, publicBasePath + "/index"); +// try { +// indexNode = JcrUtils.getOrAdd(Jcr.getRootNode(adminSession), DocumentPage.WWW, DbkType.article.get()); +// adminSession.save(); +// } catch (RepositoryException e) { +// throw new IllegalStateException(e); +// } Control page; if (landingPage != null) { page = landingPage.createUiPart(parent, indexNode); } else { - page = new DocumentPage().createUiPart(parent, indexNode); + page = defaultProvider.createUiPart(parent, indexNode); } return (Composite) page; } @@ -88,7 +92,10 @@ public class PublishingApp extends AbstractCmsApp { @Override public void refreshUi(Composite parent, String state) { parent.setLayout(new GridLayout()); - new DocumentPage().createUiPart(parent, null); + if (landingPage != null) + landingPage.createUiPart(parent, null); + else + defaultProvider.createUiPart(parent, null); } @Override @@ -105,4 +112,10 @@ public class PublishingApp extends AbstractCmsApp { this.landingPage = landingPage; } + @Override + public void handleEvent(Event event) { + // TODO listen to some events + + } + } -- 2.30.2