From 9f27727e0967bcd90d1fe7541dfe51ea2b1f5bb3 Mon Sep 17 00:00:00 2001 From: Kai Kreuzer Date: Thu, 31 Jan 2019 16:46:25 +0100 Subject: [PATCH] REST API: (Optionally) include hidden widgets in sitemap API response (#499) Also-by: Danny Baumann Signed-off-by: Kai Kreuzer --- .../org.openhab.core.io.rest.sitemap/pom.xml | 6 ++ .../sitemap/internal/SitemapResource.java | 50 ++++++++------ .../io/rest/sitemap/internal/WidgetDTO.java | 1 + .../sitemap/internal/SitemapResourceTest.java | 10 +-- .../internal/resources/beans/RootBean.java | 2 +- .../NOTICE | 19 ----- ...ipse.smarthome.io.rest.sitemap.test.launch | 45 ------------ .../pom.xml | 69 ------------------- 8 files changed, 43 insertions(+), 159 deletions(-) rename {itests/org.openhab.core.io.rest.sitemap.tests => bundles/org.openhab.core.io.rest.sitemap}/src/test/java/org/eclipse/smarthome/io/rest/sitemap/internal/SitemapResourceTest.java (98%) delete mode 100644 itests/org.openhab.core.io.rest.sitemap.tests/NOTICE delete mode 100644 itests/org.openhab.core.io.rest.sitemap.tests/org.eclipse.smarthome.io.rest.sitemap.test.launch delete mode 100644 itests/org.openhab.core.io.rest.sitemap.tests/pom.xml diff --git a/bundles/org.openhab.core.io.rest.sitemap/pom.xml b/bundles/org.openhab.core.io.rest.sitemap/pom.xml index a670326ad..57bc98538 100644 --- a/bundles/org.openhab.core.io.rest.sitemap/pom.xml +++ b/bundles/org.openhab.core.io.rest.sitemap/pom.xml @@ -24,6 +24,12 @@ org.openhab.core.ui ${project.version} + + org.openhab.core.bundles + org.openhab.core.test + ${project.version} + test + org.openhab.core.bom org.openhab.core.bom.compile-model diff --git a/bundles/org.openhab.core.io.rest.sitemap/src/main/java/org/eclipse/smarthome/io/rest/sitemap/internal/SitemapResource.java b/bundles/org.openhab.core.io.rest.sitemap/src/main/java/org/eclipse/smarthome/io/rest/sitemap/internal/SitemapResource.java index bee9ff606..e8d113581 100644 --- a/bundles/org.openhab.core.io.rest.sitemap/src/main/java/org/eclipse/smarthome/io/rest/sitemap/internal/SitemapResource.java +++ b/bundles/org.openhab.core.io.rest.sitemap/src/main/java/org/eclipse/smarthome/io/rest/sitemap/internal/SitemapResource.java @@ -237,11 +237,13 @@ public class SitemapResource implements RESTResource, SitemapSubscriptionCallbac public Response getSitemapData(@Context HttpHeaders headers, @HeaderParam(HttpHeaders.ACCEPT_LANGUAGE) @ApiParam(value = "language") String language, @PathParam("sitemapname") @ApiParam(value = "sitemap name") String sitemapname, - @QueryParam("type") String type, @QueryParam("jsoncallback") @DefaultValue("callback") String callback) { + @QueryParam("type") String type, @QueryParam("jsoncallback") @DefaultValue("callback") String callback, + @QueryParam("includeHidden") @ApiParam(value = "include hidden widgets", required = false) boolean includeHiddenWidgets) { final Locale locale = localeService.getLocale(language); logger.debug("Received HTTP GET request from IP {} at '{}' for media type '{}'.", request.getRemoteAddr(), uriInfo.getPath(), type); - Object responseObject = getSitemapBean(sitemapname, uriInfo.getBaseUriBuilder().build(), locale); + Object responseObject = getSitemapBean(sitemapname, uriInfo.getBaseUriBuilder().build(), locale, + includeHiddenWidgets); return Response.ok(responseObject).build(); } @@ -256,7 +258,8 @@ public class SitemapResource implements RESTResource, SitemapSubscriptionCallbac @HeaderParam(HttpHeaders.ACCEPT_LANGUAGE) @ApiParam(value = "language") String language, @PathParam("sitemapname") @ApiParam(value = "sitemap name") String sitemapname, @PathParam("pageid") @ApiParam(value = "page id") String pageId, - @QueryParam("subscriptionid") @ApiParam(value = "subscriptionid", required = false) String subscriptionId) { + @QueryParam("subscriptionid") @ApiParam(value = "subscriptionid", required = false) String subscriptionId, + @QueryParam("includeHidden") @ApiParam(value = "include hidden widgets", required = false) boolean includeHiddenWidgets) { final Locale locale = localeService.getLocale(language); logger.debug("Received HTTP GET request from IP {} at '{}'", request.getRemoteAddr(), uriInfo.getPath()); @@ -277,7 +280,8 @@ public class SitemapResource implements RESTResource, SitemapSubscriptionCallbac // we notice this information in the response object. timeout = blockUnlessChangeOccurs(sitemapname, pageId); } - PageDTO responseObject = getPageBean(sitemapname, pageId, uriInfo.getBaseUriBuilder().build(), locale, timeout); + PageDTO responseObject = getPageBean(sitemapname, pageId, uriInfo.getBaseUriBuilder().build(), locale, timeout, + includeHiddenWidgets); return Response.ok(responseObject).build(); } @@ -345,31 +349,33 @@ public class SitemapResource implements RESTResource, SitemapSubscriptionCallbac return eventOutput; } - private PageDTO getPageBean(String sitemapName, String pageId, URI uri, Locale locale, boolean timeout) { + private PageDTO getPageBean(String sitemapName, String pageId, URI uri, Locale locale, boolean timeout, + boolean includeHidden) { Sitemap sitemap = getSitemap(sitemapName); if (sitemap != null) { if (pageId.equals(sitemap.getName())) { EList children = itemUIRegistry.getChildren(sitemap); return createPageBean(sitemapName, sitemap.getLabel(), sitemap.getIcon(), sitemap.getName(), children, - false, isLeaf(children), uri, locale, timeout); + false, isLeaf(children), uri, locale, timeout, includeHidden); } else { Widget pageWidget = itemUIRegistry.getWidget(sitemap, pageId); if (pageWidget instanceof LinkableWidget) { EList children = itemUIRegistry.getChildren((LinkableWidget) pageWidget); PageDTO pageBean = createPageBean(sitemapName, itemUIRegistry.getLabel(pageWidget), itemUIRegistry.getCategory(pageWidget), pageId, children, false, isLeaf(children), uri, - locale, timeout); + locale, timeout, includeHidden); EObject parentPage = pageWidget.eContainer(); while (parentPage instanceof Frame) { parentPage = parentPage.eContainer(); } if (parentPage instanceof Widget) { String parentId = itemUIRegistry.getWidgetId((Widget) parentPage); - pageBean.parent = getPageBean(sitemapName, parentId, uri, locale, timeout); + pageBean.parent = getPageBean(sitemapName, parentId, uri, locale, timeout, includeHidden); pageBean.parent.widgets = null; pageBean.parent.parent = null; } else if (parentPage instanceof Sitemap) { - pageBean.parent = getPageBean(sitemapName, sitemap.getName(), uri, locale, timeout); + pageBean.parent = getPageBean(sitemapName, sitemap.getName(), uri, locale, timeout, + includeHidden); pageBean.parent.widgets = null; } return pageBean; @@ -421,10 +427,10 @@ public class SitemapResource implements RESTResource, SitemapSubscriptionCallbac return beans; } - public SitemapDTO getSitemapBean(String sitemapname, URI uri, Locale locale) { + private SitemapDTO getSitemapBean(String sitemapname, URI uri, Locale locale, boolean includeHiddenWidgets) { Sitemap sitemap = getSitemap(sitemapname); if (sitemap != null) { - return createSitemapBean(sitemapname, sitemap, uri, locale); + return createSitemapBean(sitemapname, sitemap, uri, locale, includeHiddenWidgets); } else { logger.info("Received HTTP GET request at '{}' for the unknown sitemap '{}'.", uriInfo.getPath(), sitemapname); @@ -432,7 +438,8 @@ public class SitemapResource implements RESTResource, SitemapSubscriptionCallbac } } - private SitemapDTO createSitemapBean(String sitemapName, Sitemap sitemap, URI uri, Locale locale) { + private SitemapDTO createSitemapBean(String sitemapName, Sitemap sitemap, URI uri, Locale locale, + boolean includeHiddenWidgets) { SitemapDTO bean = new SitemapDTO(); bean.name = sitemapName; @@ -441,12 +448,12 @@ public class SitemapResource implements RESTResource, SitemapSubscriptionCallbac bean.link = UriBuilder.fromUri(uri).path(SitemapResource.PATH_SITEMAPS).path(bean.name).build().toASCIIString(); bean.homepage = createPageBean(sitemap.getName(), sitemap.getLabel(), sitemap.getIcon(), sitemap.getName(), - itemUIRegistry.getChildren(sitemap), true, false, uri, locale, false); + itemUIRegistry.getChildren(sitemap), true, false, uri, locale, false, includeHiddenWidgets); return bean; } private PageDTO createPageBean(String sitemapName, String title, String icon, String pageId, EList children, - boolean drillDown, boolean isLeaf, URI uri, Locale locale, boolean timeout) { + boolean drillDown, boolean isLeaf, URI uri, Locale locale, boolean timeout, boolean includeHiddenWidgets) { PageDTO bean = new PageDTO(); bean.timeout = timeout; bean.id = pageId; @@ -457,7 +464,8 @@ public class SitemapResource implements RESTResource, SitemapSubscriptionCallbac if (children != null) { for (Widget widget : children) { String widgetId = itemUIRegistry.getWidgetId(widget); - WidgetDTO subWidget = createWidgetBean(sitemapName, widget, drillDown, uri, widgetId, locale); + WidgetDTO subWidget = createWidgetBean(sitemapName, widget, drillDown, uri, widgetId, locale, + includeHiddenWidgets); if (subWidget != null) { bean.widgets.add(subWidget); } @@ -469,9 +477,9 @@ public class SitemapResource implements RESTResource, SitemapSubscriptionCallbac } private WidgetDTO createWidgetBean(String sitemapName, Widget widget, boolean drillDown, URI uri, String widgetId, - Locale locale) { + Locale locale, boolean evenIfHidden) { // Test visibility - if (!itemUIRegistry.getVisiblity(widget)) { + if (!evenIfHidden && !itemUIRegistry.getVisiblity(widget)) { return null; } @@ -500,13 +508,15 @@ public class SitemapResource implements RESTResource, SitemapSubscriptionCallbac bean.valuecolor = itemUIRegistry.getValueColor(widget); bean.label = itemUIRegistry.getLabel(widget); bean.type = widget.eClass().getName(); + bean.visibility = itemUIRegistry.getVisiblity(widget); if (widget instanceof LinkableWidget) { LinkableWidget linkableWidget = (LinkableWidget) widget; EList children = itemUIRegistry.getChildren(linkableWidget); if (widget instanceof Frame) { for (Widget child : children) { String wID = itemUIRegistry.getWidgetId(child); - WidgetDTO subWidget = createWidgetBean(sitemapName, child, drillDown, uri, wID, locale); + WidgetDTO subWidget = createWidgetBean(sitemapName, child, drillDown, uri, wID, locale, + evenIfHidden); if (subWidget != null) { bean.widgets.add(subWidget); } @@ -515,7 +525,7 @@ public class SitemapResource implements RESTResource, SitemapSubscriptionCallbac String pageName = itemUIRegistry.getWidgetId(linkableWidget); bean.linkedPage = createPageBean(sitemapName, itemUIRegistry.getLabel(widget), itemUIRegistry.getCategory(widget), pageName, drillDown ? children : null, drillDown, - isLeaf(children), uri, locale, false); + isLeaf(children), uri, locale, false, evenIfHidden); } } if (widget instanceof Switch) { @@ -830,4 +840,4 @@ public class SitemapResource implements RESTResource, SitemapSubscriptionCallbac return itemUIRegistry != null && subscriptions != null && localeService != null; } -} +} \ No newline at end of file diff --git a/bundles/org.openhab.core.io.rest.sitemap/src/main/java/org/eclipse/smarthome/io/rest/sitemap/internal/WidgetDTO.java b/bundles/org.openhab.core.io.rest.sitemap/src/main/java/org/eclipse/smarthome/io/rest/sitemap/internal/WidgetDTO.java index 08ee3801d..7eff6d669 100644 --- a/bundles/org.openhab.core.io.rest.sitemap/src/main/java/org/eclipse/smarthome/io/rest/sitemap/internal/WidgetDTO.java +++ b/bundles/org.openhab.core.io.rest.sitemap/src/main/java/org/eclipse/smarthome/io/rest/sitemap/internal/WidgetDTO.java @@ -30,6 +30,7 @@ public class WidgetDTO { public String widgetId; public String type; public String name; + public boolean visibility; public String label; public String icon; diff --git a/itests/org.openhab.core.io.rest.sitemap.tests/src/test/java/org/eclipse/smarthome/io/rest/sitemap/internal/SitemapResourceTest.java b/bundles/org.openhab.core.io.rest.sitemap/src/test/java/org/eclipse/smarthome/io/rest/sitemap/internal/SitemapResourceTest.java similarity index 98% rename from itests/org.openhab.core.io.rest.sitemap.tests/src/test/java/org/eclipse/smarthome/io/rest/sitemap/internal/SitemapResourceTest.java rename to bundles/org.openhab.core.io.rest.sitemap/src/test/java/org/eclipse/smarthome/io/rest/sitemap/internal/SitemapResourceTest.java index fa528ec2c..74658d13d 100644 --- a/itests/org.openhab.core.io.rest.sitemap.tests/src/test/java/org/eclipse/smarthome/io/rest/sitemap/internal/SitemapResourceTest.java +++ b/bundles/org.openhab.core.io.rest.sitemap/src/test/java/org/eclipse/smarthome/io/rest/sitemap/internal/SitemapResourceTest.java @@ -173,7 +173,7 @@ public class SitemapResourceTest extends JavaTest { // non-null is sufficient here. when(headers.getRequestHeader(HTTP_HEADER_X_ATMOSPHERE_TRANSPORT)).thenReturn(Collections.emptyList()); - Response response = sitemapResource.getPageData(headers, null, SITEMAP_MODEL_NAME, SITEMAP_NAME, null); + Response response = sitemapResource.getPageData(headers, null, SITEMAP_MODEL_NAME, SITEMAP_NAME, null, false); PageDTO pageDTO = (PageDTO) response.getEntity(); assertThat(pageDTO.timeout, is(false)); // assert that the item state change did trigger the blocking method to @@ -194,7 +194,7 @@ public class SitemapResourceTest extends JavaTest { // non-null is sufficient here. when(headers.getRequestHeader(HTTP_HEADER_X_ATMOSPHERE_TRANSPORT)).thenReturn(Collections.emptyList()); - Response response = sitemapResource.getPageData(headers, null, SITEMAP_MODEL_NAME, SITEMAP_NAME, null); + Response response = sitemapResource.getPageData(headers, null, SITEMAP_MODEL_NAME, SITEMAP_NAME, null, false); PageDTO pageDTO = (PageDTO) response.getEntity(); assertThat(pageDTO.timeout, is(false)); // assert that the item state change did trigger the blocking method to @@ -215,7 +215,7 @@ public class SitemapResourceTest extends JavaTest { // non-null is sufficient here. when(headers.getRequestHeader(HTTP_HEADER_X_ATMOSPHERE_TRANSPORT)).thenReturn(Collections.emptyList()); - Response response = sitemapResource.getPageData(headers, null, SITEMAP_MODEL_NAME, SITEMAP_NAME, null); + Response response = sitemapResource.getPageData(headers, null, SITEMAP_MODEL_NAME, SITEMAP_NAME, null, false); PageDTO pageDTO = (PageDTO) response.getEntity(); assertThat(pageDTO.timeout, is(false)); // assert that the item state change did trigger the blocking method to @@ -236,7 +236,7 @@ public class SitemapResourceTest extends JavaTest { // non-null is sufficient here. when(headers.getRequestHeader(HTTP_HEADER_X_ATMOSPHERE_TRANSPORT)).thenReturn(Collections.emptyList()); - Response response = sitemapResource.getPageData(headers, null, SITEMAP_MODEL_NAME, SITEMAP_NAME, null); + Response response = sitemapResource.getPageData(headers, null, SITEMAP_MODEL_NAME, SITEMAP_NAME, null, false); PageDTO pageDTO = (PageDTO) response.getEntity(); assertThat(pageDTO.timeout, is(false)); // assert that the item state change did trigger the blocking method to @@ -251,7 +251,7 @@ public class SitemapResourceTest extends JavaTest { // Disable long polling when(headers.getRequestHeader(HTTP_HEADER_X_ATMOSPHERE_TRANSPORT)).thenReturn(null); - Response response = sitemapResource.getPageData(headers, null, SITEMAP_MODEL_NAME, SITEMAP_NAME, null); + Response response = sitemapResource.getPageData(headers, null, SITEMAP_MODEL_NAME, SITEMAP_NAME, null, false); PageDTO pageDTO = (PageDTO) response.getEntity(); assertThat(pageDTO.id, is(SITEMAP_NAME)); diff --git a/bundles/org.openhab.core.io.rest/src/main/java/org/eclipse/smarthome/io/rest/internal/resources/beans/RootBean.java b/bundles/org.openhab.core.io.rest/src/main/java/org/eclipse/smarthome/io/rest/internal/resources/beans/RootBean.java index 0b04bf968..4173eaf6f 100644 --- a/bundles/org.openhab.core.io.rest/src/main/java/org/eclipse/smarthome/io/rest/internal/resources/beans/RootBean.java +++ b/bundles/org.openhab.core.io.rest/src/main/java/org/eclipse/smarthome/io/rest/internal/resources/beans/RootBean.java @@ -24,7 +24,7 @@ import java.util.List; */ public class RootBean { - public final String version = "1"; + public final String version = "2"; public final List links = new ArrayList(); diff --git a/itests/org.openhab.core.io.rest.sitemap.tests/NOTICE b/itests/org.openhab.core.io.rest.sitemap.tests/NOTICE deleted file mode 100644 index b8675cd02..000000000 --- a/itests/org.openhab.core.io.rest.sitemap.tests/NOTICE +++ /dev/null @@ -1,19 +0,0 @@ -This content is produced and maintained by the Eclipse SmartHome project. - -* Project home: https://eclipse.org/smarthome/ - -== Declared Project Licenses - -This program and the accompanying materials are made available under the terms -of the Eclipse Public License 2.0 which is available at -https://www.eclipse.org/legal/epl-2.0/. - -== Source Code - -https://github.com/eclipse/smarthome - -== Copyright Holders - -See the NOTICE file distributed with the source code at -https://github.com/eclipse/smarthome/blob/master/NOTICE -for detailed information regarding copyright ownership. diff --git a/itests/org.openhab.core.io.rest.sitemap.tests/org.eclipse.smarthome.io.rest.sitemap.test.launch b/itests/org.openhab.core.io.rest.sitemap.tests/org.eclipse.smarthome.io.rest.sitemap.test.launch deleted file mode 100644 index cd337518d..000000000 --- a/itests/org.openhab.core.io.rest.sitemap.tests/org.eclipse.smarthome.io.rest.sitemap.test.launch +++ /dev/null @@ -1,45 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/itests/org.openhab.core.io.rest.sitemap.tests/pom.xml b/itests/org.openhab.core.io.rest.sitemap.tests/pom.xml deleted file mode 100644 index c5ce65b1c..000000000 --- a/itests/org.openhab.core.io.rest.sitemap.tests/pom.xml +++ /dev/null @@ -1,69 +0,0 @@ - - - 4.0.0 - - - org.openhab.core.itests - org.openhab.core.reactor.itests - 2.5.0-SNAPSHOT - - - org.openhab.core.io.rest.sitemap.tests - eclipse-test-plugin - - openHAB Core :: Integration Tests :: Sitemap REST Interface Tests - - - - - ${tycho-groupid} - target-platform-configuration - - - - - - eclipse-plugin - org.eclipse.equinox.ds - 0.0.0 - - - eclipse-plugin - org.eclipse.equinox.event - 0.0.0 - - - - - - - ${tycho-groupid} - tycho-surefire-plugin - - - - org.eclipse.equinox.ds - 1 - true - - - org.eclipse.equinox.event - 4 - true - - - org.eclipse.smarthome.core - 4 - true - - - org.eclipse.smarthome.config.core - 4 - true - - - - - - -