All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.thunderz99.cosmos.util.ConnectionStringUtil Maven / Gradle / Ivy

There is a newer version: 0.7.11
Show newest version
package io.github.thunderz99.cosmos.util;

import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ConnectionStringUtil {

    private static Logger log = LoggerFactory.getLogger(ConnectionStringUtil.class);

    static Pattern connectionStringPattern = Pattern.compile("AccountEndpoint=(?.+);AccountKey=(?[^;]+);?");

    public static Pair parseConnectionString(String connectionString) {

        var matcher = connectionStringPattern.matcher(connectionString);
        if (!matcher.find()) {
            throw new IllegalStateException(
                    "Make sure connectionString contains 'AccountEndpoint=' and 'AccountKey=' ");
        }
        String endpoint = matcher.group("endpoint");
        String key = matcher.group("key");

        Checker.check(StringUtils.isNotBlank(endpoint), "Make sure connectionString contains 'AccountEndpoint=' ");
        Checker.check(StringUtils.isNotBlank(key), "Make sure connectionString contains 'AccountKey='");

        if (log.isInfoEnabled()) {
            log.info("endpoint:{}", endpoint);
            log.info("key:{}...", key.substring(0, 3));
        }

        return Pair.of(endpoint, key);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy