Skip to content

Commit 6a01214

Browse files
committed
Consistent support for last-modified argument as Instant/ZonedDateTime
Issue: SPR-17571
1 parent 9abd4ed commit 6a01214

File tree

6 files changed

+65
-30
lines changed

6 files changed

+65
-30
lines changed

spring-web/src/main/java/org/springframework/http/HttpHeaders.java

+17-14
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,7 @@ public void setLastModified(long lastModified) {
11481148
/**
11491149
* Set the time the resource was last changed, as specified by the
11501150
* {@code Last-Modified} header.
1151+
* @since 5.1.4
11511152
*/
11521153
public void setLastModified(Instant lastModified) {
11531154
setInstant(LAST_MODIFIED, lastModified);
@@ -1156,9 +1157,10 @@ public void setLastModified(Instant lastModified) {
11561157
/**
11571158
* Set the time the resource was last changed, as specified by the
11581159
* {@code Last-Modified} header.
1160+
* @since 5.1.4
11591161
*/
11601162
public void setLastModified(ZonedDateTime lastModified) {
1161-
setZonedDateTime(LAST_MODIFIED, lastModified);
1163+
setZonedDateTime(LAST_MODIFIED, lastModified.withZoneSameInstant(ZoneId.of("GMT")));
11621164
}
11631165

11641166
/**
@@ -1276,19 +1278,20 @@ public List<String> getVary() {
12761278
* Set the given date under the given header name after formatting it as a string
12771279
* using the RFC-1123 date-time formatter. The equivalent of
12781280
* {@link #set(String, String)} but for date headers.
1279-
* @since 5.0
1281+
* @since 5.1.4
12801282
*/
1281-
public void setZonedDateTime(String headerName, ZonedDateTime date) {
1282-
set(headerName, DATE_FORMATTERS[0].format(date));
1283+
public void setInstant(String headerName, Instant date) {
1284+
setZonedDateTime(headerName, ZonedDateTime.ofInstant(date, GMT));
12831285
}
12841286

12851287
/**
12861288
* Set the given date under the given header name after formatting it as a string
12871289
* using the RFC-1123 date-time formatter. The equivalent of
12881290
* {@link #set(String, String)} but for date headers.
1291+
* @since 5.0
12891292
*/
1290-
public void setInstant(String headerName, Instant date) {
1291-
setZonedDateTime(headerName, ZonedDateTime.ofInstant(date, GMT));
1293+
public void setZonedDateTime(String headerName, ZonedDateTime date) {
1294+
set(headerName, DATE_FORMATTERS[0].format(date));
12921295
}
12931296

12941297
/**
@@ -1299,14 +1302,7 @@ public void setInstant(String headerName, Instant date) {
12991302
* @see #setZonedDateTime(String, ZonedDateTime)
13001303
*/
13011304
public void setDate(String headerName, long date) {
1302-
set(headerName, formatDate(date));
1303-
}
1304-
1305-
// Package private: also used in ResponseCookie..
1306-
static String formatDate(long date) {
1307-
Instant instant = Instant.ofEpochMilli(date);
1308-
ZonedDateTime time = ZonedDateTime.ofInstant(instant, GMT);
1309-
return DATE_FORMATTERS[0].format(time);
1305+
setInstant(headerName, Instant.ofEpochMilli(date));
13101306
}
13111307

13121308
/**
@@ -1656,4 +1652,11 @@ public static HttpHeaders writableHttpHeaders(HttpHeaders headers) {
16561652
}
16571653
}
16581654

1655+
// Package-private: used in ResponseCookie
1656+
static String formatDate(long date) {
1657+
Instant instant = Instant.ofEpochMilli(date);
1658+
ZonedDateTime time = ZonedDateTime.ofInstant(instant, GMT);
1659+
return DATE_FORMATTERS[0].format(time);
1660+
}
1661+
16591662
}

spring-web/src/main/java/org/springframework/http/ResponseEntity.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -366,18 +366,20 @@ public interface HeadersBuilder<B extends HeadersBuilder<B>> {
366366
* {@code Last-Modified} header.
367367
* @param lastModified the last modified date
368368
* @return this builder
369+
* @since 5.1.4
369370
* @see HttpHeaders#setLastModified(long)
370371
*/
371-
B lastModified(ZonedDateTime lastModified);
372+
B lastModified(Instant lastModified);
372373

373374
/**
374375
* Set the time the resource was last changed, as specified by the
375376
* {@code Last-Modified} header.
376377
* @param lastModified the last modified date
377378
* @return this builder
379+
* @since 5.1.4
378380
* @see HttpHeaders#setLastModified(long)
379381
*/
380-
B lastModified(Instant lastModified);
382+
B lastModified(ZonedDateTime lastModified);
381383

382384
/**
383385
* Set the location of a resource, as specified by the {@code Location} header.
@@ -516,13 +518,13 @@ public BodyBuilder lastModified(long date) {
516518
}
517519

518520
@Override
519-
public BodyBuilder lastModified(ZonedDateTime date) {
521+
public BodyBuilder lastModified(Instant date) {
520522
this.headers.setLastModified(date);
521523
return this;
522524
}
523525

524526
@Override
525-
public BodyBuilder lastModified(Instant date) {
527+
public BodyBuilder lastModified(ZonedDateTime date) {
526528
this.headers.setLastModified(date);
527529
return this;
528530
}

spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultEntityResponseBuilder.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717
package org.springframework.web.reactive.function.server;
1818

1919
import java.net.URI;
20-
import java.time.ZoneId;
20+
import java.time.Instant;
2121
import java.time.ZonedDateTime;
22-
import java.time.format.DateTimeFormatter;
2322
import java.util.Arrays;
2423
import java.util.HashMap;
2524
import java.util.LinkedHashSet;
@@ -158,11 +157,15 @@ public EntityResponse.Builder<T> hint(String key, Object value) {
158157
return this;
159158
}
160159

160+
@Override
161+
public EntityResponse.Builder<T> lastModified(Instant lastModified) {
162+
this.headers.setLastModified(lastModified);
163+
return this;
164+
}
165+
161166
@Override
162167
public EntityResponse.Builder<T> lastModified(ZonedDateTime lastModified) {
163-
ZonedDateTime gmt = lastModified.withZoneSameInstant(ZoneId.of("GMT"));
164-
String headerValue = DateTimeFormatter.RFC_1123_DATE_TIME.format(gmt);
165-
this.headers.set(HttpHeaders.LAST_MODIFIED, headerValue);
168+
this.headers.setLastModified(lastModified);
166169
return this;
167170
}
168171

@@ -174,10 +177,7 @@ public EntityResponse.Builder<T> location(URI location) {
174177

175178
@Override
176179
public EntityResponse.Builder<T> cacheControl(CacheControl cacheControl) {
177-
String ccValue = cacheControl.getHeaderValue();
178-
if (ccValue != null) {
179-
this.headers.setCacheControl(cacheControl.getHeaderValue());
180-
}
180+
this.headers.setCacheControl(cacheControl);
181181
return this;
182182
}
183183

spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilder.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,15 @@ public ServerResponse.BodyBuilder hint(String key, Object value) {
157157
return this;
158158
}
159159

160+
@Override
161+
public ServerResponse.BodyBuilder lastModified(Instant lastModified) {
162+
this.headers.setLastModified(lastModified);
163+
return this;
164+
}
165+
160166
@Override
161167
public ServerResponse.BodyBuilder lastModified(ZonedDateTime lastModified) {
162-
this.headers.setZonedDateTime(HttpHeaders.LAST_MODIFIED, lastModified);
168+
this.headers.setLastModified(lastModified);
163169
return this;
164170
}
165171

spring-webflux/src/main/java/org/springframework/web/reactive/function/server/EntityResponse.java

+15-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.web.reactive.function.server;
1818

1919
import java.net.URI;
20+
import java.time.Instant;
2021
import java.time.ZonedDateTime;
2122
import java.util.Set;
2223
import java.util.function.Consumer;
@@ -176,11 +177,23 @@ interface Builder<T> {
176177

177178
/**
178179
* Set the entity tag of the body, as specified by the {@code ETag} header.
179-
* @param eTag the new entity tag
180+
* @param etag the new entity tag
180181
* @return this builder
181182
* @see HttpHeaders#setETag(String)
182183
*/
183-
Builder<T> eTag(String eTag);
184+
Builder<T> eTag(String etag);
185+
186+
/**
187+
* Set the time the resource was last changed, as specified by the
188+
* {@code Last-Modified} header.
189+
* <p>The date should be specified as the number of milliseconds since
190+
* January 1, 1970 GMT.
191+
* @param lastModified the last modified date
192+
* @return this builder
193+
* @since 5.1.4
194+
* @see HttpHeaders#setLastModified(long)
195+
*/
196+
Builder<T> lastModified(Instant lastModified);
184197

185198
/**
186199
* Set the time the resource was last changed, as specified by the

spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ServerResponse.java

+11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.web.reactive.function.server;
1818

1919
import java.net.URI;
20+
import java.time.Instant;
2021
import java.time.ZonedDateTime;
2122
import java.util.Collection;
2223
import java.util.List;
@@ -275,6 +276,16 @@ interface HeadersBuilder<B extends HeadersBuilder<B>> {
275276
*/
276277
B eTag(String eTag);
277278

279+
/**
280+
* Set the time the resource was last changed, as specified by the
281+
* {@code Last-Modified} header.
282+
* @param lastModified the last modified date
283+
* @return this builder
284+
* @since 5.1.4
285+
* @see HttpHeaders#setLastModified(long)
286+
*/
287+
B lastModified(Instant lastModified);
288+
278289
/**
279290
* Set the time the resource was last changed, as specified by the
280291
* {@code Last-Modified} header.

0 commit comments

Comments
 (0)