class StaticResourceServlet extends HttpServlet {

    @Override
    protected void doGet(final HttpServletRequest req,
            final HttpServletResponse resp) throws ServletException,
            IOException {
        final File requestedFile = getRequestedFile(req);
        if (!requestedFile.exists() || !requestedFile.isFile()) {
            resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        final String etag = getEtag(requestedFile);
        if (etag.equals(req.getHeader("If-None-Match"))) {
            resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
            return;
        }
        // have to send data, either compressed or the original
        final File compressedStatic = getCompressedVersion(requestedFile);
        InputStream fileContent = null;
        try {
            resp.setContentType(getContentType(requestedFile.getName()));
            resp.setHeader("ETag", etag);
            resp.setStatus(HttpServletResponse.SC_OK);
            if (isClientCompressionSupported(req) && compressedStatic.exists()) {
                // gzip compressed
                resp.setHeader("Content-Encoding", "gzip");
                resp.setContentLength((int) compressedStatic.length());
                fileContent = new FileInputStream(compressedStatic);
            } else {
                // uncompressed
                resp.setContentLength((int) requestedFile.length());
                fileContent = new FileInputStream(requestedFile);
            }
            IOUtils.copy(fileContent, resp.getOutputStream());
        } finally {
            IOUtils.closeQuietly(fileContent);
        }
    }

}