From f806594e601b364dc3f57c4199953f00ca4484a9 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Wed, 22 May 2019 15:26:45 +0200 Subject: [PATCH] Introduce getOrEmpty(String) convenience method in HttpHeaders This commit introduces a getOrEmpty(String) method in HttpHeaders that returns an immutable, empty list if no values are present for the specified header name. This is provided as a convenience over the existing get(String) method which returns null in such cases. Closes gh-22949 --- .../org/springframework/http/HttpHeaders.java | 12 ++++++++++++ .../springframework/http/HttpHeadersTests.java | 17 +++++++++++++++++ 2 files changed, 29 insertions(+) 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 d966c245c22..20ce83d5ad3 100644 --- a/spring-web/src/main/java/org/springframework/http/HttpHeaders.java +++ b/spring-web/src/main/java/org/springframework/http/HttpHeaders.java @@ -71,6 +71,7 @@ import org.springframework.util.StringUtils; * @author Brian Clozel * @author Juergen Hoeller * @author Josh Long + * @author Sam Brannen * @since 3.0 */ public class HttpHeaders implements MultiValueMap, Serializable { @@ -437,6 +438,17 @@ public class HttpHeaders implements MultiValueMap, Serializable } + /** + * Get the list of header values for the given header name, if any. + * @param headerName the header name + * @return the list of header values, or an empty list + * @since 5.2 + */ + public List getOrEmpty(Object headerName) { + List values = get(headerName); + return (values != null ? values : Collections.emptyList()); + } + /** * Set the list of acceptable {@linkplain MediaType media types}, * as specified by the {@code Accept} header. diff --git a/spring-web/src/test/java/org/springframework/http/HttpHeadersTests.java b/spring-web/src/test/java/org/springframework/http/HttpHeadersTests.java index 8f2338d0e38..3d9f6e6ce58 100644 --- a/spring-web/src/test/java/org/springframework/http/HttpHeadersTests.java +++ b/spring-web/src/test/java/org/springframework/http/HttpHeadersTests.java @@ -39,8 +39,10 @@ import org.junit.Test; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.emptyCollectionOf; import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.nullValue; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -61,6 +63,21 @@ public class HttpHeadersTests { private final HttpHeaders headers = new HttpHeaders(); + @Test + public void getOrEmpty() { + String key = "FOO"; + + assertThat(headers.get(key), is(nullValue())); + assertThat(headers.getOrEmpty(key), is(empty())); + + headers.add(key, "bar"); + assertThat(headers.getOrEmpty(key), is(Arrays.asList("bar"))); + + headers.remove(key); + assertThat(headers.get(key), is(nullValue())); + assertThat(headers.getOrEmpty(key), is(empty())); + } + @Test public void getFirst() { headers.add(HttpHeaders.CACHE_CONTROL, "max-age=1000, public");