diff --git a/akka-docs/rst/java/code/docs/http/javadsl/server/directives/BasicDirectivesExamplesTest.java b/akka-docs/rst/java/code/docs/http/javadsl/server/directives/BasicDirectivesExamplesTest.java index f667a2d14a..0b93560be3 100644 --- a/akka-docs/rst/java/code/docs/http/javadsl/server/directives/BasicDirectivesExamplesTest.java +++ b/akka-docs/rst/java/code/docs/http/javadsl/server/directives/BasicDirectivesExamplesTest.java @@ -877,4 +877,17 @@ public class BasicDirectivesExamplesTest extends JUnitRouteTest { //#toStrictEntity } + @Test + public void testExtractActorSystem() { + //#extractActorSystem + final Route route = extractActorSystem(actorSystem -> + complete("Actor System extracted, hash=" + actorSystem.hashCode()) + ); + + // tests: + testRoute(route).run(HttpRequest.GET("/")) + .assertEntity("Actor System extracted, hash=" + system().hashCode()); + //#extractActorSystem + } + } diff --git a/akka-docs/rst/java/http/routing-dsl/directives/basic-directives/extractActorSystem.rst b/akka-docs/rst/java/http/routing-dsl/directives/basic-directives/extractActorSystem.rst new file mode 100644 index 0000000000..2ef2f70af5 --- /dev/null +++ b/akka-docs/rst/java/http/routing-dsl/directives/basic-directives/extractActorSystem.rst @@ -0,0 +1,19 @@ +.. _-extractActorSystem-java-: + +extractActorSystem +================== + +Description +----------- + +Extracts the ``ActorSystem`` from the ``RequestContext``, which can be useful when the external API +in your route needs one. + +.. warning:: + + This is only supported when the available Materializer is an ActorMaterializer. + +Example +------- + +.. includecode:: ../../../../code/docs/http/javadsl/server/directives/BasicDirectivesExamplesTest.java#extractActorSystem diff --git a/akka-docs/rst/java/http/routing-dsl/directives/basic-directives/index.rst b/akka-docs/rst/java/http/routing-dsl/directives/basic-directives/index.rst index b92366b453..5b507d1b8b 100644 --- a/akka-docs/rst/java/http/routing-dsl/directives/basic-directives/index.rst +++ b/akka-docs/rst/java/http/routing-dsl/directives/basic-directives/index.rst @@ -17,6 +17,7 @@ on two axes: a) provide a constant value or extract a value from the ``RequestCo a single value or a tuple of values. * :ref:`-extract-java-` + * :ref:`-extractActorSystem-java-` * :ref:`-extractDataBytes-java-` * :ref:`-extractExecutionContext-java-` * :ref:`-extractMaterializer-java-` @@ -95,6 +96,7 @@ Alphabetically cancelRejection cancelRejections extract + extractActorSystem extractDataBytes extractExecutionContext extractMaterializer diff --git a/akka-docs/rst/scala/code/docs/http/scaladsl/server/directives/BasicDirectivesExamplesSpec.scala b/akka-docs/rst/scala/code/docs/http/scaladsl/server/directives/BasicDirectivesExamplesSpec.scala index 8c502fda4f..c3ec9b4576 100644 --- a/akka-docs/rst/scala/code/docs/http/scaladsl/server/directives/BasicDirectivesExamplesSpec.scala +++ b/akka-docs/rst/scala/code/docs/http/scaladsl/server/directives/BasicDirectivesExamplesSpec.scala @@ -862,4 +862,17 @@ class BasicDirectivesExamplesSpec extends RoutingSpec { //# } + "extractActorSystem-example" in { + //#extractActorSystem-example + val route = extractActorSystem { actorSystem => + complete(s"Actor System extracted, hash=${actorSystem.hashCode()}") + } + + // tests: + Get("/") ~> route ~> check { + responseAs[String] shouldEqual s"Actor System extracted, hash=${system.hashCode()}" + } + //# + } + } diff --git a/akka-docs/rst/scala/http/routing-dsl/directives/basic-directives/extractActorSystem.rst b/akka-docs/rst/scala/http/routing-dsl/directives/basic-directives/extractActorSystem.rst new file mode 100644 index 0000000000..e3e2340257 --- /dev/null +++ b/akka-docs/rst/scala/http/routing-dsl/directives/basic-directives/extractActorSystem.rst @@ -0,0 +1,26 @@ +.. _-extractActorSystem-: + +extractActorSystem +================== + +Signature +--------- + +.. includecode2:: /../../akka-http/src/main/scala/akka/http/scaladsl/server/directives/BasicDirectives.scala + :snippet: extractActorSystem + +Description +----------- + +Extracts the ``ActorSystem`` from the ``RequestContext``, which can be useful when the external API +in your route needs one. + +.. warning:: + + This is only supported when the available Materializer is an ActorMaterializer. + +Example +------- + +.. includecode2:: ../../../../code/docs/http/scaladsl/server/directives/BasicDirectivesExamplesSpec.scala + :snippet: extractActorSystem-example diff --git a/akka-docs/rst/scala/http/routing-dsl/directives/basic-directives/index.rst b/akka-docs/rst/scala/http/routing-dsl/directives/basic-directives/index.rst index 012bc536ca..4395e8da9f 100644 --- a/akka-docs/rst/scala/http/routing-dsl/directives/basic-directives/index.rst +++ b/akka-docs/rst/scala/http/routing-dsl/directives/basic-directives/index.rst @@ -17,6 +17,7 @@ on two axes: a) provide a constant value or extract a value from the ``RequestCo a single value or a tuple of values. * :ref:`-extract-` + * :ref:`-extractActorSystem-` * :ref:`-extractDataBytes-` * :ref:`-extractExecutionContext-` * :ref:`-extractMaterializer-` @@ -97,8 +98,9 @@ Alphabetically cancelRejection cancelRejections extract - extractExecutionContext + extractActorSystem extractDataBytes + extractExecutionContext extractMaterializer extractStrictEntity extractLog diff --git a/akka-http/src/main/scala/akka/http/javadsl/server/directives/BasicDirectives.scala b/akka-http/src/main/scala/akka/http/javadsl/server/directives/BasicDirectives.scala index f87a9b55f3..8b61671039 100644 --- a/akka-http/src/main/scala/akka/http/javadsl/server/directives/BasicDirectives.scala +++ b/akka-http/src/main/scala/akka/http/javadsl/server/directives/BasicDirectives.scala @@ -6,6 +6,7 @@ package akka.http.javadsl.server.directives import java.util.function.{ Function ⇒ JFunction } +import akka.actor.ActorSystem import akka.http.impl.util.JavaMapping import akka.http.javadsl.settings.ParserSettings import akka.http.javadsl.settings.RoutingSettings @@ -200,6 +201,13 @@ abstract class BasicDirectives { def extractMaterializer(inner: JFunction[Materializer, Route]): Route = RouteAdapter( D.extractMaterializer { m ⇒ inner.apply(m).delegate }) + /** + * Extracts the [[akka.actor.ActorSystem]] if the available Materializer is an [[akka.stream.ActorMaterializer]]. + * Otherwise throws an exception as it won't be able to extract the system from arbitrary materializers. + */ + def extractActorSystem(inner: JFunction[ActorSystem, Route]): Route = RouteAdapter( + D.extractActorSystem { system ⇒ inner.apply(system).delegate }) + /** * Extracts the [[ExecutionContextExecutor]] from the [[RequestContext]]. */ diff --git a/akka-http/src/main/scala/akka/http/scaladsl/server/directives/BasicDirectives.scala b/akka-http/src/main/scala/akka/http/scaladsl/server/directives/BasicDirectives.scala index 0b0fd6a99c..bb077365d3 100644 --- a/akka-http/src/main/scala/akka/http/scaladsl/server/directives/BasicDirectives.scala +++ b/akka-http/src/main/scala/akka/http/scaladsl/server/directives/BasicDirectives.scala @@ -5,6 +5,7 @@ package akka.http.scaladsl.server package directives +import akka.actor.ActorSystem import akka.stream.scaladsl.Source import akka.util.ByteString @@ -13,7 +14,7 @@ import scala.concurrent.{ Future, ExecutionContextExecutor } import scala.collection.immutable import akka.event.LoggingAdapter import akka.stream.impl.ConstantFun.scalaIdentityFunction -import akka.stream.Materializer +import akka.stream.{ ActorMaterializerHelper, Materializer } import akka.http.scaladsl.settings.{ RoutingSettings, ParserSettings } import akka.http.scaladsl.server.util.Tuple import akka.http.scaladsl.util.FastFuture @@ -237,6 +238,16 @@ trait BasicDirectives { */ def extractMaterializer: Directive1[Materializer] = BasicDirectives._extractMaterializer + /** + * Extracts the [[akka.actor.ActorSystem]] if the available Materializer is an [[akka.stream.ActorMaterializer]]. + * Otherwise throws an exception as it won't be able to extract the system from arbitrary materializers. + * + * @group basic + */ + def extractActorSystem: Directive1[ActorSystem] = extract { ctx ⇒ + ActorMaterializerHelper.downcast(ctx.materializer).system + } + /** * Runs its inner route with the given alternative [[akka.event.LoggingAdapter]]. *