Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.yammer.dropwizard.assets;
  
 
 
 import java.net.URL;
 
 public class AssetServlet extends HttpServlet {
     private static final long serialVersionUID = 6393345594784987908L;
 
     private static class CachedAsset {
         private final byte[] resource;
         private final String eTag;
         private final long lastModifiedTime;
 
         private CachedAsset(byte[] resourcelong lastModifiedTime) {
             this. = resource;
             this. = Hashing.murmur3_128().hashBytes(resource).toString();
             this. = lastModifiedTime;
         }
 
         public byte[] getResource() {
             return ;
         }
 
         public String getETag() {
             return ;
         }
 
         public long getLastModifiedTime() {
             return ;
         }
     }
 
     private static final MediaType DEFAULT_MEDIA_TYPE = .;
 
     private final String resourcePath;
     private final String uriPath;
     private final String indexFile;
 
     private final transient MimeTypes mimeTypes;
     
     private Charset defaultCharset = .;

    
Creates a new AssetServlet that serves static assets loaded from resourceURL (typically a file: or jar: URL). The assets are served at URIs rooted at uriPath. For example, given a resourceURL of "file:/data/assets" and a uriPath of "/js", an AssetServlet would serve the contents of /data/assets/example.js in response to a request for /js/example.js. If a directory is requested and indexFile is defined, then AssetServlet will attempt to serve a file with that name in that directory. If a directory is requested and indexFile is null, it will serve a 404.

Parameters:
resourcePath the base URL from which assets are loaded
uriPath the URI path fragment in which all requests are rooted
indexFile the filename to use when directories are requested, or null to serve no indexes
See also:
com.google.common.cache.CacheBuilderSpec
 
     public AssetServlet(String resourcePath,
                         String uriPath,
                         String indexFile) {
         final String trimmedPath = CharMatcher.is('/').trimFrom(resourcePath);
 	this. = trimmedPath.isEmpty() ? trimmedPath : trimmedPath + "/";
         final String trimmedUri = CharMatcher.is('/').trimTrailingFrom(uriPath);
         this. = trimmedUri.length() == 0 ? "/" : trimmedUri;
         this. = indexFile;
         this. = new MimeTypes();
     }
 
     public URL getResourceURL() {
         return Resources.getResource();
     }
 
     public String getUriPath() {
         return ;
     }
     
     public void setDefaultCharset(Charset defaultCharset) {
         this. = defaultCharset;
     }
 
     public Charset getDefaultCharset() {
         return this.;
    }
    public String getIndexFile() {
        return ;
    }
    @Override
    protected void doGet(HttpServletRequest req,
                         HttpServletResponse respthrows ServletExceptionIOException {
        try {
            final CachedAsset cachedAsset = loadAsset(req.getRequestURI());
            if (cachedAsset == null) {
                resp.sendError(.);
                return;
            }
            if (isCachedClientSide(reqcachedAsset)) {
                resp.sendError(.);
                return;
            }
            resp.setDateHeader(.cachedAsset.getLastModifiedTime());
            resp.setHeader(.cachedAsset.getETag());
            final Buffer mimeTypeOfExtension = .getMimeByExtension(req.getRequestURI());
            MediaType mediaType = ;
            
            if (mimeTypeOfExtension != null) {
                try {
                    mediaType = MediaType.parse(mimeTypeOfExtension.toString());
                    if ( != null && mediaType.is(.)) {
                        mediaType = mediaType.withCharset();
                    }
                }
                catch (IllegalArgumentException ignore) {}
            }
            
            resp.setContentType(mediaType.type() + "/" + mediaType.subtype());
            if (mediaType.charset().isPresent()) {
                resp.setCharacterEncoding(mediaType.charset().get().toString());
            }
            final ServletOutputStream output = resp.getOutputStream();
            try {
                output.write(cachedAsset.getResource());
            } finally {
                output.close();
            }
        } catch (RuntimeException ignored) {
            resp.sendError(.);
        } catch (URISyntaxException ignored) {
            resp.sendError(.);
        }
    }
    private CachedAsset loadAsset(String keythrows URISyntaxExceptionIOException {
        Preconditions.checkArgument(key.startsWith());
        final String requestedResourcePath = CharMatcher.is('/').trimFrom(key.substring(.length()));
        final String absoluteRequestedResourcePath = this. + requestedResourcePath;
        
        URL requestedResourceURL = Resources.getResource(absoluteRequestedResourcePath);
        if (ResourceURL.isDirectory(requestedResourceURL)) {
            if ( != null) {
                requestedResourceURL = Resources.getResource(absoluteRequestedResourcePath + '/' + );
            } else {
                // directory requested but no index file defined
                return null;
            }
        }
        long lastModified = ResourceURL.getLastModified(requestedResourceURL);
        if (lastModified < 1) {
            // Something went wrong trying to get the last modified time: just use the current time
            lastModified = System.currentTimeMillis();
        }
        // zero out the millis since the date we get back from If-Modified-Since will not have them
        lastModified = (lastModified / 1000) * 1000;
        return new CachedAsset(Resources.toByteArray(requestedResourceURL), lastModified);
    }
    private boolean isCachedClientSide(HttpServletRequest reqCachedAsset cachedAsset) {
        return cachedAsset.getETag().equals(req.getHeader(.)) ||
                (req.getDateHeader(.) >= cachedAsset.getLastModifiedTime());
    }
New to GrepCode? Check out our FAQ X