From 1f70229847ad2076cdbc93a766d1a76cb924f78a Mon Sep 17 00:00:00 2001 From: "Taro L. Saito" Date: Mon, 8 Apr 2024 10:09:51 -0700 Subject: [PATCH] http (feature): Fallback to Java8 compatible URLConnectionChannel if Java's HttpClient is unavailable (#3478) --- .../airframe/http/client/JavaHttpClientBackend.scala | 8 +++++++- .../wvlet/airframe/http/client/URLConnectionChannel.scala | 5 +++++ .../airframe/http/client/URLConnectionClientBackend.scala | 1 + 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/airframe-http/.jvm/src/main/scala/wvlet/airframe/http/client/JavaHttpClientBackend.scala b/airframe-http/.jvm/src/main/scala/wvlet/airframe/http/client/JavaHttpClientBackend.scala index 72f356df66..c4bac5f2e2 100644 --- a/airframe-http/.jvm/src/main/scala/wvlet/airframe/http/client/JavaHttpClientBackend.scala +++ b/airframe-http/.jvm/src/main/scala/wvlet/airframe/http/client/JavaHttpClientBackend.scala @@ -15,7 +15,13 @@ package wvlet.airframe.http.client import wvlet.airframe.http.{ServerAddress} object JavaHttpClientBackend extends HttpClientBackend { + private val isJava8: Boolean = sys.props.get("java.version").exists(_.startsWith("1.8")) + override def newHttpChannel(serverAddress: ServerAddress, config: HttpClientConfig): HttpChannel = { - new JavaHttpClientChannel(serverAddress, config) + // For JDK8, use URLConnectionChannel + if (isJava8) + new URLConnectionChannel(serverAddress, config) + else + new JavaHttpClientChannel(serverAddress, config) } } diff --git a/airframe-http/.jvm/src/main/scala/wvlet/airframe/http/client/URLConnectionChannel.scala b/airframe-http/.jvm/src/main/scala/wvlet/airframe/http/client/URLConnectionChannel.scala index 05a79cc3ff..c36aeec7af 100644 --- a/airframe-http/.jvm/src/main/scala/wvlet/airframe/http/client/URLConnectionChannel.scala +++ b/airframe-http/.jvm/src/main/scala/wvlet/airframe/http/client/URLConnectionChannel.scala @@ -24,6 +24,11 @@ import java.util.zip.{GZIPInputStream, InflaterInputStream} import scala.concurrent.duration.Duration import scala.jdk.CollectionConverters.* +/** + * Java8-compatible HTTP channel implementation based on {{URL.openConnection}} + * @param serverAddress + * @param config + */ class URLConnectionChannel(serverAddress: ServerAddress, config: HttpClientConfig) extends HttpChannel { override def send(request: Request, channelConfig: HttpChannelConfig): Response = { val url = s"${serverAddress.uri}${if (request.uri.startsWith("/")) request.uri diff --git a/airframe-http/.jvm/src/main/scala/wvlet/airframe/http/client/URLConnectionClientBackend.scala b/airframe-http/.jvm/src/main/scala/wvlet/airframe/http/client/URLConnectionClientBackend.scala index dded73e9e6..0472017dcf 100644 --- a/airframe-http/.jvm/src/main/scala/wvlet/airframe/http/client/URLConnectionClientBackend.scala +++ b/airframe-http/.jvm/src/main/scala/wvlet/airframe/http/client/URLConnectionClientBackend.scala @@ -16,6 +16,7 @@ import wvlet.airframe.http.{ServerAddress} /** */ +@deprecated("This backend will be used automatically for Java8. No need to explicitly set this backend", "24.4.0") object URLConnectionClientBackend extends HttpClientBackend { override def newHttpChannel(serverAddress: ServerAddress, config: HttpClientConfig): HttpChannel = { new URLConnectionChannel(serverAddress, config)