public class ResourceHandlerBuilder extends Object implements MuHandlerBuilder<ResourceHandler>
Used to create a ResourceHandler
for serving static files.
In order to serve from the filesystem during development, and from classpath and deploy time, see
fileOrClasspath(String, String)
Constructor and Description |
---|
ResourceHandlerBuilder() |
Modifier and Type | Method and Description |
---|---|
ResourceHandler |
build()
Creates the handler
|
static ResourceHandlerBuilder |
classpathHandler(String classpathRoot)
Creates a handler that serves files from the classpath..
|
static ResourceHandlerBuilder |
fileHandler(File baseDirectory)
Creates a handler that serves files from the given directory.
|
static ResourceHandlerBuilder |
fileHandler(Path path)
Creates a handler that serves files from the given directory.
|
static ResourceHandlerBuilder |
fileHandler(String directoryPath)
Creates a handler that serves files from the given directory.
|
static ResourceHandlerBuilder |
fileOrClasspath(String fileRootIfExists,
String classpathRoot)
Creates a resource handler that serves from the file system if the directory exists; otherwise from the class path.
|
ResourceHandlerBuilder |
withDefaultFile(String defaultFile)
Specifies the file to use when a request such as
/web/ is made. |
ResourceHandlerBuilder |
withDirectoryListing(boolean enabled)
Specifies whether or not to allow directory listing.
|
ResourceHandlerBuilder |
withDirectoryListingCSS(String css)
Specifies CSS to use for the HTML directory listing page, if directory listing is enabled.
|
ResourceHandlerBuilder |
withDirectoryListingDateFormatter(DateTimeFormatter dateTimeFormatter)
Specifies a custom date format for the "Last modified" column when directory listing is enabled.
|
ResourceHandlerBuilder |
withExtensionToResourceType(Map<String,ResourceType> extensionToResourceType)
Specify custom filename extension to mime-type mappings.
|
ResourceHandlerBuilder |
withPathToServeFrom(String pathToServeFrom)
Deprecated.
Please use
ContextHandlerBuilder.context(pathToServeFrom).addHandler(resourceHandler) instead. |
ResourceHandlerBuilder |
withResourceCustomizer(ResourceCustomizer resourceCustomizer)
Registers a hook to intercept responses, allowing things such as response header customization based on the request.
|
public ResourceHandlerBuilder withExtensionToResourceType(Map<String,ResourceType> extensionToResourceType)
ResourceType.DEFAULT_EXTENSION_MAPPINGS
are used.extensionToResourceType
- The mappings to use.@Deprecated public ResourceHandlerBuilder withPathToServeFrom(String pathToServeFrom)
ContextHandlerBuilder.context(pathToServeFrom).addHandler(resourceHandler)
instead.pathToServeFrom
- A path that static data should be served from. Defaults to /
public ResourceHandlerBuilder withDefaultFile(String defaultFile)
/web/
is made. Defaults to index.html
defaultFile
- The default file to use when a directory is requested, or null
for no default.public ResourceHandlerBuilder withDirectoryListing(boolean enabled)
Note that directory listings will not show if there is a file in the directory
that matches the withDefaultFile(String)
setting. Set that to null
if you do not want the default file (e.g. index.html
) to display.
enabled
- true
to turn it on; false
to disable it.public ResourceHandlerBuilder withDirectoryListingDateFormatter(DateTimeFormatter dateTimeFormatter)
dateTimeFormatter
- A format object, or null to use the defaultpublic ResourceHandlerBuilder withDirectoryListingCSS(String css)
css
- CSS styles to use, or null for the defaultpublic ResourceHandlerBuilder withResourceCustomizer(ResourceCustomizer resourceCustomizer)
resourceCustomizer
- A class to intercept responsespublic ResourceHandler build()
build
in interface MuHandlerBuilder<ResourceHandler>
public static ResourceHandlerBuilder fileHandler(String directoryPath)
directoryPath
- The directory.public static ResourceHandlerBuilder fileHandler(File baseDirectory)
baseDirectory
- The directory.public static ResourceHandlerBuilder fileHandler(Path path)
path
- The directory.public static ResourceHandlerBuilder classpathHandler(String classpathRoot)
classpathRoot
- A classpath directory, such as /web
public static ResourceHandlerBuilder fileOrClasspath(String fileRootIfExists, String classpathRoot)
A common use case is for when you want to serve from the file path at development time (so you can update files without restarting) but at deploy time resources are packaged in an uber jar.
fileRootIfExists
- A path to a directory holding static content, which may not exist, e.g. src/main/resources/web
classpathRoot
- A classpath path to a directory holding static content, e.g. /web
Copyright © 2017–2021. All rights reserved.