wip #19441 convert from Option to Optional in javadsl

This commit is contained in:
Martynas Mickevičius 2016-01-22 17:01:22 +02:00 committed by Johan Andrén
parent 6ba20ac673
commit a8abbaf36b
10 changed files with 42 additions and 31 deletions

View file

@ -3,6 +3,7 @@
*/ */
package docs.http.javadsl.server; package docs.http.javadsl.server;
import java.util.Optional;
import akka.http.javadsl.model.HttpRequest; import akka.http.javadsl.model.HttpRequest;
import akka.http.javadsl.model.headers.Host; import akka.http.javadsl.model.headers.Host;
import akka.http.javadsl.server.Handler1; import akka.http.javadsl.server.Handler1;
@ -75,4 +76,4 @@ public class HttpBasicAuthenticatorExample extends JUnitRouteTest {
} }
} }

View file

@ -9,12 +9,12 @@ Akka HTTP supports TLS encryption on the client-side as well as on the :ref:`ser
Akka HTTP 1.0 does not completely validate certificates when using HTTPS. Please do not treat HTTPS connections Akka HTTP 1.0 does not completely validate certificates when using HTTPS. Please do not treat HTTPS connections
made with this version as secure. Requests are vulnerable to a Man-In-The-Middle attack via certificate substitution. made with this version as secure. Requests are vulnerable to a Man-In-The-Middle attack via certificate substitution.
The central vehicle for configuring encryption is the ``HttpsContext``, which can be created using
the static method ``HttpsContext.create`` which is defined like this:
.. includecode:: /../../akka-http-core/src/main/java/akka/http/javadsl/HttpsContext.java The central vehicle for configuring encryption is the ``HttpsConnectionContext``, which can be created using
:include: http-context-creation the static method ``ConnectionContext.https`` which is defined like this:
.. includecode:: /../../akka-http-core/src/main/scala/akka/http/javadsl/ConnectionContext.scala
:include: https-context-creation
In addition to the ``outgoingConnection``, ``newHostConnectionPool`` and ``cachedHostConnectionPool`` methods the In addition to the ``outgoingConnection``, ``newHostConnectionPool`` and ``cachedHostConnectionPool`` methods the
`akka.http.javadsl.Http`_ extension also defines ``outgoingConnectionTls``, ``newHostConnectionPoolTls`` and `akka.http.javadsl.Http`_ extension also defines ``outgoingConnectionTls``, ``newHostConnectionPoolTls`` and

View file

@ -138,11 +138,11 @@ Server-Side HTTPS Support
Akka HTTP supports TLS encryption on the server-side as well as on the :ref:`client-side <clientSideHTTPS-java>`. Akka HTTP supports TLS encryption on the server-side as well as on the :ref:`client-side <clientSideHTTPS-java>`.
The central vehicle for configuring encryption is the ``HttpsContext``, which can be created using The central vehicle for configuring encryption is the ``HttpsConnectionContext``, which can be created using
the static method ``HttpsContext.create`` which is defined like this: the static method ``ConnectionContext.https`` which is defined like this:
.. includecode:: /../../akka-http-core/src/main/java/akka/http/javadsl/HttpsContext.java .. includecode:: /../../akka-http-core/src/main/scala/akka/http/javadsl/ConnectionContext.scala
:include: http-context-creation :include: https-context-creation
On the server-side the ``bind``, and ``bindAndHandleXXX`` methods of the `akka.http.javadsl.Http`_ extension define an On the server-side the ``bind``, and ``bindAndHandleXXX`` methods of the `akka.http.javadsl.Http`_ extension define an
optional ``httpsContext`` parameter, which can receive the HTTPS configuration in the form of an ``HttpsContext`` optional ``httpsContext`` parameter, which can receive the HTTPS configuration in the form of an ``HttpsContext``

View file

@ -10,10 +10,11 @@ Akka HTTP supports TLS encryption on the client-side as well as on the :ref:`ser
Akka HTTP 1.0 does not completely validate certificates when using HTTPS. Please do not treat HTTPS connections Akka HTTP 1.0 does not completely validate certificates when using HTTPS. Please do not treat HTTPS connections
made with this version as secure. Requests are vulnerable to a Man-In-The-Middle attack via certificate substitution. made with this version as secure. Requests are vulnerable to a Man-In-The-Middle attack via certificate substitution.
The central vehicle for configuring encryption is the ``HttpsContext``, which is defined as such: The central vehicle for configuring encryption is the ``HttpsConnectionContext``, which can be created using
the static method ``ConnectionContext.https`` which is defined like this:
.. includecode2:: /../../akka-http-core/src/main/scala/akka/http/scaladsl/Http.scala .. includecode:: /../../akka-http-core/src/main/scala/akka/http/scaladsl/ConnectionContext.scala
:snippet: https-context-impl :include: https-context-creation
In addition to the ``outgoingConnection``, ``newHostConnectionPool`` and ``cachedHostConnectionPool`` methods the In addition to the ``outgoingConnection``, ``newHostConnectionPool`` and ``cachedHostConnectionPool`` methods the
`akka.http.scaladsl.Http`_ extension also defines ``outgoingConnectionTls``, ``newHostConnectionPoolTls`` and `akka.http.scaladsl.Http`_ extension also defines ``outgoingConnectionTls``, ``newHostConnectionPoolTls`` and

View file

@ -140,10 +140,11 @@ Server-Side HTTPS Support
Akka HTTP supports TLS encryption on the server-side as well as on the :ref:`client-side <clientSideHTTPS>`. Akka HTTP supports TLS encryption on the server-side as well as on the :ref:`client-side <clientSideHTTPS>`.
The central vehicle for configuring encryption is the ``HttpsContext``, which is defined as such: The central vehicle for configuring encryption is the ``HttpsConnectionContext``, which can be created using
the static method ``ConnectionContext.https`` which is defined like this:
.. includecode2:: /../../akka-http-core/src/main/scala/akka/http/scaladsl/Http.scala .. includecode:: /../../akka-http-core/src/main/scala/akka/http/scaladsl/ConnectionContext.scala
:snippet: https-context-impl :include: https-context-creation
On the server-side the ``bind``, and ``bindAndHandleXXX`` methods of the `akka.http.scaladsl.Http`_ extension define an On the server-side the ``bind``, and ``bindAndHandleXXX`` methods of the `akka.http.scaladsl.Http`_ extension define an
optional ``httpsContext`` parameter, which can receive the HTTPS configuration in the form of an ``HttpsContext`` optional ``httpsContext`` parameter, which can receive the HTTPS configuration in the form of an ``HttpsContext``

View file

@ -11,6 +11,7 @@ import akka.stream.io.ClientAuth
import scala.compat.java8.OptionConverters import scala.compat.java8.OptionConverters
object ConnectionContext { object ConnectionContext {
//#https-context-creation
/** Used to serve HTTPS traffic. */ /** Used to serve HTTPS traffic. */
def https(sslContext: SSLContext): HttpsConnectionContext = def https(sslContext: SSLContext): HttpsConnectionContext =
scaladsl.ConnectionContext.https(sslContext) scaladsl.ConnectionContext.https(sslContext)
@ -19,6 +20,7 @@ object ConnectionContext {
def https(sslContext: SSLContext, enabledCipherSuites: Optional[JCollection[String]], def https(sslContext: SSLContext, enabledCipherSuites: Optional[JCollection[String]],
enabledProtocols: Optional[JCollection[String]], clientAuth: Optional[ClientAuth], sslParameters: Optional[SSLParameters]) = enabledProtocols: Optional[JCollection[String]], clientAuth: Optional[ClientAuth], sslParameters: Optional[SSLParameters]) =
scaladsl.ConnectionContext.https(sslContext, sslParameters = OptionConverters.toScala(sslParameters)) scaladsl.ConnectionContext.https(sslContext, sslParameters = OptionConverters.toScala(sslParameters))
//#https-context-creation
/** Used to serve HTTP traffic. */ /** Used to serve HTTP traffic. */
def noEncryption(): HttpConnectionContext = def noEncryption(): HttpConnectionContext =
@ -52,4 +54,3 @@ abstract class HttpsConnectionContext extends akka.http.javadsl.ConnectionContex
/** Java API */ /** Java API */
def getSslParameters: Optional[SSLParameters] def getSslParameters: Optional[SSLParameters]
} }

View file

@ -18,6 +18,7 @@ trait ConnectionContext extends akka.http.javadsl.ConnectionContext {
} }
object ConnectionContext { object ConnectionContext {
//#https-context-creation
def https(sslContext: SSLContext, def https(sslContext: SSLContext,
enabledCipherSuites: Option[immutable.Seq[String]] = None, enabledCipherSuites: Option[immutable.Seq[String]] = None,
enabledProtocols: Option[immutable.Seq[String]] = None, enabledProtocols: Option[immutable.Seq[String]] = None,
@ -25,6 +26,7 @@ object ConnectionContext {
sslParameters: Option[SSLParameters] = None) = { sslParameters: Option[SSLParameters] = None) = {
new HttpsConnectionContext(sslContext, enabledCipherSuites, enabledProtocols, clientAuth, sslParameters) new HttpsConnectionContext(sslContext, enabledCipherSuites, enabledProtocols, clientAuth, sslParameters)
} }
//#https-context-creation
def noEncryption() = HttpConnectionContext def noEncryption() = HttpConnectionContext
} }

View file

@ -4,6 +4,8 @@
package akka.http.impl.server package akka.http.impl.server
import java.util.Optional
import akka.http.javadsl.model.HttpHeader import akka.http.javadsl.model.HttpHeader
import akka.http.javadsl.server.RequestVal import akka.http.javadsl.server.RequestVal
import akka.http.javadsl.server.values.Header import akka.http.javadsl.server.values.Header
@ -12,6 +14,7 @@ import akka.http.scaladsl.server._
import akka.http.scaladsl.server.directives.BasicDirectives._ import akka.http.scaladsl.server.directives.BasicDirectives._
import akka.http.scaladsl.server.directives.RouteDirectives._ import akka.http.scaladsl.server.directives.RouteDirectives._
import scala.compat.java8.OptionConverters._
import scala.reflect.ClassTag import scala.reflect.ClassTag
/** /**
@ -38,9 +41,9 @@ private[http] object HeaderImpl {
def directive: Directive1[U] = instanceDirective def directive: Directive1[U] = instanceDirective
} }
def optionalInstance(): RequestVal[Option[U]] = def optionalInstance(): RequestVal[Optional[U]] =
new StandaloneExtractionImpl[Option[U]] { new StandaloneExtractionImpl[Optional[U]] {
def directive: Directive1[Option[U]] = optionalDirective(uClassTag) def directive: Directive1[Optional[U]] = optionalDirective(uClassTag).map(_.asJava)
} }
def value(): RequestVal[String] = def value(): RequestVal[String] =
@ -48,9 +51,9 @@ private[http] object HeaderImpl {
def directive: Directive1[String] = instanceDirective.map(_.value) def directive: Directive1[String] = instanceDirective.map(_.value)
} }
def optionalValue(): RequestVal[Option[String]] = def optionalValue(): RequestVal[Optional[String]] =
new StandaloneExtractionImpl[Option[String]] { new StandaloneExtractionImpl[Optional[String]] {
def directive: Directive1[Option[String]] = optionalDirective(uClassTag).map(_.map(_.value)) def directive: Directive1[Optional[String]] = optionalDirective(uClassTag).map(_.map(_.value).asJava)
} }
}.asInstanceOf[Header[T]] // undeclared covariance }.asInstanceOf[Header[T]] // undeclared covariance
} }

View file

@ -8,7 +8,6 @@ import akka.http.impl.util.JavaMapping
import akka.http.javadsl.server.values.{ PathMatcher, BasicCredentials, OAuth2Credentials } import akka.http.javadsl.server.values.{ PathMatcher, BasicCredentials, OAuth2Credentials }
import akka.http.scaladsl.model.StatusCodes.Redirection import akka.http.scaladsl.model.StatusCodes.Redirection
import akka.http.scaladsl.server.util.TupleOps.Join import akka.http.scaladsl.server.util.TupleOps.Join
import scala.language.implicitConversions import scala.language.implicitConversions
import scala.annotation.tailrec import scala.annotation.tailrec
import scala.collection.immutable import scala.collection.immutable
@ -98,7 +97,7 @@ private[http] object RouteImplementation extends Directives with server.RouteCon
} }
} }
authenticator.authenticate(javaCreds).toScala.map(_.asScala)(sameThreadExecutionContext) authenticator.authenticate(javaCreds).toScala.map(_.asScala)(akka.dispatch.ExecutionContexts.sameThreadExecutionContext)
}).flatMap { user }).flatMap { user
addExtraction(authenticator.asInstanceOf[RequestVal[Any]], user) addExtraction(authenticator.asInstanceOf[RequestVal[Any]], user)
} }

View file

@ -4,6 +4,8 @@
package akka.http.javadsl.server.values package akka.http.javadsl.server.values
import java.util.Optional
import akka.http.impl.server.HeaderImpl import akka.http.impl.server.HeaderImpl
import akka.http.javadsl.model.HttpHeader import akka.http.javadsl.model.HttpHeader
import akka.http.javadsl.server.RequestVal import akka.http.javadsl.server.RequestVal
@ -11,27 +13,28 @@ import akka.http.scaladsl.model
import akka.http.scaladsl.server.Directive1 import akka.http.scaladsl.server.Directive1
import akka.http.scaladsl.server.util.ClassMagnet import akka.http.scaladsl.server.util.ClassMagnet
import scala.compat.java8.OptionConverters._
import scala.reflect.{ ClassTag, classTag } import scala.reflect.{ ClassTag, classTag }
trait Header[T <: HttpHeader] { trait Header[T <: HttpHeader] {
def instance(): RequestVal[T] def instance(): RequestVal[T]
def optionalInstance(): RequestVal[Option[T]] def optionalInstance(): RequestVal[Optional[T]]
def value(): RequestVal[String] def value(): RequestVal[String]
def optionalValue(): RequestVal[Option[String]] def optionalValue(): RequestVal[Optional[String]]
} }
object Headers { object Headers {
import akka.http.scaladsl.server.directives.BasicDirectives._ import akka.http.scaladsl.server.directives.BasicDirectives._
import akka.http.scaladsl.server.directives.HeaderDirectives._ import akka.http.scaladsl.server.directives.HeaderDirectives._
def byName(name: String): Header[HttpHeader] = def byName(name: String): Header[HttpHeader] =
HeaderImpl[HttpHeader](name, _ optionalHeaderInstanceByName(name.toLowerCase()), classTag[HttpHeader]) HeaderImpl[HttpHeader](name, _ optionalHeaderInstanceByName(name.toLowerCase()).map(_.asScala), classTag[HttpHeader])
def byClass[T <: HttpHeader](clazz: Class[T]): Header[T] = def byClass[T <: HttpHeader](clazz: Class[T]): Header[T] =
HeaderImpl[T](clazz.getSimpleName, ct optionalHeaderValueByType(ClassMagnet(ct)), ClassTag(clazz)) HeaderImpl[T](clazz.getSimpleName, ct optionalHeaderValueByType(ClassMagnet(ct)), ClassTag(clazz))
private def optionalHeaderInstanceByName(lowercaseName: String): Directive1[Option[model.HttpHeader]] = private def optionalHeaderInstanceByName(lowercaseName: String): Directive1[Optional[model.HttpHeader]] =
extract(_.request.headers.collectFirst { extract(_.request.headers.collectFirst {
case h @ model.HttpHeader(`lowercaseName`, _) h case h @ model.HttpHeader(`lowercaseName`, _) h
}) }.asJava)
} }