Interface FileHandlerSpec
-
public interface FileHandlerSpec
A specification for a handler that serves files from the file system.- See Also:
Chain.files(Action)
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description FileHandlerSpec
dir(String dir)
Specifies the file system path to the files.default FileHandlerSpec
files(String path)
A convenience method that specifies both request path and file system path bindings for serving files.FileHandlerSpec
indexFiles(String... indexFiles)
The files that should be used when a request is made for a directory.FileHandlerSpec
path(String path)
Specifies the request path to bind to for serving files.
-
-
-
Method Detail
-
path
FileHandlerSpec path(String path)
Specifies the request path to bind to for serving files.The path specified is relative to the context's
PathBinding
at request time. The portion of the request path past the path binding identifies the target file to serve. The default value is effectively""
, which means that existing path binding is used.import ratpack.test.embed.EphemeralBaseDir; import ratpack.test.embed.EmbeddedApp; import static org.junit.jupiter.api.Assertions.assertEquals; public class Example { public static void main(String... args) throws Exception { EphemeralBaseDir.tmpDir().use(baseDir -> { baseDir.write("a.txt", "a"); EmbeddedApp.of(s -> s .serverConfig(c -> c.baseDir(baseDir.getRoot())) .handlers(c -> c .files(f -> f.path("files")) .prefix("prefix", p -> p .files() .files(f -> f.path("files")) ) ) ).test(httpClient -> { // assertEquals("a", httpClient.getText("files/a.txt")); // assertEquals("a", httpClient.getText("prefix/a.txt")); assertEquals("a", httpClient.getText("prefix/files/a.txt")); }); }); } }
- Parameters:
path
- the request path to bind to- Returns:
this
-
dir
FileHandlerSpec dir(String dir)
Specifies the file system path to the files.The path specified is relative to the context's
FileSystemBinding
at request time. The default value is effectively""
, which means that existing file system binding is used.import ratpack.test.embed.EphemeralBaseDir; import ratpack.test.embed.EmbeddedApp; import static org.junit.jupiter.api.Assertions.assertEquals; public class Example { public static void main(String... args) throws Exception { EphemeralBaseDir.tmpDir().use(baseDir -> { baseDir.write("dir/a.txt", "a"); EmbeddedApp.of(s -> s .serverConfig(c -> c.baseDir(baseDir.getRoot())) .handlers(c -> c .files(f -> f.path("nopath")) .files(f -> f.path("pathed").dir("dir")) .prefix("under-binding", p -> p .fileSystem("dir", f -> f .files() ) ) ) ).test(httpClient -> { assertEquals("a", httpClient.getText("nopath/dir/a.txt")); assertEquals("a", httpClient.getText("pathed/a.txt")); assertEquals("a", httpClient.getText("under-binding/a.txt")); }); }); } }
- Parameters:
dir
- the file system path to bind to- Returns:
this
-
files
default FileHandlerSpec files(String path)
A convenience method that specifies both request path and file system path bindings for serving files.The path specified is relative to the context's
PathBinding
andFileSystemBinding
at request time. The default value is effectively""
, which means that existing path binding and file system binding is used.import ratpack.test.embed.EphemeralBaseDir; import ratpack.test.embed.EmbeddedApp; import static org.junit.jupiter.api.Assertions.assertEquals; public class Example { public static void main(String... args) throws Exception { EphemeralBaseDir.tmpDir().use(baseDir -> { baseDir.write("dir/a.txt", "a"); EmbeddedApp.of(s -> s .serverConfig(c -> c.baseDir(baseDir.getRoot())) .handlers(c -> c .files(f -> f.files("dir") // same as f.path("dir").dir("dir") ) ) ).test(httpClient -> assertEquals("a", httpClient.getText("/dir/a.txt")) ); }); } }
- Parameters:
path
- the request path and file system path to bind to- Returns:
this
-
indexFiles
FileHandlerSpec indexFiles(String... indexFiles)
The files that should be used when a request is made for a directory.If the request path matches a directory, an index file may be served. The directory is checked for presence of the given index files. The first that is found is served.
import ratpack.test.embed.EphemeralBaseDir; import ratpack.test.embed.EmbeddedApp; import static org.junit.jupiter.api.Assertions.assertEquals; public class Example { public static void main(String... args) throws Exception { EphemeralBaseDir.tmpDir().use(baseDir -> { baseDir.write("dir1/a.txt", "a1"); baseDir.write("dir1/b.txt", "b1"); baseDir.write("dir2/b.txt", "b2"); EmbeddedApp.of(s -> s .serverConfig(c -> c.baseDir(baseDir.getRoot())) .handlers(c -> c .files(f -> f.path("noIndex")) .files(f -> f.path("indexes").indexFiles("a.txt", "b.txt")) ) ).test(httpClient -> { httpClient.requestSpec(r -> r.redirects(1)); assertEquals(404, httpClient.get("noIndex/dir1").getStatusCode()); assertEquals("a1", httpClient.getText("indexes/dir1")); assertEquals("b2", httpClient.getText("indexes/dir2")); }); }); } }
- Parameters:
indexFiles
- the index files in order of precedence- Returns:
this
-
-