/** * Return the Locale associated with the current thread, if any, * or the system default Locale otherwise. This is effectively a * replacement for {@link java.util.Locale#getDefault()}, * able to optionally respect a user-level Locale setting. * <p>Note: This method has a fallback to the shared default Locale, * either at the framework level or at the JVM-wide system level. * If you'd like to check for the raw LocaleContext content * (which may indicate no specific locale through {@code null}, use * {@link #getLocaleContext()} and call {@link LocaleContext#getLocale()} * @return the current Locale, or the system default Locale if no * specific Locale has been associated with the current thread * @see #getLocaleContext() * @see LocaleContext#getLocale() * @see #setDefaultLocale(Locale) * @see java.util.Locale#getDefault() */ public static Locale getLocale() { return getLocale(getLocaleContext()); }
/** * Return the Locale associated with the current thread, if any, * or the system default Locale otherwise. This is effectively a * replacement for {@link java.util.Locale#getDefault()}, * able to optionally respect a user-level Locale setting. * <p>Note: This method has a fallback to the shared default Locale, * either at the framework level or at the JVM-wide system level. * If you'd like to check for the raw LocaleContext content * (which may indicate no specific locale through {@code null}, use * {@link #getLocaleContext()} and call {@link LocaleContext#getLocale()} * @return the current Locale, or the system default Locale if no * specific Locale has been associated with the current thread * @see #getLocaleContext() * @see LocaleContext#getLocale() * @see #setDefaultLocale(Locale) * @see java.util.Locale#getDefault() */ public static Locale getLocale() { return getLocale(getLocaleContext()); }
/** * Return the TimeZone associated with the current thread, if any, * or the system default TimeZone otherwise. This is effectively a * replacement for {@link java.util.TimeZone#getDefault()}, * able to optionally respect a user-level TimeZone setting. * <p>Note: This method has a fallback to the shared default TimeZone, * either at the framework level or at the JVM-wide system level. * If you'd like to check for the raw LocaleContext content * (which may indicate no specific time zone through {@code null}, use * {@link #getLocaleContext()} and call {@link TimeZoneAwareLocaleContext#getTimeZone()} * after downcasting to {@link TimeZoneAwareLocaleContext}. * @return the current TimeZone, or the system default TimeZone if no * specific TimeZone has been associated with the current thread * @see #getLocaleContext() * @see TimeZoneAwareLocaleContext#getTimeZone() * @see #setDefaultTimeZone(TimeZone) * @see java.util.TimeZone#getDefault() */ public static TimeZone getTimeZone() { return getTimeZone(getLocaleContext()); }
/** * Return the TimeZone associated with the current thread, if any, * or the system default TimeZone otherwise. This is effectively a * replacement for {@link java.util.TimeZone#getDefault()}, * able to optionally respect a user-level TimeZone setting. * <p>Note: This method has a fallback to the shared default TimeZone, * either at the framework level or at the JVM-wide system level. * If you'd like to check for the raw LocaleContext content * (which may indicate no specific time zone through {@code null}, use * {@link #getLocaleContext()} and call {@link TimeZoneAwareLocaleContext#getTimeZone()} * after downcasting to {@link TimeZoneAwareLocaleContext}. * @return the current TimeZone, or the system default TimeZone if no * specific TimeZone has been associated with the current thread * @see #getLocaleContext() * @see TimeZoneAwareLocaleContext#getTimeZone() * @see #setDefaultTimeZone(TimeZone) * @see java.util.TimeZone#getDefault() */ public static TimeZone getTimeZone() { return getTimeZone(getLocaleContext()); }
connection.setRequestProperty(HTTP_HEADER_CONTENT_LENGTH, Integer.toString(contentLength)); LocaleContext localeContext = LocaleContextHolder.getLocaleContext(); if (localeContext != null) { Locale locale = localeContext.getLocale();
/** * 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; }
/** * 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; }
/** * 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; }
/** * Associate the given TimeZone with the current thread, * preserving any Locale that may have been set already. * <p>Will implicitly create a LocaleContext for the given Locale. * @param timeZone the current TimeZone, or {@code null} to reset * the time zone part of the thread-bound context * @param inheritable whether to expose the LocaleContext as inheritable * for child threads (using an {@link InheritableThreadLocal}) * @see #setLocale(Locale, boolean) * @see SimpleTimeZoneAwareLocaleContext#SimpleTimeZoneAwareLocaleContext(Locale, TimeZone) */ public static void setTimeZone(@Nullable TimeZone timeZone, boolean inheritable) { LocaleContext localeContext = getLocaleContext(); Locale locale = (localeContext != null ? localeContext.getLocale() : null); if (timeZone != null) { localeContext = new SimpleTimeZoneAwareLocaleContext(locale, timeZone); } else if (locale != null) { localeContext = new SimpleLocaleContext(locale); } else { localeContext = null; } setLocaleContext(localeContext, inheritable); }
/** * Associate the given Locale with the current thread, * preserving any TimeZone that may have been set already. * <p>Will implicitly create a LocaleContext for the given Locale. * @param locale the current Locale, or {@code null} to reset * the locale part of thread-bound context * @param inheritable whether to expose the LocaleContext as inheritable * for child threads (using an {@link InheritableThreadLocal}) * @see #setTimeZone(TimeZone, boolean) * @see SimpleLocaleContext#SimpleLocaleContext(Locale) */ public static void setLocale(@Nullable Locale locale, boolean inheritable) { LocaleContext localeContext = getLocaleContext(); TimeZone timeZone = (localeContext instanceof TimeZoneAwareLocaleContext ? ((TimeZoneAwareLocaleContext) localeContext).getTimeZone() : null); if (timeZone != null) { localeContext = new SimpleTimeZoneAwareLocaleContext(locale, timeZone); } else if (locale != null) { localeContext = new SimpleLocaleContext(locale); } else { localeContext = null; } setLocaleContext(localeContext, inheritable); }
/** * Associate the given TimeZone with the current thread, * preserving any Locale that may have been set already. * <p>Will implicitly create a LocaleContext for the given Locale. * @param timeZone the current TimeZone, or {@code null} to reset * the time zone part of the thread-bound context * @param inheritable whether to expose the LocaleContext as inheritable * for child threads (using an {@link InheritableThreadLocal}) * @see #setLocale(Locale, boolean) * @see SimpleTimeZoneAwareLocaleContext#SimpleTimeZoneAwareLocaleContext(Locale, TimeZone) */ public static void setTimeZone(@Nullable TimeZone timeZone, boolean inheritable) { LocaleContext localeContext = getLocaleContext(); Locale locale = (localeContext != null ? localeContext.getLocale() : null); if (timeZone != null) { localeContext = new SimpleTimeZoneAwareLocaleContext(locale, timeZone); } else if (locale != null) { localeContext = new SimpleLocaleContext(locale); } else { localeContext = null; } setLocaleContext(localeContext, inheritable); }
/** * Associate the given Locale with the current thread, * preserving any TimeZone that may have been set already. * <p>Will implicitly create a LocaleContext for the given Locale. * @param locale the current Locale, or {@code null} to reset * the locale part of thread-bound context * @param inheritable whether to expose the LocaleContext as inheritable * for child threads (using an {@link InheritableThreadLocal}) * @see #setTimeZone(TimeZone, boolean) * @see SimpleLocaleContext#SimpleLocaleContext(Locale) */ public static void setLocale(@Nullable Locale locale, boolean inheritable) { LocaleContext localeContext = getLocaleContext(); TimeZone timeZone = (localeContext instanceof TimeZoneAwareLocaleContext ? ((TimeZoneAwareLocaleContext) localeContext).getTimeZone() : null); if (timeZone != null) { localeContext = new SimpleTimeZoneAwareLocaleContext(locale, timeZone); } else if (locale != null) { localeContext = new SimpleLocaleContext(locale); } else { localeContext = null; } setLocaleContext(localeContext, inheritable); }
@Test public void testSetTimeZoneAwareLocaleContext() { LocaleContext lc = new SimpleTimeZoneAwareLocaleContext(Locale.GERMANY, TimeZone.getTimeZone("GMT+1")); LocaleContextHolder.setLocaleContext(lc); assertSame(lc, LocaleContextHolder.getLocaleContext()); assertEquals(Locale.GERMANY, LocaleContextHolder.getLocale()); assertEquals(TimeZone.getTimeZone("GMT+1"), LocaleContextHolder.getTimeZone()); LocaleContextHolder.resetLocaleContext(); assertNull(LocaleContextHolder.getLocaleContext()); assertEquals(Locale.getDefault(), LocaleContextHolder.getLocale()); assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); }
/** * 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; }
@Test public void testSetTimeZone() { LocaleContextHolder.setTimeZone(TimeZone.getTimeZone("GMT+1")); assertEquals(Locale.getDefault(), LocaleContextHolder.getLocale()); assertEquals(TimeZone.getTimeZone("GMT+1"), LocaleContextHolder.getTimeZone()); assertTrue(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); assertNull(LocaleContextHolder.getLocaleContext().getLocale()); assertEquals(TimeZone.getTimeZone("GMT+1"), ((TimeZoneAwareLocaleContext) LocaleContextHolder.getLocaleContext()).getTimeZone()); LocaleContextHolder.setTimeZone(TimeZone.getTimeZone("GMT+2")); assertEquals(Locale.getDefault(), LocaleContextHolder.getLocale()); assertEquals(TimeZone.getTimeZone("GMT+2"), LocaleContextHolder.getTimeZone()); assertTrue(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); assertNull(LocaleContextHolder.getLocaleContext().getLocale()); assertEquals(TimeZone.getTimeZone("GMT+2"), ((TimeZoneAwareLocaleContext) LocaleContextHolder.getLocaleContext()).getTimeZone()); LocaleContextHolder.setTimeZone(null); assertNull(LocaleContextHolder.getLocaleContext()); assertEquals(Locale.getDefault(), LocaleContextHolder.getLocale()); assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); LocaleContextHolder.setDefaultTimeZone(TimeZone.getTimeZone("GMT+1")); assertEquals(TimeZone.getTimeZone("GMT+1"), LocaleContextHolder.getTimeZone()); LocaleContextHolder.setDefaultTimeZone(null); assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); }
Throwable failureCause = null; LocaleContext previousLocaleContext = LocaleContextHolder.getLocaleContext(); LocaleContext localeContext = buildLocaleContext(request);
@Test public void testSetLocale() { LocaleContextHolder.setLocale(Locale.GERMAN); assertEquals(Locale.GERMAN, LocaleContextHolder.getLocale()); assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); assertFalse(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); assertEquals(Locale.GERMAN, LocaleContextHolder.getLocaleContext().getLocale()); LocaleContextHolder.setLocale(Locale.GERMANY); assertEquals(Locale.GERMANY, LocaleContextHolder.getLocale()); assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); assertFalse(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); assertEquals(Locale.GERMANY, LocaleContextHolder.getLocaleContext().getLocale()); LocaleContextHolder.setLocale(null); assertNull(LocaleContextHolder.getLocaleContext()); assertEquals(Locale.getDefault(), LocaleContextHolder.getLocale()); assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); LocaleContextHolder.setDefaultLocale(Locale.GERMAN); assertEquals(Locale.GERMAN, LocaleContextHolder.getLocale()); LocaleContextHolder.setDefaultLocale(null); assertEquals(Locale.getDefault(), LocaleContextHolder.getLocale()); }
@Test public void testSetLocaleContext() { LocaleContext lc = new SimpleLocaleContext(Locale.GERMAN); LocaleContextHolder.setLocaleContext(lc); assertSame(lc, LocaleContextHolder.getLocaleContext()); assertEquals(Locale.GERMAN, LocaleContextHolder.getLocale()); assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); lc = new SimpleLocaleContext(Locale.GERMANY); LocaleContextHolder.setLocaleContext(lc); assertSame(lc, LocaleContextHolder.getLocaleContext()); assertEquals(Locale.GERMANY, LocaleContextHolder.getLocale()); assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); LocaleContextHolder.resetLocaleContext(); assertNull(LocaleContextHolder.getLocaleContext()); assertEquals(Locale.getDefault(), LocaleContextHolder.getLocale()); assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); }
assertEquals(Locale.GERMANY, LocaleContextHolder.getLocale()); assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); assertFalse(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); assertEquals(Locale.GERMANY, LocaleContextHolder.getLocaleContext().getLocale()); assertTrue(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); assertEquals(Locale.GERMANY, LocaleContextHolder.getLocaleContext().getLocale()); assertEquals(TimeZone.getTimeZone("GMT+1"), ((TimeZoneAwareLocaleContext) LocaleContextHolder.getLocaleContext()).getTimeZone()); assertTrue(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); assertEquals(Locale.GERMAN, LocaleContextHolder.getLocaleContext().getLocale()); assertEquals(TimeZone.getTimeZone("GMT+1"), ((TimeZoneAwareLocaleContext) LocaleContextHolder.getLocaleContext()).getTimeZone()); assertFalse(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); assertEquals(Locale.GERMAN, LocaleContextHolder.getLocaleContext().getLocale()); assertTrue(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); assertEquals(Locale.GERMAN, LocaleContextHolder.getLocaleContext().getLocale()); assertEquals(TimeZone.getTimeZone("GMT+2"), ((TimeZoneAwareLocaleContext) LocaleContextHolder.getLocaleContext()).getTimeZone()); assertTrue(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); assertNull(LocaleContextHolder.getLocaleContext().getLocale()); assertEquals(TimeZone.getTimeZone("GMT+2"), ((TimeZoneAwareLocaleContext) LocaleContextHolder.getLocaleContext()).getTimeZone()); assertNull(LocaleContextHolder.getLocaleContext());
/** * 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; }