A simple file server is composed of three components:
HttpServer that is bound to a given address, HttpHandler that serves files from a given
directory path, and Filter that prints log messages relating to
the exchanges handled by the server. The createFileServer
static factory method returns an HttpServer that is a
simple out-of-the-box file server. The server comes with an initial handler
that serves files from a given directory path (and its subdirectories).
The output level determines what log messages are printed to
System.out, if any.
Example of a simple file server:
var addr = new InetSocketAddress(8080);
var server = SimpleFileServer.createFileServer(addr, Path.of("/some/path"), OutputLevel.INFO);
server.start();
The createFileHandler static factory
method returns an HttpHandler that serves files and directory
listings. The handler supports only the HEAD and GET request
methods; to handle other request methods, one can either add additional
handlers to the server, or complement the file handler by composing a single
handler via
HttpHandlers#handleOrElse(Predicate, HttpHandler, HttpHandler).
Example of composing a single handler:
var handler = HttpHandlers.handleOrElse(
(req) -> req.getRequestMethod().equals("PUT"),
(exchange) -> {
// validate and handle PUT request
},
SimpleFileServer.createFileHandler(Path.of("/some/path")))
);
The createOutputFilter
static factory method returns a
post-processing filter that
prints log messages relating to the exchanges handled by the server. The
output format is specified by the outputLevel.
Example of an output filter:
var filter = SimpleFileServer.createOutputFilter(System.out, OutputLevel.VERBOSE);
var server = HttpServer.create(new InetSocketAddress(8080), 10, "/some/path/", new SomeHandler(), filter);
server.start();
A simple HTTP file server implementation is provided via the
jwebserver tool.
| Modifier and Type | Class and Description |
|---|---|
| public static enum | SimpleFileServer.
Describes the log message output level produced by the server when processing exchanges. |
| Modifier and Type | Field and Description |
|---|---|
| private static final UnaryOperator |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static HttpHandler | Returns: a file handlerthe root directory to be served, must be an absolute path rootDirectory)Creates a file handler that serves files from a given directory path (and its subdirectories). |
| public static HttpServer | Returns: an HttpServerthe address to listen on addr, Path the root directory to be served, must be an absolute path rootDirectory, SimpleFileServer.the log message output level outputLevelCreates a file server that serves files from a given path. |
| public static Filter | Returns: a post-processing filterthe stream to print to out, SimpleFileServer.the output level outputLevelCreates a post-processing Filter that prints log messages about exchanges. |
| MIME_TABLE | back to summary |
|---|---|
| private static final UnaryOperator<String> MIME_TABLE | |
| SimpleFileServer | back to summary |
|---|---|
| private SimpleFileServer() | |
| createFileHandler | back to summary |
|---|---|
| public static HttpHandler createFileHandler(Path rootDirectory) Creates a file handler that serves files from a given directory path (and its subdirectories). The file handler resolves the request URI against the given
The handler supports only requests with the HEAD or GET
method, and will reply with a
| |
| createFileServer | back to summary |
|---|---|
| public static HttpServer createFileServer(InetSocketAddress addr, Path rootDirectory, SimpleFileServer. Creates a file server that serves files from a given path. The server is configured with an initial context that maps the
URI An output level can be given to print log messages relating to the
exchanges handled by the server. The log messages, if any, are printed to
| |
| createOutputFilter | back to summary |
|---|---|
| public static Filter createOutputFilter(OutputStream out, SimpleFileServer. Creates a post-processing Filter that prints log messages about
exchanges. The log messages are printed to
the given API Note To not output any log messages it is recommended to not use a filter.
| |
| Modifier and Type | Field and Description |
|---|---|
| public static final SimpleFileServer. | INFO
Used to specify the informative log message output level. |
| public static final SimpleFileServer. | NONE
Used to specify no log message output level. |
| public static final SimpleFileServer. | VERBOSE
Used to specify the verbose log message output level. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static SimpleFileServer. | |
| public static SimpleFileServer. |
| INFO | back to summary |
|---|---|
| public static final SimpleFileServer. Used to specify the informative log message output level. The log message format is based on the
Common Logfile Format,
that includes the following information about an Example:
Implementation Note The fields | |
| NONE | back to summary |
|---|---|
| public static final SimpleFileServer. Used to specify no log message output level. | |
| VERBOSE | back to summary |
|---|---|
| public static final SimpleFileServer. Used to specify the verbose log message output level. Additional to the information provided by the
info level, the verbose level
includes the request and response headers of the | |
| OutputLevel | back to summary |
|---|---|
| private OutputLevel() | |
| valueOf | back to summary |
|---|---|
| public static SimpleFileServer. | |
| values | back to summary |
|---|---|
| public static SimpleFileServer. | |