Skip to content

Commit

Permalink
[weatherunderground] Removed dependency on 'org.apache.commons.lang' (#…
Browse files Browse the repository at this point in the history
…7779)

* [weatherunderground] Removed dependency on 'org.apache.commons.lang'

Relative to #7722

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
  • Loading branch information
lolodomo authored May 26, 2020
1 parent 8563777 commit 74b0787
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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();
}
Expand All @@ -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 {
Expand Down Expand Up @@ -168,7 +167,7 @@ public void handleCommand(ChannelUID channelUID, Command command) {
// not needed
}

public @Nullable String getApikey() {
public String getApikey() {
return apikey;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -537,7 +535,7 @@ private boolean updateWeatherData(Set<String> 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());
Expand All @@ -550,12 +548,13 @@ private boolean updateWeatherData(Set<String> 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;
Expand All @@ -564,8 +563,7 @@ private boolean updateWeatherData(Set<String> 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";
}
Expand Down Expand Up @@ -630,11 +628,11 @@ private boolean updateWeatherData(Set<String> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.time.ZonedDateTime;
import java.util.TimeZone;

import org.apache.commons.lang.StringUtils;
import org.slf4j.LoggerFactory;

/**
Expand Down Expand Up @@ -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) {
}
}
Expand Down

0 comments on commit 74b0787

Please sign in to comment.