@Test public void testLastModifiedAndExpiresHeadersForVersionThatIsNotSnapshot() throws IOException, ServletException { when(mockEnvironment.getProperty("dummyBuildTimePropertyKey")).thenReturn("2009-07-27 14:45:28 -0600"); when(mockEnvironment.getProperty("dummyVersionPropertyKey")).thenReturn("bar-1.1"); MockFilterConfig config = new MockFilterConfig(servletContext); config.addInitParameter("buildTimePropertyKey", "dummyBuildTimePropertyKey"); config.addInitParameter("versionPropertyKey", "dummyVersionPropertyKey"); f.init(config); f.doFilter(request, response, filterChain); assertThat(response.getHeader("Last-Modified"), equalTo("1248727528000")); assertThat(response.getHeader("Expires"), equalTo(new Long(1248727528000L + 1000L * 60L * 60L * 24L * 365L).toString())); }
public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException { HttpServletResponse response = (HttpServletResponse) res; Long buildTimeMillis = null; String buildTimeString = WebApplicationContextUtils.getWebApplicationContext(getServletContext()).getEnvironment().getProperty(getBuildTimePropertyKey()); if (StringUtils.hasText(buildTimeString)) { try { buildTimeMillis = dateFormat.parseMillis(buildTimeString); response.setDateHeader(LAST_MODIFIED_HTTP_RESPONSE_HEADER, buildTimeMillis); } catch (IllegalArgumentException e) { LOG.warn("Unable to set {} header because '{}' didn't match format '{}': {}", new Object[]{LAST_MODIFIED_HTTP_RESPONSE_HEADER, buildTimeString, buildTimeDateFormatKey, e.getMessage()}); } } else { LOG.warn("Unable to set {} header because '{}' property from Spring Environment is empty.", LAST_MODIFIED_HTTP_RESPONSE_HEADER, getBuildTimePropertyKey()); } String versionString = WebApplicationContextUtils.getWebApplicationContext(getServletContext()).getEnvironment().getProperty(getVersionPropertyKey()); if (StringUtils.hasText(versionString) && isSnapshotBuild(versionString)) { response.setDateHeader(EXPIRES_HTTP_RESPONSE_HEADER, 0); } else { if (buildTimeMillis != null) { response.setDateHeader(EXPIRES_HTTP_RESPONSE_HEADER, buildTimeMillis + MILLISECONDS_IN_ONE_YEAR); } else { LOG.warn("Unable to set {} header because '{}' property from Spring Environment is malformed or missing and build time is unknown.", EXPIRES_HTTP_RESPONSE_HEADER, getBuildTimePropertyKey()); } } filterChain.doFilter(req, response); }
BuildTimeLastModifiedResponseHeaderFilter extends GenericFilterBean { public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException { HttpServletResponse response = (HttpServletResponse) res; Long buildTimeMillis = null; String buildTimeString = WebApplicationContextUtils.getWebApplicationContext(getServletContext()).getEnvironment().getProperty(getBuildTimePropertyKey()); if (StringUtils.hasText(buildTimeString)) { try { buildTimeMillis = dateFormat.parseMillis(buildTimeString); response.setDateHeader(LAST_MODIFIED_HTTP_RESPONSE_HEADER, buildTimeMillis); } catch (IllegalArgumentException e) { LOG.warn("Unable to set {} header because '{}' didn't match format '{}': {}", new Object[]{LAST_MODIFIED_HTTP_RESPONSE_HEADER, buildTimeString, buildTimeDateFormatKey, e.getMessage()}); } } else { LOG.warn("Unable to set {} header because '{}' property from Spring Environment is empty.", LAST_MODIFIED_HTTP_RESPONSE_HEADER, getBuildTimePropertyKey()); } String versionString = WebApplicationContextUtils.getWebApplicationContext(getServletContext()).getEnvironment().getProperty(getVersionPropertyKey()); if (StringUtils.hasText(versionString) && isSnapshotBuild(versionString)) { response.setDateHeader(EXPIRES_HTTP_RESPONSE_HEADER, 0); } else { if (buildTimeMillis != null) { response.setDateHeader(EXPIRES_HTTP_RESPONSE_HEADER, buildTimeMillis + MILLISECONDS_IN_ONE_YEAR); } else { LOG.warn("Unable to set {} header because '{}' property from Spring Environment is malformed or missing and build time is unknown.", EXPIRES_HTTP_RESPONSE_HEADER, getBuildTimePropertyKey()); } } filterChain.doFilter(req, response); } }
BuildTimeLastModifiedResponseHeaderFilter extends GenericFilterBean { public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException { HttpServletResponse response = (HttpServletResponse) res; Long buildTimeMillis = null; String buildTimeString = WebApplicationContextUtils.getWebApplicationContext(getServletContext()).getEnvironment().getProperty(getBuildTimePropertyKey()); if (StringUtils.hasText(buildTimeString)) { try { buildTimeMillis = dateFormat.parseMillis(buildTimeString); response.setDateHeader(LAST_MODIFIED_HTTP_RESPONSE_HEADER, buildTimeMillis); } catch (IllegalArgumentException e) { LOG.warn("Unable to set {} header because '{}' didn't match format '{}': {}", new Object[]{LAST_MODIFIED_HTTP_RESPONSE_HEADER, buildTimeString, buildTimeDateFormatKey, e.getMessage()}); } } else { LOG.warn("Unable to set {} header because '{}' property from Spring Environment is empty.", LAST_MODIFIED_HTTP_RESPONSE_HEADER, getBuildTimePropertyKey()); } String versionString = WebApplicationContextUtils.getWebApplicationContext(getServletContext()).getEnvironment().getProperty(getVersionPropertyKey()); if (StringUtils.hasText(versionString) && isSnapshotBuild(versionString)) { response.setDateHeader(EXPIRES_HTTP_RESPONSE_HEADER, 0); } else { if (buildTimeMillis != null) { response.setDateHeader(EXPIRES_HTTP_RESPONSE_HEADER, buildTimeMillis + MILLISECONDS_IN_ONE_YEAR); } else { LOG.warn("Unable to set {} header because '{}' property from Spring Environment is malformed or missing and build time is unknown.", EXPIRES_HTTP_RESPONSE_HEADER, getBuildTimePropertyKey()); } } filterChain.doFilter(req, response); }  }
BuildTimeLastModifiedResponseHeaderFilter extends GenericFilterBean { public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException { HttpServletResponse response = (HttpServletResponse) res; Long buildTimeMillis = null; String buildTimeString = WebApplicationContextUtils.getWebApplicationContext(getServletContext()).getEnvironment().getProperty(getBuildTimePropertyKey()); if (StringUtils.hasText(buildTimeString)) { try { buildTimeMillis = dateFormat.parseMillis(buildTimeString); response.setDateHeader(LAST_MODIFIED_HTTP_RESPONSE_HEADER, buildTimeMillis); } catch (IllegalArgumentException e) { LOG.warn("Unable to set {} header because '{}' didn't match format '{}': {}", new Object[]{LAST_MODIFIED_HTTP_RESPONSE_HEADER, buildTimeString, buildTimeDateFormatKey, e.getMessage()}); } } else { LOG.warn("Unable to set {} header because '{}' property from Spring Environment is empty.", LAST_MODIFIED_HTTP_RESPONSE_HEADER, getBuildTimePropertyKey()); } String versionString = WebApplicationContextUtils.getWebApplicationContext(getServletContext()).getEnvironment().getProperty(getVersionPropertyKey()); if (StringUtils.hasText(versionString) && isSnapshotBuild(versionString)) { response.setDateHeader(EXPIRES_HTTP_RESPONSE_HEADER, 0); } else { if (buildTimeMillis != null) { response.setDateHeader(EXPIRES_HTTP_RESPONSE_HEADER, buildTimeMillis + MILLISECONDS_IN_ONE_YEAR); } else { LOG.warn("Unable to set {} header because '{}' property from Spring Environment is malformed or missing and build time is unknown.", EXPIRES_HTTP_RESPONSE_HEADER, getBuildTimePropertyKey()); } } filterChain.doFilter(req, response); }  void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain); String getBuildTimePropertyKey(); void setBuildTimePropertyKey(String buildTimePropertyKey); String getVersionPropertyKey(); void setVersionPropertyKey(String versionPropertyKey); String getBuildTimeDateFormatKey(); void setBuildTimeDateFormatKey(String buildTimeDateFormatKey); String getBuildTimeDateFormat(); }
BuildTimeLastModifiedResponseHeaderFilter extends GenericFilterBean { public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException { HttpServletResponse response = (HttpServletResponse) res; Long buildTimeMillis = null; String buildTimeString = WebApplicationContextUtils.getWebApplicationContext(getServletContext()).getEnvironment().getProperty(getBuildTimePropertyKey()); if (StringUtils.hasText(buildTimeString)) { try { buildTimeMillis = dateFormat.parseMillis(buildTimeString); response.setDateHeader(LAST_MODIFIED_HTTP_RESPONSE_HEADER, buildTimeMillis); } catch (IllegalArgumentException e) { LOG.warn("Unable to set {} header because '{}' didn't match format '{}': {}", new Object[]{LAST_MODIFIED_HTTP_RESPONSE_HEADER, buildTimeString, buildTimeDateFormatKey, e.getMessage()}); } } else { LOG.warn("Unable to set {} header because '{}' property from Spring Environment is empty.", LAST_MODIFIED_HTTP_RESPONSE_HEADER, getBuildTimePropertyKey()); } String versionString = WebApplicationContextUtils.getWebApplicationContext(getServletContext()).getEnvironment().getProperty(getVersionPropertyKey()); if (StringUtils.hasText(versionString) && isSnapshotBuild(versionString)) { response.setDateHeader(EXPIRES_HTTP_RESPONSE_HEADER, 0); } else { if (buildTimeMillis != null) { response.setDateHeader(EXPIRES_HTTP_RESPONSE_HEADER, buildTimeMillis + MILLISECONDS_IN_ONE_YEAR); } else { LOG.warn("Unable to set {} header because '{}' property from Spring Environment is malformed or missing and build time is unknown.", EXPIRES_HTTP_RESPONSE_HEADER, getBuildTimePropertyKey()); } } filterChain.doFilter(req, response); }  void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain); String getBuildTimePropertyKey(); void setBuildTimePropertyKey(String buildTimePropertyKey); String getVersionPropertyKey(); void setVersionPropertyKey(String versionPropertyKey); String getBuildTimeDateFormatKey(); void setBuildTimeDateFormatKey(String buildTimeDateFormatKey); String getBuildTimeDateFormat();  }