Class ResourceHandlerBuilder

    • Constructor Detail

      • ResourceHandlerBuilder

        public ResourceHandlerBuilder()
    • Method Detail

      • withExtensionToResourceType

        public ResourceHandlerBuilder withExtensionToResourceType​(java.util.Map<java.lang.String,​ResourceType> extensionToResourceType)
        Specify custom filename extension to mime-type mappings. By default ResourceType.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 to index.html
        Parameters:
        defaultFile - The default file to use when a directory is requested, or null 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 to null 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
      • 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.