Skip to content

Commit

Permalink
Removed dependency on 'org.apache.commons.codec' (openhab#7460)
Browse files Browse the repository at this point in the history
* Removed dependency on 'org.apache.commons.codec'
* Replaced usage of 'StringUtils'

Signed-off-by: Christoph Weitkamp <[email protected]>
  • Loading branch information
cweitkamp authored and andrewfg committed Aug 31, 2020
1 parent b3060c1 commit 9c1a3d5
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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),
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -229,7 +230,7 @@ private <T> T sendLoggedInRequest(final AbstractRequest req, final Class<T> 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);
}
}
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)'
Expand Down
1 change: 0 additions & 1 deletion itests/org.openhab.binding.mqtt.homie.tests/itest.bndrun
Original file line number Diff line number Diff line change
Expand Up @@ -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)',\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)',\
Expand Down

0 comments on commit 9c1a3d5

Please sign in to comment.