Class ClasspathAssetCache

java.lang.Object
org.apache.juneau.rest.server.util.ClasspathAssetCache

public class ClasspathAssetCache extends Object
A themeable/configurable asset-serving mixin's cache of classpath-shipped assets: reads each configured resource's bytes once, computes and caches its 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
    Constructor
    Description
    Constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    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.
    byte[]
    bytes(String classpathResource)
    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.
    hash(String classpathResource)
    Computes (and caches) the given classpath resource's 8-hex-char content hash.
    serve(String classpathResource, String contentType, String cacheControl)
    Reads (and caches) the given classpath resource and wraps it as a cacheable HttpResource - the bytes(String) + wrap(byte[],String,String) bundle every classpath-asset-serving endpoint needs.
    wrap(byte[] bytes, String contentType, String cacheControl)
    Wraps pre-computed bytes as a cacheable HttpResource carrying the given content type and Cache-Control header.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • Method Details

    • bytes

      public byte[] bytes(String classpathResource)
      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 be null 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

      public String hash(String classpathResource)
      Computes (and caches) the given classpath resource's 8-hex-char content hash.
      Parameters:
      classpathResource - A classpath-root-absolute resource path. Same constraints as bytes(String).
      Returns:
      The resource's ChecksumUtils.hash8(byte[]) content hash.
    • buildVersion

      public String 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

      public String 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.
      Parameters:
      classpathResource - A classpath-root-absolute resource path. Same constraints as bytes(String).
      Returns:
      The "?v=" + buildVersion() + "-" + hash(classpathResource) suffix.
    • wrap

      public HttpResource wrap(byte[] bytes, String contentType, String cacheControl)
      Wraps pre-computed bytes as a cacheable HttpResource carrying the given content type and Cache-Control header.
      Parameters:
      bytes - The response body bytes. Must not be null.
      contentType - The Content-Type header value (e.g. "text/css;charset=utf-8").
      cacheControl - The Cache-Control header value (e.g. "max-age=86400, public").
      Returns:
      The wrapped, cacheable resource.
    • serve

      public HttpResource serve(String classpathResource, String contentType, String cacheControl)
      Reads (and caches) the given classpath resource and wraps it as a cacheable HttpResource - the bytes(String) + wrap(byte[],String,String) bundle every classpath-asset-serving endpoint needs.
      Parameters:
      classpathResource - A classpath-root-absolute resource path. Same constraints as bytes(String).
      contentType - The Content-Type header value.
      cacheControl - The Cache-Control header value.
      Returns:
      The wrapped, cacheable resource.