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); }  @Override void init(final FilterConfig filterConfig); @Override void destroy(); @Override void doFilter(final ServletRequest request,
                         final ServletResponse response,
                         final FilterChain chain);  }
@Test public void cacheFilesWithCacheExtension() throws Exception { when(request.getRequestURI()).thenReturn("/app/hash.cache.js"); final CacheHeadersFilter filter = new CacheHeadersFilter(); filter.doFilter(request, response, chain); verify(response).setHeader(CACHE_CONTROL_HEADER, "max-age=31536000, must-revalidate"); ArgumentCaptor<String> expiresHeader = ArgumentCaptor.forClass(String.class); ArgumentCaptor<Long> expiresValue = ArgumentCaptor.forClass(Long.class); verify(response).setDateHeader(expiresHeader.capture(), expiresValue.capture()); assertEquals(EXPIRES_HEADER, expiresHeader.getValue()); final Calendar expiryDate = Calendar.getInstance(); expiryDate.setTimeInMillis(expiresValue.getValue()); final Calendar now = Calendar.getInstance(); long expiryInDays = (expiryDate.getTimeInMillis() - now.getTimeInMillis()) / (1000 * 60 * 60 * 24); assertTrue(expiryInDays >= 364); }