Class StaticFiles
app.get("/assets/*path", StaticFiles.directory(Path.of("public")).build());
app.get("/ui/*path", StaticFiles.classpath("web/ui").maxFileSize(8L * 1024 * 1024).build());
The handler serves only GET and HEAD. It reads the raw capture, refuses every path that the router's raw-path policy refuses (empty and dot segments, backslashes, NUL and controls, malformed escapes and encoded dot, slash, backslash or NUL), percent-decodes each remaining segment once as strict UTF-8, and only then resolves it against the configured root. Normalization follows validation: a candidate must stay under the root after lexical normalization and after symbolic links are resolved. Directories, missing files and targets outside the root answer 404; an unsafe path answers 400 and an oversized file answers 413. These problem responses carry fixed codes and never include the path or a file system detail.
Every response carries a fixed Cache-Control, a strong SHA-256 ETag computed
over the representation, Last-Modified when the source supplies a time,
Accept-Ranges: bytes, and a Content-Type from a bounded extension table (falling back to
application/octet-stream). HEAD sends no body and carries the representation length in
Content-Length. GET streams the file through Response.stream(int, java.lang.String, com.jsgalactic.axiom.http.StreamBody), in bounded pieces,
so memory use does not grow with file size; the stream is cancelled with the request. Registering
HttpPreconditions.conditional() gives these responses conditional and range handling,
including 304, 412, 206 and 416, without buffering them.
The handler is immutable and thread-safe; one instance may serve every route and request. Each request resolves and hashes its own file, so a file replaced while it is being served may be hashed from one version and streamed from another. Applications that require a consistent snapshot must publish immutable files.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classCollects the immutable handler's configuration. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringDefault cache policy for served files: publicly cacheable for one hour.static final StringHeader used when an extension is not in the built-in table.static final longDefault largest served file: 64 MiB, the same bound as a default response stream. -
Method Summary
Modifier and TypeMethodDescriptionstatic StaticFiles.BuilderConfigures serving from a classpath location, using the class loader of this class.static StaticFiles.Builderclasspath(String prefix, ClassLoader loader) Configures serving from a classpath location.static StaticFiles.BuilderConfigures serving from a directory.
-
Field Details
-
DEFAULT_MAX_FILE_SIZE
public static final long DEFAULT_MAX_FILE_SIZEDefault largest served file: 64 MiB, the same bound as a default response stream.- See Also:
-
DEFAULT_CACHE_CONTROL
Default cache policy for served files: publicly cacheable for one hour.- See Also:
-
DEFAULT_CONTENT_TYPE
Header used when an extension is not in the built-in table.- See Also:
-
-
Method Details
-
directory
Configures serving from a directory. The root is used as given; symbolic links are accepted only while their resolved target stays under it.- Parameters:
root- directory that owns every served file- Returns:
- builder for the handler
-
classpath
Configures serving from a classpath location, using the class loader of this class.- Parameters:
prefix- package-style prefix such asweb/ui, or empty for the classpath root- Returns:
- builder for the handler
-
classpath
Configures serving from a classpath location. Entries inside JARs have no file system paths, so there are no symbolic links to contain; directory entries on disk are contained in the same way as a directory root.- Parameters:
prefix- package-style prefix such asweb/ui, or empty for the classpath rootloader- class loader that resolves the prefix- Returns:
- builder for the handler
-