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

@@ -54,10 +54,6 @@ public class HeaderAssertionTests {
private final long currentTime = System.currentTimeMillis();
private String currentDate;
private SimpleDateFormat dateFormat;
private MockMvc mockMvc;
private PersonController personController;
@@ -65,9 +61,6 @@ public class HeaderAssertionTests {
@Before
public void setup() {
this.dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
this.dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
this.currentDate = dateFormat.format(currentTime);
this.personController = new PersonController();
this.personController.setStubTimestamp(currentTime);
this.mockMvc = standaloneSetup(this.personController).build();
@@ -76,19 +69,19 @@ public class HeaderAssertionTests {
@Test
public void stringWithCorrectResponseHeaderValue() throws Exception {
this.mockMvc.perform(get("/persons/1").header(IF_MODIFIED_SINCE, currentTime - (1000 * 60)))//
.andExpect(header().string(LAST_MODIFIED, currentDate));
.andExpect(header().string(LAST_MODIFIED, String.valueOf(currentTime)));
}
@Test
public void stringWithMatcherAndCorrectResponseHeaderValue() throws Exception {
this.mockMvc.perform(get("/persons/1").header(IF_MODIFIED_SINCE, currentTime - (1000 * 60)))//
.andExpect(header().string(LAST_MODIFIED, equalTo(currentDate)));
.andExpect(header().string(LAST_MODIFIED, equalTo(String.valueOf(currentTime))));
}
@Test
public void longValueWithCorrectResponseHeaderValue() throws Exception {
this.mockMvc.perform(get("/persons/1").header(IF_MODIFIED_SINCE, currentTime - (1000 * 60)))//
.andExpect(header().string(LAST_MODIFIED, currentDate));
.andExpect(header().longValue(LAST_MODIFIED, currentTime));
}
@Test
@@ -141,20 +134,20 @@ public class HeaderAssertionTests {
@Test
public void stringWithIncorrectResponseHeaderValue() throws Exception {
long unexpected = currentTime + 1000;
assertIncorrectResponseHeaderValue(header().string(LAST_MODIFIED, dateFormat.format(unexpected)), unexpected);
assertIncorrectResponseHeaderValue(header().string(LAST_MODIFIED, String.valueOf(unexpected)), unexpected);
}
@Test
public void stringWithMatcherAndIncorrectResponseHeaderValue() throws Exception {
long unexpected = currentTime + 1000;
assertIncorrectResponseHeaderValue(header().string(LAST_MODIFIED, equalTo(dateFormat.format(unexpected))),
assertIncorrectResponseHeaderValue(header().string(LAST_MODIFIED, equalTo(String.valueOf(unexpected))),
unexpected);
}
@Test
public void longValueWithIncorrectResponseHeaderValue() throws Exception {
long unexpected = currentTime + 1000;
assertIncorrectResponseHeaderValue(header().string(LAST_MODIFIED, dateFormat.format(unexpected)), unexpected);
assertIncorrectResponseHeaderValue(header().longValue(LAST_MODIFIED, unexpected), unexpected);
}
private void assertIncorrectResponseHeaderValue(ResultMatcher resultMatcher, long unexpected) throws Exception {
@@ -173,8 +166,8 @@ public class HeaderAssertionTests {
// We don't use assertEquals() since we cannot control the formatting
// produced by JUnit or Hamcrest.
assertMessageContains(e, "Response header " + LAST_MODIFIED);
assertMessageContains(e, dateFormat.format(unexpected));
assertMessageContains(e, currentDate);
assertMessageContains(e, String.valueOf(unexpected));
assertMessageContains(e, String.valueOf(currentTime));
}
}

View File

@@ -22,7 +22,6 @@ import java.util.Date;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -58,10 +57,6 @@ public class ServletWebRequest extends ServletRequestAttributes implements Nativ
private static final String METHOD_HEAD = "HEAD";
private static final String DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss zzz";
private static TimeZone GMT = TimeZone.getTimeZone("GMT");
private boolean notModified = false;
@@ -188,7 +183,7 @@ public class ServletWebRequest extends ServletRequestAttributes implements Nativ
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
}
if(response.getHeader(HEADER_LAST_MODIFIED) == null) {
response.setHeader(HEADER_LAST_MODIFIED, formatDate(lastModifiedTimestamp));
response.setDateHeader(HEADER_LAST_MODIFIED, lastModifiedTimestamp);
}
}
}
@@ -284,7 +279,7 @@ public class ServletWebRequest extends ServletRequestAttributes implements Nativ
response.setHeader(HEADER_ETAG, etag);
}
if(response.getHeader(HEADER_LAST_MODIFIED) == null) {
response.setHeader(HEADER_LAST_MODIFIED, formatDate(lastModifiedTimestamp));
response.setDateHeader(HEADER_LAST_MODIFIED, lastModifiedTimestamp);
}
}
}
@@ -292,12 +287,6 @@ public class ServletWebRequest extends ServletRequestAttributes implements Nativ
return this.notModified;
}
private String formatDate(long date) {
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.US);
dateFormat.setTimeZone(GMT);
return dateFormat.format(new Date(date));
}
public boolean isNotModified() {
return this.notModified;
}

View File

@@ -107,7 +107,7 @@ public class ServletWebRequestHttpMethodsTests {
assertTrue(request.checkNotModified(epochTime));
assertEquals(304, servletResponse.getStatus());
assertEquals(dateFormat.format(currentDate), servletResponse.getHeader("Last-Modified"));
assertEquals("" + epochTime, servletResponse.getHeader("Last-Modified"));
}
@Test
@@ -118,7 +118,7 @@ public class ServletWebRequestHttpMethodsTests {
assertFalse(request.checkNotModified(currentDate.getTime()));
assertEquals(200, servletResponse.getStatus());
assertEquals(dateFormat.format(currentDate), servletResponse.getHeader("Last-Modified"));
assertEquals("" + currentDate.getTime(), servletResponse.getHeader("Last-Modified"));
}
@Test
@@ -189,7 +189,7 @@ public class ServletWebRequestHttpMethodsTests {
assertEquals(304, servletResponse.getStatus());
assertEquals(eTag, servletResponse.getHeader("ETag"));
assertEquals(dateFormat.format(currentDate), servletResponse.getHeader("Last-Modified"));
assertEquals("" + currentDate.getTime(), servletResponse.getHeader("Last-Modified"));
}
@Test
@@ -204,7 +204,7 @@ public class ServletWebRequestHttpMethodsTests {
assertEquals(200, servletResponse.getStatus());
assertEquals(eTag, servletResponse.getHeader("ETag"));
assertEquals(dateFormat.format(currentDate), servletResponse.getHeader("Last-Modified"));
assertEquals("" + currentEpoch, servletResponse.getHeader("Last-Modified"));
}
@Test
@@ -219,7 +219,7 @@ public class ServletWebRequestHttpMethodsTests {
assertEquals(200, servletResponse.getStatus());
assertEquals(currentETag, servletResponse.getHeader("ETag"));
assertEquals(dateFormat.format(currentDate), servletResponse.getHeader("Last-Modified"));
assertEquals("" + epochTime, servletResponse.getHeader("Last-Modified"));
}
@Test
@@ -266,7 +266,7 @@ public class ServletWebRequestHttpMethodsTests {
assertTrue(request.checkNotModified(epochTime));
assertEquals(304, servletResponse.getStatus());
assertEquals(CURRENT_TIME, servletResponse.getHeader("Last-Modified"));
assertEquals("" + epochTime, servletResponse.getHeader("Last-Modified"));
}
@Test
@@ -278,7 +278,7 @@ public class ServletWebRequestHttpMethodsTests {
assertFalse(request.checkNotModified(epochTime));
assertEquals(200, servletResponse.getStatus());
assertEquals(CURRENT_TIME, servletResponse.getHeader("Last-Modified"));
assertEquals("" + epochTime, servletResponse.getHeader("Last-Modified"));
}
}

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 {