Skip to content

Commit 218b124

Browse files
committed
fixed url with SSL property
1 parent f64bbc1 commit 218b124

File tree

2 files changed

+40
-19
lines changed

2 files changed

+40
-19
lines changed

jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/JdbcConfiguration.java

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
import com.clickhouse.client.ClickHouseNode;
44
import com.clickhouse.client.api.Client;
55
import com.clickhouse.client.api.ClientConfigProperties;
6+
import com.clickhouse.client.api.enums.Protocol;
67
import com.clickhouse.client.api.http.ClickHouseHttpProto;
78
import com.clickhouse.data.ClickHouseUtils;
89
import com.clickhouse.jdbc.Driver;
910

1011
import java.net.MalformedURLException;
12+
import java.net.URI;
1113
import java.net.URL;
1214
import java.sql.DriverPropertyInfo;
15+
import java.sql.SQLException;
1316
import java.util.ArrayList;
1417
import java.util.Collections;
1518
import java.util.Comparator;
@@ -23,6 +26,8 @@ public class JdbcConfiguration {
2326
public static final String PREFIX_CLICKHOUSE = "jdbc:clickhouse:";
2427
public static final String PREFIX_CLICKHOUSE_SHORT = "jdbc:ch:";
2528

29+
public static final String USE_SSL_PROP = "ssl";
30+
2631
final boolean disableFrameworkDetection;
2732

2833
private final Map<String, String> clientProperties;
@@ -41,12 +46,14 @@ public boolean isDisableFrameworkDetection() {
4146
* @param url - JDBC url
4247
* @param info - Driver and Client properties.
4348
*/
44-
public JdbcConfiguration(String url, Properties info) {
45-
this.connectionUrl = cleanUrl(url);
49+
public JdbcConfiguration(String url, Properties info) throws SQLException {
4650
this.disableFrameworkDetection = Boolean.parseBoolean(info.getProperty("disable_frameworks_detection", "false"));
4751
this.clientProperties = new HashMap<>();
4852
this.driverProperties = new HashMap<>();
49-
initProperties(this.connectionUrl, info);
53+
initProperties(url, info);
54+
55+
boolean useSSL = Boolean.parseBoolean(info.getProperty("ssl", "false"));
56+
this.connectionUrl = createConnectionURL(url, useSSL);
5057
}
5158

5259
public static boolean acceptsURL(String url) {
@@ -59,20 +66,30 @@ public String getConnectionUrl() {
5966
}
6067

6168
/**
62-
* Transforms JDBC URL to WEB network. Method doesn't do guessing.
63-
* If protocol is not set - then {@code https} used by default.
64-
* User can always pass any protocol explicitly what is always should be encouraged.
69+
* Returns normalized URL that can be passed as parameter to Client#addEndpoint().
70+
* Returned url has only schema and authority and doesn't have query parameters.
71+
* Note: Some BI tools do not let pass
6572
* @param url - JDBC url
73+
* @param ssl - if SSL protocol should be used when protocol is not specified
6674
* @return URL without JDBC prefix
6775
*/
68-
protected String cleanUrl(String url) {
76+
static String createConnectionURL(String url, boolean ssl) throws SQLException {
6977
url = stripUrlPrefix(url);
7078
if (url.startsWith("//")) {
71-
url = "https:" + url;
79+
url = (ssl ? "https:" : "http:") + url;
80+
}
81+
82+
try {
83+
System.out.println(url);
84+
URI tmp = URI.create(url);
85+
System.out.println(tmp);
86+
return tmp.getScheme() + "://" + tmp.getAuthority() + tmp.getPath();
87+
} catch (Exception e) {
88+
throw new SQLException("Failed to parse url", e);
7289
}
73-
return url;
7490
}
75-
private String stripUrlPrefix(String url) {
91+
92+
private static String stripUrlPrefix(String url) {
7693
if (url.startsWith(PREFIX_CLICKHOUSE)) {
7794
return url.substring(PREFIX_CLICKHOUSE.length());
7895
} else if (url.startsWith(PREFIX_CLICKHOUSE_SHORT)) {

jdbc-v2/src/test/java/com/clickhouse/jdbc/internal/JdbcConfigurationTest.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import java.sql.DriverPropertyInfo;
99
import java.util.Arrays;
10-
import java.util.List;
1110
import java.util.Map;
1211
import java.util.Properties;
1312
import java.util.stream.Collectors;
@@ -16,17 +15,23 @@
1615

1716
public class JdbcConfigurationTest {
1817
@Test(dataProvider = "testConnectionUrlDataProvider")
19-
public void testConnectionUrl(String jdbcUrl, String connectionUrl) {
20-
JdbcConfiguration configuration = new JdbcConfiguration(jdbcUrl, new Properties());
18+
public void testConnectionUrl(String jdbcUrl, String connectionUrl, Properties properties) throws Exception {
19+
JdbcConfiguration configuration = new JdbcConfiguration(jdbcUrl, properties);
2120
assertEquals(configuration.getConnectionUrl(), connectionUrl);
2221
}
2322

2423
@DataProvider(name = "testConnectionUrlDataProvider")
2524
public static Object[][] testConnectionUrlDataProvider() {
25+
Properties defaultProps = new Properties();
26+
Properties useSSL = new Properties();
27+
useSSL.put(JdbcConfiguration.USE_SSL_PROP, "true");
28+
2629
return new Object[][] {
27-
{"jdbc:clickhouse://localhost:8123/", "https://localhost:8123/"},
28-
{"jdbc:clickhouse://localhost:8443/clickhouse?param1=value1&param2=value2", "https://localhost:8443/clickhouse?param1=value1&param2=value2"},
29-
{"jdbc:clickhouse:http://localhost:8123/clickhouse?param1=value1&param2=value2", "http://localhost:8123/clickhouse?param1=value1&param2=value2"}
30+
{"jdbc:clickhouse://localhost:8123/", "http://localhost:8123/", defaultProps},
31+
{"jdbc:clickhouse://localhost:8443/clickhouse?param1=value1&param2=value2", "http://localhost:8443/clickhouse", defaultProps},
32+
{"jdbc:clickhouse:https://localhost:8123/clickhouse?param1=value1&param2=value2", "https://localhost:8123/clickhouse", defaultProps},
33+
{"jdbc:clickhouse://localhost:8443/", "https://localhost:8443/", useSSL},
34+
{"jdbc:clickhouse://localhost:8443/default?param1=value1&custom_header1=val1,val2,val3", "https://localhost:8443/default", useSSL},
3035
};
3136
}
3237

@@ -39,11 +44,10 @@ public void testConfigurationProperties() throws Exception {
3944
ClientConfigProperties.commaSeparated(Arrays.asList("http_headers=header1=3,header2=4")));
4045
String url = "jdbc:clickhouse://localhost:8123/clickhouse?client_name=test_application&database=default1";
4146
JdbcConfiguration configuration = new JdbcConfiguration(url, properties);
42-
Assert.assertEquals(configuration.getConnectionUrl(), "https://localhost:8123/clickhouse?client_name=test_application&database=default1");
47+
Assert.assertEquals(configuration.getConnectionUrl(), "http://localhost:8123/clickhouse");
4348
Map<String, DriverPropertyInfo> infos = configuration.getDriverPropertyInfo().stream().collect(Collectors.toMap(d -> d.name, d -> d));
4449

4550
DriverPropertyInfo p = infos.get(ClientConfigProperties.DATABASE.getKey());
46-
Assert.assertEquals(p.value, "default2");
47-
51+
Assert.assertEquals(p.value, "default1");
4852
}
4953
}

0 commit comments

Comments
 (0)