!htc,str #19275 fix various scala/java interop problems in DSL

This commit is contained in:
Konrad Malawski 2016-01-21 16:14:05 +01:00
parent 1cd21d9ce2
commit 67ebaba045
6 changed files with 46 additions and 37 deletions

View file

@ -6,6 +6,7 @@ package docs.http.javadsl;
import akka.actor.AbstractActor; import akka.actor.AbstractActor;
import akka.actor.ActorSystem; import akka.actor.ActorSystem;
import akka.http.javadsl.ConnectHttp;
import akka.http.javadsl.HostConnectionPool; import akka.http.javadsl.HostConnectionPool;
import akka.japi.Pair; import akka.japi.Pair;
@ -20,6 +21,7 @@ import akka.http.javadsl.model.*;
import akka.http.javadsl.Http; import akka.http.javadsl.Http;
import scala.util.Try; import scala.util.Try;
import static akka.http.javadsl.ConnectHttp.toHost;
import static akka.pattern.Patterns.*; import static akka.pattern.Patterns.*;
@SuppressWarnings("unused") @SuppressWarnings("unused")
@ -33,7 +35,7 @@ public class HttpClientExampleDocTest {
final ActorMaterializer materializer = ActorMaterializer.create(system); final ActorMaterializer materializer = ActorMaterializer.create(system);
final Flow<HttpRequest, HttpResponse, Future<OutgoingConnection>> connectionFlow = final Flow<HttpRequest, HttpResponse, Future<OutgoingConnection>> connectionFlow =
Http.get(system).outgoingConnection("akka.io", 80); Http.get(system).outgoingConnection(toHost("akka.io", 80));
final Future<HttpResponse> responseFuture = final Future<HttpResponse> responseFuture =
Source.single(HttpRequest.create("/")) Source.single(HttpRequest.create("/"))
.via(connectionFlow) .via(connectionFlow)
@ -52,7 +54,7 @@ public class HttpClientExampleDocTest {
Pair<HttpRequest, Integer>, Pair<HttpRequest, Integer>,
Pair<Try<HttpResponse>, Integer>, Pair<Try<HttpResponse>, Integer>,
HostConnectionPool> poolClientFlow = HostConnectionPool> poolClientFlow =
Http.get(system).<Integer>cachedHostConnectionPool("akka.io", 80, materializer); Http.get(system).<Integer>cachedHostConnectionPool(toHost("akka.io", 80), materializer);
// construct a pool client flow with context type `Integer` // construct a pool client flow with context type `Integer`

View file

@ -43,7 +43,7 @@ object ConnectHttp {
* Forces an HTTPS connection to the given host, using the default HTTPS context and default port. * Forces an HTTPS connection to the given host, using the default HTTPS context and default port.
*/ */
@throws(classOf[IllegalArgumentException]) @throws(classOf[IllegalArgumentException])
def toHostHttps(uriHost: Uri): ConnectHttp.UsingHttps = { def toHostHttps(uriHost: Uri): ConnectWithHttps = {
val s = uriHost.scheme.toLowerCase(Locale.ROOT) val s = uriHost.scheme.toLowerCase(Locale.ROOT)
require(s == "" || s == "https", "toHostHttps used with non https scheme! Was: " + uriHost) require(s == "" || s == "https", "toHostHttps used with non https scheme! Was: " + uriHost)
val httpsHost = uriHost.scheme("https") // for effective port calculation val httpsHost = uriHost.scheme("https") // for effective port calculation
@ -52,12 +52,12 @@ object ConnectHttp {
/** Forces an HTTPS connection to the given host, using the default HTTPS context and default port. */ /** Forces an HTTPS connection to the given host, using the default HTTPS context and default port. */
@throws(classOf[IllegalArgumentException]) @throws(classOf[IllegalArgumentException])
def toHostHttps(host: String): ConnectHttp.UsingHttps = def toHostHttps(host: String): ConnectWithHttps =
toHostHttps(Uri.create(host)) toHostHttps(Uri.create(host))
/** Forces an HTTPS connection to the given host, using the default HTTPS context and given port. */ /** Forces an HTTPS connection to the given host, using the default HTTPS context and given port. */
@throws(classOf[IllegalArgumentException]) @throws(classOf[IllegalArgumentException])
def toHostHttps(host: String, port: Int): ConnectHttp.UsingHttps = { def toHostHttps(host: String, port: Int): ConnectWithHttps = {
require(port > 0, "port must be > 0") require(port > 0, "port must be > 0")
toHostHttps(Uri.create(host).port(port).host.address) toHostHttps(Uri.create(host).port(port).host.address)
} }
@ -75,14 +75,11 @@ object ConnectHttp {
else throw new IllegalArgumentException("Scheme is not http/https/ws/wss and no port given!") else throw new IllegalArgumentException("Scheme is not http/https/ws/wss and no port given!")
} }
trait UsingHttps extends ConnectHttp { }
def withCustomHttpsContext(context: HttpsConnectionContext): ConnectHttp.UsingCustomHttps
}
trait UsingCustomHttps extends ConnectHttp {
def withDefaultContext: ConnectHttp.UsingHttps
}
abstract class ConnectWithHttps extends ConnectHttp {
def withCustomHttpsContext(context: HttpsConnectionContext): ConnectWithHttps
def withDefaultHttpsContext(): ConnectWithHttps
} }
/** INTERNAL API */ /** INTERNAL API */
@ -93,15 +90,16 @@ final class ConnectHttpImpl(val host: String, val port: Int) extends ConnectHttp
} }
final class ConnectHttpsImpl(val host: String, val port: Int, val context: Optional[HttpsConnectionContext] = Optional.empty()) final class ConnectHttpsImpl(val host: String, val port: Int, val context: Optional[HttpsConnectionContext] = Optional.empty())
extends ConnectHttp with ConnectHttp.UsingHttps with ConnectHttp.UsingCustomHttps { extends ConnectWithHttps {
override def isHttps: Boolean = true override def isHttps: Boolean = true
override def withCustomHttpsContext(context: HttpsConnectionContext): ConnectHttp.UsingCustomHttps = override def withCustomHttpsContext(context: HttpsConnectionContext): ConnectWithHttps =
new ConnectHttpsImpl(host, port, Optional.of(context)) new ConnectHttpsImpl(host, port, Optional.of(context))
override def withDefaultContext: ConnectHttp.UsingHttps = override def withDefaultHttpsContext(): ConnectWithHttps =
new ConnectHttpsImpl(host, port, Optional.empty()) new ConnectHttpsImpl(host, port, Optional.empty())
override def connectionContext: Optional[HttpsConnectionContext] = context override def connectionContext: Optional[HttpsConnectionContext] = context
} }

View file

@ -31,12 +31,12 @@ abstract class ConnectionContext {
def getDefaultPort: Int def getDefaultPort: Int
} }
trait HttpConnectionContext extends akka.http.javadsl.ConnectionContext { abstract class HttpConnectionContext extends akka.http.javadsl.ConnectionContext {
override final def isSecure = false override final def isSecure = false
override final def getDefaultPort = 80 override final def getDefaultPort = 80
} }
trait HttpsConnectionContext extends akka.http.javadsl.ConnectionContext { abstract class HttpsConnectionContext extends akka.http.javadsl.ConnectionContext {
override final def isSecure = true override final def isSecure = true
override final def getDefaultPort = 443 override final def getDefaultPort = 443

View file

@ -35,8 +35,7 @@ final class HttpsConnectionContext(
val enabledProtocols: Option[immutable.Seq[String]] = None, val enabledProtocols: Option[immutable.Seq[String]] = None,
val clientAuth: Option[ClientAuth] = None, val clientAuth: Option[ClientAuth] = None,
val sslParameters: Option[SSLParameters] = None) val sslParameters: Option[SSLParameters] = None)
extends akka.http.javadsl.HttpsConnectionContext extends akka.http.javadsl.HttpsConnectionContext with ConnectionContext {
with ConnectionContext {
def firstSession = NegotiateNewSession(enabledCipherSuites, enabledProtocols, clientAuth, sslParameters) def firstSession = NegotiateNewSession(enabledCipherSuites, enabledProtocols, clientAuth, sslParameters)

View file

@ -10,7 +10,7 @@ import javax.net.ssl.{ SSLParameters, SSLContext }
import akka.http.javadsl.model.headers.Cookie import akka.http.javadsl.model.headers.Cookie
import akka.http.scaladsl.model import akka.http.scaladsl.model
import akka.http.scaladsl.model.headers.BasicHttpCredentials import akka.http.scaladsl.model.headers.BasicHttpCredentials
import com.typesafe.sslconfig.ssl.ClientAuth import akka.stream.io.ClientAuth
import org.scalatest.{ FreeSpec, MustMatchers } import org.scalatest.{ FreeSpec, MustMatchers }
import scala.collection.immutable import scala.collection.immutable

View file

@ -6,11 +6,13 @@ package akka.http.javadsl.client;
import akka.event.LoggingAdapter; import akka.event.LoggingAdapter;
import akka.http.ConnectionPoolSettings; import akka.http.ConnectionPoolSettings;
import akka.http.javadsl.ConnectHttp; import akka.http.javadsl.*;
import akka.http.javadsl.ConnectionContext; import akka.http.javadsl.model.HttpRequest;
import akka.http.javadsl.Http; import akka.http.javadsl.model.HttpResponse;
import akka.http.javadsl.HttpsConnectionContext;
import akka.http.javadsl.testkit.JUnitRouteTest; import akka.http.javadsl.testkit.JUnitRouteTest;
import akka.japi.Function;
import akka.stream.javadsl.Flow;
import scala.concurrent.Future;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
@ -23,6 +25,8 @@ public class HttpAPIsTest extends JUnitRouteTest {
public void compileOnly() throws Exception { public void compileOnly() throws Exception {
final Http http = Http.get(system()); final Http http = Http.get(system());
final ConnectionContext connectionContext = ConnectionContext.https(SSLContext.getDefault());
final HttpConnectionContext httpContext = ConnectionContext.noEncryption();
final HttpsConnectionContext httpsContext = ConnectionContext.https(SSLContext.getDefault()); final HttpsConnectionContext httpsContext = ConnectionContext.https(SSLContext.getDefault());
String host = ""; String host = "";
@ -31,20 +35,26 @@ public class HttpAPIsTest extends JUnitRouteTest {
LoggingAdapter log = null; LoggingAdapter log = null;
http.bind("127.0.0.1", 8080, materializer()); http.bind("127.0.0.1", 8080, materializer());
http.bind("127.0.0.1", 8080, connectionContext, materializer());
http.bind("127.0.0.1", 8080, httpContext, materializer());
http.bind("127.0.0.1", 8080, httpsContext, materializer()); http.bind("127.0.0.1", 8080, httpsContext, materializer());
http.bindAndHandle(null, "127.0.0.1", 8080, materializer()); final Flow<HttpRequest, HttpResponse, ?> handler = null;
http.bindAndHandle(null, "127.0.0.1", 8080, httpsContext, materializer()); http.bindAndHandle(handler, "127.0.0.1", 8080, materializer());
http.bindAndHandle(handler, "127.0.0.1", 8080, httpsContext, materializer());
http.bindAndHandleAsync(null, "127.0.0.1", 8080, materializer()); final Function<HttpRequest, Future<HttpResponse>> handler1 = null;
http.bindAndHandleAsync(null, "127.0.0.1", 8080, httpsContext, materializer()); http.bindAndHandleAsync(handler1, "127.0.0.1", 8080, materializer());
http.bindAndHandleAsync(handler1, "127.0.0.1", 8080, httpsContext, materializer());
http.bindAndHandleSync(null, "127.0.0.1", 8080, materializer()); final Function<HttpRequest, HttpResponse> handler2 = null;
http.bindAndHandleSync(null, "127.0.0.1", 8080, httpsContext, materializer()); http.bindAndHandleSync(handler2, "127.0.0.1", 8080, materializer());
http.bindAndHandleSync(handler2, "127.0.0.1", 8080, httpsContext, materializer());
http.singleRequest(null, materializer()); final HttpRequest handler3 = null;
http.singleRequest(null, httpsContext, materializer()); http.singleRequest(handler3, materializer());
http.singleRequest(null, httpsContext, conSettings, log, materializer()); http.singleRequest(handler3, httpsContext, materializer());
http.singleRequest(handler3, httpsContext, conSettings, log, materializer());
http.outgoingConnection("akka.io"); http.outgoingConnection("akka.io");
http.outgoingConnection("akka.io:8080"); http.outgoingConnection("akka.io:8080");
@ -57,8 +67,8 @@ public class HttpAPIsTest extends JUnitRouteTest {
http.outgoingConnection(toHostHttps("akka.io")); // default ssl context (ssl-config) http.outgoingConnection(toHostHttps("akka.io")); // default ssl context (ssl-config)
http.outgoingConnection(toHostHttps("ssh://akka.io")); // throws, we explicitly require https or "" http.outgoingConnection(toHostHttps("ssh://akka.io")); // throws, we explicitly require https or ""
http.outgoingConnection(toHostHttps("akka.io", 8081).withCustomHttpsContext(httpsContext)); http.outgoingConnection(toHostHttps("akka.io", 8081).withCustomHttpsContext(httpsContext));
http.outgoingConnection(toHostHttps("akka.io", 8081).withCustomHttpsContext(httpsContext).withDefaultContext()); http.outgoingConnection(toHostHttps("akka.io", 8081).withCustomHttpsContext(httpsContext).withDefaultHttpsContext());
http.outgoingConnection(toHostHttps("akka.io", 8081).withCustomHttpsContext(httpsContext).withDefaultContext()); http.outgoingConnection(toHostHttps("akka.io", 8081).withCustomHttpsContext(httpsContext).withDefaultHttpsContext());
// in future we can add modify(context -> Context) to "keep ssl-config defaults, but tweak them in code) // in future we can add modify(context -> Context) to "keep ssl-config defaults, but tweak them in code)
@ -85,7 +95,7 @@ public class HttpAPIsTest extends JUnitRouteTest {
http.superPool(conSettings, log, materializer()); http.superPool(conSettings, log, materializer());
http.superPool(conSettings, httpsContext, log, materializer()); http.superPool(conSettings, httpsContext, log, materializer());
ConnectHttp.UsingHttps connect = toHostHttps("akka.io", 8081).withCustomHttpsContext(httpsContext).withDefaultContext(); final ConnectWithHttps connect = toHostHttps("akka.io", 8081).withCustomHttpsContext(httpsContext).withDefaultHttpsContext();
connect.connectionContext().orElse(http.defaultClientHttpsContext()); // usage by us internally connect.effectiveConnectionContext(http.defaultClientHttpsContext()); // usage by us internally
} }
} }