Package io.muserver.handlers
Class ResourceHandlerBuilder
- java.lang.Object
-
- io.muserver.handlers.ResourceHandlerBuilder
-
- All Implemented Interfaces:
MuHandlerBuilder<ResourceHandler>
public class ResourceHandlerBuilder extends java.lang.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 Summary
Constructors Constructor Description ResourceHandlerBuilder()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ResourceHandler
build()
Creates the handlerstatic ResourceHandlerBuilder
classpathHandler(java.lang.String classpathRoot)
Creates a handler that serves files from the classpath..java.lang.String
defaultFile()
java.lang.String
directoryListingCss()
java.time.format.DateTimeFormatter
directoryListingDateFormatter()
boolean
directoryListingEnabled()
java.util.Map<java.lang.String,ResourceType>
extensionToResourceType()
static ResourceHandlerBuilder
fileHandler(java.io.File baseDirectory)
Creates a handler that serves files from the given directory.static ResourceHandlerBuilder
fileHandler(java.lang.String directoryPath)
Creates a handler that serves files from the given directory.static ResourceHandlerBuilder
fileHandler(java.nio.file.Path path)
Creates a handler that serves files from the given directory.static ResourceHandlerBuilder
fileOrClasspath(java.lang.String fileRootIfExists, java.lang.String classpathRoot)
Creates a resource handler that serves from the file system if the directory exists; otherwise from the class path.ResourceCustomizer
resourceCustomizer()
ResourceHandlerBuilder
withDefaultFile(java.lang.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(java.lang.String css)
Specifies CSS to use for the HTML directory listing page, if directory listing is enabled.ResourceHandlerBuilder
withDirectoryListingDateFormatter(java.time.format.DateTimeFormatter dateTimeFormatter)
Specifies a custom date format for the "Last modified" column when directory listing is enabled.ResourceHandlerBuilder
withExtensionToResourceType(java.util.Map<java.lang.String,ResourceType> extensionToResourceType)
Specify custom filename extension to mime-type mappings.ResourceHandlerBuilder
withResourceCustomizer(ResourceCustomizer resourceCustomizer)
Registers a hook to intercept responses, allowing things such as response header customization based on the request.
-
-
-
Method Detail
-
withExtensionToResourceType
public ResourceHandlerBuilder withExtensionToResourceType(java.util.Map<java.lang.String,ResourceType> extensionToResourceType)
Specify custom filename extension to mime-type mappings. By defaultResourceType.DEFAULT_EXTENSION_MAPPINGS
are used.- Parameters:
extensionToResourceType
- The mappings to use.- Returns:
- This builder
-
withDefaultFile
public ResourceHandlerBuilder withDefaultFile(java.lang.String defaultFile)
Specifies the file to use when a request such as/web/
is made. Defaults toindex.html
- Parameters:
defaultFile
- The default file to use when a directory is requested, ornull
for no default.- Returns:
- This builder
-
withDirectoryListing
public ResourceHandlerBuilder withDirectoryListing(boolean enabled)
Specifies whether or not to allow directory listing. This is disabled by default.Note that directory listings will not show if there is a file in the directory that matches the
withDefaultFile(String)
setting. Set that tonull
if you do not want the default file (e.g.index.html
) to display.- Parameters:
enabled
-true
to turn it on;false
to disable it.- Returns:
- This builder
-
withDirectoryListingDateFormatter
public ResourceHandlerBuilder withDirectoryListingDateFormatter(java.time.format.DateTimeFormatter dateTimeFormatter)
Specifies a custom date format for the "Last modified" column when directory listing is enabled.- Parameters:
dateTimeFormatter
- A format object, or null to use the default- Returns:
- This builder
-
withDirectoryListingCSS
public ResourceHandlerBuilder withDirectoryListingCSS(java.lang.String css)
Specifies CSS to use for the HTML directory listing page, if directory listing is enabled.- Parameters:
css
- CSS styles to use, or null for the default- Returns:
- This builder
-
withResourceCustomizer
public ResourceHandlerBuilder withResourceCustomizer(ResourceCustomizer resourceCustomizer)
Registers a hook to intercept responses, allowing things such as response header customization based on the request.- Parameters:
resourceCustomizer
- A class to intercept responses- Returns:
- This builder
-
directoryListingDateFormatter
public java.time.format.DateTimeFormatter directoryListingDateFormatter()
- Returns:
- The current value of this property
-
extensionToResourceType
public java.util.Map<java.lang.String,ResourceType> extensionToResourceType()
- Returns:
- The current value of this property
-
defaultFile
public java.lang.String defaultFile()
- Returns:
- The current value of this property
-
directoryListingEnabled
public boolean directoryListingEnabled()
- Returns:
- The current value of this property
-
directoryListingCss
public java.lang.String directoryListingCss()
- Returns:
- The current value of this property
-
resourceCustomizer
public ResourceCustomizer resourceCustomizer()
- Returns:
- The current value of this property
-
build
public ResourceHandler build()
Creates the handler- Specified by:
build
in interfaceMuHandlerBuilder<ResourceHandler>
- Returns:
- The built handler
-
fileHandler
public static ResourceHandlerBuilder fileHandler(java.lang.String directoryPath)
Creates a handler that serves files from the given directory.- Parameters:
directoryPath
- The directory.- Returns:
- A new builder.
-
fileHandler
public static ResourceHandlerBuilder fileHandler(java.io.File baseDirectory)
Creates a handler that serves files from the given directory.- Parameters:
baseDirectory
- The directory.- Returns:
- A new builder.
-
fileHandler
public static ResourceHandlerBuilder fileHandler(java.nio.file.Path path)
Creates a handler that serves files from the given directory.- Parameters:
path
- The directory.- Returns:
- A new builder.
-
classpathHandler
public static ResourceHandlerBuilder classpathHandler(java.lang.String classpathRoot)
Creates a handler that serves files from the classpath..- Parameters:
classpathRoot
- A classpath directory, such as/web
- Returns:
- A new builder.
-
fileOrClasspath
public static ResourceHandlerBuilder fileOrClasspath(java.lang.String fileRootIfExists, java.lang.String classpathRoot)
Creates a resource handler that serves from the file system if the directory exists; otherwise from the class path.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.
- Parameters:
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
- Returns:
- Returns a file-based resource handler builder or a classpath-based one.
-
-