Add set/getContentLanguage() to HttpHeaders
Issue: SPR-14536
This commit is contained in:
parent
9764d57433
commit
36da299f96
|
@ -762,6 +762,32 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
|
|||
return ContentDisposition.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the {@link Locale} of the content language,
|
||||
* as specified by the {@literal Content-Language} header.
|
||||
* <p>Use {@code set(CONTENT_LANGUAGE, ...)} if you need
|
||||
* to set multiple content languages.</p>
|
||||
*/
|
||||
public void setContentLanguage(Locale locale) {
|
||||
Assert.notNull(locale, "'locale' must not be null");
|
||||
set(CONTENT_LANGUAGE, locale.toLanguageTag());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first {@link Locale} of the content languages,
|
||||
* as specified by the {@literal Content-Language} header.
|
||||
* <p>Returns {@code null} when the content language is unknown.
|
||||
* <p>Use {@code getValuesAsList(CONTENT_LANGUAGE)} if you need
|
||||
* to get multiple content languages.</p>
|
||||
*/
|
||||
public Locale getContentLanguage() {
|
||||
return getValuesAsList(CONTENT_LANGUAGE)
|
||||
.stream()
|
||||
.findFirst()
|
||||
.map(Locale::forLanguageTag)
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the length of the body in bytes, as specified by the
|
||||
* {@code Content-Length} header.
|
||||
|
|
|
@ -439,4 +439,15 @@ public class HttpHeadersTests {
|
|||
assertArrayEquals(languageArray, languages.toArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void contentLanguage() {
|
||||
assertNull(headers.getContentLanguage());
|
||||
headers.setContentLanguage(Locale.FRANCE);
|
||||
assertEquals(Locale.FRANCE, headers.getContentLanguage());
|
||||
assertEquals("fr-FR", headers.getFirst(HttpHeaders.CONTENT_LANGUAGE));
|
||||
headers.clear();
|
||||
headers.set(HttpHeaders.CONTENT_LANGUAGE, Locale.GERMAN.toLanguageTag() + ", " + Locale.CANADA);
|
||||
assertEquals(Locale.GERMAN, headers.getContentLanguage());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue