From 4a5b9d39d4aad8ae71d1247a075be097cf57ac33 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 2 Apr 2019 14:56:20 +0200 Subject: [PATCH] Consistent java.time setters on HttpHeaders and CorsConfiguration Closes gh-22546 --- .../springframework/http/CacheControl.java | 1 + .../org/springframework/http/HttpHeaders.java | 36 ++++++++++++++++ .../web/cors/CorsConfiguration.java | 42 +++++++++++-------- .../web/cors/CorsConfigurationTests.java | 4 +- 4 files changed, 63 insertions(+), 20 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/CacheControl.java b/spring-web/src/main/java/org/springframework/http/CacheControl.java index 6b3e164972f..29366576501 100644 --- a/spring-web/src/main/java/org/springframework/http/CacheControl.java +++ b/spring-web/src/main/java/org/springframework/http/CacheControl.java @@ -77,6 +77,7 @@ public class CacheControl { @Nullable private Duration sMaxAge; + /** * Create an empty CacheControl instance. * @see #empty() diff --git a/spring-web/src/main/java/org/springframework/http/HttpHeaders.java b/spring-web/src/main/java/org/springframework/http/HttpHeaders.java index e498b7fba69..d8002d6128e 100644 --- a/spring-web/src/main/java/org/springframework/http/HttpHeaders.java +++ b/spring-web/src/main/java/org/springframework/http/HttpHeaders.java @@ -24,6 +24,7 @@ import java.nio.charset.CharsetEncoder; import java.nio.charset.StandardCharsets; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; +import java.time.Duration; import java.time.Instant; import java.time.ZoneId; import java.time.ZonedDateTime; @@ -588,6 +589,14 @@ public class HttpHeaders implements MultiValueMap, Serializable return getValuesAsList(ACCESS_CONTROL_EXPOSE_HEADERS); } + /** + * Set the (new) value of the {@code Access-Control-Max-Age} response header. + * @since 5.2 + */ + public void setAccessControlMaxAge(Duration maxAge) { + set(ACCESS_CONTROL_MAX_AGE, Long.toString(maxAge.getSeconds())); + } + /** * Set the (new) value of the {@code Access-Control-Max-Age} response header. */ @@ -928,6 +937,24 @@ public class HttpHeaders implements MultiValueMap, Serializable return (StringUtils.hasLength(value) ? MediaType.parseMediaType(value) : null); } + /** + * Set the date and time at which the message was created, as specified + * by the {@code Date} header. + * @since 5.2 + */ + public void setDate(ZonedDateTime date) { + setZonedDateTime(DATE, date); + } + + /** + * Set the date and time at which the message was created, as specified + * by the {@code Date} header. + * @since 5.2 + */ + public void setDate(Instant date) { + setInstant(DATE, date); + } + /** * Set the date and time at which the message was created, as specified * by the {@code Date} header. @@ -981,6 +1008,15 @@ public class HttpHeaders implements MultiValueMap, Serializable setZonedDateTime(EXPIRES, expires); } + /** + * Set the date and time at which the message is no longer valid, + * as specified by the {@code Expires} header. + * @since 5.2 + */ + public void setExpires(Instant expires) { + setInstant(EXPIRES, expires); + } + /** * Set the date and time at which the message is no longer valid, * as specified by the {@code Expires} header. diff --git a/spring-web/src/main/java/org/springframework/web/cors/CorsConfiguration.java b/spring-web/src/main/java/org/springframework/web/cors/CorsConfiguration.java index 3b885f39bb7..884a13add2a 100644 --- a/spring-web/src/main/java/org/springframework/web/cors/CorsConfiguration.java +++ b/spring-web/src/main/java/org/springframework/web/cors/CorsConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -16,6 +16,7 @@ package org.springframework.web.cors; +import java.time.Duration; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -52,14 +53,14 @@ public class CorsConfiguration { /** Wildcard representing all origins, methods, or headers. */ public static final String ALL = "*"; - private static final List DEFAULT_METHODS = - Collections.unmodifiableList(Arrays.asList(HttpMethod.GET, HttpMethod.HEAD)); + private static final List DEFAULT_METHODS = Collections.unmodifiableList( + Arrays.asList(HttpMethod.GET, HttpMethod.HEAD)); - private static final List DEFAULT_PERMIT_ALL = - Collections.unmodifiableList(Arrays.asList(ALL)); + private static final List DEFAULT_PERMIT_METHODS = Collections.unmodifiableList( + Arrays.asList(HttpMethod.GET.name(), HttpMethod.HEAD.name(), HttpMethod.POST.name())); - private static final List DEFAULT_PERMIT_METHODS = - Collections.unmodifiableList(Arrays.asList(HttpMethod.GET.name(), HttpMethod.HEAD.name(), HttpMethod.POST.name())); + private static final List DEFAULT_PERMIT_ALL = Collections.unmodifiableList( + Collections.singletonList(ALL)); @Nullable @@ -304,6 +305,16 @@ public class CorsConfiguration { return this.allowCredentials; } + /** + * Configure how long, as a duration, the response from a pre-flight request + * can be cached by clients. + * @since 5.2 + * @see #setMaxAge(Long) + */ + public void setMaxAge(Duration maxAge) { + this.maxAge = maxAge.getSeconds(); + } + /** * Configure how long, in seconds, the response from a pre-flight request * can be cached by clients. @@ -322,22 +333,21 @@ public class CorsConfiguration { return this.maxAge; } + /** * By default a newly created {@code CorsConfiguration} does not permit any * cross-origin requests and must be configured explicitly to indicate what * should be allowed. - * *

Use this method to flip the initialization model to start with open * defaults that permit all cross-origin requests for GET, HEAD, and POST * requests. Note however that this method will not override any existing * values already set. - * *

The following defaults are applied if not already set: *

    - *
  • Allow all origins.
  • - *
  • Allow "simple" methods {@code GET}, {@code HEAD} and {@code POST}.
  • - *
  • Allow all headers.
  • - *
  • Set max age to 1800 seconds (30 minutes).
  • + *
  • Allow all origins.
  • + *
  • Allow "simple" methods {@code GET}, {@code HEAD} and {@code POST}.
  • + *
  • Allow all headers.
  • + *
  • Set max age to 1800 seconds (30 minutes).
  • *
*/ public CorsConfiguration applyPermitDefaultValues() { @@ -361,23 +371,19 @@ public class CorsConfiguration { /** * Combine the non-null properties of the supplied * {@code CorsConfiguration} with this one. - * *

When combining single values like {@code allowCredentials} or * {@code maxAge}, {@code this} properties are overridden by non-null * {@code other} properties if any. - * *

Combining lists like {@code allowedOrigins}, {@code allowedMethods}, * {@code allowedHeaders} or {@code exposedHeaders} is done in an additive * way. For example, combining {@code ["GET", "POST"]} with * {@code ["PATCH"]} results in {@code ["GET", "POST", "PATCH"]}, but keep * in mind that combining {@code ["GET", "POST"]} with {@code ["*"]} * results in {@code ["*"]}. - * *

Notice that default permit values set by * {@link CorsConfiguration#applyPermitDefaultValues()} are overridden by * any value explicitly defined. - * - * @return the combined {@code CorsConfiguration} or {@code this} + * @return the combined {@code CorsConfiguration}, or {@code this} * configuration if the supplied configuration is {@code null} */ @Nullable diff --git a/spring-web/src/test/java/org/springframework/web/cors/CorsConfigurationTests.java b/spring-web/src/test/java/org/springframework/web/cors/CorsConfigurationTests.java index e188f8d80b2..b4495cf4016 100644 --- a/spring-web/src/test/java/org/springframework/web/cors/CorsConfigurationTests.java +++ b/spring-web/src/test/java/org/springframework/web/cors/CorsConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 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. @@ -47,7 +47,7 @@ public class CorsConfigurationTests { assertNull(config.getExposedHeaders()); config.setAllowCredentials(null); assertNull(config.getAllowCredentials()); - config.setMaxAge(null); + config.setMaxAge((Long) null); assertNull(config.getMaxAge()); }