Skip to content

Commit 4a5b9d3

Browse files
committed
Consistent java.time setters on HttpHeaders and CorsConfiguration
Closes gh-22546
1 parent d6c1a65 commit 4a5b9d3

File tree

4 files changed

+63
-20
lines changed

4 files changed

+63
-20
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public class CacheControl {
7777
@Nullable
7878
private Duration sMaxAge;
7979

80+
8081
/**
8182
* Create an empty CacheControl instance.
8283
* @see #empty()

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

+36
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.nio.charset.StandardCharsets;
2525
import java.text.DecimalFormat;
2626
import java.text.DecimalFormatSymbols;
27+
import java.time.Duration;
2728
import java.time.Instant;
2829
import java.time.ZoneId;
2930
import java.time.ZonedDateTime;
@@ -588,6 +589,14 @@ public List<String> getAccessControlExposeHeaders() {
588589
return getValuesAsList(ACCESS_CONTROL_EXPOSE_HEADERS);
589590
}
590591

592+
/**
593+
* Set the (new) value of the {@code Access-Control-Max-Age} response header.
594+
* @since 5.2
595+
*/
596+
public void setAccessControlMaxAge(Duration maxAge) {
597+
set(ACCESS_CONTROL_MAX_AGE, Long.toString(maxAge.getSeconds()));
598+
}
599+
591600
/**
592601
* Set the (new) value of the {@code Access-Control-Max-Age} response header.
593602
*/
@@ -928,6 +937,24 @@ public MediaType getContentType() {
928937
return (StringUtils.hasLength(value) ? MediaType.parseMediaType(value) : null);
929938
}
930939

940+
/**
941+
* Set the date and time at which the message was created, as specified
942+
* by the {@code Date} header.
943+
* @since 5.2
944+
*/
945+
public void setDate(ZonedDateTime date) {
946+
setZonedDateTime(DATE, date);
947+
}
948+
949+
/**
950+
* Set the date and time at which the message was created, as specified
951+
* by the {@code Date} header.
952+
* @since 5.2
953+
*/
954+
public void setDate(Instant date) {
955+
setInstant(DATE, date);
956+
}
957+
931958
/**
932959
* Set the date and time at which the message was created, as specified
933960
* by the {@code Date} header.
@@ -981,6 +1008,15 @@ public void setExpires(ZonedDateTime expires) {
9811008
setZonedDateTime(EXPIRES, expires);
9821009
}
9831010

1011+
/**
1012+
* Set the date and time at which the message is no longer valid,
1013+
* as specified by the {@code Expires} header.
1014+
* @since 5.2
1015+
*/
1016+
public void setExpires(Instant expires) {
1017+
setInstant(EXPIRES, expires);
1018+
}
1019+
9841020
/**
9851021
* Set the date and time at which the message is no longer valid,
9861022
* as specified by the {@code Expires} header.

spring-web/src/main/java/org/springframework/web/cors/CorsConfiguration.java

+24-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.web.cors;
1818

19+
import java.time.Duration;
1920
import java.util.ArrayList;
2021
import java.util.Arrays;
2122
import java.util.Collections;
@@ -52,14 +53,14 @@ public class CorsConfiguration {
5253
/** Wildcard representing <em>all</em> origins, methods, or headers. */
5354
public static final String ALL = "*";
5455

55-
private static final List<HttpMethod> DEFAULT_METHODS =
56-
Collections.unmodifiableList(Arrays.asList(HttpMethod.GET, HttpMethod.HEAD));
56+
private static final List<HttpMethod> DEFAULT_METHODS = Collections.unmodifiableList(
57+
Arrays.asList(HttpMethod.GET, HttpMethod.HEAD));
5758

58-
private static final List<String> DEFAULT_PERMIT_ALL =
59-
Collections.unmodifiableList(Arrays.asList(ALL));
59+
private static final List<String> DEFAULT_PERMIT_METHODS = Collections.unmodifiableList(
60+
Arrays.asList(HttpMethod.GET.name(), HttpMethod.HEAD.name(), HttpMethod.POST.name()));
6061

61-
private static final List<String> DEFAULT_PERMIT_METHODS =
62-
Collections.unmodifiableList(Arrays.asList(HttpMethod.GET.name(), HttpMethod.HEAD.name(), HttpMethod.POST.name()));
62+
private static final List<String> DEFAULT_PERMIT_ALL = Collections.unmodifiableList(
63+
Collections.singletonList(ALL));
6364

6465

6566
@Nullable
@@ -304,6 +305,16 @@ public Boolean getAllowCredentials() {
304305
return this.allowCredentials;
305306
}
306307

308+
/**
309+
* Configure how long, as a duration, the response from a pre-flight request
310+
* can be cached by clients.
311+
* @since 5.2
312+
* @see #setMaxAge(Long)
313+
*/
314+
public void setMaxAge(Duration maxAge) {
315+
this.maxAge = maxAge.getSeconds();
316+
}
317+
307318
/**
308319
* Configure how long, in seconds, the response from a pre-flight request
309320
* can be cached by clients.
@@ -322,22 +333,21 @@ public Long getMaxAge() {
322333
return this.maxAge;
323334
}
324335

336+
325337
/**
326338
* By default a newly created {@code CorsConfiguration} does not permit any
327339
* cross-origin requests and must be configured explicitly to indicate what
328340
* should be allowed.
329-
*
330341
* <p>Use this method to flip the initialization model to start with open
331342
* defaults that permit all cross-origin requests for GET, HEAD, and POST
332343
* requests. Note however that this method will not override any existing
333344
* values already set.
334-
*
335345
* <p>The following defaults are applied if not already set:
336346
* <ul>
337-
* <li>Allow all origins.</li>
338-
* <li>Allow "simple" methods {@code GET}, {@code HEAD} and {@code POST}.</li>
339-
* <li>Allow all headers.</li>
340-
* <li>Set max age to 1800 seconds (30 minutes).</li>
347+
* <li>Allow all origins.</li>
348+
* <li>Allow "simple" methods {@code GET}, {@code HEAD} and {@code POST}.</li>
349+
* <li>Allow all headers.</li>
350+
* <li>Set max age to 1800 seconds (30 minutes).</li>
341351
* </ul>
342352
*/
343353
public CorsConfiguration applyPermitDefaultValues() {
@@ -361,23 +371,19 @@ public CorsConfiguration applyPermitDefaultValues() {
361371
/**
362372
* Combine the non-null properties of the supplied
363373
* {@code CorsConfiguration} with this one.
364-
*
365374
* <p>When combining single values like {@code allowCredentials} or
366375
* {@code maxAge}, {@code this} properties are overridden by non-null
367376
* {@code other} properties if any.
368-
*
369377
* <p>Combining lists like {@code allowedOrigins}, {@code allowedMethods},
370378
* {@code allowedHeaders} or {@code exposedHeaders} is done in an additive
371379
* way. For example, combining {@code ["GET", "POST"]} with
372380
* {@code ["PATCH"]} results in {@code ["GET", "POST", "PATCH"]}, but keep
373381
* in mind that combining {@code ["GET", "POST"]} with {@code ["*"]}
374382
* results in {@code ["*"]}.
375-
*
376383
* <p>Notice that default permit values set by
377384
* {@link CorsConfiguration#applyPermitDefaultValues()} are overridden by
378385
* any value explicitly defined.
379-
*
380-
* @return the combined {@code CorsConfiguration} or {@code this}
386+
* @return the combined {@code CorsConfiguration}, or {@code this}
381387
* configuration if the supplied configuration is {@code null}
382388
*/
383389
@Nullable

spring-web/src/test/java/org/springframework/web/cors/CorsConfigurationTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -47,7 +47,7 @@ public void setNullValues() {
4747
assertNull(config.getExposedHeaders());
4848
config.setAllowCredentials(null);
4949
assertNull(config.getAllowCredentials());
50-
config.setMaxAge(null);
50+
config.setMaxAge((Long) null);
5151
assertNull(config.getMaxAge());
5252
}
5353

0 commit comments

Comments
 (0)