class CacheHeadersFilter implements Filter {

    @Override
    public void doFilter(final ServletRequest request,
                         final ServletResponse response,
                         final FilterChain chain) throws IOException, ServletException {

        final HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        final HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        final String requestURI = httpServletRequest.getRequestURI();

        if (requestURI.contains(".cache.")) {
            final Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.SECOND,
                         YEAR_IN_SECONDS);

            httpServletResponse.setHeader(CACHE_CONTROL_HEADER,
                                          "max-age=" + YEAR_IN_SECONDS + ", must-revalidate");
            httpServletResponse.setHeader(PRAGMA_HEADER, "cache");
            httpServletResponse.setDateHeader(EXPIRES_HEADER,
                                              calendar.getTime().getTime());
        } else if (requestURI.endsWith(".nocache.js") || (requestURI.endsWith(".html"))) {
            httpServletResponse.setHeader(CACHE_CONTROL_HEADER,
                                          "no-cache, no-store, must-revalidate");
            httpServletResponse.setHeader(PRAGMA_HEADER,
                                          "no-cache");
            httpServletResponse.setDateHeader(EXPIRES_HEADER,
                                              0);
        }

        chain.doFilter(request,
                       response);
    }

}