Skip to content

Commit 9b43c63

Browse files
authored
feat: add hstreams schema and default hstream port (#163)
1 parent 68f411e commit 9b43c63

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package io.hstream;
2+
3+
public enum UrlSchema {
4+
HSTREAM,
5+
HSTREAMS
6+
}

client/src/main/java/io/hstream/impl/HStreamClientBuilderImpl.java

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
import io.hstream.HStreamClient;
77
import io.hstream.HStreamClientBuilder;
88
import io.hstream.HStreamDBClientException;
9+
import io.hstream.UrlSchema;
910
import java.io.File;
1011
import java.io.IOException;
12+
import java.util.Arrays;
1113
import java.util.List;
14+
import java.util.stream.Collectors;
15+
import org.apache.commons.lang3.tuple.Pair;
1216

1317
public class HStreamClientBuilderImpl implements HStreamClientBuilder {
1418

@@ -58,29 +62,53 @@ public HStreamClientBuilder tlsCertPath(String certPath) {
5862
@Override
5963
public HStreamClient build() {
6064
checkNotNull(serviceUrl);
61-
List<String> serverUrls = parseServerUrls(serviceUrl);
65+
Pair<UrlSchema, List<String>> schemaHosts = parseServerUrls(serviceUrl);
66+
// FIXME: remove enableTls option
67+
if (schemaHosts.getKey().equals(UrlSchema.HSTREAMS) && !enableTls) {
68+
throw new HStreamDBClientException("hstreams url schema should enable tls");
69+
}
6270
if (enableTls) {
6371
try {
6472
TlsChannelCredentials.Builder credentialsBuilder =
6573
TlsChannelCredentials.newBuilder().trustManager(new File(caPath));
6674
if (enableTlsAuthentication) {
6775
credentialsBuilder = credentialsBuilder.keyManager(new File(certPath), new File(keyPath));
6876
}
69-
return new HStreamClientKtImpl(serverUrls, credentialsBuilder.build());
77+
return new HStreamClientKtImpl(schemaHosts.getRight(), credentialsBuilder.build());
7078
} catch (IOException e) {
7179
throw new HStreamDBClientException(String.format("invalid tls options, %s", e));
7280
}
7381
}
74-
return new HStreamClientKtImpl(serverUrls, null);
82+
return new HStreamClientKtImpl(schemaHosts.getRight(), null);
7583
}
7684

77-
private List<String> parseServerUrls(String url) {
78-
var prefix = "hstream://";
85+
private Pair<UrlSchema, List<String>> parseServerUrls(String url) {
7986
String uriStr = url.strip();
80-
if (!uriStr.startsWith(prefix)) {
87+
var schemaHosts = uriStr.split("://");
88+
if (schemaHosts.length != 2) {
8189
throw new HStreamDBClientException(
8290
"incorrect serviceUrl:" + uriStr + " (correct example: hstream://127.0.0.1:6570)");
8391
}
84-
return List.of(uriStr.substring(prefix.length()).split(","));
92+
var schemaStr = schemaHosts[0];
93+
UrlSchema urlSchema;
94+
try {
95+
urlSchema = UrlSchema.valueOf(schemaStr.toUpperCase());
96+
} catch (IllegalArgumentException e) {
97+
throw new HStreamDBClientException("Invalid url schema:" + schemaStr);
98+
}
99+
var hosts = schemaHosts[1];
100+
return Pair.of(urlSchema, parseHosts(hosts));
101+
}
102+
103+
private List<String> parseHosts(String hosts) {
104+
return Arrays.stream(hosts.split(",")).map(this::normalizeHost).collect(Collectors.toList());
105+
}
106+
107+
private String normalizeHost(String host) {
108+
var address_port = host.split(":");
109+
if (address_port.length == 1) {
110+
return host + ":6570";
111+
}
112+
return host;
85113
}
86114
}

0 commit comments

Comments
 (0)