use of org.springframework.context.i18n.LocaleContext in project spring-framework by spring-projects.
the class SimpleHttpInvokerRequestExecutor method prepareConnection.
/**
* Prepare the given HTTP connection.
* <p>The default implementation specifies POST as method,
* "application/x-java-serialized-object" as "Content-Type" header,
* and the given content length as "Content-Length" header.
* @param connection the HTTP connection to prepare
* @param contentLength the length of the content to send
* @throws IOException if thrown by HttpURLConnection methods
* @see java.net.HttpURLConnection#setRequestMethod
* @see java.net.HttpURLConnection#setRequestProperty
*/
protected void prepareConnection(HttpURLConnection connection, int contentLength) throws IOException {
if (this.connectTimeout >= 0) {
connection.setConnectTimeout(this.connectTimeout);
}
if (this.readTimeout >= 0) {
connection.setReadTimeout(this.readTimeout);
}
connection.setDoOutput(true);
connection.setRequestMethod(HTTP_METHOD_POST);
connection.setRequestProperty(HTTP_HEADER_CONTENT_TYPE, getContentType());
connection.setRequestProperty(HTTP_HEADER_CONTENT_LENGTH, Integer.toString(contentLength));
LocaleContext localeContext = LocaleContextHolder.getLocaleContext();
if (localeContext != null) {
Locale locale = localeContext.getLocale();
if (locale != null) {
connection.setRequestProperty(HTTP_HEADER_ACCEPT_LANGUAGE, StringUtils.toLanguageTag(locale));
}
}
if (isAcceptGzipEncoding()) {
connection.setRequestProperty(HTTP_HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
}
}
use of org.springframework.context.i18n.LocaleContext in project spring-boot by spring-projects.
the class WebFluxAutoConfigurationTests method whenFixedLocalContextResolverIsUsedThenAcceptLanguagesHeaderIsIgnored.
@Test
void whenFixedLocalContextResolverIsUsedThenAcceptLanguagesHeaderIsIgnored() {
this.contextRunner.withPropertyValues("spring.web.locale:en_UK", "spring.web.locale-resolver=fixed").run((context) -> {
MockServerHttpRequest request = MockServerHttpRequest.get("/").acceptLanguageAsLocales(StringUtils.parseLocaleString("nl_NL")).build();
MockServerWebExchange exchange = MockServerWebExchange.from(request);
LocaleContextResolver localeContextResolver = context.getBean(LocaleContextResolver.class);
assertThat(localeContextResolver).isInstanceOf(FixedLocaleContextResolver.class);
LocaleContext localeContext = localeContextResolver.resolveLocaleContext(exchange);
assertThat(localeContext.getLocale()).isEqualTo(StringUtils.parseLocaleString("en_UK"));
});
}
use of org.springframework.context.i18n.LocaleContext in project spring-boot by spring-projects.
the class WebFluxAutoConfigurationTests method whenAcceptHeaderLocaleContextResolverIsUsedAndHeaderIsAbsentThenConfiguredLocaleIsUsed.
@Test
void whenAcceptHeaderLocaleContextResolverIsUsedAndHeaderIsAbsentThenConfiguredLocaleIsUsed() {
this.contextRunner.withPropertyValues("spring.web.locale:en_UK").run((context) -> {
MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
MockServerWebExchange exchange = MockServerWebExchange.from(request);
LocaleContextResolver localeContextResolver = context.getBean(LocaleContextResolver.class);
assertThat(localeContextResolver).isInstanceOf(AcceptHeaderLocaleContextResolver.class);
LocaleContext localeContext = localeContextResolver.resolveLocaleContext(exchange);
assertThat(localeContext.getLocale()).isEqualTo(StringUtils.parseLocaleString("en_UK"));
});
}
use of org.springframework.context.i18n.LocaleContext in project spring-framework by spring-projects.
the class CookieLocaleResolverTests method testResolveLocaleContextWithTimeZone.
@Test
public void testResolveLocaleContextWithTimeZone() {
MockHttpServletRequest request = new MockHttpServletRequest();
Cookie cookie = new Cookie("LanguageKoekje", "nl GMT+1");
request.setCookies(cookie);
CookieLocaleResolver resolver = new CookieLocaleResolver();
resolver.setCookieName("LanguageKoekje");
LocaleContext loc = resolver.resolveLocaleContext(request);
assertThat(loc.getLocale().getLanguage()).isEqualTo("nl");
boolean condition = loc instanceof TimeZoneAwareLocaleContext;
assertThat(condition).isTrue();
assertThat(((TimeZoneAwareLocaleContext) loc).getTimeZone()).isEqualTo(TimeZone.getTimeZone("GMT+1"));
}
use of org.springframework.context.i18n.LocaleContext in project spring-framework by spring-projects.
the class CookieLocaleResolverTests method testResolveLocaleContextWithInvalidTimeZoneOnErrorDispatch.
@Test
public void testResolveLocaleContextWithInvalidTimeZoneOnErrorDispatch() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE, new ServletException());
Cookie cookie = new Cookie("LanguageKoekje", "nl X-MT");
request.setCookies(cookie);
CookieLocaleResolver resolver = new CookieLocaleResolver();
resolver.setDefaultTimeZone(TimeZone.getTimeZone("GMT+2"));
resolver.setCookieName("LanguageKoekje");
LocaleContext loc = resolver.resolveLocaleContext(request);
assertThat(loc.getLocale().getLanguage()).isEqualTo("nl");
boolean condition = loc instanceof TimeZoneAwareLocaleContext;
assertThat(condition).isTrue();
assertThat(((TimeZoneAwareLocaleContext) loc).getTimeZone()).isEqualTo(TimeZone.getTimeZone("GMT+2"));
}
Aggregations