Skip to content
Merged
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
17 changes: 12 additions & 5 deletions src/main/java/com/influxdb/v3/client/InfluxDBClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,8 @@ Stream<VectorSchemaRoot> queryBatches(@Nonnull final String query,
* Creates a new instance of the {@link InfluxDBClient} for interacting with an InfluxDB server, simplifying
* common operations such as writing, querying.
*
* @param host the URL of the InfluxDB server
* @param host the URL of the InfluxDB server.<br>
* NOTE: IPv6 must be wrapped inside square brackets .e.g: http://[2001:db8::1].
* @param token the authentication token for accessing the InfluxDB server, can be null
* @param database the database to be used for InfluxDB operations, can be null
* @return new instance of the {@link InfluxDBClient}
Expand All @@ -505,7 +506,8 @@ static InfluxDBClient getInstance(@Nonnull final String host,
* Creates a new instance of the {@link InfluxDBClient} for interacting with an InfluxDB server, simplifying
* common operations such as writing, querying.
*
* @param host the URL of the InfluxDB server
* @param host the URL of the InfluxDB server.<br>
* NOTE: IPv6 must be wrapped inside square brackets .e.g: http://[2001:db8::1].
* @param token the authentication token for accessing the InfluxDB server, can be null
* @param database the database to be used for InfluxDB operations, can be null
* @param defaultTags tags to be added by default to writes of points
Expand Down Expand Up @@ -561,7 +563,8 @@ static InfluxDBClient getInstance(@Nonnull final ClientConfig config) {
* <li>writeUseV2Api - use V2 API endpoint</li>
* </ul>
*
* @param connectionString connection string
* @param connectionString connection string.<br>
* NOTE: IPv6 must be wrapped inside square brackets .e.g: http://[2001:db8::1].
* @return instance of {@link InfluxDBClient}
*/
@Nonnull
Expand All @@ -584,7 +587,10 @@ static InfluxDBClient getInstance(@Nonnull final String connectionString) {
* <p>
* Supported environment variables:
* <ul>
* <li>INFLUX_HOST - cloud/server URL <i>required</i></li>
* <li>INFLUX_HOST - cloud/server URL.<br>
* NOTE: IPv6 must be wrapped inside square brackets .e.g: http://[2001:db8::1].
* <i>required</i>
* </li>
* <li>INFLUX_TOKEN - authentication token <i>required</i></li>
* <li>INFLUX_AUTH_SCHEME - authentication scheme</li>
* <li>INFLUX_ORG - organization name</li>
Expand All @@ -597,7 +603,8 @@ static InfluxDBClient getInstance(@Nonnull final String connectionString) {
* </ul>
* Supported system properties:
* <ul>
* <li>influx.host - cloud/server URL <i>required</i></li>
* <li>influx.host - cloud/server URL.<br>
* NOTE: IPv6 must be wrapped inside square brackets .e.g: http://[2001:db8::1]. <i>required</i></li>
* <li>influx.token - authentication token <i>required</i></li>
* <li>influx.authScheme - authentication scheme</li>
* <li>influx.org - organization name</li>
Expand Down
26 changes: 21 additions & 5 deletions src/main/java/com/influxdb/v3/client/config/ClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.net.Authenticator;
import java.net.MalformedURLException;
import java.net.ProxySelector;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.time.Duration;
import java.util.Arrays;
Expand All @@ -48,7 +50,10 @@
* <p>
* You can configure following properties:
* <ul>
* <li><code>host</code> - hostname or IP address of the InfluxDB server</li>
* <li>
* <code>host</code> - hostname or IP address of the InfluxDB server.<br>
* NOTE: IPv6 must be wrapped inside square brackets .e.g: http://[2001:db8::1].
* </li>
* <li><code>token</code> - authentication token for accessing the InfluxDB server</li>
* <li><code>authScheme</code> - authentication scheme</li>
* <li><code>organization</code> - organization to be used for operations</li>
Expand Down Expand Up @@ -375,8 +380,17 @@ public List<ClientInterceptor> getInterceptors() {
* Validates the configuration properties.
*/
public void validate() {
if (host == null || host.isBlank()) {
throw new IllegalArgumentException("The URL of the InfluxDB server has to be defined.");
try {
if (host == null || host.isBlank()) {
throw new IllegalArgumentException("Invalid URL.");
}

URI uri = new URI(host);
if (uri.getHost() == null) {
throw new URISyntaxException(host, "Invalid URL.");
}
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Invalid URL.");
}
}

Expand Down Expand Up @@ -481,7 +495,8 @@ public static final class Builder {
private List<ClientInterceptor> interceptors;

/**
* Sets the URL of the InfluxDB server.
* Sets the URL of the InfluxDB server.<br>
* NOTE: IPv6 must be wrapped inside square brackets .e.g: http://[2001:db8::1].
*
* @param host URL of the InfluxDB server
* @return this
Expand Down Expand Up @@ -827,7 +842,8 @@ public ClientConfig build() {
/**
* Build an instance of {@code ClientConfig} from connection string.
*
* @param connectionString connection string in URL format
* @param connectionString connection string in URL format.<br>
* NOTE: IPv6 must be wrapped inside square brackets .e.g: http://[2001:db8::1].
* @return the configuration for an {@code InfluxDBClient}
* @throws MalformedURLException when argument is not valid URL
*/
Expand Down
42 changes: 40 additions & 2 deletions src/test/java/com/influxdb/v3/client/InfluxDBClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
*/
package com.influxdb.v3.client;

import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;
import java.util.Properties;

Expand All @@ -40,6 +43,38 @@ void withProxyUrl() {
Assertions.assertThat(clientConfig.getProxyUrl()).isEqualTo(proxyUrl);
}

@Test
void parseIpv6() throws UnknownHostException, URISyntaxException {
record Test(String url, boolean isCorrect) {
}
var tests = List.of(
new Test("http://[2001:db8::1]/", true),
new Test("http://[2001:db8:a0b:12f0::1]/index.html", true),
new Test("http://[2001:db8:a0b:12f0::1]:80/index.html", true),
new Test("https://[2001:db8:a0b:12f0::1%25eth0]:15000/", true),
new Test("http://[2607:f8b0:4005:802::1007]/", true),
new Test("http://2001:db8::1/", false),
new Test("http://2001:db8::1:8080/", false)
);
for (Test test : tests) {
if (!test.isCorrect()) {
Assertions.assertThatThrownBy(() -> {
try (var client = InfluxDBClient.getInstance(test.url(), "my-token".toCharArray(), "bucket0")) {
client.getServerVersion();
} catch (Exception e) {
throw new RuntimeException(e);
}
}).hasMessageContaining("Invalid URL.");
} else {
Assertions.assertThatNoException().isThrownBy(() -> {
try (var ignored = InfluxDBClient.getInstance(test.url(), "my-token".toCharArray(), "bucket0")) {
Assertions.assertThat(true);
}
});
}
}
}

@Test
void withSslRootsFilePath() {
String path = "/path/to/cert";
Expand All @@ -51,10 +86,13 @@ void withSslRootsFilePath() {

@Test
void requiredHost() {

Assertions.assertThatThrownBy(() -> InfluxDBClient.getInstance(null, "my-token".toCharArray(), "my-database"))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("The URL of the InfluxDB server has to be defined.");
.hasMessage("Invalid URL.");

Assertions.assertThatThrownBy(() -> InfluxDBClient.getInstance(" ", "my-token".toCharArray(), "my-database"))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Invalid URL.");
}

@Test
Expand Down
Loading