001/*
002 * Copyright 2002-2017 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.servlet.i18n;
018
019import java.util.Locale;
020import java.util.TimeZone;
021
022import javax.servlet.http.HttpServletRequest;
023import javax.servlet.http.HttpServletResponse;
024
025import org.springframework.context.i18n.SimpleLocaleContext;
026import org.springframework.lang.Nullable;
027import org.springframework.web.servlet.LocaleContextResolver;
028
029/**
030 * Abstract base class for {@link LocaleContextResolver} implementations.
031 * Provides support for a default locale and a default time zone.
032 *
033 * <p>Also provides pre-implemented versions of {@link #resolveLocale} and {@link #setLocale},
034 * delegating to {@link #resolveLocaleContext} and {@link #setLocaleContext}.
035 *
036 * @author Juergen Hoeller
037 * @since 4.0
038 * @see #setDefaultLocale
039 * @see #setDefaultTimeZone
040 */
041public abstract class AbstractLocaleContextResolver extends AbstractLocaleResolver implements LocaleContextResolver {
042
043        @Nullable
044        private TimeZone defaultTimeZone;
045
046
047        /**
048         * Set a default TimeZone that this resolver will return if no other time zone found.
049         */
050        public void setDefaultTimeZone(@Nullable TimeZone defaultTimeZone) {
051                this.defaultTimeZone = defaultTimeZone;
052        }
053
054        /**
055         * Return the default TimeZone that this resolver is supposed to fall back to, if any.
056         */
057        @Nullable
058        public TimeZone getDefaultTimeZone() {
059                return this.defaultTimeZone;
060        }
061
062
063        @Override
064        public Locale resolveLocale(HttpServletRequest request) {
065                Locale locale = resolveLocaleContext(request).getLocale();
066                return (locale != null ? locale : request.getLocale());
067        }
068
069        @Override
070        public void setLocale(HttpServletRequest request, @Nullable HttpServletResponse response, @Nullable Locale locale) {
071                setLocaleContext(request, response, (locale != null ? new SimpleLocaleContext(locale) : null));
072        }
073
074}