001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.server.i18n;
018
019import java.util.Locale;
020import java.util.TimeZone;
021
022import org.springframework.context.i18n.LocaleContext;
023import org.springframework.context.i18n.TimeZoneAwareLocaleContext;
024import org.springframework.lang.Nullable;
025import org.springframework.util.Assert;
026import org.springframework.web.server.ServerWebExchange;
027
028/**
029 * {@link LocaleContextResolver} implementation that always returns a fixed locale
030 * and optionally time zone. Default is the current JVM's default locale.
031 *
032 * <p>Note: Does not support {@link #setLocaleContext}, as the fixed locale and
033 * time zone cannot be changed.
034 *
035 * @author Sebastien Deleuze
036 * @since 5.0
037 */
038public class FixedLocaleContextResolver implements LocaleContextResolver {
039
040        private final Locale locale;
041
042        @Nullable
043        private final TimeZone timeZone;
044
045
046        /**
047         * Create a default FixedLocaleResolver, exposing a configured default
048         * locale (or the JVM's default locale as fallback).
049         */
050        public FixedLocaleContextResolver() {
051                this(Locale.getDefault());
052        }
053
054        /**
055         * Create a FixedLocaleResolver that exposes the given locale.
056         * @param locale the locale to expose
057         */
058        public FixedLocaleContextResolver(Locale locale) {
059                this(locale, null);
060        }
061
062        /**
063         * Create a FixedLocaleResolver that exposes the given locale and time zone.
064         * @param locale the locale to expose
065         * @param timeZone the time zone to expose
066         */
067        public FixedLocaleContextResolver(Locale locale, @Nullable TimeZone timeZone) {
068                Assert.notNull(locale, "Locale must not be null");
069                this.locale = locale;
070                this.timeZone = timeZone;
071        }
072
073
074        @Override
075        public LocaleContext resolveLocaleContext(ServerWebExchange exchange) {
076                return new TimeZoneAwareLocaleContext() {
077                        @Override
078                        public Locale getLocale() {
079                                return locale;
080                        }
081                        @Override
082                        @Nullable
083                        public TimeZone getTimeZone() {
084                                return timeZone;
085                        }
086                };
087        }
088
089        @Override
090        public void setLocaleContext(ServerWebExchange exchange, @Nullable LocaleContext localeContext) {
091                throw new UnsupportedOperationException(
092                                "Cannot change fixed locale - use a different locale context resolution strategy");
093        }
094
095}