Skip to content

SPR-17571 Allow Instant and ZonedDateTime as Last-Modified header #2039

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions spring-web/src/main/java/org/springframework/http/HttpHeaders.java
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,22 @@ public void setLastModified(long lastModified) {
setDate(LAST_MODIFIED, lastModified);
}

/**
* Set the time the resource was last changed, as specified by the
* {@code Last-Modified} header.
*/
public void setLastModified(Instant lastModified) {
setInstant(LAST_MODIFIED, lastModified);
}

/**
* Set the time the resource was last changed, as specified by the
* {@code Last-Modified} header.
*/
public void setLastModified(ZonedDateTime lastModified) {
setZonedDateTime(LAST_MODIFIED, lastModified);
}

/**
* Return the time the resource was last changed, as specified by the
* {@code Last-Modified} header.
Expand Down Expand Up @@ -1266,6 +1282,15 @@ public void setZonedDateTime(String headerName, ZonedDateTime date) {
set(headerName, DATE_FORMATTERS[0].format(date));
}

/**
* Set the given date under the given header name after formatting it as a string
* using the RFC-1123 date-time formatter. The equivalent of
* {@link #set(String, String)} but for date headers.
*/
public void setInstant(String headerName, Instant date) {
setZonedDateTime(headerName, ZonedDateTime.ofInstant(date, GMT));
}

/**
* Set the given date under the given header name after formatting it as a string
* using the RFC-1123 date-time formatter. The equivalent of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package org.springframework.http;

import java.net.URI;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Optional;
Expand Down Expand Up @@ -359,6 +361,24 @@ public interface HeadersBuilder<B extends HeadersBuilder<B>> {
*/
B lastModified(long lastModified);

/**
* Set the time the resource was last changed, as specified by the
* {@code Last-Modified} header.
* @param lastModified the last modified date
* @return this builder
* @see HttpHeaders#setLastModified(long)
*/
B lastModified(ZonedDateTime lastModified);

/**
* Set the time the resource was last changed, as specified by the
* {@code Last-Modified} header.
* @param lastModified the last modified date
* @return this builder
* @see HttpHeaders#setLastModified(long)
*/
B lastModified(Instant lastModified);

/**
* Set the location of a resource, as specified by the {@code Location} header.
* @param location the location
Expand Down Expand Up @@ -495,6 +515,18 @@ public BodyBuilder lastModified(long date) {
return this;
}

@Override
public BodyBuilder lastModified(ZonedDateTime date) {
this.headers.setLastModified(date);
return this;
}

@Override
public BodyBuilder lastModified(Instant date) {
this.headers.setLastModified(date);
return this;
}

@Override
public BodyBuilder location(URI location) {
this.headers.setLocation(location);
Expand Down