From 38fa23604d1a9717988ce212322e3ca28b34e27f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Dywicki?= Date: Tue, 14 Jan 2020 08:28:21 +0100 Subject: [PATCH] Make it possible to style Dashboard UI with bundle fragments. (#168) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Solves #167. Signed-off-by: Ɓukasz Dywicki --- bundles/org.openhab.ui.dashboard/bnd.bnd | 3 +++ .../dashboard/internal/DashboardService.java | 22 ++++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 bundles/org.openhab.ui.dashboard/bnd.bnd diff --git a/bundles/org.openhab.ui.dashboard/bnd.bnd b/bundles/org.openhab.ui.dashboard/bnd.bnd new file mode 100644 index 000000000..a5daa0ef0 --- /dev/null +++ b/bundles/org.openhab.ui.dashboard/bnd.bnd @@ -0,0 +1,3 @@ +Bundle-SymbolicName: ${project.artifactId} +Bundle-ClassPath: patch/,. + diff --git a/bundles/org.openhab.ui.dashboard/src/main/java/org/openhab/ui/dashboard/internal/DashboardService.java b/bundles/org.openhab.ui.dashboard/src/main/java/org/openhab/ui/dashboard/internal/DashboardService.java index 15545ed85..65382ce7c 100644 --- a/bundles/org.openhab.ui.dashboard/src/main/java/org/openhab/ui/dashboard/internal/DashboardService.java +++ b/bundles/org.openhab.ui.dashboard/src/main/java/org/openhab/ui/dashboard/internal/DashboardService.java @@ -28,6 +28,7 @@ import org.eclipse.smarthome.core.i18n.LocaleProvider; import org.eclipse.smarthome.core.i18n.TranslationProvider; import org.eclipse.smarthome.core.net.HttpServiceUtil; import org.eclipse.smarthome.core.net.NetworkAddressService; +import org.eclipse.smarthome.io.http.HttpContextFactoryService; import org.openhab.ui.dashboard.DashboardReady; import org.openhab.ui.dashboard.DashboardTile; import org.osgi.framework.BundleContext; @@ -67,6 +68,7 @@ public class DashboardService implements DashboardReady { protected NetworkAddressService networkAddressService; protected TranslationProvider i18nProvider; protected LocaleProvider localeProvider; + protected HttpContextFactoryService httpContextFactoryService; protected Set tiles = new CopyOnWriteArraySet<>(); @@ -83,7 +85,8 @@ public class DashboardService implements DashboardReady { Hashtable props = new Hashtable<>(); httpService.registerServlet(DASHBOARD_ALIAS + "/" + SERVLET_NAME, createServlet(), props, httpService.createDefaultHttpContext()); - httpService.registerResources(DASHBOARD_ALIAS, "web", null); + httpService.registerResources(DASHBOARD_ALIAS, "web", + httpContextFactoryService.createDefaultHttpContext(bundleContext.getBundle())); if (HttpServiceUtil.getHttpServicePort(bundleContext) > 0) { logger.info("Started Dashboard at http://{}:{}", networkAddressService.getPrimaryIpv4HostAddress(), @@ -167,7 +170,7 @@ public class DashboardService implements DashboardReady { String warnTemplate; String setupTemplate; - URL index = bundleContext.getBundle().getEntry("templates/index.html"); + URL index = bundleContext.getBundle().getResource("templates/index.html"); if (index != null) { try { indexTemplate = IOUtils.toString(index.openStream()); @@ -178,7 +181,7 @@ public class DashboardService implements DashboardReady { throw new ComponentException("Cannot find index.html - failed to initialize Dashboard servlet"); } - URL entry = bundleContext.getBundle().getEntry("templates/entry.html"); + URL entry = bundleContext.getBundle().getResource("templates/entry.html"); if (entry != null) { try { entryTemplate = IOUtils.toString(entry.openStream()); @@ -189,7 +192,7 @@ public class DashboardService implements DashboardReady { throw new ComponentException("Cannot find entry.html - failed to initialize Dashboard servlet"); } - URL warn = bundleContext.getBundle().getEntry("templates/warn.html"); + URL warn = bundleContext.getBundle().getResource("templates/warn.html"); if (warn != null) { try { warnTemplate = IOUtils.toString(warn.openStream()); @@ -200,7 +203,7 @@ public class DashboardService implements DashboardReady { throw new RuntimeException("Cannot find warn.html - failed to initialize Dashboard servlet"); } - URL setup = bundleContext.getBundle().getEntry("templates/setup.html"); + URL setup = bundleContext.getBundle().getResource("templates/setup.html"); if (setup != null) { try { setupTemplate = IOUtils.toString(setup.openStream()); @@ -261,4 +264,13 @@ public class DashboardService implements DashboardReady { return i18nProvider.getText(bundleContext.getBundle(), key, key, useLocale); } } + + @Reference(policy = ReferencePolicy.STATIC) + public void setHttpContextFactoryService(HttpContextFactoryService httpContextFactoryService) { + this.httpContextFactoryService = httpContextFactoryService; + } + + public void unsetHttpContextFactoryService(HttpContextFactoryService httpContextFactoryService) { + this.httpContextFactoryService = null; + } }