Skip to content

Commit dba46c1

Browse files
committed
Partial revert of SPR-13090
Use ServletHttpResponse.setDateHeader whenever possible and avoid using SimpleDateFormat.
1 parent 19fcb72 commit dba46c1

File tree

6 files changed

+23
-52
lines changed

6 files changed

+23
-52
lines changed

spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/HeaderAssertionTests.java

+8-15
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,13 @@ public class HeaderAssertionTests {
5454

5555
private final long currentTime = System.currentTimeMillis();
5656

57-
private String currentDate;
58-
59-
private SimpleDateFormat dateFormat;
60-
6157
private MockMvc mockMvc;
6258

6359
private PersonController personController;
6460

6561

6662
@Before
6763
public void setup() {
68-
this.dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
69-
this.dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
70-
this.currentDate = dateFormat.format(currentTime);
7164
this.personController = new PersonController();
7265
this.personController.setStubTimestamp(currentTime);
7366
this.mockMvc = standaloneSetup(this.personController).build();
@@ -76,19 +69,19 @@ public void setup() {
7669
@Test
7770
public void stringWithCorrectResponseHeaderValue() throws Exception {
7871
this.mockMvc.perform(get("/persons/1").header(IF_MODIFIED_SINCE, currentTime - (1000 * 60)))//
79-
.andExpect(header().string(LAST_MODIFIED, currentDate));
72+
.andExpect(header().string(LAST_MODIFIED, String.valueOf(currentTime)));
8073
}
8174

8275
@Test
8376
public void stringWithMatcherAndCorrectResponseHeaderValue() throws Exception {
8477
this.mockMvc.perform(get("/persons/1").header(IF_MODIFIED_SINCE, currentTime - (1000 * 60)))//
85-
.andExpect(header().string(LAST_MODIFIED, equalTo(currentDate)));
78+
.andExpect(header().string(LAST_MODIFIED, equalTo(String.valueOf(currentTime))));
8679
}
8780

8881
@Test
8982
public void longValueWithCorrectResponseHeaderValue() throws Exception {
9083
this.mockMvc.perform(get("/persons/1").header(IF_MODIFIED_SINCE, currentTime - (1000 * 60)))//
91-
.andExpect(header().string(LAST_MODIFIED, currentDate));
84+
.andExpect(header().longValue(LAST_MODIFIED, currentTime));
9285
}
9386

9487
@Test
@@ -141,20 +134,20 @@ public void doesNotExistFail() throws Exception {
141134
@Test
142135
public void stringWithIncorrectResponseHeaderValue() throws Exception {
143136
long unexpected = currentTime + 1000;
144-
assertIncorrectResponseHeaderValue(header().string(LAST_MODIFIED, dateFormat.format(unexpected)), unexpected);
137+
assertIncorrectResponseHeaderValue(header().string(LAST_MODIFIED, String.valueOf(unexpected)), unexpected);
145138
}
146139

147140
@Test
148141
public void stringWithMatcherAndIncorrectResponseHeaderValue() throws Exception {
149142
long unexpected = currentTime + 1000;
150-
assertIncorrectResponseHeaderValue(header().string(LAST_MODIFIED, equalTo(dateFormat.format(unexpected))),
143+
assertIncorrectResponseHeaderValue(header().string(LAST_MODIFIED, equalTo(String.valueOf(unexpected))),
151144
unexpected);
152145
}
153146

154147
@Test
155148
public void longValueWithIncorrectResponseHeaderValue() throws Exception {
156149
long unexpected = currentTime + 1000;
157-
assertIncorrectResponseHeaderValue(header().string(LAST_MODIFIED, dateFormat.format(unexpected)), unexpected);
150+
assertIncorrectResponseHeaderValue(header().longValue(LAST_MODIFIED, unexpected), unexpected);
158151
}
159152

160153
private void assertIncorrectResponseHeaderValue(ResultMatcher resultMatcher, long unexpected) throws Exception {
@@ -173,8 +166,8 @@ private void assertIncorrectResponseHeaderValue(ResultMatcher resultMatcher, lon
173166
// We don't use assertEquals() since we cannot control the formatting
174167
// produced by JUnit or Hamcrest.
175168
assertMessageContains(e, "Response header " + LAST_MODIFIED);
176-
assertMessageContains(e, dateFormat.format(unexpected));
177-
assertMessageContains(e, currentDate);
169+
assertMessageContains(e, String.valueOf(unexpected));
170+
assertMessageContains(e, String.valueOf(currentTime));
178171
}
179172
}
180173

spring-web/src/main/java/org/springframework/web/context/request/ServletWebRequest.java

+2-13
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.Iterator;
2323
import java.util.Locale;
2424
import java.util.Map;
25-
import java.util.TimeZone;
2625

2726
import javax.servlet.http.HttpServletRequest;
2827
import javax.servlet.http.HttpServletResponse;
@@ -58,10 +57,6 @@ public class ServletWebRequest extends ServletRequestAttributes implements Nativ
5857

5958
private static final String METHOD_HEAD = "HEAD";
6059

61-
private static final String DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss zzz";
62-
63-
private static TimeZone GMT = TimeZone.getTimeZone("GMT");
64-
6560

6661
private boolean notModified = false;
6762

@@ -188,7 +183,7 @@ public boolean checkNotModified(long lastModifiedTimestamp) {
188183
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
189184
}
190185
if(response.getHeader(HEADER_LAST_MODIFIED) == null) {
191-
response.setHeader(HEADER_LAST_MODIFIED, formatDate(lastModifiedTimestamp));
186+
response.setDateHeader(HEADER_LAST_MODIFIED, lastModifiedTimestamp);
192187
}
193188
}
194189
}
@@ -284,20 +279,14 @@ public boolean checkNotModified(String etag, long lastModifiedTimestamp) {
284279
response.setHeader(HEADER_ETAG, etag);
285280
}
286281
if(response.getHeader(HEADER_LAST_MODIFIED) == null) {
287-
response.setHeader(HEADER_LAST_MODIFIED, formatDate(lastModifiedTimestamp));
282+
response.setDateHeader(HEADER_LAST_MODIFIED, lastModifiedTimestamp);
288283
}
289284
}
290285
}
291286
}
292287
return this.notModified;
293288
}
294289

295-
private String formatDate(long date) {
296-
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.US);
297-
dateFormat.setTimeZone(GMT);
298-
return dateFormat.format(new Date(date));
299-
}
300-
301290
public boolean isNotModified() {
302291
return this.notModified;
303292
}

spring-web/src/test/java/org/springframework/web/context/request/ServletWebRequestHttpMethodsTests.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public void checkNotModifiedTimestamp() throws Exception {
107107
assertTrue(request.checkNotModified(epochTime));
108108

109109
assertEquals(304, servletResponse.getStatus());
110-
assertEquals(dateFormat.format(currentDate), servletResponse.getHeader("Last-Modified"));
110+
assertEquals("" + epochTime, servletResponse.getHeader("Last-Modified"));
111111
}
112112

113113
@Test
@@ -118,7 +118,7 @@ public void checkModifiedTimestamp() {
118118
assertFalse(request.checkNotModified(currentDate.getTime()));
119119

120120
assertEquals(200, servletResponse.getStatus());
121-
assertEquals(dateFormat.format(currentDate), servletResponse.getHeader("Last-Modified"));
121+
assertEquals("" + currentDate.getTime(), servletResponse.getHeader("Last-Modified"));
122122
}
123123

124124
@Test
@@ -189,7 +189,7 @@ public void checkNotModifiedETagAndTimestamp() {
189189

190190
assertEquals(304, servletResponse.getStatus());
191191
assertEquals(eTag, servletResponse.getHeader("ETag"));
192-
assertEquals(dateFormat.format(currentDate), servletResponse.getHeader("Last-Modified"));
192+
assertEquals("" + currentDate.getTime(), servletResponse.getHeader("Last-Modified"));
193193
}
194194

195195
@Test
@@ -204,7 +204,7 @@ public void checkNotModifiedETagAndModifiedTimestamp() {
204204

205205
assertEquals(200, servletResponse.getStatus());
206206
assertEquals(eTag, servletResponse.getHeader("ETag"));
207-
assertEquals(dateFormat.format(currentDate), servletResponse.getHeader("Last-Modified"));
207+
assertEquals("" + currentEpoch, servletResponse.getHeader("Last-Modified"));
208208
}
209209

210210
@Test
@@ -219,7 +219,7 @@ public void checkModifiedETagAndNotModifiedTimestamp() throws Exception {
219219

220220
assertEquals(200, servletResponse.getStatus());
221221
assertEquals(currentETag, servletResponse.getHeader("ETag"));
222-
assertEquals(dateFormat.format(currentDate), servletResponse.getHeader("Last-Modified"));
222+
assertEquals("" + epochTime, servletResponse.getHeader("Last-Modified"));
223223
}
224224

225225
@Test
@@ -266,7 +266,7 @@ public void checkNotModifiedTimestampWithLengthPart() throws Exception {
266266
assertTrue(request.checkNotModified(epochTime));
267267

268268
assertEquals(304, servletResponse.getStatus());
269-
assertEquals(CURRENT_TIME, servletResponse.getHeader("Last-Modified"));
269+
assertEquals("" + epochTime, servletResponse.getHeader("Last-Modified"));
270270
}
271271

272272
@Test
@@ -278,7 +278,7 @@ public void checkModifiedTimestampWithLengthPart() throws Exception {
278278
assertFalse(request.checkNotModified(epochTime));
279279

280280
assertEquals(200, servletResponse.getStatus());
281-
assertEquals(CURRENT_TIME, servletResponse.getHeader("Last-Modified"));
281+
assertEquals("" + epochTime, servletResponse.getHeader("Last-Modified"));
282282
}
283283

284284
}

spring-webmvc/src/main/java/org/springframework/web/servlet/support/WebContentGenerator.java

+2-8
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@ public abstract class WebContentGenerator extends WebApplicationObjectSupport {
9494

9595
private boolean usePreviousHttpCachingBehavior = false;
9696

97-
private final SimpleDateFormat dateFormat;
98-
9997
private CacheControl cacheControl;
10098

10199

@@ -120,8 +118,6 @@ public WebContentGenerator(boolean restrictDefaultSupportedMethods) {
120118
this.supportedMethods.add(METHOD_HEAD);
121119
this.supportedMethods.add(METHOD_POST);
122120
}
123-
dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
124-
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
125121
}
126122

127123
/**
@@ -130,8 +126,6 @@ public WebContentGenerator(boolean restrictDefaultSupportedMethods) {
130126
*/
131127
public WebContentGenerator(String... supportedMethods) {
132128
this.supportedMethods = new HashSet<String>(Arrays.asList(supportedMethods));
133-
dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
134-
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
135129
}
136130

137131

@@ -426,7 +420,7 @@ else if (cacheSeconds == 0) {
426420
protected final void cacheForSeconds(HttpServletResponse response, int seconds, boolean mustRevalidate) {
427421
if (this.useExpiresHeader) {
428422
// HTTP 1.0 header
429-
response.setHeader(HEADER_EXPIRES, dateFormat.format(System.currentTimeMillis() + seconds * 1000L));
423+
response.setDateHeader(HEADER_EXPIRES, System.currentTimeMillis() + seconds * 1000L);
430424
}
431425
if (this.useCacheControlHeader) {
432426
// HTTP 1.1 header
@@ -446,7 +440,7 @@ protected final void preventCaching(HttpServletResponse response) {
446440
response.setHeader(HEADER_PRAGMA, "no-cache");
447441
if (this.useExpiresHeader) {
448442
// HTTP 1.0 header
449-
response.setHeader(HEADER_EXPIRES, dateFormat.format(System.currentTimeMillis()));
443+
response.setDateHeader(HEADER_EXPIRES, System.currentTimeMillis());
450444
}
451445
if (this.useCacheControlHeader) {
452446
// HTTP 1.1 header: "no-cache" is the standard value,

spring-webmvc/src/test/java/org/springframework/web/servlet/DispatcherServletTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public void testLocaleRequest() throws Exception {
177177
MockHttpServletResponse response = new MockHttpServletResponse();
178178
simpleDispatcherServlet.service(request, response);
179179
assertTrue("Not forwarded", response.getForwardedUrl() == null);
180-
assertEquals("Wed, 01 Apr 2015 00:00:00 GMT", response.getHeader("Last-Modified"));
180+
assertEquals("1427846400000", response.getHeader("Last-Modified"));
181181
}
182182

183183
public void testUnknownRequest() throws Exception {
@@ -205,7 +205,7 @@ public void testAnotherLocaleRequest() throws Exception {
205205
assertTrue(request.getAttribute("test3") != null);
206206
assertTrue(request.getAttribute("test3x") != null);
207207
assertTrue(request.getAttribute("test3y") != null);
208-
assertEquals("Wed, 01 Apr 2015 00:00:01 GMT", response.getHeader("Last-Modified"));
208+
assertEquals("1427846401000", response.getHeader("Last-Modified"));
209209
}
210210

211211
public void testExistingMultipartRequest() throws Exception {

spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java

+2-7
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@
5656
*/
5757
public class ResourceHttpRequestHandlerTests {
5858

59-
private SimpleDateFormat dateFormat;
60-
6159
private ResourceHttpRequestHandler handler;
6260

6361
private MockHttpServletRequest request;
@@ -67,9 +65,6 @@ public class ResourceHttpRequestHandlerTests {
6765

6866
@Before
6967
public void setUp() throws Exception {
70-
dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
71-
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
72-
7368
List<Resource> paths = new ArrayList<>(2);
7469
paths.add(new ClassPathResource("test/", getClass()));
7570
paths.add(new ClassPathResource("testalternatepath/", getClass()));
@@ -136,7 +131,7 @@ public void getResourcePreviousBehaviorNoCache() throws Exception {
136131

137132
assertEquals("no-cache", this.response.getHeader("Pragma"));
138133
assertThat(this.response.getHeaderValues("Cache-Control"), Matchers.contains("no-cache", "no-store"));
139-
assertEquals(this.response.getHeaderValue("Expires"), dateFormat.format(System.currentTimeMillis()));
134+
assertTrue(headerAsLong("Expires") <= System.currentTimeMillis());
140135
assertTrue(this.response.containsHeader("Last-Modified"));
141136
assertEquals(headerAsLong("Last-Modified") / 1000, resourceLastModified("test/foo.css") / 1000);
142137
}
@@ -477,7 +472,7 @@ public void writeContentNotClosingInputStream() throws Exception {
477472

478473

479474
private long headerAsLong(String responseHeaderName) throws Exception {
480-
return dateFormat.parse(this.response.getHeader(responseHeaderName)).getTime();
475+
return Long.valueOf(this.response.getHeader(responseHeaderName));
481476
}
482477

483478
private long resourceLastModified(String resourceName) throws IOException {

0 commit comments

Comments
 (0)