Browse Source

Add RFC-8246 support to CacheControl

This commit adds the "immutable" Cache-Control directives in `CacheControl`.

Closes gh-29955
pull/29975/head
Toshiaki Maki 3 years ago committed by Brian Clozel
parent
commit
616e7779e0
  1. 17
      spring-web/src/main/java/org/springframework/http/CacheControl.java
  2. 6
      spring-web/src/test/java/org/springframework/http/CacheControlTests.java

17
spring-web/src/main/java/org/springframework/http/CacheControl.java

@ -77,6 +77,8 @@ public class CacheControl { @@ -77,6 +77,8 @@ public class CacheControl {
@Nullable
private Duration sMaxAge;
private boolean immutable = false;
/**
* Create an empty CacheControl instance.
@ -320,6 +322,18 @@ public class CacheControl { @@ -320,6 +322,18 @@ public class CacheControl {
return this;
}
/**
* Add an "immutable" directive.
* <p>This directive indicates that the origin server will not update the
* representation of that resource during the freshness lifetime of the response.
* @return {@code this}, to facilitate method chaining
* @see <a href="https://tools.ietf.org/html/rfc8246">rfc8246</a>
*/
public CacheControl immutable() {
this.immutable = true;
return this;
}
/**
* Return the "Cache-Control" header value, if any.
* @return the header value, or {@code null} if no directive was added
@ -369,6 +383,9 @@ public class CacheControl { @@ -369,6 +383,9 @@ public class CacheControl {
if (this.staleWhileRevalidate != null) {
appendDirective(headerValue, "stale-while-revalidate=" + this.staleWhileRevalidate.getSeconds());
}
if (this.immutable) {
appendDirective(headerValue, "immutable");
}
return headerValue.toString();
}

6
spring-web/src/test/java/org/springframework/http/CacheControlTests.java

@ -101,4 +101,10 @@ public class CacheControlTests { @@ -101,4 +101,10 @@ public class CacheControlTests {
assertThat(cc.getHeaderValue()).isEqualTo("max-age=3600, stale-while-revalidate=7200");
}
@Test
public void immutable() throws Exception {
CacheControl cc = CacheControl.maxAge(Duration.ofHours(1)).immutable();
assertThat(cc.getHeaderValue()).isEqualTo("max-age=3600, immutable");
}
}

Loading…
Cancel
Save