Browse Source

Add set/getContentLanguage() to HttpHeaders

Issue: SPR-14536
pull/1270/merge
Sebastien Deleuze 9 years ago
parent
commit
36da299f96
  1. 26
      spring-web/src/main/java/org/springframework/http/HttpHeaders.java
  2. 11
      spring-web/src/test/java/org/springframework/http/HttpHeadersTests.java

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

@ -762,6 +762,32 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable @@ -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.

11
spring-web/src/test/java/org/springframework/http/HttpHeadersTests.java

@ -439,4 +439,15 @@ public class HttpHeadersTests { @@ -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…
Cancel
Save