]> git.basschouten.com Git - openhab-addons.git/commitdiff
adapt to core StringUtils (#15787)
authorlsiepel <leosiepel@gmail.com>
Sat, 21 Oct 2023 09:54:50 +0000 (11:54 +0200)
committerGitHub <noreply@github.com>
Sat, 21 Oct 2023 09:54:50 +0000 (11:54 +0200)
Signed-off-by: Leo Siepel <leosiepel@gmail.com>
bundles/org.openhab.io.openhabcloud/src/main/java/org/openhab/io/openhabcloud/internal/CloudService.java

index 66d65169b20800a13bac25e8272a53a300356c56..e640c30b419a8f03bff80c26381b9af212ca16c4 100644 (file)
@@ -17,7 +17,6 @@ import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
-import java.security.SecureRandom;
 import java.util.Arrays;
 import java.util.HashSet;
 import java.util.List;
@@ -47,6 +46,7 @@ import org.openhab.core.model.script.engine.action.ActionService;
 import org.openhab.core.net.HttpServiceUtil;
 import org.openhab.core.types.Command;
 import org.openhab.core.types.TypeParser;
+import org.openhab.core.util.StringUtils;
 import org.openhab.io.openhabcloud.NotificationAction;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
@@ -80,8 +80,6 @@ public class CloudService implements ActionService, CloudClientListener, EventSu
     private static final int DEFAULT_LOCAL_OPENHAB_MAX_CONCURRENT_REQUESTS = 200;
     private static final int DEFAULT_LOCAL_OPENHAB_REQUEST_TIMEOUT = 30000;
     private static final String HTTPCLIENT_NAME = "openhabcloud";
-    private static final String CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
-    private static final SecureRandom SR = new SecureRandom();
 
     private final Logger logger = LoggerFactory.getLogger(CloudService.class);
 
@@ -292,14 +290,6 @@ public class CloudService implements ActionService, CloudClientListener, EventSu
         }
     }
 
-    private String randomString(int length) {
-        StringBuilder sb = new StringBuilder(length);
-        for (int i = 0; i < length; i++) {
-            sb.append(CHARS.charAt(SR.nextInt(CHARS.length())));
-        }
-        return sb.toString();
-    }
-
     /**
      * Creates a random secret and writes it to the <code>userdata/openhabcloud</code>
      * directory. An existing <code>secret</code> file won't be overwritten.
@@ -310,7 +300,7 @@ public class CloudService implements ActionService, CloudClientListener, EventSu
         String newSecretString = "";
 
         if (!file.exists()) {
-            newSecretString = randomString(20);
+            newSecretString = StringUtils.getRandomAlphanumeric(20);
             logger.debug("New secret = {}", censored(newSecretString));
             writeFile(file, newSecretString);
         } else {