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