!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.ActorSystem;
import akka.http.javadsl.ConnectHttp;
import akka.http.javadsl.HostConnectionPool;
import akka.japi.Pair;
@ -20,6 +21,7 @@ import akka.http.javadsl.model.*;
import akka.http.javadsl.Http;
import scala.util.Try;
import static akka.http.javadsl.ConnectHttp.toHost;
import static akka.pattern.Patterns.*;
@SuppressWarnings("unused")
@ -33,7 +35,7 @@ public class HttpClientExampleDocTest {
final ActorMaterializer materializer = ActorMaterializer.create(system);
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 =
Source.single(HttpRequest.create("/"))
.via(connectionFlow)
@ -52,7 +54,7 @@ public class HttpClientExampleDocTest {
Pair<HttpRequest, Integer>,
Pair<Try<HttpResponse>, Integer>,
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`

View file

@ -43,7 +43,7 @@ object ConnectHttp {
* Forces an HTTPS connection to the given host, using the default HTTPS context and default port.
*/
@throws(classOf[IllegalArgumentException])
def toHostHttps(uriHost: Uri): ConnectHttp.UsingHttps = {
def toHostHttps(uriHost: Uri): ConnectWithHttps = {
val s = uriHost.scheme.toLowerCase(Locale.ROOT)
require(s == "" || s == "https", "toHostHttps used with non https scheme! Was: " + uriHost)
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. */
@throws(classOf[IllegalArgumentException])
def toHostHttps(host: String): ConnectHttp.UsingHttps =
def toHostHttps(host: String): ConnectWithHttps =
toHostHttps(Uri.create(host))
/** Forces an HTTPS connection to the given host, using the default HTTPS context and given port. */
@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")
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!")
}
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 */
@ -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())
extends ConnectHttp with ConnectHttp.UsingHttps with ConnectHttp.UsingCustomHttps {
extends ConnectWithHttps {
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))
override def withDefaultContext: ConnectHttp.UsingHttps =
override def withDefaultHttpsContext(): ConnectWithHttps =
new ConnectHttpsImpl(host, port, Optional.empty())
override def connectionContext: Optional[HttpsConnectionContext] = context
}

View file

@ -31,12 +31,12 @@ abstract class ConnectionContext {
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 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 getDefaultPort = 443

View file

@ -35,8 +35,7 @@ final class HttpsConnectionContext(
val enabledProtocols: Option[immutable.Seq[String]] = None,
val clientAuth: Option[ClientAuth] = None,
val sslParameters: Option[SSLParameters] = None)
extends akka.http.javadsl.HttpsConnectionContext
with ConnectionContext {
extends akka.http.javadsl.HttpsConnectionContext with ConnectionContext {
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.scaladsl.model
import akka.http.scaladsl.model.headers.BasicHttpCredentials
import com.typesafe.sslconfig.ssl.ClientAuth
import akka.stream.io.ClientAuth
import org.scalatest.{ FreeSpec, MustMatchers }
import scala.collection.immutable

View file

@ -6,11 +6,13 @@ package akka.http.javadsl.client;
import akka.event.LoggingAdapter;
import akka.http.ConnectionPoolSettings;
import akka.http.javadsl.ConnectHttp;
import akka.http.javadsl.ConnectionContext;
import akka.http.javadsl.Http;
import akka.http.javadsl.HttpsConnectionContext;
import akka.http.javadsl.*;
import akka.http.javadsl.model.HttpRequest;
import akka.http.javadsl.model.HttpResponse;
import akka.http.javadsl.testkit.JUnitRouteTest;
import akka.japi.Function;
import akka.stream.javadsl.Flow;
import scala.concurrent.Future;
import javax.net.ssl.SSLContext;
@ -23,6 +25,8 @@ public class HttpAPIsTest extends JUnitRouteTest {
public void compileOnly() throws Exception {
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());
String host = "";
@ -31,20 +35,26 @@ public class HttpAPIsTest extends JUnitRouteTest {
LoggingAdapter log = null;
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.bindAndHandle(null, "127.0.0.1", 8080, materializer());
http.bindAndHandle(null, "127.0.0.1", 8080, httpsContext, materializer());
final Flow<HttpRequest, HttpResponse, ?> handler = null;
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());
http.bindAndHandleAsync(null, "127.0.0.1", 8080, httpsContext, materializer());
final Function<HttpRequest, Future<HttpResponse>> handler1 = null;
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());
http.bindAndHandleSync(null, "127.0.0.1", 8080, httpsContext, materializer());
final Function<HttpRequest, HttpResponse> handler2 = null;
http.bindAndHandleSync(handler2, "127.0.0.1", 8080, materializer());
http.bindAndHandleSync(handler2, "127.0.0.1", 8080, httpsContext, materializer());
http.singleRequest(null, materializer());
http.singleRequest(null, httpsContext, materializer());
http.singleRequest(null, httpsContext, conSettings, log, materializer());
final HttpRequest handler3 = null;
http.singleRequest(handler3, materializer());
http.singleRequest(handler3, httpsContext, materializer());
http.singleRequest(handler3, httpsContext, conSettings, log, materializer());
http.outgoingConnection("akka.io");
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("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).withDefaultContext());
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).withDefaultHttpsContext());
// 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, httpsContext, log, materializer());
ConnectHttp.UsingHttps connect = toHostHttps("akka.io", 8081).withCustomHttpsContext(httpsContext).withDefaultContext();
connect.connectionContext().orElse(http.defaultClientHttpsContext()); // usage by us internally
final ConnectWithHttps connect = toHostHttps("akka.io", 8081).withCustomHttpsContext(httpsContext).withDefaultHttpsContext();
connect.effectiveConnectionContext(http.defaultClientHttpsContext()); // usage by us internally
}
}