Here are the examples of the java api org.springframework.context.i18n.LocaleContext taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
67 Examples
18
Source : ServerWebExchangeMethodArgumentResolver.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@Nullable
private TimeZone getTimeZone(LocaleContext localeContext) {
TimeZone timeZone = null;
if (localeContext instanceof TimeZoneAwareLocaleContext) {
timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
}
return timeZone;
}
18
Source : FixedLocaleContextResolver.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@Override
public void setLocaleContext(ServerWebExchange exchange, @Nullable LocaleContext localeContext) {
throw new UnsupportedOperationException("Cannot change fixed locale - use a different locale context resolution strategy");
}
18
Source : AcceptHeaderLocaleContextResolver.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@Override
public void setLocaleContext(ServerWebExchange exchange, @Nullable LocaleContext locale) {
throw new UnsupportedOperationException("Cannot change HTTP accept header - use a different locale context resolution strategy");
}
17
Source : RequestContextUtils.java
with Apache License 2.0
from langtianya
with Apache License 2.0
from langtianya
/**
* Retrieve the current time zone from the given request, using the
* TimeZoneAwareLocaleResolver bound to the request by the DispatcherServlet
* (if available), falling back to the system's default time zone.
* <p>Note: This method returns {@code null} if no specific time zone can be
* resolved for the given request. This is in contrast to {@link #getLocale}
* where there is always the request's accept-header locale to fall back to.
* <p>Consider using {@link org.springframework.context.i18n.LocaleContextHolder#getTimeZone()}
* which will normally be populated with the same TimeZone: That method only
* differs in terms of its fallback to the system time zone if the LocaleResolver
* hasn't provided provided a specific time zone (instead of this method's {@code null}).
* @param request current HTTP request
* @return the current time zone for the given request, either from the
* TimeZoneAwareLocaleResolver or {@code null} if none replacedociated
* @see #getLocaleResolver
* @see org.springframework.context.i18n.LocaleContextHolder#getTimeZone()
*/
public static TimeZone getTimeZone(HttpServletRequest request) {
LocaleResolver localeResolver = getLocaleResolver(request);
if (localeResolver instanceof LocaleContextResolver) {
LocaleContext localeContext = ((LocaleContextResolver) localeResolver).resolveLocaleContext(request);
if (localeContext instanceof TimeZoneAwareLocaleContext) {
return ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
}
}
return null;
}
16
Source : MailService.java
with Apache License 2.0
from xm-online
with Apache License 2.0
from xm-online
private MailParams resolve(String subject, String from, String templateName, Locale locale) {
MailParams mailParams = new MailParams(subject, from);
List<MailSetting> settings = tenantConfigService.getXmEnreplacedyTenantConfig().getMailSettings();
LocaleContext localeContext = getLocaleContext();
setLocale(locale);
mailParams = settings.stream().filter(it -> templateName.equals(it.getTemplateName())).findFirst().map(mailSetting -> new MailParams(getI18nName(mailSetting.getSubject()).orElse(subject), getI18nName(mailSetting.getFrom()).orElse(from))).orElse(mailParams);
setLocaleContext(localeContext);
return mailParams;
}
16
Source : RequestContextUtils.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
/**
* Retrieve the current time zone from the given request, using the
* TimeZoneAwareLocaleResolver bound to the request by the DispatcherServlet
* (if available), falling back to the system's default time zone.
* <p>Note: This method returns {@code null} if no specific time zone can be
* resolved for the given request. This is in contrast to {@link #getLocale}
* where there is always the request's accept-header locale to fall back to.
* <p>Consider using {@link org.springframework.context.i18n.LocaleContextHolder#getTimeZone()}
* which will normally be populated with the same TimeZone: That method only
* differs in terms of its fallback to the system time zone if the LocaleResolver
* hasn't provided a specific time zone (instead of this method's {@code null}).
* @param request current HTTP request
* @return the current time zone for the given request, either from the
* TimeZoneAwareLocaleResolver or {@code null} if none replacedociated
* @see #getLocaleResolver
* @see org.springframework.context.i18n.LocaleContextHolder#getTimeZone()
*/
@Nullable
public static TimeZone getTimeZone(HttpServletRequest request) {
LocaleResolver localeResolver = getLocaleResolver(request);
if (localeResolver instanceof LocaleContextResolver) {
LocaleContext localeContext = ((LocaleContextResolver) localeResolver).resolveLocaleContext(request);
if (localeContext instanceof TimeZoneAwareLocaleContext) {
return ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
}
}
return null;
}
16
Source : FixedLocaleResolver.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@Override
public void setLocaleContext(HttpServletRequest request, @Nullable HttpServletResponse response, @Nullable LocaleContext localeContext) {
throw new UnsupportedOperationException("Cannot change fixed locale - use a different locale resolution strategy");
}
16
Source : DateTimeContext.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
/**
* Get the DateTimeFormatter with the this context's settings
* applied to the base {@code formatter}.
* @param formatter the base formatter that establishes default
* formatting rules, generally context-independent
* @return the contextual DateTimeFormatter
*/
public DateTimeFormatter getFormatter(DateTimeFormatter formatter) {
if (this.chronology != null) {
formatter = formatter.withChronology(this.chronology);
}
if (this.timeZone != null) {
formatter = formatter.withZone(this.timeZone);
} else {
LocaleContext localeContext = LocaleContextHolder.getLocaleContext();
if (localeContext instanceof TimeZoneAwareLocaleContext) {
TimeZone timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
if (timeZone != null) {
formatter = formatter.withZone(timeZone.toZoneId());
}
}
}
return formatter;
}
16
Source : JodaTimeContext.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
/**
* Get the DateTimeFormatter with the this context's settings
* applied to the base {@code formatter}.
* @param formatter the base formatter that establishes default
* formatting rules, generally context-independent
* @return the contextual DateTimeFormatter
*/
public DateTimeFormatter getFormatter(DateTimeFormatter formatter) {
if (this.chronology != null) {
formatter = formatter.withChronology(this.chronology);
}
if (this.timeZone != null) {
formatter = formatter.withZone(this.timeZone);
} else {
LocaleContext localeContext = LocaleContextHolder.getLocaleContext();
if (localeContext instanceof TimeZoneAwareLocaleContext) {
TimeZone timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
if (timeZone != null) {
formatter = formatter.withZone(DateTimeZone.forTimeZone(timeZone));
}
}
}
return formatter;
}
16
Source : PortletRequestContextUtils.java
with Apache License 2.0
from liferay
with Apache License 2.0
from liferay
/**
* Retrieve the current time zone from the given request, using the TimeZoneAwareLocaleResolver bound to the request
* by the DispatcherServlet (if available), falling back to the system's default time zone.
*
* <p>Note: This method returns {@code null} if no specific time zone can be resolved for the given request. This is
* in contrast to {@link #getLocale} where there is always the request's accept-header locale to fall back to.
*
* <p>Consider using {@link org.springframework.context.i18n.LocaleContextHolder#getTimeZone()} which will normally
* be populated with the same TimeZone: That method only differs in terms of its fallback to the system time zone if
* the PortletLocaleResolver hasn't provided a specific time zone (instead of this method's {@code null}).
*
* @param request current HTTP request
*
* @return the current time zone for the given request, either from the TimeZoneAwareLocaleResolver or {@code null}
* if none replacedociated
*
* @see #getPortletLocaleResolver
* @see org.springframework.context.i18n.LocaleContextHolder#getTimeZone()
*/
@Nullable
public static TimeZone getTimeZone(PortletRequest request) {
PortletLocaleResolver localeResolver = getPortletLocaleResolver(request);
if (localeResolver instanceof LocaleContextResolver) {
LocaleContext localeContext = ((PortletLocaleContextResolver) localeResolver).resolveLocaleContext(request);
if (localeContext instanceof TimeZoneAwareLocaleContext) {
return ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
}
}
return null;
}
16
Source : FixedLocaleResolver.java
with Apache License 2.0
from langtianya
with Apache License 2.0
from langtianya
@Override
public void setLocaleContext(HttpServletRequest request, HttpServletResponse response, LocaleContext localeContext) {
throw new UnsupportedOperationException("Cannot change fixed locale - use a different locale resolution strategy");
}
16
Source : LocalizationConfiguration.java
with MIT License
from AlicanAkkus
with MIT License
from AlicanAkkus
@Override
public void setLocaleContext(ServerWebExchange exchange, LocaleContext localeContext) {
LocaleContextHolder.setLocale(localeContext.getLocale());
}
15
Source : CookieLocaleResolverTests.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@Test
public void testResolveLocaleContext() {
MockHttpServletRequest request = new MockHttpServletRequest();
Cookie cookie = new Cookie("LanguageKoekje", "nl");
request.setCookies(cookie);
CookieLocaleResolver resolver = new CookieLocaleResolver();
resolver.setCookieName("LanguageKoekje");
LocaleContext loc = resolver.resolveLocaleContext(request);
replacedertEquals("nl", loc.getLocale().getLanguage());
replacedertTrue(loc instanceof TimeZoneAwareLocaleContext);
replacedertNull(((TimeZoneAwareLocaleContext) loc).getTimeZone());
}
15
Source : FrameworkServlet.java
with MIT License
from mindcarver
with MIT License
from mindcarver
private void resetContextHolders(HttpServletRequest request, @Nullable LocaleContext prevLocaleContext, @Nullable RequestAttributes previousAttributes) {
LocaleContextHolder.setLocaleContext(prevLocaleContext, this.threadContextInheritable);
RequestContextHolder.setRequestAttributes(previousAttributes, this.threadContextInheritable);
}
15
Source : FrameworkServlet.java
with MIT License
from mindcarver
with MIT License
from mindcarver
private void initContextHolders(HttpServletRequest request, @Nullable LocaleContext localeContext, @Nullable RequestAttributes requestAttributes) {
if (localeContext != null) {
LocaleContextHolder.setLocaleContext(localeContext, this.threadContextInheritable);
}
if (requestAttributes != null) {
RequestContextHolder.setRequestAttributes(requestAttributes, this.threadContextInheritable);
}
}
15
Source : CookieLocaleResolverTests.java
with Apache License 2.0
from langtianya
with Apache License 2.0
from langtianya
@Test
public void testResolveLocaleContext() {
MockHttpServletRequest request = new MockHttpServletRequest();
Cookie cookie = new Cookie("LanguageKoekje", "nl");
request.setCookies(cookie);
CookieLocaleResolver resolver = new CookieLocaleResolver();
// yup, koekje is the Dutch name for Cookie ;-)
resolver.setCookieName("LanguageKoekje");
LocaleContext loc = resolver.resolveLocaleContext(request);
replacedertEquals("nl", loc.getLocale().getLanguage());
replacedertTrue(loc instanceof TimeZoneAwareLocaleContext);
replacedertNull(((TimeZoneAwareLocaleContext) loc).getTimeZone());
}
15
Source : FrameworkServlet.java
with Apache License 2.0
from langtianya
with Apache License 2.0
from langtianya
private void initContextHolders(HttpServletRequest request, LocaleContext localeContext, RequestAttributes requestAttributes) {
if (localeContext != null) {
LocaleContextHolder.setLocaleContext(localeContext, this.threadContextInheritable);
}
if (requestAttributes != null) {
RequestContextHolder.setRequestAttributes(requestAttributes, this.threadContextInheritable);
}
if (logger.isTraceEnabled()) {
logger.trace("Bound request context to thread: " + request);
}
}
15
Source : FrameworkServlet.java
with Apache License 2.0
from langtianya
with Apache License 2.0
from langtianya
private void resetContextHolders(HttpServletRequest request, LocaleContext prevLocaleContext, RequestAttributes previousAttributes) {
LocaleContextHolder.setLocaleContext(prevLocaleContext, this.threadContextInheritable);
RequestContextHolder.setRequestAttributes(previousAttributes, this.threadContextInheritable);
if (logger.isTraceEnabled()) {
logger.trace("Cleared thread-bound request context: " + request);
}
}
15
Source : AngularCookieLocaleResolverTest.java
with Apache License 2.0
from jhipster
with Apache License 2.0
from jhipster
@Test
public void testDefaults() {
when(request.getCookies()).thenReturn(new Cookie[] {});
LocaleContext context = resolver.resolveLocaleContext(request);
replacedertThat(context).isNotNull();
replacedertThat(context).isInstanceOf(TimeZoneAwareLocaleContext.clreplaced);
replacedertThat(((TimeZoneAwareLocaleContext) context).getLocale()).isEqualTo(LOCALE_DEFAULT);
replacedertThat(((TimeZoneAwareLocaleContext) context).getTimeZone()).isEqualTo(TIMEZONE_DEFAULT);
List<Event> events = recorder.play();
replacedertThat(events).isEmpty();
}
14
Source : CookieLocaleResolverTests.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@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);
replacedertEquals("nl", loc.getLocale().getLanguage());
replacedertTrue(loc instanceof TimeZoneAwareLocaleContext);
replacedertEquals(TimeZone.getTimeZone("GMT+1"), ((TimeZoneAwareLocaleContext) loc).getTimeZone());
}
14
Source : CookieLocaleResolverTests.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@Test
public void testResolveLocaleContextWithoutCookie() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addPreferredLocale(Locale.TAIWAN);
CookieLocaleResolver resolver = new CookieLocaleResolver();
LocaleContext loc = resolver.resolveLocaleContext(request);
replacedertEquals(request.getLocale(), loc.getLocale());
replacedertTrue(loc instanceof TimeZoneAwareLocaleContext);
replacedertNull(((TimeZoneAwareLocaleContext) loc).getTimeZone());
}
14
Source : CookieLocaleResolverTests.java
with Apache License 2.0
from SourceHot
with Apache License 2.0
from SourceHot
@Test
public void testResolveLocaleContext() {
MockHttpServletRequest request = new MockHttpServletRequest();
Cookie cookie = new Cookie("LanguageKoekje", "nl");
request.setCookies(cookie);
CookieLocaleResolver resolver = new CookieLocaleResolver();
resolver.setCookieName("LanguageKoekje");
LocaleContext loc = resolver.resolveLocaleContext(request);
replacedertThat(loc.getLocale().getLanguage()).isEqualTo("nl");
boolean condition = loc instanceof TimeZoneAwareLocaleContext;
replacedertThat(condition).isTrue();
replacedertThat(((TimeZoneAwareLocaleContext) loc).getTimeZone()).isNull();
}
14
Source : CookieLocaleResolverTests.java
with Apache License 2.0
from langtianya
with Apache License 2.0
from langtianya
@Test
public void testResolveLocaleContextWithTimeZone() {
MockHttpServletRequest request = new MockHttpServletRequest();
Cookie cookie = new Cookie("LanguageKoekje", "nl GMT+1");
request.setCookies(cookie);
CookieLocaleResolver resolver = new CookieLocaleResolver();
// yup, koekje is the Dutch name for Cookie ;-)
resolver.setCookieName("LanguageKoekje");
LocaleContext loc = resolver.resolveLocaleContext(request);
replacedertEquals("nl", loc.getLocale().getLanguage());
replacedertTrue(loc instanceof TimeZoneAwareLocaleContext);
replacedertEquals(TimeZone.getTimeZone("GMT+1"), ((TimeZoneAwareLocaleContext) loc).getTimeZone());
}
14
Source : CookieLocaleResolverTests.java
with Apache License 2.0
from langtianya
with Apache License 2.0
from langtianya
@Test
public void testResolveLocaleContextWithoutCookie() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addPreferredLocale(Locale.TAIWAN);
CookieLocaleResolver resolver = new CookieLocaleResolver();
LocaleContext loc = resolver.resolveLocaleContext(request);
replacedertEquals(request.getLocale(), loc.getLocale());
replacedertTrue(loc instanceof TimeZoneAwareLocaleContext);
replacedertNull(((TimeZoneAwareLocaleContext) loc).getTimeZone());
}
14
Source : RequestContext.java
with Apache License 2.0
from langtianya
with Apache License 2.0
from langtianya
/**
* Initialize this context with the given request, using the given model attributes for Errors retrieval.
* <p>Delegates to {@code getFallbackLocale} and {@code getFallbackTheme} for determining the fallback
* locale and theme, respectively, if no LocaleResolver and/or ThemeResolver can be found in the request.
* @param request current HTTP request
* @param servletContext the servlet context of the web application (can be {@code null}; necessary for
* fallback to root WebApplicationContext)
* @param model the model attributes for the current view (can be {@code null}, using the request attributes
* for Errors retrieval)
* @see #getFallbackLocale
* @see #getFallbackTheme
* @see org.springframework.web.servlet.DispatcherServlet#LOCALE_RESOLVER_ATTRIBUTE
* @see org.springframework.web.servlet.DispatcherServlet#THEME_RESOLVER_ATTRIBUTE
*/
protected void initContext(HttpServletRequest request, HttpServletResponse response, ServletContext servletContext, Map<String, Object> model) {
this.request = request;
this.response = response;
this.model = model;
// Fetch WebApplicationContext, either from DispatcherServlet or the root context.
// ServletContext needs to be specified to be able to fall back to the root context!
this.webApplicationContext = (WebApplicationContext) request.getAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE);
if (this.webApplicationContext == null) {
this.webApplicationContext = RequestContextUtils.findWebApplicationContext(request, servletContext);
if (this.webApplicationContext == null) {
throw new IllegalStateException("No WebApplicationContext found: not in a DispatcherServlet " + "request and no ContextLoaderListener registered?");
}
}
// Determine locale to use for this RequestContext.
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
if (localeResolver instanceof LocaleContextResolver) {
LocaleContext localeContext = ((LocaleContextResolver) localeResolver).resolveLocaleContext(request);
this.locale = localeContext.getLocale();
if (localeContext instanceof TimeZoneAwareLocaleContext) {
this.timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
}
} else if (localeResolver != null) {
// Try LocaleResolver (we're within a DispatcherServlet request).
this.locale = localeResolver.resolveLocale(request);
}
// Try JSTL fallbacks if necessary.
if (this.locale == null) {
this.locale = getFallbackLocale();
}
if (this.timeZone == null) {
this.timeZone = getFallbackTimeZone();
}
// Determine default HTML escape setting from the "defaultHtmlEscape"
// context-param in web.xml, if any.
this.defaultHtmlEscape = WebUtils.getDefaultHtmlEscape(this.webApplicationContext.getServletContext());
// Determine response-encoded HTML escape setting from the "responseEncodedHtmlEscape"
// context-param in web.xml, if any.
this.responseEncodedHtmlEscape = WebUtils.getResponseEncodedHtmlEscape(this.webApplicationContext.getServletContext());
this.urlPathHelper = new UrlPathHelper();
if (this.webApplicationContext.containsBean(RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME)) {
this.requestDataValueProcessor = this.webApplicationContext.getBean(RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME, RequestDataValueProcessor.clreplaced);
}
}
13
Source : CookieLocaleResolverTests.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@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);
replacedertEquals(Locale.GERMAN, loc.getLocale());
replacedertTrue(loc instanceof TimeZoneAwareLocaleContext);
replacedertEquals(TimeZone.getTimeZone("GMT+1"), ((TimeZoneAwareLocaleContext) loc).getTimeZone());
}
13
Source : SessionLocaleResolver.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@Override
public void setLocaleContext(HttpServletRequest request, @Nullable HttpServletResponse response, @Nullable LocaleContext localeContext) {
Locale locale = null;
TimeZone timeZone = null;
if (localeContext != null) {
locale = localeContext.getLocale();
if (localeContext instanceof TimeZoneAwareLocaleContext) {
timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
}
}
WebUtils.setSessionAttribute(request, this.localeAttributeName, locale);
WebUtils.setSessionAttribute(request, this.timeZoneAttributeName, timeZone);
}
13
Source : CookieLocaleResolverTests.java
with Apache License 2.0
from SourceHot
with Apache License 2.0
from SourceHot
@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);
replacedertThat(loc.getLocale().getLanguage()).isEqualTo("nl");
boolean condition = loc instanceof TimeZoneAwareLocaleContext;
replacedertThat(condition).isTrue();
replacedertThat(((TimeZoneAwareLocaleContext) loc).getTimeZone()).isEqualTo(TimeZone.getTimeZone("GMT+1"));
}
13
Source : CookieLocaleResolverTests.java
with Apache License 2.0
from SourceHot
with Apache License 2.0
from SourceHot
@Test
public void testResolveLocaleContextWithoutCookie() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addPreferredLocale(Locale.TAIWAN);
CookieLocaleResolver resolver = new CookieLocaleResolver();
LocaleContext loc = resolver.resolveLocaleContext(request);
replacedertThat(loc.getLocale()).isEqualTo(request.getLocale());
boolean condition = loc instanceof TimeZoneAwareLocaleContext;
replacedertThat(condition).isTrue();
replacedertThat(((TimeZoneAwareLocaleContext) loc).getTimeZone()).isNull();
}
13
Source : CookieLocaleResolverTests.java
with Apache License 2.0
from langtianya
with Apache License 2.0
from langtianya
@Test
public void testResolveLocaleContextWithoutCookieAndDefaultLocale() throws Exception {
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);
replacedertEquals(Locale.GERMAN, loc.getLocale());
replacedertTrue(loc instanceof TimeZoneAwareLocaleContext);
replacedertEquals(TimeZone.getTimeZone("GMT+1"), ((TimeZoneAwareLocaleContext) loc).getTimeZone());
}
13
Source : SessionLocaleResolver.java
with Apache License 2.0
from langtianya
with Apache License 2.0
from langtianya
@Override
public void setLocaleContext(HttpServletRequest request, HttpServletResponse response, LocaleContext localeContext) {
Locale locale = null;
TimeZone timeZone = null;
if (localeContext != null) {
locale = localeContext.getLocale();
if (localeContext instanceof TimeZoneAwareLocaleContext) {
timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
}
}
WebUtils.setSessionAttribute(request, LOCALE_SESSION_ATTRIBUTE_NAME, locale);
WebUtils.setSessionAttribute(request, TIME_ZONE_SESSION_ATTRIBUTE_NAME, timeZone);
}
13
Source : CookieLocaleResolver.java
with Apache License 2.0
from langtianya
with Apache License 2.0
from langtianya
@Override
public void setLocaleContext(HttpServletRequest request, HttpServletResponse response, LocaleContext localeContext) {
Locale locale = null;
TimeZone timeZone = null;
if (localeContext != null) {
locale = localeContext.getLocale();
if (localeContext instanceof TimeZoneAwareLocaleContext) {
timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
}
addCookie(response, (locale != null ? locale : "-") + (timeZone != null ? ' ' + timeZone.getID() : ""));
} else {
removeCookie(response);
}
request.setAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME, (locale != null ? locale : determineDefaultLocale(request)));
request.setAttribute(TIME_ZONE_REQUEST_ATTRIBUTE_NAME, (timeZone != null ? timeZone : determineDefaultTimeZone(request)));
}
13
Source : AngularCookieLocaleResolverTest.java
with Apache License 2.0
from jhipster
with Apache License 2.0
from jhipster
@Test
public void testPresets() {
when(request.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME)).thenReturn(LOCALE_DEFAULT);
when(request.getAttribute(TIME_ZONE_REQUEST_ATTRIBUTE_NAME)).thenReturn(TIMEZONE_DEFAULT);
LocaleContext context = resolver.resolveLocaleContext(request);
replacedertThat(context).isNotNull();
replacedertThat(context).isInstanceOf(TimeZoneAwareLocaleContext.clreplaced);
Locale locale = ((TimeZoneAwareLocaleContext) context).getLocale();
TimeZone zone = ((TimeZoneAwareLocaleContext) context).getTimeZone();
replacedertThat(locale).isNotNull();
replacedertThat(locale).isEqualTo(LOCALE_DEFAULT);
replacedertThat(zone).isEqualTo(TIMEZONE_DEFAULT);
List<Event> events = recorder.play();
replacedertThat(events).isEmpty();
}
12
Source : MailService.java
with Apache License 2.0
from xm-online
with Apache License 2.0
from xm-online
private String resolve(String key, String defaultValue, String templateName, Locale locale) {
Object settings = tenantConfigService.getConfig().get(MAIL_SETTINGS);
LocaleContext localeContext = getLocaleContext();
setLocale(locale);
String result = Optional.ofNullable(settings).filter(List.clreplaced::isInstance).map(it -> (List<Object>) it).flatMap(mailSettings -> mailSettings.stream().filter(Map.clreplaced::isInstance).map(it -> (Map<String, Object>) it).filter(it -> templateName.equals(it.get(TEMPLATE_NAME))).findFirst().filter(it -> it.containsKey(key)).map(it -> it.get(key)).filter(Map.clreplaced::isInstance).map(it -> (Map<String, String>) it).flatMap(this::getI18nName)).orElse(defaultValue);
setLocaleContext(localeContext);
return result;
}
12
Source : CookieLocaleResolverTests.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@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);
replacedertEquals("nl", loc.getLocale().getLanguage());
replacedertTrue(loc instanceof TimeZoneAwareLocaleContext);
replacedertEquals(TimeZone.getTimeZone("GMT+2"), ((TimeZoneAwareLocaleContext) loc).getTimeZone());
}
12
Source : CookieLocaleResolverTests.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@Test
public void testResolveLocaleContextWithCookieWithoutLocale() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addPreferredLocale(Locale.TAIWAN);
Cookie cookie = new Cookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME, "");
request.setCookies(cookie);
CookieLocaleResolver resolver = new CookieLocaleResolver();
LocaleContext loc = resolver.resolveLocaleContext(request);
replacedertEquals(request.getLocale(), loc.getLocale());
replacedertTrue(loc instanceof TimeZoneAwareLocaleContext);
replacedertNull(((TimeZoneAwareLocaleContext) loc).getTimeZone());
}
12
Source : CookieLocaleResolverTests.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@Test
public void testSetAndResolveLocaleContext() {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
CookieLocaleResolver resolver = new CookieLocaleResolver();
resolver.setLocaleContext(request, response, new SimpleLocaleContext(new Locale("nl", "")));
Cookie cookie = response.getCookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME);
request = new MockHttpServletRequest();
request.setCookies(cookie);
resolver = new CookieLocaleResolver();
LocaleContext loc = resolver.resolveLocaleContext(request);
replacedertEquals("nl", loc.getLocale().getLanguage());
replacedertTrue(loc instanceof TimeZoneAwareLocaleContext);
replacedertNull(((TimeZoneAwareLocaleContext) loc).getTimeZone());
}
12
Source : CookieLocaleResolver.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@Override
public void setLocaleContext(HttpServletRequest request, @Nullable HttpServletResponse response, @Nullable LocaleContext localeContext) {
replacedert.notNull(response, "HttpServletResponse is required for CookieLocaleResolver");
Locale locale = null;
TimeZone timeZone = null;
if (localeContext != null) {
locale = localeContext.getLocale();
if (localeContext instanceof TimeZoneAwareLocaleContext) {
timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
}
addCookie(response, (locale != null ? toLocaleValue(locale) : "-") + (timeZone != null ? '/' + timeZone.getID() : ""));
} else {
removeCookie(response);
}
request.setAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME, (locale != null ? locale : determineDefaultLocale(request)));
request.setAttribute(TIME_ZONE_REQUEST_ATTRIBUTE_NAME, (timeZone != null ? timeZone : determineDefaultTimeZone(request)));
}
12
Source : CookieLocaleResolverTests.java
with Apache License 2.0
from SourceHot
with Apache License 2.0
from SourceHot
@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);
replacedertThat(loc.getLocale()).isEqualTo(Locale.GERMAN);
boolean condition = loc instanceof TimeZoneAwareLocaleContext;
replacedertThat(condition).isTrue();
replacedertThat(((TimeZoneAwareLocaleContext) loc).getTimeZone()).isEqualTo(TimeZone.getTimeZone("GMT+1"));
}
12
Source : CookieLocaleResolverTests.java
with Apache License 2.0
from langtianya
with Apache License 2.0
from langtianya
@Test
public void testResolveLocaleContextWithCookieWithoutLocale() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addPreferredLocale(Locale.TAIWAN);
Cookie cookie = new Cookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME, "");
request.setCookies(cookie);
CookieLocaleResolver resolver = new CookieLocaleResolver();
LocaleContext loc = resolver.resolveLocaleContext(request);
replacedertEquals(request.getLocale(), loc.getLocale());
replacedertTrue(loc instanceof TimeZoneAwareLocaleContext);
replacedertNull(((TimeZoneAwareLocaleContext) loc).getTimeZone());
}
12
Source : AngularCookieLocaleResolverTest.java
with Apache License 2.0
from jhipster
with Apache License 2.0
from jhipster
@Test
public void testTimeZoneWithQuotes() {
String value = resolver.quote("- " + TIMEZONE_CUSTOM.getID());
Cookie cookie = new Cookie(DEFAULT_COOKIE_NAME, value);
when(request.getCookies()).thenReturn(new Cookie[] { cookie });
LocaleContext context = resolver.resolveLocaleContext(request);
replacedertThat(context).isNotNull();
replacedertThat(context).isInstanceOf(TimeZoneAwareLocaleContext.clreplaced);
Locale locale = ((TimeZoneAwareLocaleContext) context).getLocale();
TimeZone zone = ((TimeZoneAwareLocaleContext) context).getTimeZone();
replacedertThat(locale).isEqualTo(LOCALE_DEFAULT);
replacedertThat(zone).isEqualTo(TIMEZONE_CUSTOM);
List<Event> events = recorder.play();
replacedertThat(events).isEmpty();
}
12
Source : AngularCookieLocaleResolverTest.java
with Apache License 2.0
from jhipster
with Apache License 2.0
from jhipster
@Test
public void testTimeZone() {
String value = "- " + TIMEZONE_CUSTOM.getID();
Cookie cookie = new Cookie(DEFAULT_COOKIE_NAME, value);
when(request.getCookies()).thenReturn(new Cookie[] { cookie });
LocaleContext context = resolver.resolveLocaleContext(request);
replacedertThat(context).isNotNull();
replacedertThat(context).isInstanceOf(TimeZoneAwareLocaleContext.clreplaced);
Locale locale = ((TimeZoneAwareLocaleContext) context).getLocale();
TimeZone zone = ((TimeZoneAwareLocaleContext) context).getTimeZone();
replacedertThat(locale).isEqualTo(LOCALE_DEFAULT);
replacedertThat(zone).isEqualTo(TIMEZONE_CUSTOM);
List<Event> events = recorder.play();
replacedertThat(events).isEmpty();
}
12
Source : AngularCookieLocaleResolverTest.java
with Apache License 2.0
from jhipster
with Apache License 2.0
from jhipster
@Test
public void testLocaleAndTimeZone() {
String value = LOCALE_CUSTOM + " " + TIMEZONE_CUSTOM.getID();
Cookie cookie = new Cookie(DEFAULT_COOKIE_NAME, value);
when(request.getCookies()).thenReturn(new Cookie[] { cookie });
LocaleContext context = resolver.resolveLocaleContext(request);
replacedertThat(context).isNotNull();
replacedertThat(context).isInstanceOf(TimeZoneAwareLocaleContext.clreplaced);
Locale locale = ((TimeZoneAwareLocaleContext) context).getLocale();
TimeZone zone = ((TimeZoneAwareLocaleContext) context).getTimeZone();
replacedertThat(locale).isEqualTo(LOCALE_CUSTOM);
replacedertThat(zone).isEqualTo(TIMEZONE_CUSTOM);
List<Event> events = recorder.play();
replacedertThat(events).isEmpty();
}
12
Source : AngularCookieLocaleResolverTest.java
with Apache License 2.0
from jhipster
with Apache License 2.0
from jhipster
@Test
public void testLocaleAndTimeZoneWithQuotes() {
String value = resolver.quote(LOCALE_CUSTOM.toString() + " " + TIMEZONE_CUSTOM.getID());
Cookie cookie = new Cookie(DEFAULT_COOKIE_NAME, value);
when(request.getCookies()).thenReturn(new Cookie[] { cookie });
LocaleContext context = resolver.resolveLocaleContext(request);
replacedertThat(context).isNotNull();
replacedertThat(context).isInstanceOf(TimeZoneAwareLocaleContext.clreplaced);
Locale locale = ((TimeZoneAwareLocaleContext) context).getLocale();
TimeZone zone = ((TimeZoneAwareLocaleContext) context).getTimeZone();
replacedertThat(locale).isEqualTo(LOCALE_CUSTOM);
replacedertThat(zone).isEqualTo(TIMEZONE_CUSTOM);
List<Event> events = recorder.play();
replacedertThat(events).isEmpty();
}
11
Source : CookieLocaleResolverTests.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@Test
public void testSetAndResolveLocaleContextWithTimeZone() {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
CookieLocaleResolver resolver = new CookieLocaleResolver();
resolver.setLocaleContext(request, response, new SimpleTimeZoneAwareLocaleContext(new Locale("nl", ""), TimeZone.getTimeZone("GMT+1")));
Cookie cookie = response.getCookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME);
request = new MockHttpServletRequest();
request.setCookies(cookie);
resolver = new CookieLocaleResolver();
LocaleContext loc = resolver.resolveLocaleContext(request);
replacedertEquals("nl", loc.getLocale().getLanguage());
replacedertTrue(loc instanceof TimeZoneAwareLocaleContext);
replacedertEquals(TimeZone.getTimeZone("GMT+1"), ((TimeZoneAwareLocaleContext) loc).getTimeZone());
}
11
Source : HttpComponentsHttpInvokerRequestExecutor.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
/**
* Create a HttpPost for the given configuration.
* <p>The default implementation creates a standard HttpPost with
* "application/x-java-serialized-object" as "Content-Type" header.
* @param config the HTTP invoker configuration that specifies the
* target service
* @return the HttpPost instance
* @throws java.io.IOException if thrown by I/O methods
*/
protected HttpPost createHttpPost(HttpInvokerClientConfiguration config) throws IOException {
HttpPost httpPost = new HttpPost(config.getServiceUrl());
RequestConfig requestConfig = createRequestConfig(config);
if (requestConfig != null) {
httpPost.setConfig(requestConfig);
}
LocaleContext localeContext = LocaleContextHolder.getLocaleContext();
if (localeContext != null) {
Locale locale = localeContext.getLocale();
if (locale != null) {
httpPost.addHeader(HTTP_HEADER_ACCEPT_LANGUAGE, locale.toLanguageTag());
}
}
if (isAcceptGzipEncoding()) {
httpPost.addHeader(HTTP_HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
}
return httpPost;
}
11
Source : CookieLocaleResolverTests.java
with Apache License 2.0
from SourceHot
with Apache License 2.0
from SourceHot
@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);
replacedertThat(loc.getLocale().getLanguage()).isEqualTo("nl");
boolean condition = loc instanceof TimeZoneAwareLocaleContext;
replacedertThat(condition).isTrue();
replacedertThat(((TimeZoneAwareLocaleContext) loc).getTimeZone()).isEqualTo(TimeZone.getTimeZone("GMT+2"));
}
11
Source : CookieLocaleResolverTests.java
with Apache License 2.0
from SourceHot
with Apache License 2.0
from SourceHot
@Test
public void testResolveLocaleContextWithCookieWithoutLocale() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addPreferredLocale(Locale.TAIWAN);
Cookie cookie = new Cookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME, "");
request.setCookies(cookie);
CookieLocaleResolver resolver = new CookieLocaleResolver();
LocaleContext loc = resolver.resolveLocaleContext(request);
replacedertThat(loc.getLocale()).isEqualTo(request.getLocale());
boolean condition = loc instanceof TimeZoneAwareLocaleContext;
replacedertThat(condition).isTrue();
replacedertThat(((TimeZoneAwareLocaleContext) loc).getTimeZone()).isNull();
}
11
Source : CookieLocaleResolverTests.java
with Apache License 2.0
from SourceHot
with Apache License 2.0
from SourceHot
@Test
public void testSetAndResolveLocaleContext() {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
CookieLocaleResolver resolver = new CookieLocaleResolver();
resolver.setLocaleContext(request, response, new SimpleLocaleContext(new Locale("nl", "")));
Cookie cookie = response.getCookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME);
request = new MockHttpServletRequest();
request.setCookies(cookie);
resolver = new CookieLocaleResolver();
LocaleContext loc = resolver.resolveLocaleContext(request);
replacedertThat(loc.getLocale().getLanguage()).isEqualTo("nl");
boolean condition = loc instanceof TimeZoneAwareLocaleContext;
replacedertThat(condition).isTrue();
replacedertThat(((TimeZoneAwareLocaleContext) loc).getTimeZone()).isNull();
}
11
Source : HttpComponentsHttpInvokerRequestExecutor.java
with Apache License 2.0
from SourceHot
with Apache License 2.0
from SourceHot
/**
* Create an HttpPost for the given configuration.
* <p>The default implementation creates a standard HttpPost with
* "application/x-java-serialized-object" as "Content-Type" header.
* @param config the HTTP invoker configuration that specifies the
* target service
* @return the HttpPost instance
* @throws java.io.IOException if thrown by I/O methods
*/
protected HttpPost createHttpPost(HttpInvokerClientConfiguration config) throws IOException {
HttpPost httpPost = new HttpPost(config.getServiceUrl());
RequestConfig requestConfig = createRequestConfig(config);
if (requestConfig != null) {
httpPost.setConfig(requestConfig);
}
LocaleContext localeContext = LocaleContextHolder.getLocaleContext();
if (localeContext != null) {
Locale locale = localeContext.getLocale();
if (locale != null) {
httpPost.addHeader(HTTP_HEADER_ACCEPT_LANGUAGE, locale.toLanguageTag());
}
}
if (isAcceptGzipEncoding()) {
httpPost.addHeader(HTTP_HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
}
return httpPost;
}
See More Examples