Skip to content

Commit

Permalink
Downgrade to JDK8 API
Browse files Browse the repository at this point in the history
  • Loading branch information
zhicwu committed Oct 10, 2021
1 parent 4f93201 commit cd32678
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ru.yandex.clickhouse;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
Expand All @@ -26,18 +27,25 @@ public class ClickhouseJdbcUrlParser {
private ClickhouseJdbcUrlParser() {
}

public static ClickHouseProperties parse(String jdbcUrl, Properties defaults) throws URISyntaxException {
if (!jdbcUrl.startsWith(JDBC_CLICKHOUSE_PREFIX)) {
throw new URISyntaxException(jdbcUrl, "'" + JDBC_CLICKHOUSE_PREFIX + "' prefix is mandatory");
private static String decode(String str) {
try {
return URLDecoder.decode(str, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
// don't print the content here as it may contain password
log.warn("Failed to decode given string, fallback to the original string");
return str;
}
return parseClickhouseUrl(jdbcUrl.substring(JDBC_PREFIX.length()), defaults);
}

private static ClickHouseProperties parseClickhouseUrl(String uriString, Properties defaults)
throws URISyntaxException {
URI uri = new URI(uriString);
Properties urlProperties = parseUriQueryPart(uri.getQuery(), defaults);
ClickHouseProperties props = new ClickHouseProperties(urlProperties);
String host = uri.getHost();
if (host == null || host.isEmpty()) {
throw new IllegalArgumentException("host is missed or wrong");
}
props.setHost(uri.getHost());
int port = uri.getPort();
if (port == -1) {
Expand All @@ -47,14 +55,11 @@ private static ClickHouseProperties parseClickhouseUrl(String uriString, Propert
String credentials = uri.getRawUserInfo();
if (credentials != null && !credentials.isEmpty()) {
int index = credentials.indexOf(':');
String userName = index == 0 ? ""
: URLDecoder.decode(index > 0 ? credentials.substring(0, index) : credentials,
StandardCharsets.UTF_8);
String userName = index == 0 ? "" : decode(index > 0 ? credentials.substring(0, index) : credentials);
if (!userName.isEmpty()) {
props.setUser(userName);
}
String password = index < 0 ? ""
: URLDecoder.decode(credentials.substring(index + 1), StandardCharsets.UTF_8);
String password = index < 0 ? "" : decode(credentials.substring(index + 1));
if (!password.isEmpty()) {
props.setPassword(password);
}
Expand Down Expand Up @@ -103,4 +108,11 @@ static Properties parseUriQueryPart(String query, Properties defaults) {
}
return urlProps;
}

public static ClickHouseProperties parse(String jdbcUrl, Properties defaults) throws URISyntaxException {
if (!jdbcUrl.startsWith(JDBC_CLICKHOUSE_PREFIX)) {
throw new URISyntaxException(jdbcUrl, "'" + JDBC_CLICKHOUSE_PREFIX + "' prefix is mandatory");
}
return parseClickhouseUrl(jdbcUrl.substring(JDBC_PREFIX.length()), defaults);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ public void testParseCredentials() throws Exception {
props.asProperties());
Assert.assertEquals(chProps.getUser(), "user");
Assert.assertEquals(chProps.getPassword(), "a:passwd");

chProps = ClickhouseJdbcUrlParser.parse("jdbc:clickhouse://let%40me%3Ain:let%40me%[email protected]",
props.asProperties());
Assert.assertEquals(chProps.getUser(), "let@me:in");
Assert.assertEquals(chProps.getPassword(), "let@me:in");
}

@Test(groups = "unit")
Expand Down

0 comments on commit cd32678

Please sign in to comment.