diff --git a/jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/JdbcConfiguration.java b/jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/JdbcConfiguration.java index e703bed1d..c87e430cd 100644 --- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/JdbcConfiguration.java +++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/JdbcConfiguration.java @@ -50,30 +50,44 @@ public boolean isDisableFrameworkDetection() { } public JdbcConfiguration(String url, Properties info) { + this.allProperties = new ConcurrentHashMap<>(); + info.forEach((k, v) -> allProperties.put(k.toString(), v.toString())); + this.jdbcUrl = url;//Raw URL this.url = cleanUrl(url); this.user = info.getProperty("user", "default"); this.password = info.getProperty("password", ""); this.disableFrameworkDetection = Boolean.parseBoolean(info.getProperty("disable_frameworks_detection", "false")); - this.allProperties = new ConcurrentHashMap<>(); - info.forEach((k, v) -> allProperties.put(k.toString(), v.toString())); } public static boolean acceptsURL(String url) { return url.startsWith(PREFIX_CLICKHOUSE) || url.startsWith(PREFIX_CLICKHOUSE_SHORT); } - private String cleanUrl(String url) { + protected String cleanUrl(String url) { url = stripUrlPrefix(url); + boolean setSSL = false; + boolean ssl = false; + try { + ssl = Boolean.parseBoolean(allProperties.get("ssl")); + setSSL = true; + } catch (Exception e) { + log.trace("Failed to parse SSL property.", e); + } + if (url.startsWith("//")) { - url = "http:" + url;//Default to HTTP - try { - URL parsedUrl = new URL(url); - if (parsedUrl.getPort() == ClickHouseHttpProto.DEFAULT_HTTPS_PORT) {//If port is 8443, switch to HTTPS - url = "https:" + url; + if (setSSL) { + url = (ssl ? "https:" : "http:") + url; + } else { + url = "http:" + url;//Default to HTTP + try { + URL parsedUrl = new URL(url); + if (parsedUrl.getPort() == ClickHouseHttpProto.DEFAULT_HTTPS_PORT) {//If port is 8443, switch to HTTPS + url = "https:" + url.substring(5); + } + } catch (MalformedURLException e) { + throw new IllegalArgumentException("URL is not valid.", e); } - } catch (MalformedURLException e) { - throw new IllegalArgumentException("URL is not valid.", e); } } diff --git a/jdbc-v2/src/test/java/com/clickhouse/jdbc/internal/JdbcConfigurationTest.java b/jdbc-v2/src/test/java/com/clickhouse/jdbc/internal/JdbcConfigurationTest.java new file mode 100644 index 000000000..3114a4a32 --- /dev/null +++ b/jdbc-v2/src/test/java/com/clickhouse/jdbc/internal/JdbcConfigurationTest.java @@ -0,0 +1,35 @@ +package com.clickhouse.jdbc.internal; + +import com.clickhouse.jdbc.JdbcIntegrationTest; +import org.testng.annotations.BeforeTest; +import org.testng.annotations.Test; + +import java.util.Properties; + +import static org.testng.Assert.assertEquals; + +public class JdbcConfigurationTest extends JdbcIntegrationTest { + @Test(groups = { "integration" }) + public void testCleanUrl() { + JdbcConfiguration configuration = new JdbcConfiguration("jdbc:clickhouse://localhost:8123/clickhouse?param1=value1¶m2=value2", new Properties()); + + String url = "jdbc:clickhouse://localhost:8123/clickhouse?param1=value1¶m2=value2"; + String cleanUrl = configuration.cleanUrl(url); + assertEquals(cleanUrl, "http://localhost:8123/clickhouse?param1=value1¶m2=value2"); + + url = "jdbc:clickhouse://localhost:8443/clickhouse?param1=value1¶m2=value2"; + cleanUrl = configuration.cleanUrl(url); + assertEquals(cleanUrl, "https://localhost:8443/clickhouse?param1=value1¶m2=value2"); + + Properties info = new Properties(); + info.setProperty("ssl", "true"); + configuration = new JdbcConfiguration("jdbc:clickhouse://localhost:8123/clickhouse?param1=value1¶m2=value2", info); + cleanUrl = configuration.cleanUrl(url); + assertEquals(cleanUrl, "https://localhost:8123/clickhouse?param1=value1¶m2=value2"); + + info.setProperty("ssl", "false"); + configuration = new JdbcConfiguration("jdbc:clickhouse://localhost:8443/clickhouse?param1=value1¶m2=value2", info); + cleanUrl = configuration.cleanUrl(url); + assertEquals(cleanUrl, "http://localhost:8443/clickhouse?param1=value1¶m2=value2"); + } +}