Browse Source

XML configuration support for RFC 5861

This commit adds the XML+XSD configuration part of the RFC 5861
Cache-Control directives added in Spring's `CacheControl`.

Issue: SPR-13841
pull/953/head
Brian Clozel 9 years ago
parent
commit
cdda839426
  1. 62
      spring-web/src/main/java/org/springframework/http/CacheControl.java
  2. 2
      spring-web/src/test/java/org/springframework/http/CacheControlTests.java
  3. 10
      spring-webmvc/src/main/java/org/springframework/web/servlet/config/ResourcesBeanDefinitionParser.java
  4. 16
      spring-webmvc/src/main/resources/org/springframework/web/servlet/config/spring-mvc-4.3.xsd

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -212,34 +212,34 @@ public class CacheControl { @@ -212,34 +212,34 @@ public class CacheControl {
}
/**
* Add an "stale-while-revalidate" directive.
* <p>This directive indicates that caches MAY serve the response in
* which it appears after it becomes stale, up to the indicated number of seconds.
* If a cached response is served stale due to the presence of this extension,
* the cache SHOULD attempt to revalidate it while still serving stale responses (i.e., without blocking).
* @param staleWhileRevalidate the maximum time the response should be used while being revalidated
* @param unit the time unit of the {@code sMaxAge} argument
* @return {@code this}, to facilitate method chaining
* @see <a href="http://tools.ietf.org/html/rfc5861#section-3">rfc5861 section 3</a>
*/
public CacheControl staleWhileRevalidate(long staleWhileRevalidate, TimeUnit unit) {
this.staleWhileRevalidate = unit.toSeconds(staleWhileRevalidate);
return this;
}
/**
* Add an "stale-if-error" directive.
* <p>This directive indicates that that when an error is encountered, a cached stale response MAY be used to satisfy
* the request, regardless of other freshness information..
* @param staleIfError the maximum time the response should be used when errors are encountered
* @param unit the time unit of the {@code sMaxAge} argument
* @return {@code this}, to facilitate method chaining
* @see <a href="http://tools.ietf.org/html/rfc5861#section-4">rfc5861 section 4</a>
*/
public CacheControl staleIfError(long staleIfError, TimeUnit unit) {
this.staleIfError = unit.toSeconds(staleIfError);
return this;
}
* Add a "stale-while-revalidate" directive.
* <p>This directive indicates that caches MAY serve the response in
* which it appears after it becomes stale, up to the indicated number of seconds.
* If a cached response is served stale due to the presence of this extension,
* the cache SHOULD attempt to revalidate it while still serving stale responses (i.e., without blocking).
* @param staleWhileRevalidate the maximum time the response should be used while being revalidated
* @param unit the time unit of the {@code staleWhileRevalidate} argument
* @return {@code this}, to facilitate method chaining
* @see <a href="https://tools.ietf.org/html/rfc5861#section-3">rfc5861 section 3</a>
*/
public CacheControl staleWhileRevalidate(long staleWhileRevalidate, TimeUnit unit) {
this.staleWhileRevalidate = unit.toSeconds(staleWhileRevalidate);
return this;
}
/**
* Add a "stale-if-error" directive.
* <p>This directive indicates that when an error is encountered, a cached stale response MAY be used to satisfy
* the request, regardless of other freshness information.
* @param staleIfError the maximum time the response should be used when errors are encountered
* @param unit the time unit of the {@code staleIfError} argument
* @return {@code this}, to facilitate method chaining
* @see <a href="https://tools.ietf.org/html/rfc5861#section-4">rfc5861 section 4</a>
*/
public CacheControl staleIfError(long staleIfError, TimeUnit unit) {
this.staleIfError = unit.toSeconds(staleIfError);
return this;
}
/**
@ -275,10 +275,10 @@ public class CacheControl { @@ -275,10 +275,10 @@ public class CacheControl {
if (this.sMaxAge != -1) {
appendDirective(ccValue, "s-maxage=" + Long.toString(this.sMaxAge));
}
if(this.staleIfError != -1) {
if (this.staleIfError != -1) {
appendDirective(ccValue, "stale-if-error=" + Long.toString(this.staleIfError));
}
if(this.staleWhileRevalidate != -1) {
if (this.staleWhileRevalidate != -1) {
appendDirective(ccValue, "stale-while-revalidate=" + Long.toString(this.staleWhileRevalidate));
}

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

10
spring-webmvc/src/main/java/org/springframework/web/servlet/config/ResourcesBeanDefinitionParser.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -242,6 +242,14 @@ class ResourcesBeanDefinitionParser implements BeanDefinitionParser { @@ -242,6 +242,14 @@ class ResourcesBeanDefinitionParser implements BeanDefinitionParser {
if (element.hasAttribute("s-maxage")) {
cacheControl = cacheControl.sMaxAge(Long.parseLong(element.getAttribute("s-maxage")), TimeUnit.SECONDS);
}
if (element.hasAttribute("stale-while-revalidate")) {
cacheControl = cacheControl.staleWhileRevalidate(
Long.parseLong(element.getAttribute("stale-while-revalidate")), TimeUnit.SECONDS);
}
if (element.hasAttribute("stale-if-error")) {
cacheControl = cacheControl.staleIfError(
Long.parseLong(element.getAttribute("stale-if-error")), TimeUnit.SECONDS);
}
return cacheControl;
}

16
spring-webmvc/src/main/resources/org/springframework/web/servlet/config/spring-mvc-4.3.xsd

@ -581,6 +581,22 @@ @@ -581,6 +581,22 @@
<xsd:annotation>
<xsd:documentation><![CDATA[
Adds a "s-maxage" directive in the Cache-Control header.
This directive has the same meaning as the "max-age" directive, except it only applies to shared caches.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="stale-while-revalidate" type="xsd:int" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[
Adds a "stale-while-revalidate" directive in the Cache-Control header.
This indicates that caches may serve the response after it becomes stale up to the given number of seconds.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="stale-if-error" type="xsd:int" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[
Adds a "s-maxage" directive in the Cache-Control header.
This directive has the same meaning as the "max-age" directive, except it only applies to shared caches.
]]></xsd:documentation>
</xsd:annotation>

Loading…
Cancel
Save