From 9c1a3d56daad2b906cd31a008e947c6a893dc3f7 Mon Sep 17 00:00:00 2001 From: Christoph Weitkamp Date: Fri, 24 Apr 2020 13:59:18 +0200 Subject: [PATCH] Removed dependency on 'org.apache.commons.codec' (#7460) * Removed dependency on 'org.apache.commons.codec' * Replaced usage of 'StringUtils' Signed-off-by: Christoph Weitkamp --- .../internal/api/FreeboxApiManager.java | 16 ++++++--------- .../internal/security/LxWsSecurity.java | 6 ++---- .../handler/MillheatAccountHandler.java | 20 ++++++++++--------- .../itest.bndrun | 1 - .../itest.bndrun | 1 - .../itest.bndrun | 1 - 6 files changed, 19 insertions(+), 26 deletions(-) diff --git a/bundles/org.openhab.binding.freebox/src/main/java/org/openhab/binding/freebox/internal/api/FreeboxApiManager.java b/bundles/org.openhab.binding.freebox/src/main/java/org/openhab/binding/freebox/internal/api/FreeboxApiManager.java index 06b4934ba681c..d3378735bd1ba 100644 --- a/bundles/org.openhab.binding.freebox/src/main/java/org/openhab/binding/freebox/internal/api/FreeboxApiManager.java +++ b/bundles/org.openhab.binding.freebox/src/main/java/org/openhab/binding/freebox/internal/api/FreeboxApiManager.java @@ -28,8 +28,7 @@ import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; -import org.apache.commons.codec.binary.Hex; -import org.apache.commons.lang.StringUtils; +import org.eclipse.smarthome.core.util.HexUtils; import org.eclipse.smarthome.io.net.http.HttpUtil; import org.openhab.binding.freebox.internal.api.model.FreeboxAirMediaConfig; import org.openhab.binding.freebox.internal.api.model.FreeboxAirMediaConfigResponse; @@ -133,7 +132,7 @@ public boolean authorize(boolean useHttps, String fqdn, String apiBaseUrl, Strin boolean granted = false; try { String token = appToken; - if (StringUtils.isEmpty(token)) { + if (token == null || token.isEmpty()) { FreeboxAuthorizeRequest request = new FreeboxAuthorizeRequest(appId, appName, appVersion, deviceName); FreeboxAuthorizeResult response = executePostUrl("login/authorize/", gson.toJson(request), FreeboxAuthorizeResponse.class, false); @@ -351,7 +350,7 @@ public void playMedia(String url, String airPlayName, String airPlayPassword) th FreeboxAirMediaReceiverRequest request = new FreeboxAirMediaReceiverRequest(); request.setStartAction(); request.setVideoMediaType(); - if (StringUtils.isNotEmpty(airPlayPassword)) { + if (airPlayPassword != null && !airPlayPassword.isEmpty()) { request.setPassword(airPlayPassword); } request.setMedia(url); @@ -363,7 +362,7 @@ public void stopMedia(String airPlayName, String airPlayPassword) throws Freebox FreeboxAirMediaReceiverRequest request = new FreeboxAirMediaReceiverRequest(); request.setStopAction(); request.setVideoMediaType(); - if (StringUtils.isNotEmpty(airPlayPassword)) { + if (airPlayPassword != null && !airPlayPassword.isEmpty()) { request.setPassword(airPlayPassword); } executePostUrl("airmedia/receivers/" + encodeUrl(airPlayName) + "/", gson.toJson(request), @@ -493,11 +492,8 @@ private static String hmacSha1(String key, String value) throws FreeboxException // Compute the hmac on input data bytes byte[] rawHmac = mac.doFinal(value.getBytes()); - // Convert raw bytes to Hex - byte[] hexBytes = new Hex().encode(rawHmac); - - // Covert array of Hex bytes to a String - return new String(hexBytes, StandardCharsets.UTF_8); + // Convert raw bytes to a String + return HexUtils.bytesToHex(rawHmac); } catch (IllegalArgumentException | NoSuchAlgorithmException | InvalidKeyException | IllegalStateException e) { throw new FreeboxException("Computing the hmac-sha1 of the challenge and the app token failed", e); } diff --git a/bundles/org.openhab.binding.loxone/src/main/java/org/openhab/binding/loxone/internal/security/LxWsSecurity.java b/bundles/org.openhab.binding.loxone/src/main/java/org/openhab/binding/loxone/internal/security/LxWsSecurity.java index 35ca7be176bfd..8c1c2eac2e27b 100644 --- a/bundles/org.openhab.binding.loxone/src/main/java/org/openhab/binding/loxone/internal/security/LxWsSecurity.java +++ b/bundles/org.openhab.binding.loxone/src/main/java/org/openhab/binding/loxone/internal/security/LxWsSecurity.java @@ -21,8 +21,6 @@ import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; -import org.apache.commons.codec.DecoderException; -import org.apache.commons.codec.binary.Hex; import org.eclipse.smarthome.core.util.HexUtils; import org.openhab.binding.loxone.internal.LxServerHandler; import org.openhab.binding.loxone.internal.LxServerHandlerApi; @@ -135,13 +133,13 @@ String hashString(String string, String hashKeyHex) { return null; } try { - byte[] hashKeyBytes = Hex.decodeHex(hashKeyHex.toCharArray()); + byte[] hashKeyBytes = HexUtils.hexToBytes(hashKeyHex); SecretKeySpec signKey = new SecretKeySpec(hashKeyBytes, "HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(signKey); byte[] rawData = mac.doFinal(string.getBytes()); return HexUtils.bytesToHex(rawData); - } catch (DecoderException | NoSuchAlgorithmException | InvalidKeyException e) { + } catch (NoSuchAlgorithmException | InvalidKeyException e) { return null; } } diff --git a/bundles/org.openhab.binding.millheat/src/main/java/org/openhab/binding/millheat/internal/handler/MillheatAccountHandler.java b/bundles/org.openhab.binding.millheat/src/main/java/org/openhab/binding/millheat/internal/handler/MillheatAccountHandler.java index cdcf19c9807d9..eeb3f3173d69e 100644 --- a/bundles/org.openhab.binding.millheat/src/main/java/org/openhab/binding/millheat/internal/handler/MillheatAccountHandler.java +++ b/bundles/org.openhab.binding.millheat/src/main/java/org/openhab/binding/millheat/internal/handler/MillheatAccountHandler.java @@ -12,8 +12,8 @@ */ package org.openhab.binding.millheat.internal.handler; -import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.List; import java.util.Optional; @@ -23,8 +23,6 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; -import org.apache.commons.codec.digest.DigestUtils; -import org.apache.commons.lang.StringUtils; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jetty.client.HttpClient; @@ -44,6 +42,7 @@ import org.eclipse.smarthome.core.thing.binding.BaseBridgeHandler; import org.eclipse.smarthome.core.thing.binding.ThingHandler; import org.eclipse.smarthome.core.types.Command; +import org.eclipse.smarthome.core.util.HexUtils; import org.openhab.binding.millheat.internal.MillheatCommunicationException; import org.openhab.binding.millheat.internal.client.BooleanSerializer; import org.openhab.binding.millheat.internal.client.RequestLogger; @@ -88,6 +87,7 @@ */ @NonNullByDefault public class MillheatAccountHandler extends BaseBridgeHandler { + private static final String SHA_1_ALGORITHM = "SHA-1"; private static final int MIN_TIME_BETWEEEN_MODEL_UPDATES_MS = 30_000; private static final int NUM_NONCE_CHARS = 16; private static final String CONTENT_TYPE = "application/x-zc-object"; @@ -155,15 +155,16 @@ public boolean doLogin() { rsp.errorDescription)); } else { // No error provided on login, proceed to find token and userid - token = StringUtils.trimToNull(rsp.token); + String localToken = rsp.token.trim(); userId = rsp.userId == null ? null : rsp.userId.toString(); - if (token == null) { + if (localToken == null || localToken.isEmpty()) { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "error login in, no token provided"); } else if (userId == null) { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "error login in, no userId provided"); } else { + token = localToken; return true; } } @@ -229,7 +230,7 @@ private T sendLoggedInRequest(final AbstractRequest req, final Class resp try { final Request request = buildLoggedInRequest(req); return sendRequest(request, req, responseType); - } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) { + } catch (NoSuchAlgorithmException e) { throw new MillheatCommunicationException("Error building Millheat request: " + e.getMessage(), e); } } @@ -343,12 +344,13 @@ private void updateThingStatuses() { } } - private Request buildLoggedInRequest(final AbstractRequest req) - throws NoSuchAlgorithmException, UnsupportedEncodingException { + private Request buildLoggedInRequest(final AbstractRequest req) throws NoSuchAlgorithmException { final String nonce = getRandomString(NUM_NONCE_CHARS); final String timestamp = String.valueOf(System.currentTimeMillis() / 1000); final String signatureBasis = REQUEST_TIMEOUT + timestamp + nonce + token; - final String signature = DigestUtils.shaHex(signatureBasis); + MessageDigest md = MessageDigest.getInstance(SHA_1_ALGORITHM); + byte[] sha1Hash = md.digest(signatureBasis.getBytes(StandardCharsets.UTF_8)); + final String signature = HexUtils.bytesToHex(sha1Hash).toLowerCase(); final String reqJson = gson.toJson(req); final Request request = httpClient.newRequest(serviceEndpoint + req.getRequestUrl()); diff --git a/itests/org.openhab.binding.mqtt.homeassistant.tests/itest.bndrun b/itests/org.openhab.binding.mqtt.homeassistant.tests/itest.bndrun index 59b2378e04df2..efa3264738020 100644 --- a/itests/org.openhab.binding.mqtt.homeassistant.tests/itest.bndrun +++ b/itests/org.openhab.binding.mqtt.homeassistant.tests/itest.bndrun @@ -67,7 +67,6 @@ Fragment-Host: org.openhab.binding.mqtt.homeassistant tec.uom.se;version='[1.0.10,1.0.11)',\ ch.qos.logback.classic;version='[1.2.3,1.2.4)',\ org.apache.servicemix.bundles.jaxb-impl;version='[2.2.11,2.2.12)',\ - org.apache.commons.codec;version='[1.10.0,1.10.1)',\ net.bytebuddy.byte-buddy;version='[1.9.10,1.9.11)',\ net.bytebuddy.byte-buddy-agent;version='[1.9.10,1.9.11)',\ org.mockito.mockito-core;version='[3.1.0,3.1.1)' diff --git a/itests/org.openhab.binding.mqtt.homie.tests/itest.bndrun b/itests/org.openhab.binding.mqtt.homie.tests/itest.bndrun index befe3cd22923a..818f8bf72a26a 100644 --- a/itests/org.openhab.binding.mqtt.homie.tests/itest.bndrun +++ b/itests/org.openhab.binding.mqtt.homie.tests/itest.bndrun @@ -66,7 +66,6 @@ Fragment-Host: org.openhab.binding.mqtt.homie tec.uom.lib.uom-lib-common;version='[1.0.3,1.0.4)',\ tec.uom.se;version='[1.0.10,1.0.11)',\ org.apache.servicemix.bundles.jaxb-impl;version='[2.2.11,2.2.12)',\ - org.apache.commons.codec;version='[1.10.0,1.10.1)',\ ch.qos.logback.classic;version='[1.2.3,1.2.4)',\ net.bytebuddy.byte-buddy;version='[1.9.10,1.9.11)',\ net.bytebuddy.byte-buddy-agent;version='[1.9.10,1.9.11)',\ diff --git a/itests/org.openhab.io.mqttembeddedbroker.tests/itest.bndrun b/itests/org.openhab.io.mqttembeddedbroker.tests/itest.bndrun index 6000b2502bd85..174a7de3c5774 100644 --- a/itests/org.openhab.io.mqttembeddedbroker.tests/itest.bndrun +++ b/itests/org.openhab.io.mqttembeddedbroker.tests/itest.bndrun @@ -25,7 +25,6 @@ Fragment-Host: org.openhab.io.mqttembeddedbroker io.netty.resolver;version='[4.1.42,4.1.43)',\ io.netty.transport;version='[4.1.42,4.1.43)',\ javax.measure.unit-api;version='[1.0.0,1.0.1)',\ - org.apache.commons.codec;version='[1.10.0,1.10.1)',\ org.apache.commons.io;version='[2.2.0,2.2.1)',\ org.apache.commons.lang;version='[2.6.0,2.6.1)',\ org.apache.felix.configadmin;version='[1.9.8,1.9.9)',\