adapt to core StringUtils (#15785)

Signed-off-by: Leo Siepel <leosiepel@gmail.com>
This commit is contained in:
lsiepel 2023-10-21 11:56:27 +02:00 committed by GitHub
parent 041c30159d
commit bf0cc2ba0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,10 +15,8 @@ package org.openhab.binding.millheat.internal.handler;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
@ -73,6 +71,7 @@ import org.openhab.core.thing.binding.BaseBridgeHandler;
import org.openhab.core.thing.binding.ThingHandler;
import org.openhab.core.types.Command;
import org.openhab.core.util.HexUtils;
import org.openhab.core.util.StringUtils;
import org.osgi.framework.BundleContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -93,7 +92,6 @@ public class MillheatAccountHandler extends BaseBridgeHandler {
private static final int NUM_NONCE_CHARS = 16;
private static final String CONTENT_TYPE = "application/x-zc-object";
private static final String ALLOWED_NONCE_CHARACTERS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final int ALLOWED_NONCE_CHARACTERS_LENGTH = ALLOWED_NONCE_CHARACTERS.length();
private static final String REQUEST_TIMEOUT = "300";
public static String authEndpoint = "https://eurouter.ablecloud.cn:9005/zc-account/v1/";
public static String serviceEndpoint = "https://eurouter.ablecloud.cn:9005/millService/v1/";
@ -107,15 +105,6 @@ public class MillheatAccountHandler extends BaseBridgeHandler {
private @Nullable ScheduledFuture<?> statusFuture;
private @NonNullByDefault({}) MillheatAccountConfiguration config;
private static String getRandomString(final int sizeOfRandomString) {
final Random random = new SecureRandom();
final StringBuilder sb = new StringBuilder(sizeOfRandomString);
for (int i = 0; i < sizeOfRandomString; ++i) {
sb.append(ALLOWED_NONCE_CHARACTERS.charAt(random.nextInt(ALLOWED_NONCE_CHARACTERS_LENGTH)));
}
return sb.toString();
}
public MillheatAccountHandler(final Bridge bridge, final HttpClient httpClient, final BundleContext context) {
super(bridge);
this.httpClient = httpClient;
@ -343,7 +332,7 @@ public class MillheatAccountHandler extends BaseBridgeHandler {
}
private Request buildLoggedInRequest(final AbstractRequest req) throws NoSuchAlgorithmException {
final String nonce = getRandomString(NUM_NONCE_CHARS);
final String nonce = StringUtils.getRandomString(NUM_NONCE_CHARS, ALLOWED_NONCE_CHARACTERS);
final String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
final String signatureBasis = REQUEST_TIMEOUT + timestamp + nonce + token;
MessageDigest md = MessageDigest.getInstance(SHA_1_ALGORITHM);