]> git.basschouten.com Git - openhab-addons.git/commitdiff
adapt to core StringUtils (#15785)
authorlsiepel <leosiepel@gmail.com>
Sat, 21 Oct 2023 09:56:27 +0000 (11:56 +0200)
committerGitHub <noreply@github.com>
Sat, 21 Oct 2023 09:56:27 +0000 (11:56 +0200)
Signed-off-by: Leo Siepel <leosiepel@gmail.com>
bundles/org.openhab.binding.millheat/src/main/java/org/openhab/binding/millheat/internal/handler/MillheatAccountHandler.java

index 9ce18af1f8f6abdc03d46dd328cd18fc69579e5b..dfb912396fe0ddb9d6e7685682f2badbc5f7ad9b 100644 (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);