Polishing

This commit is contained in:
Juergen Hoeller
2018-12-12 13:04:59 +01:00
parent b00f98fccf
commit ce05a5b5c0
3 changed files with 28 additions and 27 deletions

View File

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

View File

@@ -43,18 +43,17 @@ import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
/**
* Represents HTTP request and response headers, mapping string header names to a list of string values.
* A data structure representing HTTP request or response headers, mapping String header names
* to a list of String values, also offering accessors for common application-level data types.
*
* <p>In addition to the normal methods defined by {@link Map}, this class offers the following
* convenience methods:
* <p>In addition to the regular methods defined by {@link Map}, this class offers many common
* convenience methods, for example:
* <ul>
* <li>{@link #getFirst(String)} returns the first value associated with a given header name</li>
* <li>{@link #add(String, String)} adds a header value to the list of values for a header name</li>
* <li>{@link #set(String, String)} sets the header value to a single string value</li>
* </ul>
*
* <p>Inspired by {@code com.sun.net.httpserver.Headers}.
*
* @author Arjen Poutsma
* @author Sebastien Deleuze
* @author Brian Clozel
@@ -66,6 +65,7 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
private static final long serialVersionUID = -8578554704772377436L;
/**
* 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>
@@ -390,7 +390,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() {
this(new LinkedCaseInsensitiveMap<List<String>>(8, Locale.ENGLISH), false);
@@ -744,8 +744,8 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
* as specified by the {@code Content-Type} header.
*/
public void setContentType(MediaType mediaType) {
Assert.isTrue(!mediaType.isWildcardType(), "'Content-Type' cannot contain wildcard type '*'");
Assert.isTrue(!mediaType.isWildcardSubtype(), "'Content-Type' cannot contain wildcard subtype '*'");
Assert.isTrue(!mediaType.isWildcardType(), "Content-Type cannot contain wildcard type '*'");
Assert.isTrue(!mediaType.isWildcardSubtype(), "Content-Type cannot contain wildcard subtype '*'");
set(CONTENT_TYPE, mediaType.toString());
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -63,6 +63,7 @@ import org.springframework.util.ObjectUtils;
* @author Arjen Poutsma
* @author Brian Clozel
* @since 3.0.2
* @param <T> the body type
* @see #getStatusCode()
*/
public class ResponseEntity<T> extends HttpEntity<T> {
@@ -295,8 +296,8 @@ public class ResponseEntity<T> extends HttpEntity<T> {
/**
* Defines a builder that adds headers to the response entity.
* @param <B> the builder subclass
* @since 4.1
* @param <B> the builder subclass
*/
public interface HeadersBuilder<B extends HeadersBuilder<B>> {
@@ -494,7 +495,7 @@ public class ResponseEntity<T> extends HttpEntity<T> {
public BodyBuilder cacheControl(CacheControl cacheControl) {
String ccValue = cacheControl.getHeaderValue();
if (ccValue != null) {
this.headers.setCacheControl(cacheControl.getHeaderValue());
this.headers.setCacheControl(ccValue);
}
return this;
}