From 6edc41364051a4e875cff1cfb481b149b9a5be91 Mon Sep 17 00:00:00 2001 From: Christoph Weitkamp Date: Sun, 30 Jan 2022 13:20:11 +0100 Subject: [PATCH] Fixed NPE and exceptiion while parsing config value (#2718) Signed-off-by: Christoph Weitkamp --- .../community/CommunityMarketplaceAddonService.java | 10 +++++++--- .../marketplace/internal/json/JsonAddonService.java | 10 +++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityMarketplaceAddonService.java b/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityMarketplaceAddonService.java index 912a58e460..f7a40eca15 100644 --- a/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityMarketplaceAddonService.java +++ b/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityMarketplaceAddonService.java @@ -23,6 +23,7 @@ import java.net.URLConnection; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; +import java.util.Dictionary; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -47,6 +48,7 @@ import org.openhab.core.addon.marketplace.internal.community.model.DiscourseCate import org.openhab.core.addon.marketplace.internal.community.model.DiscourseCategoryResponseDTO.DiscourseUser; import org.openhab.core.addon.marketplace.internal.community.model.DiscourseTopicResponseDTO; import org.openhab.core.addon.marketplace.internal.community.model.DiscourseTopicResponseDTO.DiscoursePostLink; +import org.openhab.core.config.core.ConfigParser; import org.openhab.core.config.core.ConfigurableService; import org.openhab.core.events.Event; import org.openhab.core.events.EventPublisher; @@ -85,6 +87,7 @@ public class CommunityMarketplaceAddonService implements AddonService { static final String CONFIG_URI = "system:marketplace"; static final String CONFIG_API_KEY = "apiKey"; static final String CONFIG_SHOW_UNPUBLISHED_ENTRIES_KEY = "showUnpublished"; + private static final String CFG_REMOTE = "remote"; private static final String COMMUNITY_BASE_URL = "https://community.openhab.org"; private static final String COMMUNITY_MARKETPLACE_URL = COMMUNITY_BASE_URL + "/c/marketplace/69/l/latest"; @@ -479,11 +482,12 @@ public class CommunityMarketplaceAddonService implements AddonService { private boolean remoteEnabled() { try { Configuration configuration = configurationAdmin.getConfiguration("org.openhab.addons", null); - if (configuration.getProperties() != null) { - return (boolean) Objects.requireNonNullElse(configuration.getProperties().get("remote"), true); - } else { + Dictionary properties = configuration.getProperties(); + if (properties == null) { + // if we can't determine a set property, we use true (default is remote enabled) return true; } + return ConfigParser.valueAsOrElse(properties.get(CFG_REMOTE), Boolean.class, true); } catch (IOException e) { return true; } diff --git a/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/json/JsonAddonService.java b/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/json/JsonAddonService.java index c73bd3f60b..facbc69879 100644 --- a/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/json/JsonAddonService.java +++ b/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/json/JsonAddonService.java @@ -21,6 +21,7 @@ import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.Arrays; +import java.util.Dictionary; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -39,6 +40,7 @@ import org.openhab.core.addon.AddonType; import org.openhab.core.addon.marketplace.MarketplaceAddonHandler; import org.openhab.core.addon.marketplace.MarketplaceHandlerException; import org.openhab.core.addon.marketplace.internal.json.model.AddonEntryDTO; +import org.openhab.core.config.core.ConfigParser; import org.openhab.core.config.core.ConfigurableService; import org.openhab.core.events.Event; import org.openhab.core.events.EventPublisher; @@ -79,6 +81,7 @@ public class JsonAddonService implements AddonService { private static final String CONFIG_URLS = "urls"; private static final String CONFIG_SHOW_UNSTABLE = "showUnstable"; + private static final String CFG_REMOTE = "remote"; private static final Map TAG_ADDON_TYPE_MAP = Map.of( // "automation", new AddonType("automation", "Automation"), // @@ -269,11 +272,12 @@ public class JsonAddonService implements AddonService { private boolean remoteEnabled() { try { Configuration configuration = configurationAdmin.getConfiguration("org.openhab.addons", null); - if (configuration.getProperties() != null) { - return (boolean) Objects.requireNonNullElse(configuration.getProperties().get("remote"), true); - } else { + Dictionary properties = configuration.getProperties(); + if (properties == null) { + // if we can't determine a set property, we use true (default is remote enabled) return true; } + return ConfigParser.valueAsOrElse(properties.get(CFG_REMOTE), Boolean.class, true); } catch (IOException e) { return true; }