example snippet for akka http java dsl: FileAndResourceDirectives (#20758)
This commit is contained in:
parent
16cde39de8
commit
efb18c95b1
12 changed files with 151 additions and 19 deletions
|
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -19,4 +19,5 @@ For more details refer to :ref:`-getFromBrowseableDirectory-java-`.
|
||||||
|
|
||||||
Example
|
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
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,8 @@ For more details refer to :ref:`-getFromBrowseableDirectory-java-`.
|
||||||
|
|
||||||
Example
|
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
|
Default file listing page example
|
||||||
|
|
|
||||||
|
|
@ -27,4 +27,5 @@ Note that it's not required to wrap this directive with ``get`` as this directiv
|
||||||
|
|
||||||
Example
|
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
|
||||||
|
|
|
||||||
|
|
@ -27,4 +27,5 @@ Note that it's not required to wrap this directive with ``get`` as this directiv
|
||||||
|
|
||||||
Example
|
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
|
||||||
|
|
|
||||||
|
|
@ -15,4 +15,5 @@ Note that it's not required to wrap this directive with ``get`` as this directiv
|
||||||
|
|
||||||
Example
|
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
|
||||||
|
|
|
||||||
|
|
@ -15,4 +15,5 @@ Note that it's not required to wrap this directive with ``get`` as this directiv
|
||||||
|
|
||||||
Example
|
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
|
||||||
|
|
|
||||||
|
|
@ -20,4 +20,5 @@ Note that it's not required to wrap this directive with ``get`` as this directiv
|
||||||
|
|
||||||
Example
|
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
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ class FileAndResourceDirectivesExamplesSpec extends RoutingSpec {
|
||||||
|
|
||||||
val route =
|
val route =
|
||||||
path("logs" / Segment) { name =>
|
path("logs" / Segment) { name =>
|
||||||
getFromFile(".log") // uses implicit ContentTypeResolver
|
getFromFile(s"$name.log") // uses implicit ContentTypeResolver
|
||||||
}
|
}
|
||||||
|
|
||||||
// tests:
|
// tests:
|
||||||
|
|
@ -32,7 +32,7 @@ class FileAndResourceDirectivesExamplesSpec extends RoutingSpec {
|
||||||
|
|
||||||
val route =
|
val route =
|
||||||
path("logs" / Segment) { name =>
|
path("logs" / Segment) { name =>
|
||||||
getFromResource(".log") // uses implicit ContentTypeResolver
|
getFromResource(s"$name.log") // uses implicit ContentTypeResolver
|
||||||
}
|
}
|
||||||
|
|
||||||
// tests:
|
// tests:
|
||||||
|
|
@ -46,6 +46,7 @@ class FileAndResourceDirectivesExamplesSpec extends RoutingSpec {
|
||||||
listDirectoryContents("/tmp")
|
listDirectoryContents("/tmp")
|
||||||
} ~
|
} ~
|
||||||
path("custom") {
|
path("custom") {
|
||||||
|
// implement your custom renderer here
|
||||||
val renderer = new DirectoryRenderer {
|
val renderer = new DirectoryRenderer {
|
||||||
override def marshaller(renderVanityFooter: Boolean): ToEntityMarshaller[DirectoryListing] = ???
|
override def marshaller(renderVanityFooter: Boolean): ToEntityMarshaller[DirectoryListing] = ???
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue