Class ClasspathAssetCache
content hash, and wraps
bytes as a cacheable HttpResource - the read+cache+hash+cache-buster-URL+serve bundle that
ConsoleChromeMixin (juneau-console) and ViewsMixin (juneau-console-views)
previously each hand-rolled as their own private statics and static cache maps.
Version-anchor:
buildVersion() resolves Package.getImplementationVersion() from the constructor-supplied
anchor class, not from this class's own class/package. A mixin composes one ClasspathAssetCache
per mixin class (mirroring the static caches this replaces) and anchors it on its own class, so the
resolved implementation version - and therefore every ?v=<buildVersion>-<hash8> cache-buster URL this
cache builds - matches what that mixin's own jar shipped with, exactly as it did before extraction. Anchoring
on this class instead would resolve juneau-rest-server's own implementation version for every
consuming mixin, silently changing every consumer's cache-buster URLs.
Thread-safety:
Backed by ConcurrentHashMaps; safe to share across concurrent requests. Classpath-shipped resources
are assumed immutable for the lifetime of the JVM, so bytes and hashes are computed at most once per resource.
- Since:
- 10.0.0
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionResolves the framework build version for asset cache-busting from this cache's anchor class's package, falling back to"dev"when unset (e.g. running from an IDE/test classpath rather than a packaged jar) - see the class javadoc's version-anchor section.byte[]Reads (and caches) the given classpath resource's bytes, resolved relative to this cache's anchor class.cacheBuster(String classpathResource) Builds the?v=<buildVersion>-<hash8>content-sensitive cache-buster query suffix for the given classpath resource, suitable for appending to that resource's served URL.Computes (and caches) the given classpath resource's 8-hex-char content hash.Reads (and caches) the given classpath resource and wraps it as a cacheableHttpResource- thebytes(String)+wrap(byte[],String,String)bundle every classpath-asset-serving endpoint needs.Wraps pre-computed bytes as a cacheableHttpResourcecarrying the given content type andCache-Controlheader.
-
Constructor Details
-
ClasspathAssetCache
Constructor.- Parameters:
anchor- The class that anchors both classpath resource resolution (Class.getResourceAsStream(String)) andbuildVersion()'sPackage.getImplementationVersion()resolution. Typically the owning mixin's own class (see the class javadoc's version-anchor section). Must not benull .
-
-
Method Details
-
bytes
Reads (and caches) the given classpath resource's bytes, resolved relative to this cache's anchor class.- Parameters:
classpathResource- A classpath-root-absolute resource path (e.g."/org/apache/juneau/foo/bar.js"). Must not benull and must resolve to an existing resource on the anchor's classpath.- Returns:
- The resource's bytes (served from cache on every call after the first for that resource).
-
hash
Computes (and caches) the given classpath resource's 8-hex-char content hash.- Parameters:
classpathResource- A classpath-root-absolute resource path. Same constraints asbytes(String).- Returns:
- The resource's
ChecksumUtils.hash8(byte[])content hash.
-
buildVersion
Resolves the framework build version for asset cache-busting from this cache's anchor class's package, falling back to"dev"when unset (e.g. running from an IDE/test classpath rather than a packaged jar) - see the class javadoc's version-anchor section.- Returns:
- The anchor's
Package.getImplementationVersion(), or"dev"if unset.
-
cacheBuster
Builds the?v=<buildVersion>-<hash8>content-sensitive cache-buster query suffix for the given classpath resource, suitable for appending to that resource's served URL.- Parameters:
classpathResource- A classpath-root-absolute resource path. Same constraints asbytes(String).- Returns:
- The
"?v=" + buildVersion() + "-" + hash(classpathResource)suffix.
-
wrap
Wraps pre-computed bytes as a cacheableHttpResourcecarrying the given content type andCache-Controlheader.- Parameters:
bytes- The response body bytes. Must not benull .contentType- TheContent-Typeheader value (e.g."text/css;charset=utf-8").cacheControl- TheCache-Controlheader value (e.g."max-age=86400, public").- Returns:
- The wrapped, cacheable resource.
-
serve
Reads (and caches) the given classpath resource and wraps it as a cacheableHttpResource- thebytes(String)+wrap(byte[],String,String)bundle every classpath-asset-serving endpoint needs.- Parameters:
classpathResource- A classpath-root-absolute resource path. Same constraints asbytes(String).contentType- TheContent-Typeheader value.cacheControl- TheCache-Controlheader value.- Returns:
- The wrapped, cacheable resource.
-