diff --git a/bundles/org.openhab.binding.weatherunderground/src/main/java/org/openhab/binding/weatherunderground/internal/handler/WeatherUndergroundBridgeHandler.java b/bundles/org.openhab.binding.weatherunderground/src/main/java/org/openhab/binding/weatherunderground/internal/handler/WeatherUndergroundBridgeHandler.java index 19cb5ff9765ce..a64a09c11786c 100644 --- a/bundles/org.openhab.binding.weatherunderground/src/main/java/org/openhab/binding/weatherunderground/internal/handler/WeatherUndergroundBridgeHandler.java +++ b/bundles/org.openhab.binding.weatherunderground/src/main/java/org/openhab/binding/weatherunderground/internal/handler/WeatherUndergroundBridgeHandler.java @@ -16,7 +16,6 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; -import org.apache.commons.lang.StringUtils; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.smarthome.config.core.Configuration; @@ -54,8 +53,7 @@ public class WeatherUndergroundBridgeHandler extends BaseBridgeHandler { @Nullable private ScheduledFuture controlApiKeyJob; - @Nullable - private String apikey; + private String apikey = ""; public WeatherUndergroundBridgeHandler(Bridge bridge) { super(bridge); @@ -68,12 +66,13 @@ public void initialize() { Configuration config = getThing().getConfiguration(); // Check if an api key has been provided during the bridge creation - if (StringUtils.trimToNull((String) config.get(WeatherUndergroundBindingConstants.APIKEY)) == null) { + Object configApiKey = config.get(WeatherUndergroundBindingConstants.APIKEY); + if (configApiKey == null || !(configApiKey instanceof String) || ((String) configApiKey).trim().isEmpty()) { logger.debug("Setting thing '{}' to OFFLINE: Parameter 'apikey' must be configured.", getThing().getUID()); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "@text/offline.conf-error-missing-apikey"); } else { - apikey = (String) config.get(WeatherUndergroundBindingConstants.APIKEY); + apikey = ((String) configApiKey).trim(); updateStatus(ThingStatus.UNKNOWN); startControlApiKeyJob(); } @@ -95,7 +94,7 @@ public void run() { // Check if the provided api key is valid for use with the weatherunderground service try { - String urlStr = URL.replace("%APIKEY%", StringUtils.trimToEmpty(getApikey())); + String urlStr = URL.replace("%APIKEY%", getApikey()); // Run the HTTP request and get the JSON response from Weather Underground String response = null; try { @@ -168,7 +167,7 @@ public void handleCommand(ChannelUID channelUID, Command command) { // not needed } - public @Nullable String getApikey() { + public String getApikey() { return apikey; } } diff --git a/bundles/org.openhab.binding.weatherunderground/src/main/java/org/openhab/binding/weatherunderground/internal/handler/WeatherUndergroundHandler.java b/bundles/org.openhab.binding.weatherunderground/src/main/java/org/openhab/binding/weatherunderground/internal/handler/WeatherUndergroundHandler.java index 454961005d97d..a24d57f11ad3a 100644 --- a/bundles/org.openhab.binding.weatherunderground/src/main/java/org/openhab/binding/weatherunderground/internal/handler/WeatherUndergroundHandler.java +++ b/bundles/org.openhab.binding.weatherunderground/src/main/java/org/openhab/binding/weatherunderground/internal/handler/WeatherUndergroundHandler.java @@ -28,7 +28,6 @@ import javax.measure.Quantity; import javax.measure.Unit; -import org.apache.commons.lang.StringUtils; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.smarthome.core.i18n.LocaleProvider; @@ -233,14 +232,13 @@ private void initializeThingHandler(@Nullable ThingHandler bridgeHandler, @Nulla String errors = ""; String statusDescr = null; - if (StringUtils.trimToNull(config.location) == null) { + if (config.location == null || config.location.trim().isEmpty()) { errors += " Parameter 'location' must be configured."; statusDescr = "@text/offline.conf-error-missing-location"; validConfig = false; } if (config.language != null) { - String lang = StringUtils.trimToEmpty(config.language); - if (lang.length() != 2) { + if (config.language.trim().length() != 2) { errors += " Parameter 'language' must be 2 letters."; statusDescr = "@text/offline.conf-error-syntax-language"; validConfig = false; @@ -537,7 +535,7 @@ private boolean updateWeatherData(Set features) { String urlStr = URL_QUERY.replace("%FEATURES%", String.join("/", features)); - String lang = StringUtils.trimToEmpty(config.language); + String lang = config.language == null ? "" : config.language.trim(); if (lang.isEmpty()) { // If language is not set in the configuration, you try deducing it from the system language lang = getCodeFromLanguage(localeProvider.getLocale()); @@ -550,12 +548,13 @@ private boolean updateWeatherData(Set features) { urlStr = urlStr.replace("%SETTINGS%", "lang:" + lang.toUpperCase()); } - urlStr = urlStr.replace("%QUERY%", StringUtils.trimToEmpty(config.location)); + String location = config.location == null ? "" : config.location.trim(); + urlStr = urlStr.replace("%QUERY%", location); if (logger.isDebugEnabled()) { logger.debug("URL = {}", urlStr.replace("%APIKEY%", "***")); } - urlStr = urlStr.replace("%APIKEY%", StringUtils.trimToEmpty(bridgeHandler.getApikey())); + urlStr = urlStr.replace("%APIKEY%", bridgeHandler.getApikey()); // Run the HTTP request and get the JSON response from Weather Underground String response = null; @@ -564,8 +563,7 @@ private boolean updateWeatherData(Set features) { logger.debug("weatherData = {}", response); } catch (IllegalArgumentException e) { // catch Illegal character in path at index XX: http://api.wunderground.com/... - error = "Error creating URI with location parameter: '" + StringUtils.trimToEmpty(config.location) - + "'"; + error = "Error creating URI with location parameter: '" + location + "'"; errorDetail = e.getMessage(); statusDescr = "@text/offline.uri-error"; } @@ -630,11 +628,11 @@ private boolean updateWeatherData(Set features) { */ public static String getCodeFromLanguage(Locale locale) { String key = locale.getLanguage() + "-" + locale.getCountry(); - String language = StringUtils.trimToEmpty(LANG_COUNTRY_TO_WU_CODES.get(key)); - if (language.isEmpty()) { - language = StringUtils.trimToEmpty(LANG_ISO_TO_WU_CODES.get(locale.getLanguage().toUpperCase())); + String language = LANG_COUNTRY_TO_WU_CODES.get(key); + if (language == null) { + language = LANG_ISO_TO_WU_CODES.get(locale.getLanguage().toUpperCase()); } - return language; + return language != null ? language : ""; } private WUQuantity getTemperature(BigDecimal siValue, BigDecimal imperialValue) { diff --git a/bundles/org.openhab.binding.weatherunderground/src/main/java/org/openhab/binding/weatherunderground/internal/json/WeatherUndergroundJsonUtils.java b/bundles/org.openhab.binding.weatherunderground/src/main/java/org/openhab/binding/weatherunderground/internal/json/WeatherUndergroundJsonUtils.java index cf4dc5f5294f2..f01ee524a3dcb 100644 --- a/bundles/org.openhab.binding.weatherunderground/src/main/java/org/openhab/binding/weatherunderground/internal/json/WeatherUndergroundJsonUtils.java +++ b/bundles/org.openhab.binding.weatherunderground/src/main/java/org/openhab/binding/weatherunderground/internal/json/WeatherUndergroundJsonUtils.java @@ -20,7 +20,6 @@ import java.time.ZonedDateTime; import java.util.TimeZone; -import org.apache.commons.lang.StringUtils; import org.slf4j.LoggerFactory; /** @@ -104,9 +103,9 @@ private static boolean isValid(String value) { */ public static URL getValidUrl(String url) { URL validUrl = null; - if (StringUtils.isNotBlank(url)) { + if (url != null && !url.trim().isEmpty()) { try { - validUrl = new URL(url); + validUrl = new URL(url.trim()); } catch (MalformedURLException e) { } }