Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for custom url path #948

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -125,28 +125,34 @@ static String buildUrl(ClickHouseNode server, ClickHouseRequest<?> request) {
StringBuilder builder = new StringBuilder();
builder.append(config.isSsl() ? "https" : "http").append("://").append(server.getHost()).append(':')
.append(server.getPort()).append('/');

builder.append(buildUrlPath(config));

String query = buildQueryParams(request);
if (!query.isEmpty()) {
builder.append('?').append(query);
}

return builder.toString();
}

private static String buildUrlPath(ClickHouseConfig config) {
StringBuilder contextBuilder = new StringBuilder();
String context = (String) config.getOption(ClickHouseHttpOption.WEB_CONTEXT);
if (context != null && !context.isEmpty()) {

char prev = '/';
for (int i = 0, len = context.length(); i < len; i++) {
char ch = context.charAt(i);
if (ch != '/' || ch != prev) {
builder.append(ch);
contextBuilder.append(ch);
}
prev = ch;
}

if (prev != '/') {
builder.append('/');
}
}

String query = buildQueryParams(request);
if (!query.isEmpty()) {
builder.append('?').append(query);
}

return builder.toString();
return contextBuilder.toString();
}

protected final ClickHouseConfig config;
Expand Down Expand Up @@ -224,6 +230,17 @@ protected String getBaseUrl() {
return baseUrl;
}

protected String getPingUrl() {
StringBuilder pingUrlBuilder = new StringBuilder();
String baseUrl = getBaseUrl();
pingUrlBuilder.append(baseUrl);
if (!baseUrl.endsWith("/")) {
pingUrlBuilder.append("/");
}
pingUrlBuilder.append("ping");
return pingUrlBuilder.toString();
}

/**
* Creates a merged map.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ public boolean ping(int timeout) {
String response = (String) config.getOption(ClickHouseHttpOption.DEFAULT_RESPONSE);
HttpURLConnection c = null;
try {
c = newConnection(getBaseUrl() + "ping", false);
String pingUrl = getPingUrl();
c = newConnection(pingUrl, false);
c.setConnectTimeout(timeout);
c.setReadTimeout(timeout);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import com.clickhouse.client.logging.Logger;
import com.clickhouse.client.logging.LoggerFactory;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -119,7 +118,7 @@ protected HttpClientConnectionImpl(ClickHouseNode server, ClickHouseRequest<?> r
}

httpClient = builder.build();
pingRequest = newRequest(getBaseUrl() + "ping");
pingRequest = newRequest(getPingUrl());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void testBuildUrl() throws Exception {
"http://localhost:8123/?compress=1&extremes=0");
Assert.assertEquals(
ClickHouseHttpConnection.buildUrl(server, request.option(ClickHouseHttpOption.WEB_CONTEXT, ".")),
"http://localhost:8123/./?compress=1&extremes=0");
"http://localhost:8123/.?compress=1&extremes=0");
Assert.assertEquals(
ClickHouseHttpConnection.buildUrl(server, request.option(ClickHouseHttpOption.WEB_CONTEXT, "./")),
"http://localhost:8123/./?compress=1&extremes=0");
Expand Down