Search in sources :

Example 16 with LocaleContext

use of org.springframework.context.i18n.LocaleContext in project spring-framework by spring-projects.

the class CookieLocaleResolverTests method testResolveLocaleContextWithInvalidLocaleOnErrorDispatch.

@Test
public void testResolveLocaleContextWithInvalidLocaleOnErrorDispatch() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addPreferredLocale(Locale.GERMAN);
    request.setAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE, new ServletException());
    Cookie cookie = new Cookie("LanguageKoekje", "++ GMT+1");
    request.setCookies(cookie);
    CookieLocaleResolver resolver = new CookieLocaleResolver();
    resolver.setDefaultTimeZone(TimeZone.getTimeZone("GMT+2"));
    resolver.setCookieName("LanguageKoekje");
    LocaleContext loc = resolver.resolveLocaleContext(request);
    assertThat(loc.getLocale()).isEqualTo(Locale.GERMAN);
    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)

Example 17 with LocaleContext

use of org.springframework.context.i18n.LocaleContext in project spring-framework by spring-projects.

the class CookieLocaleResolverTests method testResolveLocaleContextWithoutCookieAndDefaultLocale.

@Test
public void testResolveLocaleContextWithoutCookieAndDefaultLocale() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addPreferredLocale(Locale.TAIWAN);
    CookieLocaleResolver resolver = new CookieLocaleResolver();
    resolver.setDefaultLocale(Locale.GERMAN);
    resolver.setDefaultTimeZone(TimeZone.getTimeZone("GMT+1"));
    LocaleContext loc = resolver.resolveLocaleContext(request);
    assertThat(loc.getLocale()).isEqualTo(Locale.GERMAN);
    boolean condition = loc instanceof TimeZoneAwareLocaleContext;
    assertThat(condition).isTrue();
    assertThat(((TimeZoneAwareLocaleContext) loc).getTimeZone()).isEqualTo(TimeZone.getTimeZone("GMT+1"));
}
Also used : 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 18 with LocaleContext

use of org.springframework.context.i18n.LocaleContext in project spring-framework by spring-projects.

the class CookieLocaleResolverTests method testSetAndResolveLocaleContextWithTimeZoneOnly.

@Test
public void testSetAndResolveLocaleContextWithTimeZoneOnly() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    CookieLocaleResolver resolver = new CookieLocaleResolver();
    resolver.setLocaleContext(request, response, new SimpleTimeZoneAwareLocaleContext(null, TimeZone.getTimeZone("GMT+1")));
    Cookie cookie = response.getCookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME);
    request = new MockHttpServletRequest();
    request.addPreferredLocale(Locale.GERMANY);
    request.setCookies(cookie);
    resolver = new CookieLocaleResolver();
    LocaleContext loc = resolver.resolveLocaleContext(request);
    assertThat(loc.getLocale()).isEqualTo(Locale.GERMANY);
    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) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) TimeZoneAwareLocaleContext(org.springframework.context.i18n.TimeZoneAwareLocaleContext) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 19 with LocaleContext

use of org.springframework.context.i18n.LocaleContext in project spring-framework by spring-projects.

the class ServerWebExchangeMethodArgumentResolver method resolveArgumentValue.

@Override
public Object resolveArgumentValue(MethodParameter methodParameter, BindingContext context, ServerWebExchange exchange) {
    Class<?> paramType = methodParameter.getParameterType();
    if (ServerWebExchange.class.isAssignableFrom(paramType)) {
        return exchange;
    } else if (ServerHttpRequest.class.isAssignableFrom(paramType)) {
        return exchange.getRequest();
    } else if (ServerHttpResponse.class.isAssignableFrom(paramType)) {
        return exchange.getResponse();
    } else if (HttpMethod.class == paramType) {
        return exchange.getRequest().getMethod();
    } else if (Locale.class == paramType) {
        return exchange.getLocaleContext().getLocale();
    } else if (TimeZone.class == paramType) {
        LocaleContext localeContext = exchange.getLocaleContext();
        TimeZone timeZone = getTimeZone(localeContext);
        return (timeZone != null ? timeZone : TimeZone.getDefault());
    } else if (ZoneId.class == paramType) {
        LocaleContext localeContext = exchange.getLocaleContext();
        TimeZone timeZone = getTimeZone(localeContext);
        return (timeZone != null ? timeZone.toZoneId() : ZoneId.systemDefault());
    } else if (UriBuilder.class == paramType || UriComponentsBuilder.class == paramType) {
        URI uri = exchange.getRequest().getURI();
        String contextPath = exchange.getRequest().getPath().contextPath().value();
        return UriComponentsBuilder.fromUri(uri).replacePath(contextPath).replaceQuery(null);
    } else {
        // should never happen...
        throw new IllegalArgumentException("Unknown parameter type: " + paramType + " in method: " + methodParameter.getMethod());
    }
}
Also used : TimeZone(java.util.TimeZone) TimeZoneAwareLocaleContext(org.springframework.context.i18n.TimeZoneAwareLocaleContext) LocaleContext(org.springframework.context.i18n.LocaleContext) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) URI(java.net.URI) HttpMethod(org.springframework.http.HttpMethod)

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