Class WebJarResolver

java.lang.Object
org.apache.juneau.rest.server.staticfile.WebJarResolver

public final class WebJarResolver extends Object
Resolves a WebJar file to a cache-busted URL under the WebJarsMixin mount.

An asset is named groupId:artifactId:path (see asset(String, String, String)). The path is relative to META-INF/resources/webjars/ and may contain VERSION_TOKEN, which is replaced with the version read from the WebJar's own META-INF/maven/<groupId>/<artifactId>/pom.properties. The URL gets a ?v=<version> cache-buster, so a version bump in the app's pom changes every URL.

A missing WebJar, or a missing file in a present WebJar, throws IllegalStateException, so a page never renders a tag that would 404. Versions and file checks are cached for the life of the class loader.

Example:

String asset = WebJarResolver.asset("org.webjars", "jquery", "jquery/{version}/jquery.min.js"); String url = WebJarResolver.resolve(req, asset); // .../webjars/jquery/3.7.1/jquery.min.js?v=3.7.1

Since:
10.0.0
  • Field Details

  • Method Details

    • asset

      public static String asset(String groupId, String artifactId, String path)
      Builds an asset id.
      Parameters:
      groupId - The WebJar group id, e.g. org.webjars.
      artifactId - The WebJar artifact id, e.g. jquery.
      path - The file path under META-INF/resources/webjars/, optionally containing VERSION_TOKEN.
      Returns:
      The asset id groupId:artifactId:path.
    • version

      public static String version(String groupId, String artifactId)
      Returns the version of a WebJar on the classpath.
      Parameters:
      groupId - The WebJar group id.
      artifactId - The WebJar artifact id.
      Returns:
      The version from the WebJar's pom.properties.
      Throws:
      IllegalStateException - If the WebJar isn't on the classpath or names no version.
    • resolvePath

      public static String resolvePath(String asset)
      Resolves an asset id to its file path under META-INF/resources/webjars/, checking that the file exists.
      Parameters:
      asset - The asset id (see asset(String, String, String)).
      Returns:
      The path with VERSION_TOKEN filled in.
      Throws:
      IllegalArgumentException - If the asset id is malformed.
      IllegalStateException - If the WebJar or the file isn't on the classpath.
    • resolve

      public static String resolve(RestRequest req, String asset)
      Resolves an asset id to an absolute, cache-busted URL under the WebJarsMixin mount of the request's servlet.
      Parameters:
      req - The in-flight request.
      asset - The asset id (see asset(String, String, String)).
      Returns:
      The URL.