Skip to main content

Static files

The BasicRestServlet and BasicRestResource classes come with built-in support for serving up static files through the HtdocMixin op-mixin, which provides the following REST operation:

@RestGet(path="/htdocs/*")
public HttpResource getHtdoc(RestRequest req, @Path("/*") String path, Locale locale) throws NotFound {
return StaticFilesMixin.resolveStaticFile(req, path, locale);
}

HtdocMixin delegates to StaticFilesMixin.resolveStaticFile(...) — the same static-file resolution chokepoint used by StaticFilesMixin's own handlers — so there's a single implementation of the lookup logic even though the two mixins expose different mounts (HtdocMixin keeps its legacy, fixed, GET-only /htdocs/* mount; StaticFilesMixin mounts a configurable, GET+HEAD path defaulting to /static/*).

The static file finder can be accessed through either of the following methods — the RestRequest shortcut is a convenience delegate for the RestContext accessor:

RestRequestgetStaticFiles() RestContextgetStaticFiles()

By default, the StaticFiles bean is configured as follows:

StaticFiles
.create()
.beanStore(beanStore) // Allow injected beans in constructor.
.type(BasicStaticFiles.class) // Default implementation class.
.dir("static") // Look in working /static directory.
.dir("htdocs") // Look in working /htdocs directory.
.cp(resourceClass, "htdocs", true) // Look in htdocs subpackage.
.cp(resourceClass, "/htdocs", true) // Look in htdocs package.
.caching(1_000_000) // Cache files in memory up to 1MB.
.exclude("(?i).*\\.(class|properties)") // Ignore class/properties files.
.headers(cacheControl("max-age=86400, public")); // Add cache control.
Relationship to StaticFilesMixin

HtdocMixin and StaticFilesMixin both serve static content off the same StaticFiles bean, but no longer compete for the same mount by default: HtdocMixin owns the fixed legacy /htdocs/* path, while StaticFilesMixin defaults to /static/* (configurable via the ${juneau.staticfiles.path:static} SVL variable — set it to htdocs to reach the same URL HtdocMixin uses). Adding StaticFilesMixin to a BasicRestServlet subclass is therefore safe out of the box; both mixins resolve through the shared StaticFilesMixin.resolveStaticFile(...) logic.

Static files can be configured programmatically through the following APIs:

Beanname="staticFiles"