Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package io.dropwizard.bundles.webjars;
A servlet that will load resources from WebJars found in the classpath. In order to make it more convenient to use a WebJar, this servlet will automatically determine the version of the WebJar present in the classpath and make it so that you don't need to explicitly specify a version number as part of the URL. This allows WebJars to be upgraded entirely via maven dependencies without having to update all of the references to the WebJar in your UI code.
 public class WebJarServlet extends HttpServlet {
The URL prefix that webjars are served out of.
   public static final String URL_PREFIX = "/webjars/";

The default maven group(s) that WebJars are searched for in.
   public static final String[] DEFAULT_MAVEN_GROUPS = {"org.webjars"};

A path parser that can determine the library and library resource a particular path is for.
   private static final Pattern PATH_PARSER = Pattern.compile( + "([^/]+)/(.+)");

An If-None-Match header parser, splits the header into the multiple ETags if present.
   private static final Splitter IF_NONE_MATCH_SPLITTER =
   private static final Logger LOG = LoggerFactory.getLogger(WebJarServlet.class);
   private final transient LoadingCache<AssetIdAssetcache;

Construct the actual servlet that this bundle will install.

builder The cache definition for our webjar bundle.
groups The allowed maven groups of webjars to look for and match against.
   public WebJarServlet(CacheBuilder builderIterable<Stringgroups) {
     if (builder == null) {
       builder = CacheBuilder.newBuilder()
           .maximumWeight(5 * 1024 * 1024)
           .expireAfterAccess(5, .);
     if (groups == null || Iterables.isEmpty(groups)) {
       groups = ImmutableList.copyOf();
     AssetLoader loader = new AssetLoader(new VersionLoader(groups));
      = builder.weigher(new AssetWeigher()).build(loader);
   protected void doGet(HttpServletRequest reqHttpServletResponse resp)
       throws ServletExceptionIOException {
     try {
     } catch (Exception e) {
       .info("Error processing request: {}"reqe);
   private void handle(HttpServletRequest reqHttpServletResponse respthrows IOException {
     String path = getFullPath(req);
     // Check to see if this is a valid path that we know how to deal with.
     // If so parse out the library and resource.
     Matcher match = .matcher(path);
     if (!match.matches()) {
     // The path is valid, try to load the asset.
     AssetId id = new AssetId(,;
     Asset asset = .getUnchecked(id);
     if (asset == .) {
     // We know we've found the asset. No matter what happens, make sure we send back its last
    // modification time as well as its ETag
    // Check the If-None-Match header to see if any ETags match this resource
    String ifNoneMatch = req.getHeader(.);
    if (ifNoneMatch != null) {
      for (String etag : .split(ifNoneMatch)) {
        if ("*".equals(etag) || asset.hash.equals(etag2hash(etag))) {
    // Check the If-Modified-Since header to see if this resource is newer
    if (asset.lastModifiedTime <= req.getDateHeader(.)) {
    // Send back the correct content type and character encoding headers
    if (asset.mediaType.charset().isPresent()) {
    // Finally write the bytes of the asset out
    ServletOutputStream output = resp.getOutputStream();
    try {
    } finally {
  private static String getFullPath(HttpServletRequest request) {
    StringBuilder sb = new StringBuilder(request.getServletPath());
    if (request.getPathInfo() != null) {
    return sb.toString();
  private static String hash2etag(String hash) {
    return new EntityTag(hash).toString();
  private static String etag2hash(String etag) {
    String hash;
    try {
      hash = EntityTag.valueOf(etag).getValue();
    } catch (Exception e) {
      return null;
    // Jetty insists on adding a -gzip suffix to ETags sometimes.  If it's there, then strip it off.
    if (hash.endsWith("-gzip")) {
      hash = hash.substring(0, hash.length() - 5);
    return hash;
New to GrepCode? Check out our FAQ X