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"));
}
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"));
}
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"));
}
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());
}
}
Aggregations