Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.yammer.dropwizard.servlets;
 
 
public class AssetServlet extends HttpServlet {
    private static final long serialVersionUID = 6393345594784987908L;
    private static class AssetLoader extends CacheLoader<Stringbyte[]> {
        private static final String INDEX_FILENAME = "index.htm"// TODO: Make this configurable.
        private final String resourcePath;
        private final String uriPath;
        private AssetLoader(String resourcePathString uriPath) {
            this. = resourcePath;
            this. = uriPath;
        }
        @Override
        public byte[] load(String keythrows Exception {
            final String resource = key.substring(.length());
            String fullResourcePath = this. + resource;
            if (key.equals(this.)) {
                fullResourcePath =  + ;
            }
            return Resources.toByteArray(Resources.getResource(fullResourcePath.substring(1)));
        }
    }
    private static final String DEFAULT_MIME_TYPE = "text/html";
    private final transient LoadingCache<Stringbyte[]> cache;
    private final transient MimeTypes mimeTypes;
    public AssetServlet(String resourcePathint maxCacheSizeString uriPath) {
        this. = buildCache(resourcePathmaxCacheSizeuriPath);
        this. = new MimeTypes();
    }
    private static LoadingCache<Stringbyte[]> buildCache(String resourcePathint maxCacheSizeString uriPath) {
        return CacheBuilder.newBuilder()
                           .maximumSize(maxCacheSize)
                           .build(new AssetLoader(resourcePathuriPath));
    }
    @Override
    protected void doGet(HttpServletRequest req,
                         HttpServletResponse respthrows ServletExceptionIOException {
        try {
            final byte[] resource = .getUnchecked(req.getRequestURI());
            final Buffer mimeType = .getMimeByExtension(req.getRequestURI());
            if (mimeType == null) {
                resp.setContentType();
            } else {
                resp.setContentType(mimeType.toString());
            }
            final ServletOutputStream output = resp.getOutputStream();
            try {
                output.write(resource);
            } finally {
                output.close();
            }
        } catch (RuntimeException ignored) {
            resp.sendError(.);
        }
    }
New to GrepCode? Check out our FAQ X