From 4679fd4a15023116c4626fe12227fc7901aaf23c Mon Sep 17 00:00:00 2001 From: Jakub Bednar Date: Thu, 8 Jul 2021 08:48:46 +0200 Subject: [PATCH 1/2] fix: set default http protocol to HTTP 1.1 --- .../client/flux/FluxConnectionOptions.java | 5 ++++- .../flux/FluxConnectionOptionsTest.java | 15 +++++++++++++ .../client/InfluxDBClientOptions.java | 8 +++++-- .../client/InfluxDBClientOptionsTest.java | 16 ++++++++++++++ .../influx/InfluxDB2AutoConfiguration.java | 4 ++++ .../InfluxDB2AutoConfigurationTest.java | 22 ++++++++++++++++++- 6 files changed, 66 insertions(+), 4 deletions(-) diff --git a/client-legacy/src/main/java/com/influxdb/client/flux/FluxConnectionOptions.java b/client-legacy/src/main/java/com/influxdb/client/flux/FluxConnectionOptions.java index 9db6173f825..491fb86428e 100644 --- a/client-legacy/src/main/java/com/influxdb/client/flux/FluxConnectionOptions.java +++ b/client-legacy/src/main/java/com/influxdb/client/flux/FluxConnectionOptions.java @@ -21,6 +21,7 @@ */ package com.influxdb.client.flux; +import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Set; @@ -35,6 +36,7 @@ import okhttp3.HttpUrl; import okhttp3.OkHttpClient; +import okhttp3.Protocol; /** * FluxConnectionOptions are used to configure queries to the Flux. @@ -121,7 +123,8 @@ public Map getParameters() { public static class Builder { private String url; - private OkHttpClient.Builder okHttpClient = new OkHttpClient.Builder(); + private OkHttpClient.Builder okHttpClient = new OkHttpClient.Builder() + .protocols(Collections.singletonList(Protocol.HTTP_1_1)); private Map parameters = new HashMap<>(); /** diff --git a/client-legacy/src/test/java/com/influxdb/client/flux/FluxConnectionOptionsTest.java b/client-legacy/src/test/java/com/influxdb/client/flux/FluxConnectionOptionsTest.java index 1ee818d46a9..58a8309c1a1 100644 --- a/client-legacy/src/test/java/com/influxdb/client/flux/FluxConnectionOptionsTest.java +++ b/client-legacy/src/test/java/com/influxdb/client/flux/FluxConnectionOptionsTest.java @@ -21,7 +21,10 @@ */ package com.influxdb.client.flux; +import java.util.List; + import okhttp3.OkHttpClient; +import okhttp3.Protocol; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.platform.runner.JUnitPlatform; @@ -65,4 +68,16 @@ void okHttpClientValue() { Assertions.assertThat(fluxConnectionOptions.getOkHttpClient()).isEqualTo(okHttpClient); } + + @Test + void protocolVersion() { + + FluxConnectionOptions options = FluxConnectionOptions.builder() + .url("http://localhost:8093") + .build(); + + List protocols = options.getOkHttpClient().build().protocols(); + Assertions.assertThat(protocols).hasSize(1); + Assertions.assertThat(protocols).contains(Protocol.HTTP_1_1); + } } \ No newline at end of file diff --git a/client/src/main/java/com/influxdb/client/InfluxDBClientOptions.java b/client/src/main/java/com/influxdb/client/InfluxDBClientOptions.java index 6afe444c3c0..0dfdcf48cf6 100644 --- a/client/src/main/java/com/influxdb/client/InfluxDBClientOptions.java +++ b/client/src/main/java/com/influxdb/client/InfluxDBClientOptions.java @@ -25,6 +25,7 @@ import java.io.InputStream; import java.time.Duration; import java.time.temporal.ChronoUnit; +import java.util.Collections; import java.util.Properties; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -39,6 +40,7 @@ import okhttp3.HttpUrl; import okhttp3.OkHttpClient; +import okhttp3.Protocol; /** * InfluxDBClientOptions are used to configure theInfluxDB 2.0 connections. @@ -445,7 +447,8 @@ public InfluxDBClientOptions build() { } if (okHttpClient == null) { - okHttpClient = new OkHttpClient.Builder(); + okHttpClient = new OkHttpClient.Builder() + .protocols(Collections.singletonList(Protocol.HTTP_1_1)); } if (logLevel == null) { @@ -477,7 +480,8 @@ private InfluxDBClientOptions.Builder configure(@Nonnull final String url, logLevel(Enum.valueOf(LogLevel.class, logLevel)); } - okHttpClient = new OkHttpClient.Builder(); + okHttpClient = new OkHttpClient.Builder() + .protocols(Collections.singletonList(Protocol.HTTP_1_1)); if (readTimeout != null) { okHttpClient.readTimeout(toDuration(readTimeout)); } diff --git a/client/src/test/java/com/influxdb/client/InfluxDBClientOptionsTest.java b/client/src/test/java/com/influxdb/client/InfluxDBClientOptionsTest.java index 2901a2a0a1a..4c3cad5693e 100644 --- a/client/src/test/java/com/influxdb/client/InfluxDBClientOptionsTest.java +++ b/client/src/test/java/com/influxdb/client/InfluxDBClientOptionsTest.java @@ -21,7 +21,10 @@ */ package com.influxdb.client; +import java.util.List; + import okhttp3.OkHttpClient; +import okhttp3.Protocol; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.platform.runner.JUnitPlatform; @@ -87,4 +90,17 @@ void authorizationSession() { Assertions.assertThat(options.getAuthScheme()).isEqualTo(InfluxDBClientOptions.AuthScheme.SESSION); } + + @Test + void protocolVersion() { + + InfluxDBClientOptions options = InfluxDBClientOptions.builder() + .url("http://localhost:9999") + .authenticateToken("xyz".toCharArray()) + .build(); + + List protocols = options.getOkHttpClient().build().protocols(); + Assertions.assertThat(protocols).hasSize(1); + Assertions.assertThat(protocols).contains(Protocol.HTTP_1_1); + } } \ No newline at end of file diff --git a/spring/src/main/java/com/influxdb/spring/influx/InfluxDB2AutoConfiguration.java b/spring/src/main/java/com/influxdb/spring/influx/InfluxDB2AutoConfiguration.java index 042336b5270..51453560050 100644 --- a/spring/src/main/java/com/influxdb/spring/influx/InfluxDB2AutoConfiguration.java +++ b/spring/src/main/java/com/influxdb/spring/influx/InfluxDB2AutoConfiguration.java @@ -21,11 +21,14 @@ */ package com.influxdb.spring.influx; +import java.util.Collections; + import com.influxdb.client.InfluxDBClient; import com.influxdb.client.InfluxDBClientFactory; import com.influxdb.client.InfluxDBClientOptions; import okhttp3.OkHttpClient; +import okhttp3.Protocol; import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; @@ -64,6 +67,7 @@ public InfluxDBClient influxDBClient() { OkHttpClient.Builder okHttpBuilder; if (builderProvider == null) { okHttpBuilder = new OkHttpClient.Builder() + .protocols(Collections.singletonList(Protocol.HTTP_1_1)) .readTimeout(properties.getReadTimeout()) .writeTimeout(properties.getWriteTimeout()) .connectTimeout(properties.getConnectTimeout()); diff --git a/spring/src/test/java/com/influxdb/spring/influx/InfluxDB2AutoConfigurationTest.java b/spring/src/test/java/com/influxdb/spring/influx/InfluxDB2AutoConfigurationTest.java index bca79e152e1..69d2c276f61 100644 --- a/spring/src/test/java/com/influxdb/spring/influx/InfluxDB2AutoConfigurationTest.java +++ b/spring/src/test/java/com/influxdb/spring/influx/InfluxDB2AutoConfigurationTest.java @@ -21,11 +21,15 @@ */ package com.influxdb.spring.influx; +import java.util.List; import java.util.concurrent.TimeUnit; +import javax.annotation.Nonnull; + import com.influxdb.client.InfluxDBClient; import okhttp3.OkHttpClient; +import okhttp3.Protocol; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.platform.runner.JUnitPlatform; @@ -96,11 +100,27 @@ public void influxDBClientWithReadTimeout() { }); } + @Test + public void protocolVersion() { + this.contextRunner.withPropertyValues("spring.influx2.url=http://localhost:8086/", "spring.influx2.token:token") + .run((context) -> { + List protocols = getOkHttpClient(context).protocols(); + Assertions.assertThat(protocols).hasSize(1); + Assertions.assertThat(protocols).contains(Protocol.HTTP_1_1); + }); + } + private int getReadTimeoutProperty(AssertableApplicationContext context) { + OkHttpClient callFactory = getOkHttpClient(context); + return callFactory.readTimeoutMillis(); + } + + @Nonnull + private OkHttpClient getOkHttpClient(final AssertableApplicationContext context) { InfluxDBClient influxDB = context.getBean(InfluxDBClient.class); Retrofit retrofit = (Retrofit) ReflectionTestUtils.getField(influxDB, "retrofit"); OkHttpClient callFactory = (OkHttpClient) retrofit.callFactory(); - return callFactory.readTimeoutMillis(); + return callFactory; } @Configuration From dac506990e63028d6044267219fc4bb5fe3de784 Mon Sep 17 00:00:00 2001 From: Jakub Bednar Date: Thu, 8 Jul 2021 08:51:49 +0200 Subject: [PATCH 2/2] docs: update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a0f7609488..dd35aab1058 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,7 @@ The `shift()` function renamed to `timeShift()`. ### Bug Fixes 1. [#227](https://github.com/influxdata/influxdb-client-java/pull/227): Connection URL with custom base path 1. [#236](https://github.com/influxdata/influxdb-client-java/pull/236): Rename `shift()` to `timeShift()` [FluxDSL] +1. [#241](https://github.com/influxdata/influxdb-client-java/pull/241): Set default HTTP protocol to HTTP 1.1 ### Dependencies 1. [#227](https://github.com/influxdata/influxdb-client-java/pull/227): Update dependencies: