Class StaticFiles

java.lang.Object
com.soklet.StaticFiles

@ThreadSafe public final class StaticFiles extends Object
Safe helper for serving files from a configured root directory.
Author:
Mark Allen
  • Method Details

    • withRoot

      Begins configuration for serving files under root.

      Relative roots are resolved against the JVM's current working directory when the builder is built. Production deployments should generally pass an absolute path.

      File matching follows the underlying filesystem's case-sensitivity and Unicode normalization rules. Production deployments that need portable behavior across operating systems should standardize on ASCII-safe asset names.

      Parameters:
      root - the static-file root directory
      Returns:
      a builder for static-file responses
    • marshaledResponseFor

      Produces a marshaled response for relativePath, if the request targets a safe, readable file under the configured root.

      Returns Optional.empty() for HTTP methods other than HttpMethod.GET and HttpMethod.HEAD, unsafe path input, directories without a configured index file, unreadable files, and missing files. Once a path resolves, non-200 file responses such as 206, 304, 412, and 416 are returned inside the optional.

      Resolvers receive the resolved file path after index-file resolution, not the original relativePath.

      Parameters:
      relativePath - the root-relative file path
      request - the incoming request
      Returns:
      the marshaled response, if a static file was resolved