example snippet for akka http java dsl: FileAndResourceDirectives (#20758)

This commit is contained in:
Hawstein 2016-06-09 21:34:25 +08:00 committed by Konrad Malawski
parent 16cde39de8
commit efb18c95b1
12 changed files with 151 additions and 19 deletions

View file

@ -0,0 +1,124 @@
/*
* Copyright (C) 2016-2016 Lightbend Inc. <http://www.lightbend.com>
*/
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
}
}

View file

@ -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 <https://github.com/akka/akka/issues/20466>`_.
.. includecode:: ../../../../code/docs/http/javadsl/server/directives/FileAndResourceDirectivesExamplesTest.java#getFromBrowseableDirectories

View file

@ -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 <https://github.com/akka/akka/issues/20466>`_.
.. includecode:: ../../../../code/docs/http/javadsl/server/directives/FileAndResourceDirectivesExamplesTest.java#getFromBrowseableDirectory
Default file listing page example

View file

@ -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 <https://github.com/akka/akka/issues/20466>`_.
.. includecode:: ../../../../code/docs/http/javadsl/server/directives/FileAndResourceDirectivesExamplesTest.java#getFromDirectory

View file

@ -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 <https://github.com/akka/akka/issues/20466>`_.
.. includecode:: ../../../../code/docs/http/javadsl/server/directives/FileAndResourceDirectivesExamplesTest.java#getFromFile

View file

@ -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 <https://github.com/akka/akka/issues/20466>`_.
.. includecode:: ../../../../code/docs/http/javadsl/server/directives/FileAndResourceDirectivesExamplesTest.java#getFromResource

View file

@ -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 <https://github.com/akka/akka/issues/20466>`_.
.. includecode:: ../../../../code/docs/http/javadsl/server/directives/FileAndResourceDirectivesExamplesTest.java#getFromResourceDirectory

View file

@ -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 <https://github.com/akka/akka/issues/20466>`_.
.. includecode:: ../../../../code/docs/http/javadsl/server/directives/FileAndResourceDirectivesExamplesTest.java#listDirectoryContents

View file

@ -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] = ???
}

View file

@ -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._

View file

@ -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 {

View file

@ -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