Search in sources :

Example 11 with LocaleContext

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);
    }
}
Also used : Locale(java.util.Locale) LocaleContext(org.springframework.context.i18n.LocaleContext)

Example 12 with LocaleContext

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"));
    });
}
Also used : LocaleContext(org.springframework.context.i18n.LocaleContext) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) FixedLocaleContextResolver(org.springframework.web.server.i18n.FixedLocaleContextResolver) LocaleContextResolver(org.springframework.web.server.i18n.LocaleContextResolver) AcceptHeaderLocaleContextResolver(org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 13 with LocaleContext

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"));
    });
}
Also used : LocaleContext(org.springframework.context.i18n.LocaleContext) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) FixedLocaleContextResolver(org.springframework.web.server.i18n.FixedLocaleContextResolver) LocaleContextResolver(org.springframework.web.server.i18n.LocaleContextResolver) AcceptHeaderLocaleContextResolver(org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 14 with LocaleContext

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"));
}
Also used : Cookie(jakarta.servlet.http.Cookie) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) SimpleLocaleContext(org.springframework.context.i18n.SimpleLocaleContext) TimeZoneAwareLocaleContext(org.springframework.context.i18n.TimeZoneAwareLocaleContext) LocaleContext(org.springframework.context.i18n.LocaleContext) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) TimeZoneAwareLocaleContext(org.springframework.context.i18n.TimeZoneAwareLocaleContext) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test)

Example 15 with LocaleContext

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"));
}
Also used : ServletException(jakarta.servlet.ServletException) Cookie(jakarta.servlet.http.Cookie) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) SimpleLocaleContext(org.springframework.context.i18n.SimpleLocaleContext) TimeZoneAwareLocaleContext(org.springframework.context.i18n.TimeZoneAwareLocaleContext) LocaleContext(org.springframework.context.i18n.LocaleContext) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) TimeZoneAwareLocaleContext(org.springframework.context.i18n.TimeZoneAwareLocaleContext) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test)

Aggregations

LocaleContext (org.springframework.context.i18n.LocaleContext)19 Test (org.junit.jupiter.api.Test)13 TimeZoneAwareLocaleContext (org.springframework.context.i18n.TimeZoneAwareLocaleContext)13 SimpleLocaleContext (org.springframework.context.i18n.SimpleLocaleContext)12 SimpleTimeZoneAwareLocaleContext (org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext)12 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)10 Cookie (jakarta.servlet.http.Cookie)8 Locale (java.util.Locale)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 MockServerHttpRequest (org.springframework.mock.http.server.reactive.MockServerHttpRequest)3 MockServerWebExchange (org.springframework.mock.web.server.MockServerWebExchange)3 AcceptHeaderLocaleContextResolver (org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver)3 FixedLocaleContextResolver (org.springframework.web.server.i18n.FixedLocaleContextResolver)3 LocaleContextResolver (org.springframework.web.server.i18n.LocaleContextResolver)3 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)3 ServletException (jakarta.servlet.ServletException)2 LocaleContextResolver (org.springframework.web.servlet.LocaleContextResolver)2 IOException (java.io.IOException)1 URI (java.net.URI)1 TimeZone (java.util.TimeZone)1