Browse Source

Polishing

pull/23430/head
Juergen Hoeller 7 years ago
parent
commit
7377764d02
  1. 32
      spring-web/src/main/java/org/springframework/http/CacheControl.java
  2. 15
      spring-web/src/main/java/org/springframework/http/HttpHeaders.java
  3. 2
      spring-web/src/main/java/org/springframework/http/ResponseEntity.java
  4. 2
      spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultEntityResponseBuilder.java
  5. 2
      spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilder.java
  6. 6
      spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceWebHandler.java

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

@ -253,48 +253,48 @@ public class CacheControl {
/** /**
* Return the "Cache-Control" header value. * Return the "Cache-Control" header value, if any.
* @return {@code null} if no directive was added, or the header value otherwise * @return the header value, or {@code null} if no directive was added
*/ */
@Nullable @Nullable
public String getHeaderValue() { public String getHeaderValue() {
StringBuilder ccValue = new StringBuilder(); StringBuilder headerValue = new StringBuilder();
if (this.maxAge != -1) { if (this.maxAge != -1) {
appendDirective(ccValue, "max-age=" + Long.toString(this.maxAge)); appendDirective(headerValue, "max-age=" + this.maxAge);
} }
if (this.noCache) { if (this.noCache) {
appendDirective(ccValue, "no-cache"); appendDirective(headerValue, "no-cache");
} }
if (this.noStore) { if (this.noStore) {
appendDirective(ccValue, "no-store"); appendDirective(headerValue, "no-store");
} }
if (this.mustRevalidate) { if (this.mustRevalidate) {
appendDirective(ccValue, "must-revalidate"); appendDirective(headerValue, "must-revalidate");
} }
if (this.noTransform) { if (this.noTransform) {
appendDirective(ccValue, "no-transform"); appendDirective(headerValue, "no-transform");
} }
if (this.cachePublic) { if (this.cachePublic) {
appendDirective(ccValue, "public"); appendDirective(headerValue, "public");
} }
if (this.cachePrivate) { if (this.cachePrivate) {
appendDirective(ccValue, "private"); appendDirective(headerValue, "private");
} }
if (this.proxyRevalidate) { if (this.proxyRevalidate) {
appendDirective(ccValue, "proxy-revalidate"); appendDirective(headerValue, "proxy-revalidate");
} }
if (this.sMaxAge != -1) { if (this.sMaxAge != -1) {
appendDirective(ccValue, "s-maxage=" + Long.toString(this.sMaxAge)); appendDirective(headerValue, "s-maxage=" + this.sMaxAge);
} }
if (this.staleIfError != -1) { if (this.staleIfError != -1) {
appendDirective(ccValue, "stale-if-error=" + Long.toString(this.staleIfError)); appendDirective(headerValue, "stale-if-error=" + this.staleIfError);
} }
if (this.staleWhileRevalidate != -1) { if (this.staleWhileRevalidate != -1) {
appendDirective(ccValue, "stale-while-revalidate=" + Long.toString(this.staleWhileRevalidate)); appendDirective(headerValue, "stale-while-revalidate=" + this.staleWhileRevalidate);
} }
String ccHeaderValue = ccValue.toString(); String valueString = headerValue.toString();
return (StringUtils.hasText(ccHeaderValue) ? ccHeaderValue : null); return (StringUtils.hasText(valueString) ? valueString : null);
} }
private void appendDirective(StringBuilder builder, String value) { private void appendDirective(StringBuilder builder, String value) {

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

@ -72,10 +72,7 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
private static final long serialVersionUID = -8578554704772377436L; private static final long serialVersionUID = -8578554704772377436L;
/**
* The empty {@code HttpHeaders} instance (immutable).
*/
public static final HttpHeaders EMPTY = new HttpHeaders(new LinkedHashMap<>(), true);
/** /**
* The HTTP {@code Accept} header field name. * The HTTP {@code Accept} header field name.
* @see <a href="http://tools.ietf.org/html/rfc7231#section-5.3.2">Section 5.3.2 of RFC 7231</a> * @see <a href="http://tools.ietf.org/html/rfc7231#section-5.3.2">Section 5.3.2 of RFC 7231</a>
@ -377,6 +374,12 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
*/ */
public static final String WWW_AUTHENTICATE = "WWW-Authenticate"; public static final String WWW_AUTHENTICATE = "WWW-Authenticate";
/**
* The empty {@code HttpHeaders} instance (immutable).
*/
public static final HttpHeaders EMPTY = new HttpHeaders(new LinkedHashMap<>(), true);
/** /**
* Pattern matching ETag multiple field values in headers such as "If-Match", "If-None-Match". * Pattern matching ETag multiple field values in headers such as "If-Match", "If-None-Match".
* @see <a href="https://tools.ietf.org/html/rfc7232#section-2.3">Section 2.3 of RFC 7232</a> * @see <a href="https://tools.ietf.org/html/rfc7232#section-2.3">Section 2.3 of RFC 7232</a>
@ -394,7 +397,7 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
private static final DateTimeFormatter[] DATE_FORMATTERS = new DateTimeFormatter[] { private static final DateTimeFormatter[] DATE_FORMATTERS = new DateTimeFormatter[] {
DateTimeFormatter.RFC_1123_DATE_TIME, DateTimeFormatter.RFC_1123_DATE_TIME,
DateTimeFormatter.ofPattern("EEEE, dd-MMM-yy HH:mm:ss zz", Locale.US), DateTimeFormatter.ofPattern("EEEE, dd-MMM-yy HH:mm:ss zz", Locale.US),
DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss yyyy",Locale.US).withZone(GMT) DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss yyyy", Locale.US).withZone(GMT)
}; };
@ -404,7 +407,7 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
/** /**
* Constructs a new, empty instance of the {@code HttpHeaders} object. * Construct a new, empty instance of the {@code HttpHeaders} object.
*/ */
public HttpHeaders() { public HttpHeaders() {
this(new LinkedCaseInsensitiveMap<>(8, Locale.ENGLISH), false); this(new LinkedCaseInsensitiveMap<>(8, Locale.ENGLISH), false);

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

@ -491,7 +491,7 @@ public class ResponseEntity<T> extends HttpEntity<T> {
public BodyBuilder cacheControl(CacheControl cacheControl) { public BodyBuilder cacheControl(CacheControl cacheControl) {
String ccValue = cacheControl.getHeaderValue(); String ccValue = cacheControl.getHeaderValue();
if (ccValue != null) { if (ccValue != null) {
this.headers.setCacheControl(cacheControl.getHeaderValue()); this.headers.setCacheControl(ccValue);
} }
return this; return this;
} }

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

@ -172,7 +172,7 @@ class DefaultEntityResponseBuilder<T> implements EntityResponse.Builder<T> {
public EntityResponse.Builder<T> cacheControl(CacheControl cacheControl) { public EntityResponse.Builder<T> cacheControl(CacheControl cacheControl) {
String ccValue = cacheControl.getHeaderValue(); String ccValue = cacheControl.getHeaderValue();
if (ccValue != null) { if (ccValue != null) {
this.headers.setCacheControl(cacheControl.getHeaderValue()); this.headers.setCacheControl(ccValue);
} }
return this; return this;
} }

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

@ -172,7 +172,7 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
public ServerResponse.BodyBuilder cacheControl(CacheControl cacheControl) { public ServerResponse.BodyBuilder cacheControl(CacheControl cacheControl) {
String ccValue = cacheControl.getHeaderValue(); String ccValue = cacheControl.getHeaderValue();
if (ccValue != null) { if (ccValue != null) {
this.headers.setCacheControl(cacheControl.getHeaderValue()); this.headers.setCacheControl(ccValue);
} }
return this; return this;
} }

6
spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceWebHandler.java

@ -283,9 +283,9 @@ public class ResourceWebHandler implements WebHandler, InitializingBean {
// Apply cache settings, if any // Apply cache settings, if any
if (getCacheControl() != null) { if (getCacheControl() != null) {
String value = getCacheControl().getHeaderValue(); String ccValue = getCacheControl().getHeaderValue();
if (value != null) { if (ccValue != null) {
exchange.getResponse().getHeaders().setCacheControl(value); exchange.getResponse().getHeaders().setCacheControl(ccValue);
} }
} }

Loading…
Cancel
Save