Add support for setting the "Vary" response header

Issue: SPR-14070
This commit is contained in:
Rossen Stoyanchev
2016-03-22 21:47:55 -04:00
parent 6bfe0c050d
commit 7a5e93ff16
6 changed files with 246 additions and 2 deletions

View File

@@ -38,6 +38,7 @@ import java.util.TimeZone;
import org.springframework.util.Assert;
import org.springframework.util.LinkedCaseInsensitiveMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
@@ -947,6 +948,24 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
return getFirst(UPGRADE);
}
/**
* Set the request header names (e.g. "Accept-Language") for which the
* response is subject to content negotiation and variances based on the
* value of those request headers.
* @param requestHeaders the request header names
* @since 4.3
*/
public void setVary(List<String> requestHeaders) {
set(VARY, toCommaDelimitedString(requestHeaders));
}
/**
* Return the request header names subject to content negotiation.
*/
public List<String> getVary() {
return getFirstValueAsList(VARY);
}
/**
* Parse the first header value for the given header name as a date,
* return -1 if there is no value, or raise {@link IllegalArgumentException}

View File

@@ -332,6 +332,17 @@ public class ResponseEntity<T> extends HttpEntity<T> {
*/
B cacheControl(CacheControl cacheControl);
/**
* Configure one or more request header names (e.g. "Accept-Language") to
* add to the "Vary" response header to inform clients that the response is
* subject to content negotiation and variances based on the value of the
* given request headers. The configured request header names are added only
* if not already present in the response "Vary" header.
* @param requestHeaders request header names
* @since 4.3
*/
B varyBy(String... requestHeaders);
/**
* Build the response entity with no body.
* @return the response entity
@@ -454,6 +465,12 @@ public class ResponseEntity<T> extends HttpEntity<T> {
return this;
}
@Override
public BodyBuilder varyBy(String... requestHeaders) {
this.headers.setVary(Arrays.asList(requestHeaders));
return this;
}
@Override
public ResponseEntity<Void> build() {
return new ResponseEntity<Void>(null, this.headers, this.status);