From efb18c95b169a37531bd5590e314cee5325eb81e Mon Sep 17 00:00:00 2001 From: Hawstein Date: Thu, 9 Jun 2016 21:34:25 +0800 Subject: [PATCH] example snippet for akka http java dsl: FileAndResourceDirectives (#20758) --- ...FileAndResourceDirectivesExamplesTest.java | 124 ++++++++++++++++++ .../getFromBrowseableDirectories.rst | 3 +- .../getFromBrowseableDirectory.rst | 3 +- .../getFromDirectory.rst | 3 +- .../getFromFile.rst | 3 +- .../getFromResource.rst | 3 +- .../getFromResourceDirectory.rst | 3 +- .../listDirectoryContents.rst | 3 +- ...ileAndResourceDirectivesExamplesSpec.scala | 5 +- .../scaladsl/server/RequestContextImpl.scala | 8 +- .../akka/http/scaladsl/server/Route.scala | 8 +- .../MultipartUnmarshallers.scala | 4 +- 12 files changed, 151 insertions(+), 19 deletions(-) create mode 100644 akka-docs/rst/java/code/docs/http/javadsl/server/directives/FileAndResourceDirectivesExamplesTest.java diff --git a/akka-docs/rst/java/code/docs/http/javadsl/server/directives/FileAndResourceDirectivesExamplesTest.java b/akka-docs/rst/java/code/docs/http/javadsl/server/directives/FileAndResourceDirectivesExamplesTest.java new file mode 100644 index 0000000000..b9b0da0ebc --- /dev/null +++ b/akka-docs/rst/java/code/docs/http/javadsl/server/directives/FileAndResourceDirectivesExamplesTest.java @@ -0,0 +1,124 @@ +/* + * Copyright (C) 2016-2016 Lightbend Inc. + */ +package docs.http.javadsl.server.directives; + +import akka.http.javadsl.model.HttpRequest; +import akka.http.javadsl.model.StatusCodes; +import akka.http.javadsl.server.PathMatchers; +import akka.http.javadsl.server.Route; +import akka.http.javadsl.server.directives.DirectoryRenderer; +import akka.http.javadsl.testkit.JUnitRouteTest; +import org.junit.Ignore; +import org.junit.Test; +import scala.NotImplementedError; + +import static akka.http.javadsl.server.PathMatchers.segment; + +public class FileAndResourceDirectivesExamplesTest extends JUnitRouteTest { + + @Ignore("Compile only test") + @Test + public void testGetFromFile() { + //#getFromFile + final Route route = path(PathMatchers.segment("logs").slash(segment()), name -> + getFromFile(name + ".log") + ); + + // tests: + testRoute(route).run(HttpRequest.GET("/logs/example")) + .assertEntity("example file contents"); + //#getFromFile + } + + @Ignore("Compile only test") + @Test + public void testGetFromResource() { + //#getFromResource + final Route route = path(PathMatchers.segment("logs").slash(segment()), name -> + getFromResource(name + ".log") + ); + + // tests: + testRoute(route).run(HttpRequest.GET("/logs/example")) + .assertEntity("example file contents"); + //#getFromResource + } + + @Ignore("Compile only test") + @Test + public void testListDirectoryContents() { + //#listDirectoryContents + final Route route = route( + path("tmp", () -> listDirectoryContents("/tmp")), + path("custom", () -> { + // implement your custom renderer here + final DirectoryRenderer renderer = renderVanityFooter -> { + throw new NotImplementedError(); + }; + return listDirectoryContents(renderer, "/tmp"); + }) + ); + + // tests: + testRoute(route).run(HttpRequest.GET("/logs/example")) + .assertEntity("example file contents"); + //#listDirectoryContents + } + + @Ignore("Compile only test") + @Test + public void testGetFromBrowseableDirectory() { + //#getFromBrowseableDirectory + final Route route = path("tmp", () -> + getFromBrowseableDirectory("/tmp") + ); + + // tests: + testRoute(route).run(HttpRequest.GET("/tmp")) + .assertStatusCode(StatusCodes.OK); + //#getFromBrowseableDirectory + } + + @Ignore("Compile only test") + @Test + public void testGetFromBrowseableDirectories() { + //#getFromBrowseableDirectories + final Route route = path("tmp", () -> + getFromBrowseableDirectories("/main", "/backups") + ); + + // tests: + testRoute(route).run(HttpRequest.GET("/tmp")) + .assertStatusCode(StatusCodes.OK); + //#getFromBrowseableDirectories + } + + @Ignore("Compile only test") + @Test + public void testGetFromDirectory() { + //#getFromDirectory + final Route route = pathPrefix("tmp", () -> + getFromDirectory("/tmp") + ); + + // tests: + testRoute(route).run(HttpRequest.GET("/tmp/example")) + .assertEntity("example file contents"); + //#getFromDirectory + } + + @Ignore("Compile only test") + @Test + public void testGetFromResourceDirectory() { + //#getFromResourceDirectory + final Route route = pathPrefix("examples", () -> + getFromResourceDirectory("/examples") + ); + + // tests: + testRoute(route).run(HttpRequest.GET("/examples/example-1")) + .assertEntity("example file contents"); + //#getFromResourceDirectory + } +} diff --git a/akka-docs/rst/java/http/routing-dsl/directives/file-and-resource-directives/getFromBrowseableDirectories.rst b/akka-docs/rst/java/http/routing-dsl/directives/file-and-resource-directives/getFromBrowseableDirectories.rst index 502e30a32d..0aed8331c3 100644 --- a/akka-docs/rst/java/http/routing-dsl/directives/file-and-resource-directives/getFromBrowseableDirectories.rst +++ b/akka-docs/rst/java/http/routing-dsl/directives/file-and-resource-directives/getFromBrowseableDirectories.rst @@ -19,4 +19,5 @@ For more details refer to :ref:`-getFromBrowseableDirectory-java-`. Example ------- -TODO: Example snippets for JavaDSL are subject to community contributions! Help us complete the docs, read more about it here: `write example snippets for Akka HTTP Java DSL #20466 `_. + +.. includecode:: ../../../../code/docs/http/javadsl/server/directives/FileAndResourceDirectivesExamplesTest.java#getFromBrowseableDirectories diff --git a/akka-docs/rst/java/http/routing-dsl/directives/file-and-resource-directives/getFromBrowseableDirectory.rst b/akka-docs/rst/java/http/routing-dsl/directives/file-and-resource-directives/getFromBrowseableDirectory.rst index 0523adb48e..72c4ae7d97 100644 --- a/akka-docs/rst/java/http/routing-dsl/directives/file-and-resource-directives/getFromBrowseableDirectory.rst +++ b/akka-docs/rst/java/http/routing-dsl/directives/file-and-resource-directives/getFromBrowseableDirectory.rst @@ -19,7 +19,8 @@ For more details refer to :ref:`-getFromBrowseableDirectory-java-`. Example ------- -TODO: Example snippets for JavaDSL are subject to community contributions! Help us complete the docs, read more about it here: `write example snippets for Akka HTTP Java DSL #20466 `_. + +.. includecode:: ../../../../code/docs/http/javadsl/server/directives/FileAndResourceDirectivesExamplesTest.java#getFromBrowseableDirectory Default file listing page example diff --git a/akka-docs/rst/java/http/routing-dsl/directives/file-and-resource-directives/getFromDirectory.rst b/akka-docs/rst/java/http/routing-dsl/directives/file-and-resource-directives/getFromDirectory.rst index 1459b17392..7fe40d9675 100644 --- a/akka-docs/rst/java/http/routing-dsl/directives/file-and-resource-directives/getFromDirectory.rst +++ b/akka-docs/rst/java/http/routing-dsl/directives/file-and-resource-directives/getFromDirectory.rst @@ -27,4 +27,5 @@ Note that it's not required to wrap this directive with ``get`` as this directiv Example ------- -TODO: Example snippets for JavaDSL are subject to community contributions! Help us complete the docs, read more about it here: `write example snippets for Akka HTTP Java DSL #20466 `_. + +.. includecode:: ../../../../code/docs/http/javadsl/server/directives/FileAndResourceDirectivesExamplesTest.java#getFromDirectory diff --git a/akka-docs/rst/java/http/routing-dsl/directives/file-and-resource-directives/getFromFile.rst b/akka-docs/rst/java/http/routing-dsl/directives/file-and-resource-directives/getFromFile.rst index 81d26733be..5042cc5749 100644 --- a/akka-docs/rst/java/http/routing-dsl/directives/file-and-resource-directives/getFromFile.rst +++ b/akka-docs/rst/java/http/routing-dsl/directives/file-and-resource-directives/getFromFile.rst @@ -27,4 +27,5 @@ Note that it's not required to wrap this directive with ``get`` as this directiv Example ------- -TODO: Example snippets for JavaDSL are subject to community contributions! Help us complete the docs, read more about it here: `write example snippets for Akka HTTP Java DSL #20466 `_. + +.. includecode:: ../../../../code/docs/http/javadsl/server/directives/FileAndResourceDirectivesExamplesTest.java#getFromFile diff --git a/akka-docs/rst/java/http/routing-dsl/directives/file-and-resource-directives/getFromResource.rst b/akka-docs/rst/java/http/routing-dsl/directives/file-and-resource-directives/getFromResource.rst index 17754ec360..d7032776df 100644 --- a/akka-docs/rst/java/http/routing-dsl/directives/file-and-resource-directives/getFromResource.rst +++ b/akka-docs/rst/java/http/routing-dsl/directives/file-and-resource-directives/getFromResource.rst @@ -15,4 +15,5 @@ Note that it's not required to wrap this directive with ``get`` as this directiv Example ------- -TODO: Example snippets for JavaDSL are subject to community contributions! Help us complete the docs, read more about it here: `write example snippets for Akka HTTP Java DSL #20466 `_. + +.. includecode:: ../../../../code/docs/http/javadsl/server/directives/FileAndResourceDirectivesExamplesTest.java#getFromResource diff --git a/akka-docs/rst/java/http/routing-dsl/directives/file-and-resource-directives/getFromResourceDirectory.rst b/akka-docs/rst/java/http/routing-dsl/directives/file-and-resource-directives/getFromResourceDirectory.rst index 32d8369cae..1e56be9cff 100644 --- a/akka-docs/rst/java/http/routing-dsl/directives/file-and-resource-directives/getFromResourceDirectory.rst +++ b/akka-docs/rst/java/http/routing-dsl/directives/file-and-resource-directives/getFromResourceDirectory.rst @@ -15,4 +15,5 @@ Note that it's not required to wrap this directive with ``get`` as this directiv Example ------- -TODO: Example snippets for JavaDSL are subject to community contributions! Help us complete the docs, read more about it here: `write example snippets for Akka HTTP Java DSL #20466 `_. + +.. includecode:: ../../../../code/docs/http/javadsl/server/directives/FileAndResourceDirectivesExamplesTest.java#getFromResourceDirectory diff --git a/akka-docs/rst/java/http/routing-dsl/directives/file-and-resource-directives/listDirectoryContents.rst b/akka-docs/rst/java/http/routing-dsl/directives/file-and-resource-directives/listDirectoryContents.rst index b0b0de9455..d8e58f51e3 100644 --- a/akka-docs/rst/java/http/routing-dsl/directives/file-and-resource-directives/listDirectoryContents.rst +++ b/akka-docs/rst/java/http/routing-dsl/directives/file-and-resource-directives/listDirectoryContents.rst @@ -20,4 +20,5 @@ Note that it's not required to wrap this directive with ``get`` as this directiv Example ------- -TODO: Example snippets for JavaDSL are subject to community contributions! Help us complete the docs, read more about it here: `write example snippets for Akka HTTP Java DSL #20466 `_. + +.. includecode:: ../../../../code/docs/http/javadsl/server/directives/FileAndResourceDirectivesExamplesTest.java#listDirectoryContents diff --git a/akka-docs/rst/scala/code/docs/http/scaladsl/server/directives/FileAndResourceDirectivesExamplesSpec.scala b/akka-docs/rst/scala/code/docs/http/scaladsl/server/directives/FileAndResourceDirectivesExamplesSpec.scala index f22a8e2bd4..53bee2bb06 100644 --- a/akka-docs/rst/scala/code/docs/http/scaladsl/server/directives/FileAndResourceDirectivesExamplesSpec.scala +++ b/akka-docs/rst/scala/code/docs/http/scaladsl/server/directives/FileAndResourceDirectivesExamplesSpec.scala @@ -18,7 +18,7 @@ class FileAndResourceDirectivesExamplesSpec extends RoutingSpec { val route = path("logs" / Segment) { name => - getFromFile(".log") // uses implicit ContentTypeResolver + getFromFile(s"$name.log") // uses implicit ContentTypeResolver } // tests: @@ -32,7 +32,7 @@ class FileAndResourceDirectivesExamplesSpec extends RoutingSpec { val route = path("logs" / Segment) { name => - getFromResource(".log") // uses implicit ContentTypeResolver + getFromResource(s"$name.log") // uses implicit ContentTypeResolver } // tests: @@ -46,6 +46,7 @@ class FileAndResourceDirectivesExamplesSpec extends RoutingSpec { listDirectoryContents("/tmp") } ~ path("custom") { + // implement your custom renderer here val renderer = new DirectoryRenderer { override def marshaller(renderVanityFooter: Boolean): ToEntityMarshaller[DirectoryListing] = ??? } diff --git a/akka-http/src/main/scala/akka/http/scaladsl/server/RequestContextImpl.scala b/akka-http/src/main/scala/akka/http/scaladsl/server/RequestContextImpl.scala index b477b3858d..c1c008647f 100644 --- a/akka-http/src/main/scala/akka/http/scaladsl/server/RequestContextImpl.scala +++ b/akka-http/src/main/scala/akka/http/scaladsl/server/RequestContextImpl.scala @@ -4,11 +4,11 @@ package akka.http.scaladsl.server -import scala.concurrent.{ExecutionContextExecutor, Future} -import akka.stream.{ActorMaterializer, ActorMaterializerHelper, Materializer} +import scala.concurrent.{ ExecutionContextExecutor, Future } +import akka.stream.{ ActorMaterializer, ActorMaterializerHelper, Materializer } import akka.event.LoggingAdapter -import akka.http.scaladsl.settings.{ParserSettings, RoutingSettings} -import akka.http.scaladsl.marshalling.{Marshal, ToResponseMarshallable} +import akka.http.scaladsl.settings.{ ParserSettings, RoutingSettings } +import akka.http.scaladsl.marshalling.{ Marshal, ToResponseMarshallable } import akka.http.scaladsl.model._ import akka.http.scaladsl.util.FastFuture import akka.http.scaladsl.util.FastFuture._ diff --git a/akka-http/src/main/scala/akka/http/scaladsl/server/Route.scala b/akka-http/src/main/scala/akka/http/scaladsl/server/Route.scala index d1de8d5a44..e60a109578 100644 --- a/akka-http/src/main/scala/akka/http/scaladsl/server/Route.scala +++ b/akka-http/src/main/scala/akka/http/scaladsl/server/Route.scala @@ -5,12 +5,12 @@ package akka.http.scaladsl.server import akka.NotUsed -import akka.http.scaladsl.settings.{ParserSettings, RoutingSettings} -import akka.stream.{ActorMaterializer, ActorMaterializerHelper, Materializer} +import akka.http.scaladsl.settings.{ ParserSettings, RoutingSettings } +import akka.stream.{ ActorMaterializer, ActorMaterializerHelper, Materializer } -import scala.concurrent.{ExecutionContextExecutor, Future} +import scala.concurrent.{ ExecutionContextExecutor, Future } import akka.stream.scaladsl.Flow -import akka.http.scaladsl.model.{HttpRequest, HttpResponse} +import akka.http.scaladsl.model.{ HttpRequest, HttpResponse } import akka.http.scaladsl.util.FastFuture._ object Route { diff --git a/akka-http/src/main/scala/akka/http/scaladsl/unmarshalling/MultipartUnmarshallers.scala b/akka-http/src/main/scala/akka/http/scaladsl/unmarshalling/MultipartUnmarshallers.scala index 8bf35e5656..380f50fdd6 100644 --- a/akka-http/src/main/scala/akka/http/scaladsl/unmarshalling/MultipartUnmarshallers.scala +++ b/akka-http/src/main/scala/akka/http/scaladsl/unmarshalling/MultipartUnmarshallers.scala @@ -9,8 +9,8 @@ import akka.http.scaladsl.settings.ParserSettings import scala.collection.immutable import scala.collection.immutable.VectorBuilder import akka.util.ByteString -import akka.event.{LoggingAdapter, NoLogging} -import akka.stream.{ActorMaterializer, ActorMaterializerHelper} +import akka.event.{ LoggingAdapter, NoLogging } +import akka.stream.{ ActorMaterializer, ActorMaterializerHelper } import akka.stream.impl.fusing.IteratorInterpreter import akka.stream.scaladsl._ import akka.http.impl.engine.parsing.BodyPartParser