From 0c6a4ec8a881239378aed512e246f6f949589526 Mon Sep 17 00:00:00 2001 From: Christopher Batey Date: Wed, 15 Aug 2018 16:51:13 +0100 Subject: [PATCH] Preserve http2 option when doing from javadsl to scaladsl The only information passed from javadsl to the scaladsl in Http was the context which for Http connections was always the default one meaning that setting http2 always had no effect. --- .../src/main/scala/akka/http/javadsl/ConnectHttp.scala | 8 ++++++-- .../main/scala/akka/http/scaladsl/ConnectionContext.scala | 3 ++- .../test/scala/akka/http/javadsl/ConnectHttpSpec.scala | 5 +++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/akka-http-core/src/main/scala/akka/http/javadsl/ConnectHttp.scala b/akka-http-core/src/main/scala/akka/http/javadsl/ConnectHttp.scala index 22a5231a257..dc66ba5ef88 100644 --- a/akka-http-core/src/main/scala/akka/http/javadsl/ConnectHttp.scala +++ b/akka-http-core/src/main/scala/akka/http/javadsl/ConnectHttp.scala @@ -7,8 +7,9 @@ package akka.http.javadsl import java.util.Locale import java.util.Optional -import akka.annotation.{ DoNotInherit, InternalApi } +import akka.annotation.{DoNotInherit, InternalApi} import akka.http.javadsl.model.Uri +import akka.http.scaladsl.HttpConnectionContext import akka.http.scaladsl.UseHttp2.Negotiated @DoNotInherit @@ -23,7 +24,7 @@ abstract class ConnectHttp { final def effectiveHttpsConnectionContext(fallbackContext: HttpsConnectionContext): HttpsConnectionContext = connectionContext.orElse(fallbackContext) - final def effectiveConnectionContext(fallbackContext: ConnectionContext): ConnectionContext = + def effectiveConnectionContext(fallbackContext: ConnectionContext): ConnectionContext = if (connectionContext.isPresent) connectionContext.get() else fallbackContext @@ -167,6 +168,9 @@ final class ConnectHttpImpl(val host: String, val port: Int, val http2: UseHttp2 def isHttps: Boolean = false def connectionContext: Optional[HttpsConnectionContext] = Optional.empty() + + override def effectiveConnectionContext(fallbackContext: ConnectionContext): ConnectionContext = + HttpConnectionContext(http2.asScala) } /** INTERNAL API */ diff --git a/akka-http-core/src/main/scala/akka/http/scaladsl/ConnectionContext.scala b/akka-http-core/src/main/scala/akka/http/scaladsl/ConnectionContext.scala index 8e398145b9e..c61e84e2cbf 100644 --- a/akka-http-core/src/main/scala/akka/http/scaladsl/ConnectionContext.scala +++ b/akka-http-core/src/main/scala/akka/http/scaladsl/ConnectionContext.scala @@ -9,7 +9,7 @@ import akka.stream.TLSProtocol._ import com.typesafe.sslconfig.akka.AkkaSSLConfig import scala.collection.JavaConverters._ -import java.util.{ Optional, Collection ⇒ JCollection } +import java.util.{Optional, Collection => JCollection} import akka.http.scaladsl.UseHttp2.Negotiated import javax.net.ssl._ @@ -98,6 +98,7 @@ final class HttpsConnectionContext( sealed class HttpConnectionContext(http2: UseHttp2) extends akka.http.javadsl.HttpConnectionContext(http2) with ConnectionContext { def this() = this(Negotiated) } + final object HttpConnectionContext extends HttpConnectionContext(Negotiated) { /** Java API */ def getInstance() = this diff --git a/akka-http-core/src/test/scala/akka/http/javadsl/ConnectHttpSpec.scala b/akka-http-core/src/test/scala/akka/http/javadsl/ConnectHttpSpec.scala index 9547b8ef875..08dfb44cdef 100644 --- a/akka-http-core/src/test/scala/akka/http/javadsl/ConnectHttpSpec.scala +++ b/akka-http-core/src/test/scala/akka/http/javadsl/ConnectHttpSpec.scala @@ -99,5 +99,10 @@ class ConnectHttpSpec extends WordSpec with Matchers with BeforeAndAfterAll { } ex.getMessage should include("non https scheme!") } + "connect toHost HTTP/2 only" in { + val connect = ConnectHttp.toHost("http://127.0.0.1", 8080, UseHttp2.always) + connect.effectiveConnectionContext(httpsContext).http2 should equal(UseHttp2.always) + } + } }