HttpHeaders.java 40.3 KB
Newer Older
1
/*
S
Sam Brannen 已提交
2
 * Copyright 2002-2016 the original author or authors.
3 4 5 6 7
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9 10 11 12 13 14 15 16
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

17
package org.springframework.http;
18

19
import java.io.Serializable;
20
import java.net.URI;
21
import java.nio.charset.Charset;
A
Arjen Poutsma 已提交
22 23
import java.text.ParseException;
import java.text.SimpleDateFormat;
24 25 26
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
A
Arjen Poutsma 已提交
27
import java.util.Date;
28
import java.util.EnumSet;
29
import java.util.Iterator;
30
import java.util.LinkedHashMap;
31 32
import java.util.LinkedList;
import java.util.List;
33
import java.util.Locale;
34 35
import java.util.Map;
import java.util.Set;
A
Arjen Poutsma 已提交
36
import java.util.TimeZone;
37 38
import java.util.regex.Matcher;
import java.util.regex.Pattern;
39 40

import org.springframework.util.Assert;
41
import org.springframework.util.LinkedCaseInsensitiveMap;
A
Arjen Poutsma 已提交
42
import org.springframework.util.MultiValueMap;
43 44 45
import org.springframework.util.StringUtils;

/**
S
Sam Brannen 已提交
46
 * Represents HTTP request and response headers, mapping string header names to a list of string values.
47
 *
J
Juergen Hoeller 已提交
48 49
 * <p>In addition to the normal methods defined by {@link Map}, this class offers the following
 * convenience methods:
50 51 52 53 54
 * <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>
55
 *
S
Sam Brannen 已提交
56
 * <p>Inspired by {@code com.sun.net.httpserver.Headers}.
57 58
 *
 * @author Arjen Poutsma
59
 * @author Sebastien Deleuze
60
 * @author Brian Clozel
61 62
 * @since 3.0
 */
63 64 65
public class HttpHeaders implements MultiValueMap<String, String>, Serializable {

	private static final long serialVersionUID = -8578554704772377436L;
66

67 68 69 70
	/**
	 * 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>
	 */
71
	public static final String ACCEPT = "Accept";
72 73 74 75
	/**
	 * The HTTP {@code Accept-Charset} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7231#section-5.3.3">Section 5.3.3 of RFC 7231</a>
	 */
76
	public static final String ACCEPT_CHARSET = "Accept-Charset";
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
	/**
	 * The HTTP {@code Accept-Encoding} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7231#section-5.3.4">Section 5.3.4 of RFC 7231</a>
	 */
	public static final String ACCEPT_ENCODING = "Accept-Encoding";
	/**
	 * The HTTP {@code Accept-Language} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7231#section-5.3.5">Section 5.3.5 of RFC 7231</a>
	 */
	public static final String ACCEPT_LANGUAGE = "Accept-Language";
	/**
	 * The HTTP {@code Accept-Ranges} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7233#section-2.3">Section 5.3.5 of RFC 7233</a>
	 */
	public static final String ACCEPT_RANGES = "Accept-Ranges";
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
	/**
	 * The CORS {@code Access-Control-Allow-Credentials} response header field name.
	 * @see <a href="http://www.w3.org/TR/cors/">CORS W3C recommandation</a>
	 */
	public static final String ACCESS_CONTROL_ALLOW_CREDENTIALS = "Access-Control-Allow-Credentials";
	/**
	 * The CORS {@code Access-Control-Allow-Headers} response header field name.
	 * @see <a href="http://www.w3.org/TR/cors/">CORS W3C recommandation</a>
	 */
	public static final String ACCESS_CONTROL_ALLOW_HEADERS = "Access-Control-Allow-Headers";
	/**
	 * The CORS {@code Access-Control-Allow-Methods} response header field name.
	 * @see <a href="http://www.w3.org/TR/cors/">CORS W3C recommandation</a>
	 */
	public static final String ACCESS_CONTROL_ALLOW_METHODS = "Access-Control-Allow-Methods";
	/**
	 * The CORS {@code Access-Control-Allow-Origin} response header field name.
	 * @see <a href="http://www.w3.org/TR/cors/">CORS W3C recommandation</a>
	 */
	public static final String ACCESS_CONTROL_ALLOW_ORIGIN = "Access-Control-Allow-Origin";
	/**
	 * The CORS {@code Access-Control-Expose-Headers} response header field name.
	 * @see <a href="http://www.w3.org/TR/cors/">CORS W3C recommandation</a>
	 */
	public static final String ACCESS_CONTROL_EXPOSE_HEADERS = "Access-Control-Expose-Headers";
	/**
	 * The CORS {@code Access-Control-Max-Age} response header field name.
	 * @see <a href="http://www.w3.org/TR/cors/">CORS W3C recommandation</a>
	 */
	public static final String ACCESS_CONTROL_MAX_AGE = "Access-Control-Max-Age";
	/**
	 * The CORS {@code Access-Control-Request-Headers} request header field name.
	 * @see <a href="http://www.w3.org/TR/cors/">CORS W3C recommandation</a>
	 */
	public static final String ACCESS_CONTROL_REQUEST_HEADERS = "Access-Control-Request-Headers";
	/**
	 * The CORS {@code Access-Control-Request-Method} request header field name.
	 * @see <a href="http://www.w3.org/TR/cors/">CORS W3C recommandation</a>
	 */
	public static final String ACCESS_CONTROL_REQUEST_METHOD = "Access-Control-Request-Method";
132 133 134 135 136 137 138 139 140
	/**
	 * The HTTP {@code Age} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7234#section-5.1">Section 5.1 of RFC 7234</a>
	 */
	public static final String AGE = "Age";
	/**
	 * The HTTP {@code Allow} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7231#section-7.4.1">Section 7.4.1 of RFC 7231</a>
	 */
141
	public static final String ALLOW = "Allow";
142 143 144 145 146 147 148 149 150
	/**
	 * The HTTP {@code Authorization} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7235#section-4.2">Section 4.2 of RFC 7235</a>
	 */
	public static final String AUTHORIZATION = "Authorization";
	/**
	 * The HTTP {@code Cache-Control} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7234#section-5.2">Section 5.2 of RFC 7234</a>
	 */
151
	public static final String CACHE_CONTROL = "Cache-Control";
152 153 154 155
	/**
	 * The HTTP {@code Connection} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7230#section-6.1">Section 6.1 of RFC 7230</a>
	 */
156
	public static final String CONNECTION = "Connection";
157 158 159 160 161 162 163 164 165
	/**
	 * The HTTP {@code Content-Encoding} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7231#section-3.1.2.2">Section 3.1.2.2 of RFC 7231</a>
	 */
	public static final String CONTENT_ENCODING = "Content-Encoding";
	/**
	 * The HTTP {@code Content-Disposition} header field name
	 * @see <a href="http://tools.ietf.org/html/rfc6266">RFC 6266</a>
	 */
166
	public static final String CONTENT_DISPOSITION = "Content-Disposition";
167 168 169 170 171 172 173 174 175
	/**
	 * The HTTP {@code Content-Language} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7231#section-3.1.3.2">Section 3.1.3.2 of RFC 7231</a>
	 */
	public static final String CONTENT_LANGUAGE = "Content-Language";
	/**
	 * The HTTP {@code Content-Length} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7230#section-3.3.2">Section 3.3.2 of RFC 7230</a>
	 */
176
	public static final String CONTENT_LENGTH = "Content-Length";
177 178 179 180 181 182 183 184 185 186 187 188 189 190
	/**
	 * The HTTP {@code Content-Location} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7231#section-3.1.4.2">Section 3.1.4.2 of RFC 7231</a>
	 */
	public static final String CONTENT_LOCATION = "Content-Location";
	/**
	 * The HTTP {@code Content-Range} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7233#section-4.2">Section 4.2 of RFC 7233</a>
	 */
	public static final String CONTENT_RANGE = "Content-Range";
	/**
	 * The HTTP {@code Content-Type} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7231#section-3.1.1.5">Section 3.1.1.5 of RFC 7231</a>
	 */
191
	public static final String CONTENT_TYPE = "Content-Type";
192 193 194 195 196 197 198 199 200
	/**
	 * The HTTP {@code Cookie} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc2109#section-4.3.4">Section 4.3.4 of RFC 2109</a>
	 */
	public static final String COOKIE = "Cookie";
	/**
	 * The HTTP {@code Date} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7231#section-7.1.1.2">Section 7.1.1.2 of RFC 7231</a>
	 */
201
	public static final String DATE = "Date";
202 203 204 205
	/**
	 * The HTTP {@code ETag} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7232#section-2.3">Section 2.3 of RFC 7232</a>
	 */
206
	public static final String ETAG = "ETag";
207 208 209 210 211 212 213 214 215
	/**
	 * The HTTP {@code Expect} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7231#section-5.1.1">Section 5.1.1 of RFC 7231</a>
	 */
	public static final String EXPECT = "Expect";
	/**
	 * The HTTP {@code Expires} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7234#section-5.3">Section 5.3 of RFC 7234</a>
	 */
216
	public static final String EXPIRES = "Expires";
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
	/**
	 * The HTTP {@code From} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7231#section-5.5.1">Section 5.5.1 of RFC 7231</a>
	 */
	public static final String FROM = "From";
	/**
	 * The HTTP {@code Host} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7230#section-5.4">Section 5.4 of RFC 7230</a>
	 */
	public static final String HOST = "Host";
	/**
	 * The HTTP {@code If-Match} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7232#section-3.1">Section 3.1 of RFC 7232</a>
	 */
	public static final String IF_MATCH = "If-Match";
	/**
	 * The HTTP {@code If-Modified-Since} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7232#section-3.3">Section 3.3 of RFC 7232</a>
	 */
236
	public static final String IF_MODIFIED_SINCE = "If-Modified-Since";
237 238 239 240
	/**
	 * The HTTP {@code If-None-Match} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7232#section-3.2">Section 3.2 of RFC 7232</a>
	 */
241
	public static final String IF_NONE_MATCH = "If-None-Match";
242 243 244 245 246 247 248 249 250 251 252 253 254 255
	/**
	 * The HTTP {@code If-Range} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7233#section-3.2">Section 3.2 of RFC 7233</a>
	 */
	public static final String IF_RANGE = "If-Range";
	/**
	 * The HTTP {@code If-Unmodified-Since} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7232#section-3.4">Section 3.4 of RFC 7232</a>
	 */
	public static final String IF_UNMODIFIED_SINCE = "If-Unmodified-Since";
	/**
	 * The HTTP {@code Last-Modified} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7232#section-2.2">Section 2.2 of RFC 7232</a>
	 */
256
	public static final String LAST_MODIFIED = "Last-Modified";
257 258 259 260 261 262 263 264 265
	/**
	 * The HTTP {@code Link} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc5988">RFC 5988</a>
	 */
	public static final String LINK = "Link";
	/**
	 * The HTTP {@code Location} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7231#section-7.1.2">Section 7.1.2 of RFC 7231</a>
	 */
266
	public static final String LOCATION = "Location";
267 268 269 270 271 272 273 274 275
	/**
	 * The HTTP {@code Max-Forwards} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7231#section-5.1.2">Section 5.1.2 of RFC 7231</a>
	 */
	public static final String MAX_FORWARDS = "Max-Forwards";
	/**
	 * The HTTP {@code Origin} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc6454">RFC 6454</a>
	 */
276
	public static final String ORIGIN = "Origin";
277 278 279 280
	/**
	 * The HTTP {@code Pragma} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7234#section-5.4">Section 5.4 of RFC 7234</a>
	 */
281
	public static final String PRAGMA = "Pragma";
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
	/**
	 * The HTTP {@code Proxy-Authenticate} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7235#section-4.3">Section 4.3 of RFC 7235</a>
	 */
	public static final String PROXY_AUTHENTICATE = "Proxy-Authenticate";
	/**
	 * The HTTP {@code Proxy-Authorization} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7235#section-4.4">Section 4.4 of RFC 7235</a>
	 */
	public static final String PROXY_AUTHORIZATION = "Proxy-Authorization";
	/**
	 * The HTTP {@code Range} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7233#section-3.1">Section 3.1 of RFC 7233</a>
	 */
	public static final String RANGE = "Range";
	/**
	 * The HTTP {@code Referer} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7231#section-5.5.2">Section 5.5.2 of RFC 7231</a>
	 */
	public static final String REFERER = "Referer";
	/**
	 * The HTTP {@code Retry-After} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7231#section-7.1.3">Section 7.1.3 of RFC 7231</a>
	 */
	public static final String RETRY_AFTER = "Retry-After";
	/**
	 * The HTTP {@code Server} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7231#section-7.4.2">Section 7.4.2 of RFC 7231</a>
	 */
	public static final String SERVER = "Server";
	/**
	 * The HTTP {@code Set-Cookie} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc2109#section-4.2.2">Section 4.2.2 of RFC 2109</a>
	 */
	public static final String SET_COOKIE = "Set-Cookie";
	/**
	 * The HTTP {@code Set-Cookie2} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc2965">RFC 2965</a>
	 */
	public static final String SET_COOKIE2 = "Set-Cookie2";
	/**
	 * The HTTP {@code TE} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7230#section-4.3">Section 4.3 of RFC 7230</a>
	 */
	public static final String TE = "TE";
	/**
	 * The HTTP {@code Trailer} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7230#section-4.4">Section 4.4 of RFC 7230</a>
	 */
	public static final String TRAILER = "Trailer";
	/**
	 * The HTTP {@code Transfer-Encoding} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7230#section-3.3.1">Section 3.3.1 of RFC 7230</a>
	 */
	public static final String TRANSFER_ENCODING = "Transfer-Encoding";
	/**
	 * The HTTP {@code Upgrade} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7230#section-6.7">Section 6.7 of RFC 7230</a>
	 */
341
	public static final String UPGRADE = "Upgrade";
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
	/**
	 * The HTTP {@code User-Agent} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7231#section-5.5.3">Section 5.5.3 of RFC 7231</a>
	 */
	public static final String USER_AGENT = "User-Agent";
	/**
	 * The HTTP {@code Vary} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7231#section-7.1.4">Section 7.1.4 of RFC 7231</a>
	 */
	public static final String VARY = "Vary";
	/**
	 * The HTTP {@code Via} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7230#section-5.7.1">Section 5.7.1 of RFC 7230</a>
	 */
	public static final String VIA = "Via";
	/**
	 * The HTTP {@code Warning} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7234#section-5.5">Section 5.5 of RFC 7234</a>
	 */
	public static final String WARNING = "Warning";
	/**
	 * The HTTP {@code WWW-Authenticate} header field name.
	 * @see <a href="http://tools.ietf.org/html/rfc7235#section-4.1">Section 4.1 of RFC 7235</a>
	 */
	public static final String WWW_AUTHENTICATE = "WWW-Authenticate";
A
Arjen Poutsma 已提交
367

368 369 370 371
	/**
	 * Date formats as specified in the HTTP RFC
	 * @see <a href="https://tools.ietf.org/html/rfc7231#section-7.1.1.1">Section 7.1.1.1 of RFC 7231</a>
	 */
A
Arjen Poutsma 已提交
372
	private static final String[] DATE_FORMATS = new String[] {
373 374 375
			"EEE, dd MMM yyyy HH:mm:ss zzz",
			"EEE, dd-MMM-yy HH:mm:ss zzz",
			"EEE MMM dd HH:mm:ss yyyy"
A
Arjen Poutsma 已提交
376 377
	};

378 379 380 381 382 383
	/**
	 * 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>
	 */
	private static final Pattern ETAG_HEADER_VALUE_PATTERN = Pattern.compile("\\*|\\s*((W\\/)?(\"[^\"]*\"))\\s*,?");

A
Arjen Poutsma 已提交
384
	private static TimeZone GMT = TimeZone.getTimeZone("GMT");
385

J
Juergen Hoeller 已提交
386

387 388
	private final Map<String, List<String>> headers;

389

J
Juergen Hoeller 已提交
390 391 392 393 394 395 396
	/**
	 * Constructs a new, empty instance of the {@code HttpHeaders} object.
	 */
	public HttpHeaders() {
		this(new LinkedCaseInsensitiveMap<List<String>>(8, Locale.ENGLISH), false);
	}

397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
	/**
	 * Private constructor that can create read-only {@code HttpHeader} instances.
	 */
	private HttpHeaders(Map<String, List<String>> headers, boolean readOnly) {
		Assert.notNull(headers, "'headers' must not be null");
		if (readOnly) {
			Map<String, List<String>> map =
					new LinkedCaseInsensitiveMap<List<String>>(headers.size(), Locale.ENGLISH);
			for (Entry<String, List<String>> entry : headers.entrySet()) {
				List<String> values = Collections.unmodifiableList(entry.getValue());
				map.put(entry.getKey(), values);
			}
			this.headers = Collections.unmodifiableMap(map);
		}
		else {
			this.headers = headers;
		}
	}
415 416 417


	/**
J
Juergen Hoeller 已提交
418 419
	 * Set the list of acceptable {@linkplain MediaType media types},
	 * as specified by the {@code Accept} header.
420 421 422 423
	 */
	public void setAccept(List<MediaType> acceptableMediaTypes) {
		set(ACCEPT, MediaType.toString(acceptableMediaTypes));
	}
424 425

	/**
J
Juergen Hoeller 已提交
426 427
	 * Return the list of acceptable {@linkplain MediaType media types},
	 * as specified by the {@code Accept} header.
428
	 * <p>Returns an empty list when the acceptable media types are unspecified.
429 430 431
	 */
	public List<MediaType> getAccept() {
		String value = getFirst(ACCEPT);
J
Juergen Hoeller 已提交
432
		List<MediaType> result = (value != null ? MediaType.parseMediaTypes(value) : Collections.<MediaType>emptyList());
433 434

		// Some containers parse 'Accept' into multiple values
J
Juergen Hoeller 已提交
435 436 437 438 439 440
		if (result.size() == 1) {
			List<String> acceptHeader = get(ACCEPT);
			if (acceptHeader.size() > 1) {
				value = StringUtils.collectionToCommaDelimitedString(acceptHeader);
				result = MediaType.parseMediaTypes(value);
			}
441 442 443
		}

		return result;
444 445
	}

446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
	/**
	 * Set the (new) value of the {@code Access-Control-Allow-Credentials} response header.
	 */
	public void setAccessControlAllowCredentials(boolean allowCredentials) {
		set(ACCESS_CONTROL_ALLOW_CREDENTIALS, Boolean.toString(allowCredentials));
	}

	/**
	 * Returns the value of the {@code Access-Control-Allow-Credentials} response header.
	 */
	public boolean getAccessControlAllowCredentials() {
		return new Boolean(getFirst(ACCESS_CONTROL_ALLOW_CREDENTIALS));
	}

	/**
	 * Set the (new) value of the {@code Access-Control-Allow-Headers} response header.
	 */
	public void setAccessControlAllowHeaders(List<String> allowedHeaders) {
		set(ACCESS_CONTROL_ALLOW_HEADERS, toCommaDelimitedString(allowedHeaders));
	}

	/**
	 * Returns the value of the {@code Access-Control-Allow-Headers} response header.
	 */
	public List<String> getAccessControlAllowHeaders() {
471
		return getValuesAsList(ACCESS_CONTROL_ALLOW_HEADERS);
472 473 474 475 476 477 478 479 480 481
	}

	/**
	 * Set the (new) value of the {@code Access-Control-Allow-Methods} response header.
	 */
	public void setAccessControlAllowMethods(List<HttpMethod> allowedMethods) {
		set(ACCESS_CONTROL_ALLOW_METHODS, StringUtils.collectionToCommaDelimitedString(allowedMethods));
	}

	/**
482
	 * Return the value of the {@code Access-Control-Allow-Methods} response header.
483 484 485 486 487
	 */
	public List<HttpMethod> getAccessControlAllowMethods() {
		List<HttpMethod> result = new ArrayList<HttpMethod>();
		String value = getFirst(ACCESS_CONTROL_ALLOW_METHODS);
		if (value != null) {
488
			String[] tokens = StringUtils.tokenizeToStringArray(value, ",", true, true);
489
			for (String token : tokens) {
490 491 492 493
				HttpMethod resolved = HttpMethod.resolve(token);
				if (resolved != null) {
					result.add(resolved);
				}
494 495 496 497 498 499 500 501 502 503 504 505 506
			}
		}
		return result;
	}

	/**
	 * Set the (new) value of the {@code Access-Control-Allow-Origin} response header.
	 */
	public void setAccessControlAllowOrigin(String allowedOrigin) {
		set(ACCESS_CONTROL_ALLOW_ORIGIN, allowedOrigin);
	}

	/**
507
	 * Return the value of the {@code Access-Control-Allow-Origin} response header.
508 509
	 */
	public String getAccessControlAllowOrigin() {
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
		return getFieldValues(ACCESS_CONTROL_ALLOW_ORIGIN);
	}

	protected String getFieldValues(String headerName) {
		List<String> headerValues = this.headers.get(headerName);
		if (headerValues != null) {
			StringBuilder builder = new StringBuilder();
			for (Iterator<String> iterator = headerValues.iterator(); iterator.hasNext(); ) {
				String ifNoneMatch = iterator.next();
				builder.append(ifNoneMatch);
				if (iterator.hasNext()) {
					builder.append(", ");
				}
			}
			return builder.toString();
		}
		return null;
527 528 529 530 531 532 533 534 535 536 537 538 539
	}

	/**
	 * Set the (new) value of the {@code Access-Control-Expose-Headers} response header.
	 */
	public void setAccessControlExposeHeaders(List<String> exposedHeaders) {
		set(ACCESS_CONTROL_EXPOSE_HEADERS, toCommaDelimitedString(exposedHeaders));
	}

	/**
	 * Returns the value of the {@code Access-Control-Expose-Headers} response header.
	 */
	public List<String> getAccessControlExposeHeaders() {
540
		return getValuesAsList(ACCESS_CONTROL_EXPOSE_HEADERS);
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
	}

	/**
	 * Set the (new) value of the {@code Access-Control-Max-Age} response header.
	 */
	public void setAccessControlMaxAge(long maxAge) {
		set(ACCESS_CONTROL_MAX_AGE, Long.toString(maxAge));
	}

	/**
	 * Returns the value of the {@code Access-Control-Max-Age} response header.
	 * <p>Returns -1 when the max age is unknown.
	 */
	public long getAccessControlMaxAge() {
		String value = getFirst(ACCESS_CONTROL_MAX_AGE);
		return (value != null ? Long.parseLong(value) : -1);
	}

	/**
	 * Set the (new) value of the {@code Access-Control-Request-Headers} request header.
	 */
	public void setAccessControlRequestHeaders(List<String> requestHeaders) {
		set(ACCESS_CONTROL_REQUEST_HEADERS, toCommaDelimitedString(requestHeaders));
	}

	/**
	 * Returns the value of the {@code Access-Control-Request-Headers} request header.
	 */
	public List<String> getAccessControlRequestHeaders() {
570
		return getValuesAsList(ACCESS_CONTROL_REQUEST_HEADERS);
571 572 573 574 575 576 577 578 579 580
	}

	/**
	 * Set the (new) value of the {@code Access-Control-Request-Method} request header.
	 */
	public void setAccessControlRequestMethod(HttpMethod requestedMethod) {
		set(ACCESS_CONTROL_REQUEST_METHOD, requestedMethod.name());
	}

	/**
581
	 * Return the value of the {@code Access-Control-Request-Method} request header.
582 583
	 */
	public HttpMethod getAccessControlRequestMethod() {
584
		return HttpMethod.resolve(getFirst(ACCESS_CONTROL_REQUEST_METHOD));
585 586
	}

587
	/**
J
Juergen Hoeller 已提交
588 589
	 * Set the list of acceptable {@linkplain Charset charsets},
	 * as specified by the {@code Accept-Charset} header.
590
	 */
591 592 593 594 595 596 597 598 599 600
	public void setAcceptCharset(List<Charset> acceptableCharsets) {
		StringBuilder builder = new StringBuilder();
		for (Iterator<Charset> iterator = acceptableCharsets.iterator(); iterator.hasNext();) {
			Charset charset = iterator.next();
			builder.append(charset.name().toLowerCase(Locale.ENGLISH));
			if (iterator.hasNext()) {
				builder.append(", ");
			}
		}
		set(ACCEPT_CHARSET, builder.toString());
601 602
	}

603
	/**
J
Juergen Hoeller 已提交
604 605
	 * Return the list of acceptable {@linkplain Charset charsets},
	 * as specified by the {@code Accept-Charset} header.
606 607 608 609 610 611 612 613
	 */
	public List<Charset> getAcceptCharset() {
		List<Charset> result = new ArrayList<Charset>();
		String value = getFirst(ACCEPT_CHARSET);
		if (value != null) {
			String[] tokens = value.split(",\\s*");
			for (String token : tokens) {
				int paramIdx = token.indexOf(';');
614
				String charsetName;
615
				if (paramIdx == -1) {
616
					charsetName = token;
617 618
				}
				else {
619 620 621 622
					charsetName = token.substring(0, paramIdx);
				}
				if (!charsetName.equals("*")) {
					result.add(Charset.forName(charsetName));
623 624 625 626 627 628 629
				}
			}
		}
		return result;
	}

	/**
J
Juergen Hoeller 已提交
630 631
	 * Set the set of allowed {@link HttpMethod HTTP methods},
	 * as specified by the {@code Allow} header.
632
	 */
633 634
	public void setAllow(Set<HttpMethod> allowedMethods) {
		set(ALLOW, StringUtils.collectionToCommaDelimitedString(allowedMethods));
635 636
	}

637
	/**
J
Juergen Hoeller 已提交
638 639
	 * Return the set of allowed {@link HttpMethod HTTP methods},
	 * as specified by the {@code Allow} header.
640
	 * <p>Returns an empty set when the allowed methods are unspecified.
641
	 */
642
	public Set<HttpMethod> getAllow() {
643
		String value = getFirst(ALLOW);
644
		if (!StringUtils.isEmpty(value)) {
645
			List<HttpMethod> result = new LinkedList<HttpMethod>();
646 647
			String[] tokens = value.split(",\\s*");
			for (String token : tokens) {
648 649 650 651
				HttpMethod resolved = HttpMethod.resolve(token);
				if (resolved != null) {
					result.add(resolved);
				}
652
			}
653
			return EnumSet.copyOf(result);
654 655 656 657 658 659
		}
		else {
			return EnumSet.noneOf(HttpMethod.class);
		}
	}

A
Arjen Poutsma 已提交
660
	/**
J
Juergen Hoeller 已提交
661
	 * Set the (new) value of the {@code Cache-Control} header.
A
Arjen Poutsma 已提交
662 663 664 665 666 667
	 */
	public void setCacheControl(String cacheControl) {
		set(CACHE_CONTROL, cacheControl);
	}

	/**
668
	 * Return the value of the {@code Cache-Control} header.
A
Arjen Poutsma 已提交
669 670
	 */
	public String getCacheControl() {
671
		return getFieldValues(CACHE_CONTROL);
A
Arjen Poutsma 已提交
672 673
	}

674
	/**
J
Juergen Hoeller 已提交
675
	 * Set the (new) value of the {@code Connection} header.
676 677 678 679 680 681
	 */
	public void setConnection(String connection) {
		set(CONNECTION, connection);
	}

	/**
J
Juergen Hoeller 已提交
682
	 * Set the (new) value of the {@code Connection} header.
683 684 685 686 687 688
	 */
	public void setConnection(List<String> connection) {
		set(CONNECTION, toCommaDelimitedString(connection));
	}

	/**
689
	 * Return the value of the {@code Connection} header.
690 691
	 */
	public List<String> getConnection() {
692
		return getValuesAsList(CONNECTION);
693 694
	}

695
	/**
J
Juergen Hoeller 已提交
696 697
	 * Set the (new) value of the {@code Content-Disposition} header
	 * for {@code form-data}.
698
	 * @param name the control name
J
Juergen Hoeller 已提交
699
	 * @param filename the filename (may be {@code null})
700 701 702 703 704 705 706 707 708 709 710 711
	 */
	public void setContentDispositionFormData(String name, String filename) {
		Assert.notNull(name, "'name' must not be null");
		StringBuilder builder = new StringBuilder("form-data; name=\"");
		builder.append(name).append('\"');
		if (filename != null) {
			builder.append("; filename=\"");
			builder.append(filename).append('\"');
		}
		set(CONTENT_DISPOSITION, builder.toString());
	}

712
	/**
J
Juergen Hoeller 已提交
713 714
	 * Set the length of the body in bytes, as specified by the
	 * {@code Content-Length} header.
715
	 */
716 717
	public void setContentLength(long contentLength) {
		set(CONTENT_LENGTH, Long.toString(contentLength));
718 719 720
	}

	/**
J
Juergen Hoeller 已提交
721 722
	 * Return the length of the body in bytes, as specified by the
	 * {@code Content-Length} header.
723
	 * <p>Returns -1 when the content-length is unknown.
724 725 726
	 */
	public long getContentLength() {
		String value = getFirst(CONTENT_LENGTH);
727
		return (value != null ? Long.parseLong(value) : -1);
728 729 730
	}

	/**
J
Juergen Hoeller 已提交
731 732
	 * Set the {@linkplain MediaType media type} of the body,
	 * as specified by the {@code Content-Type} header.
733
	 */
734 735 736 737
	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 '*'");
		set(CONTENT_TYPE, mediaType.toString());
738 739 740
	}

	/**
J
Juergen Hoeller 已提交
741 742
	 * Return the {@linkplain MediaType media type} of the body, as specified
	 * by the {@code Content-Type} header.
A
Arjen Poutsma 已提交
743
	 * <p>Returns {@code null} when the content-type is unknown.
744 745 746
	 */
	public MediaType getContentType() {
		String value = getFirst(CONTENT_TYPE);
747
		return (StringUtils.hasLength(value) ? MediaType.parseMediaType(value) : null);
748 749 750
	}

	/**
J
Juergen Hoeller 已提交
751 752 753 754
	 * Set the date and time at which the message was created, as specified
	 * by the {@code Date} header.
	 * <p>The date should be specified as the number of milliseconds since
	 * January 1, 1970 GMT.
A
Arjen Poutsma 已提交
755 756 757 758 759 760
	 */
	public void setDate(long date) {
		setDate(DATE, date);
	}

	/**
J
Juergen Hoeller 已提交
761 762 763 764
	 * Return the date and time at which the message was created, as specified
	 * by the {@code Date} header.
	 * <p>The date is returned as the number of milliseconds since
	 * January 1, 1970 GMT. Returns -1 when the date is unknown.
A
Arjen Poutsma 已提交
765 766 767 768 769 770 771
	 * @throws IllegalArgumentException if the value can't be converted to a date
	 */
	public long getDate() {
		return getFirstDate(DATE);
	}

	/**
J
Juergen Hoeller 已提交
772
	 * Set the (new) entity tag of the body, as specified by the {@code ETag} header.
A
Arjen Poutsma 已提交
773 774
	 */
	public void setETag(String eTag) {
775
		if (eTag != null) {
J
Juergen Hoeller 已提交
776 777
			Assert.isTrue(eTag.startsWith("\"") || eTag.startsWith("W/"),
					"Invalid eTag, does not start with W/ or \"");
778 779
			Assert.isTrue(eTag.endsWith("\""), "Invalid eTag, does not end with \"");
		}
J
Juergen Hoeller 已提交
780
		set(ETAG, eTag);
A
Arjen Poutsma 已提交
781 782 783
	}

	/**
J
Juergen Hoeller 已提交
784
	 * Return the entity tag of the body, as specified by the {@code ETag} header.
A
Arjen Poutsma 已提交
785 786
	 */
	public String getETag() {
787
		return getFirst(ETAG);
A
Arjen Poutsma 已提交
788 789 790
	}

	/**
J
Juergen Hoeller 已提交
791 792
	 * Set the date and time at which the message is no longer valid,
	 * as specified by the {@code Expires} header.
J
Juergen Hoeller 已提交
793 794
	 * <p>The date should be specified as the number of milliseconds since
	 * January 1, 1970 GMT.
A
Arjen Poutsma 已提交
795 796 797 798 799 800
	 */
	public void setExpires(long expires) {
		setDate(EXPIRES, expires);
	}

	/**
J
Juergen Hoeller 已提交
801
	 * Return the date and time at which the message is no longer valid,
J
Juergen Hoeller 已提交
802
	 * as specified by the {@code Expires} header.
J
Juergen Hoeller 已提交
803 804
	 * <p>The date is returned as the number of milliseconds since
	 * January 1, 1970 GMT. Returns -1 when the date is unknown.
A
Arjen Poutsma 已提交
805 806
	 */
	public long getExpires() {
807
		return getFirstDate(EXPIRES, false);
A
Arjen Poutsma 已提交
808 809
	}

810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
	/**
	 * Set the (new) value of the {@code If-Match} header.
	 */
	public void setIfMatch(String ifMatch) {
		set(IF_MATCH, ifMatch);
	}

	/**
	 * Set the (new) value of the {@code If-Match} header.
	 */
	public void setIfMatch(List<String> ifMatchList) {
		set(IF_MATCH, toCommaDelimitedString(ifMatchList));
	}

	protected String toCommaDelimitedString(List<String> list) {
		StringBuilder builder = new StringBuilder();
		for (Iterator<String> iterator = list.iterator(); iterator.hasNext(); ) {
			String ifNoneMatch = iterator.next();
			builder.append(ifNoneMatch);
			if (iterator.hasNext()) {
				builder.append(", ");
			}
		}
		return builder.toString();
	}

	/**
	 * Return the value of the {@code If-Match} header.
	 */
	public List<String> getIfMatch() {
		return getETagValuesAsList(IF_MATCH);
	}

	protected List<String> getETagValuesAsList(String headerName) {
		List<String> values = get(headerName);
		if (values != null) {
			List<String> result = new ArrayList<String>();
			for (String value : values) {
				if (value != null) {
					Matcher matcher = ETAG_HEADER_VALUE_PATTERN.matcher(value);
					while (matcher.find()) {
						if ("*".equals(matcher.group())) {
							result.add(matcher.group());
						}
						else {
							result.add(matcher.group(1));
						}
					}
					if(result.size() == 0) {
						throw new IllegalArgumentException("Could not parse '" + headerName + "' value=" + value);
					}
				}
			}
			return result;
		}
		return Collections.emptyList();
	}

A
Arjen Poutsma 已提交
868
	/**
J
Juergen Hoeller 已提交
869
	 * Set the (new) value of the {@code If-Modified-Since} header.
J
Juergen Hoeller 已提交
870 871
	 * <p>The date should be specified as the number of milliseconds since
	 * January 1, 1970 GMT.
A
Arjen Poutsma 已提交
872 873 874 875 876
	 */
	public void setIfModifiedSince(long ifModifiedSince) {
		setDate(IF_MODIFIED_SINCE, ifModifiedSince);
	}

877
	/**
J
Juergen Hoeller 已提交
878 879 880
	 * Return the value of the {@code If-Modified-Since} header.
	 * <p>The date is returned as the number of milliseconds since
	 * January 1, 1970 GMT. Returns -1 when the date is unknown.
881 882
	 */
	public long getIfModifiedSince() {
883
		return getFirstDate(IF_MODIFIED_SINCE, false);
A
Arjen Poutsma 已提交
884 885
	}

A
Arjen Poutsma 已提交
886
	/**
J
Juergen Hoeller 已提交
887
	 * Set the (new) value of the {@code If-None-Match} header.
A
Arjen Poutsma 已提交
888 889
	 */
	public void setIfNoneMatch(String ifNoneMatch) {
890
		set(IF_NONE_MATCH, ifNoneMatch);
A
Arjen Poutsma 已提交
891 892 893
	}

	/**
J
Juergen Hoeller 已提交
894
	 * Set the (new) values of the {@code If-None-Match} header.
A
Arjen Poutsma 已提交
895 896
	 */
	public void setIfNoneMatch(List<String> ifNoneMatchList) {
897 898 899
		set(IF_NONE_MATCH, toCommaDelimitedString(ifNoneMatchList));
	}

A
Arjen Poutsma 已提交
900
	/**
J
Juergen Hoeller 已提交
901
	 * Return the value of the {@code If-None-Match} header.
A
Arjen Poutsma 已提交
902 903
	 */
	public List<String> getIfNoneMatch() {
904
		return getETagValuesAsList(IF_NONE_MATCH);
905 906
	}

907 908 909
	/**
	 * Return all values of a given header name,
	 * even if this header is set multiple times.
J
Juergen Hoeller 已提交
910
	 * @since 4.3
911 912 913 914 915 916 917 918 919 920 921 922
	 */
	public List<String> getValuesAsList(String headerName) {
		List<String> values = get(headerName);
		if (values != null) {
			List<String> result = new ArrayList<String>();
			for (String value : values) {
				if (value != null) {
					String[] tokens = StringUtils.tokenizeToStringArray(value, ",");
					for (String token : tokens) {
						result.add(token);
					}
				}
A
Arjen Poutsma 已提交
923
			}
924
			return result;
A
Arjen Poutsma 已提交
925
		}
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
		return Collections.emptyList();
	}

	/**
	 * Set the (new) value of the {@code If-Unmodified-Since} header.
	 * <p>The date should be specified as the number of milliseconds since
	 * January 1, 1970 GMT.
	 */
	public void setIfUnmodifiedSince(long ifUnmodifiedSince) {
		setDate(IF_UNMODIFIED_SINCE, ifUnmodifiedSince);
	}

	/**
	 * Return the value of the {@code If-Unmodified-Since} header.
	 * <p>The date is returned as the number of milliseconds since
	 * January 1, 1970 GMT. Returns -1 when the date is unknown.
	 */
	public long getIfUnmodifiedSince() {
		return getFirstDate(IF_UNMODIFIED_SINCE, false);
A
Arjen Poutsma 已提交
945 946 947
	}

	/**
J
Juergen Hoeller 已提交
948 949 950 951
	 * Set the time the resource was last changed, as specified by the
	 * {@code Last-Modified} header.
	 * <p>The date should be specified as the number of milliseconds since
	 * January 1, 1970 GMT.
A
Arjen Poutsma 已提交
952 953 954 955 956 957
	 */
	public void setLastModified(long lastModified) {
		setDate(LAST_MODIFIED, lastModified);
	}

	/**
J
Juergen Hoeller 已提交
958 959 960 961
	 * Return the time the resource was last changed, as specified by the
	 * {@code Last-Modified} header.
	 * <p>The date is returned as the number of milliseconds since
	 * January 1, 1970 GMT. Returns -1 when the date is unknown.
A
Arjen Poutsma 已提交
962 963
	 */
	public long getLastModified() {
964
		return getFirstDate(LAST_MODIFIED, false);
A
Arjen Poutsma 已提交
965 966 967
	}

	/**
J
Juergen Hoeller 已提交
968 969
	 * Set the (new) location of a resource,
	 * as specified by the {@code Location} header.
970
	 */
971 972
	public void setLocation(URI location) {
		set(LOCATION, location.toASCIIString());
973 974 975
	}

	/**
J
Juergen Hoeller 已提交
976 977
	 * Return the (new) location of a resource
	 * as specified by the {@code Location} header.
A
Arjen Poutsma 已提交
978
	 * <p>Returns {@code null} when the location is unknown.
979 980 981
	 */
	public URI getLocation() {
		String value = getFirst(LOCATION);
982
		return (value != null ? URI.create(value) : null);
983 984
	}

985
	/**
J
Juergen Hoeller 已提交
986
	 * Set the (new) value of the {@code Origin} header.
987 988 989 990 991 992
	 */
	public void setOrigin(String origin) {
		set(ORIGIN, origin);
	}

	/**
J
Juergen Hoeller 已提交
993
	 * Return the value of the {@code Origin} header.
994 995 996 997 998
	 */
	public String getOrigin() {
		return getFirst(ORIGIN);
	}

A
Arjen Poutsma 已提交
999
	/**
J
Juergen Hoeller 已提交
1000
	 * Set the (new) value of the {@code Pragma} header.
A
Arjen Poutsma 已提交
1001 1002 1003 1004 1005 1006
	 */
	public void setPragma(String pragma) {
		set(PRAGMA, pragma);
	}

	/**
J
Juergen Hoeller 已提交
1007
	 * Return the value of the {@code Pragma} header.
A
Arjen Poutsma 已提交
1008 1009 1010 1011 1012
	 */
	public String getPragma() {
		return getFirst(PRAGMA);
	}

1013 1014 1015 1016 1017 1018 1019 1020 1021
	/**
	 * Sets the (new) value of the {@code Range} header.
	 */
	public void setRange(List<HttpRange> ranges) {
		String value = HttpRange.toString(ranges);
		set(RANGE, value);
	}

	/**
1022
	 * Return the value of the {@code Range} header.
1023 1024 1025 1026 1027 1028 1029
	 * <p>Returns an empty list when the range is unknown.
	 */
	public List<HttpRange> getRange() {
		String value = getFirst(RANGE);
		return HttpRange.parseRanges(value);
	}

1030
	/**
J
Juergen Hoeller 已提交
1031
	 * Set the (new) value of the {@code Upgrade} header.
1032 1033
	 */
	public void setUpgrade(String upgrade) {
R
Rossen Stoyanchev 已提交
1034
		set(UPGRADE, upgrade);
1035 1036 1037
	}

	/**
1038
	 * Return the value of the {@code Upgrade} header.
1039 1040
	 */
	public String getUpgrade() {
R
Rossen Stoyanchev 已提交
1041
		return getFirst(UPGRADE);
1042 1043
	}

1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
	/**
	 * 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() {
1059
		return getValuesAsList(VARY);
1060 1061
	}

1062
	/**
J
Juergen Hoeller 已提交
1063 1064 1065
	 * Parse the first header value for the given header name as a date,
	 * return -1 if there is no value, or raise {@link IllegalArgumentException}
	 * if the value cannot be parsed as a date.
1066 1067
	 * @param headerName the header name
	 * @return the parsed date header, or -1 if none
1068 1069
	 */
	public long getFirstDate(String headerName) {
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
		return getFirstDate(headerName, true);
	}

	/**
	 * Parse the first header value for the given header name as a date,
	 * return -1 if there is no value or also in case of an invalid value
	 * (if {@code rejectInvalid=false}), or raise {@link IllegalArgumentException}
	 * if the value cannot be parsed as a date.
	 * @param headerName the header name
	 * @param rejectInvalid whether to reject invalid values with an
	 * {@link IllegalArgumentException} ({@code true}) or rather return -1
	 * in that case ({@code false})
	 * @return the parsed date header, or -1 if none (or invalid)
 	 */
	private long getFirstDate(String headerName, boolean rejectInvalid) {
A
Arjen Poutsma 已提交
1085 1086
		String headerValue = getFirst(headerName);
		if (headerValue == null) {
1087
			// No header value sent at all
A
Arjen Poutsma 已提交
1088 1089
			return -1;
		}
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
		if (headerValue.length() >= 3) {
			// Short "0" or "-1" like values are never valid HTTP date headers...
			// Let's only bother with SimpleDateFormat parsing for long enough values.
			for (String dateFormat : DATE_FORMATS) {
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat, Locale.US);
				simpleDateFormat.setTimeZone(GMT);
				try {
					return simpleDateFormat.parse(headerValue).getTime();
				}
				catch (ParseException ex) {
					// ignore
				}
A
Arjen Poutsma 已提交
1102 1103
			}
		}
1104 1105 1106 1107 1108
		if (rejectInvalid) {
			throw new IllegalArgumentException("Cannot parse date value \"" + headerValue +
					"\" for \"" + headerName + "\" header");
		}
		return -1;
A
Arjen Poutsma 已提交
1109 1110
	}

1111 1112 1113 1114 1115 1116
	/**
	 * Set the given date under the given header name after formatting it as a string
	 * using the pattern {@code "EEE, dd MMM yyyy HH:mm:ss zzz"}. The equivalent of
	 * {@link #set(String, String)} but for date headers.
	 */
	public void setDate(String headerName, long date) {
1117
		SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMATS[0], Locale.US);
A
Arjen Poutsma 已提交
1118 1119 1120
		dateFormat.setTimeZone(GMT);
		set(headerName, dateFormat.format(new Date(date)));
	}
1121 1122

	/**
1123
	 * Return the first header value for the given header name, if any.
1124
	 * @param headerName the header name
J
Juergen Hoeller 已提交
1125
	 * @return the first header value, or {@code null} if none
1126
	 */
1127
	@Override
1128
	public String getFirst(String headerName) {
J
Juergen Hoeller 已提交
1129 1130
		List<String> headerValues = this.headers.get(headerName);
		return (headerValues != null ? headerValues.get(0) : null);
1131 1132 1133
	}

	/**
1134
	 * Add the given, single header value under the given name.
J
Juergen Hoeller 已提交
1135
	 * @param headerName the header name
1136 1137 1138 1139 1140
	 * @param headerValue the header value
	 * @throws UnsupportedOperationException if adding headers is not supported
	 * @see #put(String, List)
	 * @see #set(String, String)
	 */
1141
	@Override
1142
	public void add(String headerName, String headerValue) {
J
Juergen Hoeller 已提交
1143
		List<String> headerValues = this.headers.get(headerName);
1144 1145
		if (headerValues == null) {
			headerValues = new LinkedList<String>();
1146
			this.headers.put(headerName, headerValues);
1147 1148 1149 1150 1151
		}
		headerValues.add(headerValue);
	}

	/**
1152
	 * Set the given, single header value under the given name.
J
Juergen Hoeller 已提交
1153
	 * @param headerName the header name
1154 1155 1156 1157 1158
	 * @param headerValue the header value
	 * @throws UnsupportedOperationException if adding headers is not supported
	 * @see #put(String, List)
	 * @see #add(String, String)
	 */
1159
	@Override
1160 1161 1162
	public void set(String headerName, String headerValue) {
		List<String> headerValues = new LinkedList<String>();
		headerValues.add(headerValue);
J
Juergen Hoeller 已提交
1163
		this.headers.put(headerName, headerValues);
1164 1165
	}

1166
	@Override
1167 1168 1169 1170 1171 1172
	public void setAll(Map<String, String> values) {
		for (Entry<String, String> entry : values.entrySet()) {
			set(entry.getKey(), entry.getValue());
		}
	}

1173
	@Override
1174 1175
	public Map<String, String> toSingleValueMap() {
		LinkedHashMap<String, String> singleValueMap = new LinkedHashMap<String,String>(this.headers.size());
J
Juergen Hoeller 已提交
1176
		for (Entry<String, List<String>> entry : this.headers.entrySet()) {
1177 1178 1179 1180
			singleValueMap.put(entry.getKey(), entry.getValue().get(0));
		}
		return singleValueMap;
	}
1181

J
Juergen Hoeller 已提交
1182

1183
	// Map implementation
1184

1185
	@Override
1186
	public int size() {
1187
		return this.headers.size();
1188 1189
	}

1190
	@Override
1191
	public boolean isEmpty() {
1192
		return this.headers.isEmpty();
1193 1194
	}

1195
	@Override
1196
	public boolean containsKey(Object key) {
1197
		return this.headers.containsKey(key);
1198 1199
	}

1200
	@Override
1201
	public boolean containsValue(Object value) {
1202
		return this.headers.containsValue(value);
1203 1204
	}

1205
	@Override
1206
	public List<String> get(Object key) {
1207
		return this.headers.get(key);
1208 1209
	}

1210
	@Override
1211
	public List<String> put(String key, List<String> value) {
1212
		return this.headers.put(key, value);
1213 1214
	}

1215
	@Override
1216
	public List<String> remove(Object key) {
1217
		return this.headers.remove(key);
1218 1219
	}

1220
	@Override
J
Juergen Hoeller 已提交
1221 1222
	public void putAll(Map<? extends String, ? extends List<String>> map) {
		this.headers.putAll(map);
1223 1224
	}

1225
	@Override
1226
	public void clear() {
1227
		this.headers.clear();
1228 1229
	}

1230
	@Override
1231
	public Set<String> keySet() {
1232
		return this.headers.keySet();
1233 1234
	}

1235
	@Override
1236
	public Collection<List<String>> values() {
1237
		return this.headers.values();
1238 1239
	}

1240
	@Override
1241
	public Set<Entry<String, List<String>>> entrySet() {
1242
		return this.headers.entrySet();
1243 1244
	}

1245

1246
	@Override
1247 1248
	public boolean equals(Object other) {
		if (this == other) {
1249 1250
			return true;
		}
1251 1252
		if (!(other instanceof HttpHeaders)) {
			return false;
1253
		}
1254 1255 1256 1257 1258 1259 1260
		HttpHeaders otherHeaders = (HttpHeaders) other;
		return this.headers.equals(otherHeaders.headers);
	}

	@Override
	public int hashCode() {
		return this.headers.hashCode();
1261 1262 1263 1264
	}

	@Override
	public String toString() {
1265
		return this.headers.toString();
1266
	}
1267

J
Juergen Hoeller 已提交
1268 1269 1270 1271 1272 1273 1274 1275

	/**
	 * Return a {@code HttpHeaders} object that can only be read, not written to.
	 */
	public static HttpHeaders readOnlyHttpHeaders(HttpHeaders headers) {
		return new HttpHeaders(headers, true);
	}

1276
}