Partial revert of SPR-13090

Use ServletHttpResponse.setDateHeader whenever possible and avoid using
SimpleDateFormat.
This commit is contained in:
Brian Clozel
2015-07-20 22:48:20 +02:00
parent 19fcb72d70
commit dba46c1358
6 changed files with 23 additions and 52 deletions

View File

@@ -94,8 +94,6 @@ public abstract class WebContentGenerator extends WebApplicationObjectSupport {
private boolean usePreviousHttpCachingBehavior = false;
private final SimpleDateFormat dateFormat;
private CacheControl cacheControl;
@@ -120,8 +118,6 @@ public abstract class WebContentGenerator extends WebApplicationObjectSupport {
this.supportedMethods.add(METHOD_HEAD);
this.supportedMethods.add(METHOD_POST);
}
dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
}
/**
@@ -130,8 +126,6 @@ public abstract class WebContentGenerator extends WebApplicationObjectSupport {
*/
public WebContentGenerator(String... supportedMethods) {
this.supportedMethods = new HashSet<String>(Arrays.asList(supportedMethods));
dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
}
@@ -426,7 +420,7 @@ public abstract class WebContentGenerator extends WebApplicationObjectSupport {
protected final void cacheForSeconds(HttpServletResponse response, int seconds, boolean mustRevalidate) {
if (this.useExpiresHeader) {
// HTTP 1.0 header
response.setHeader(HEADER_EXPIRES, dateFormat.format(System.currentTimeMillis() + seconds * 1000L));
response.setDateHeader(HEADER_EXPIRES, System.currentTimeMillis() + seconds * 1000L);
}
if (this.useCacheControlHeader) {
// HTTP 1.1 header
@@ -446,7 +440,7 @@ public abstract class WebContentGenerator extends WebApplicationObjectSupport {
response.setHeader(HEADER_PRAGMA, "no-cache");
if (this.useExpiresHeader) {
// HTTP 1.0 header
response.setHeader(HEADER_EXPIRES, dateFormat.format(System.currentTimeMillis()));
response.setDateHeader(HEADER_EXPIRES, System.currentTimeMillis());
}
if (this.useCacheControlHeader) {
// HTTP 1.1 header: "no-cache" is the standard value,

View File

@@ -177,7 +177,7 @@ public class DispatcherServletTests extends TestCase {
MockHttpServletResponse response = new MockHttpServletResponse();
simpleDispatcherServlet.service(request, response);
assertTrue("Not forwarded", response.getForwardedUrl() == null);
assertEquals("Wed, 01 Apr 2015 00:00:00 GMT", response.getHeader("Last-Modified"));
assertEquals("1427846400000", response.getHeader("Last-Modified"));
}
public void testUnknownRequest() throws Exception {
@@ -205,7 +205,7 @@ public class DispatcherServletTests extends TestCase {
assertTrue(request.getAttribute("test3") != null);
assertTrue(request.getAttribute("test3x") != null);
assertTrue(request.getAttribute("test3y") != null);
assertEquals("Wed, 01 Apr 2015 00:00:01 GMT", response.getHeader("Last-Modified"));
assertEquals("1427846401000", response.getHeader("Last-Modified"));
}
public void testExistingMultipartRequest() throws Exception {

View File

@@ -56,8 +56,6 @@ import org.springframework.web.servlet.HandlerMapping;
*/
public class ResourceHttpRequestHandlerTests {
private SimpleDateFormat dateFormat;
private ResourceHttpRequestHandler handler;
private MockHttpServletRequest request;
@@ -67,9 +65,6 @@ public class ResourceHttpRequestHandlerTests {
@Before
public void setUp() throws Exception {
dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
List<Resource> paths = new ArrayList<>(2);
paths.add(new ClassPathResource("test/", getClass()));
paths.add(new ClassPathResource("testalternatepath/", getClass()));
@@ -136,7 +131,7 @@ public class ResourceHttpRequestHandlerTests {
assertEquals("no-cache", this.response.getHeader("Pragma"));
assertThat(this.response.getHeaderValues("Cache-Control"), Matchers.contains("no-cache", "no-store"));
assertEquals(this.response.getHeaderValue("Expires"), dateFormat.format(System.currentTimeMillis()));
assertTrue(headerAsLong("Expires") <= System.currentTimeMillis());
assertTrue(this.response.containsHeader("Last-Modified"));
assertEquals(headerAsLong("Last-Modified") / 1000, resourceLastModified("test/foo.css") / 1000);
}
@@ -477,7 +472,7 @@ public class ResourceHttpRequestHandlerTests {
private long headerAsLong(String responseHeaderName) throws Exception {
return dateFormat.parse(this.response.getHeader(responseHeaderName)).getTime();
return Long.valueOf(this.response.getHeader(responseHeaderName));
}
private long resourceLastModified(String resourceName) throws IOException {