Add ServerRequest::firstHeader

This commit introduces the method firstHeaderiin both WebMvc.fn and
WebFlux.fn, which return the first header value of a given header name,
if any.
This commit is contained in:
Arjen Poutsma 2020-03-10 12:06:11 +01:00
parent b92515bdee
commit fc12891006
4 changed files with 32 additions and 4 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@ -371,6 +371,18 @@ public interface ServerRequest {
*/
List<String> header(String headerName);
/**
* Get the first header value, if any, for the header for the given name.
* <p>Returns {@code null} if no header values are found.
* @param headerName the header name
* @since 5.2.5
*/
@Nullable
default String firstHeader(String headerName) {
List<String> list = header(headerName);
return list.isEmpty() ? null : list.get(0);
}
/**
* Get the headers as an instance of {@link HttpHeaders}.
*/

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@ -208,6 +208,8 @@ public class DefaultServerRequestTests {
assertThat(headers.acceptCharset()).isEqualTo(acceptCharset);
assertThat(headers.contentLength()).isEqualTo(OptionalLong.of(contentLength));
assertThat(headers.contentType()).isEqualTo(Optional.of(contentType));
assertThat(headers.header(HttpHeaders.CONTENT_TYPE)).containsExactly(MediaType.TEXT_PLAIN_VALUE);
assertThat(headers.firstHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo(MediaType.TEXT_PLAIN_VALUE);
assertThat(headers.asHttpHeaders()).isEqualTo(httpHeaders);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@ -307,6 +307,18 @@ public interface ServerRequest {
*/
List<String> header(String headerName);
/**
* Get the first header value, if any, for the header for the given name.
* <p>Returns {@code null} if no header values are found.
* @param headerName the header name
* @since 5.2.5
*/
@Nullable
default String firstHeader(String headerName) {
List<String> list = header(headerName);
return list.isEmpty() ? null : list.get(0);
}
/**
* Get the headers as an instance of {@link HttpHeaders}.
*/

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@ -209,6 +209,8 @@ public class DefaultServerRequestTests {
assertThat(headers.acceptCharset()).isEqualTo(acceptCharset);
assertThat(headers.contentLength()).isEqualTo(OptionalLong.of(contentLength));
assertThat(headers.contentType()).isEqualTo(Optional.of(contentType));
assertThat(headers.header(HttpHeaders.CONTENT_TYPE)).containsExactly(MediaType.TEXT_PLAIN_VALUE);
assertThat(headers.firstHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo(MediaType.TEXT_PLAIN_VALUE);
assertThat(headers.asHttpHeaders()).isEqualTo(httpHeaders);
}