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.LocaleContext; 026import org.springframework.context.i18n.TimeZoneAwareLocaleContext; 027import org.springframework.lang.Nullable; 028 029/** 030 * {@link org.springframework.web.servlet.LocaleResolver} implementation 031 * that always returns a fixed default locale and optionally time zone. 032 * Default is the current JVM's default locale. 033 * 034 * <p>Note: Does not support {@code setLocale(Context)}, as the fixed 035 * locale and time zone cannot be changed. 036 * 037 * @author Juergen Hoeller 038 * @since 1.1 039 * @see #setDefaultLocale 040 * @see #setDefaultTimeZone 041 */ 042public class FixedLocaleResolver extends AbstractLocaleContextResolver { 043 044 /** 045 * Create a default FixedLocaleResolver, exposing a configured default 046 * locale (or the JVM's default locale as fallback). 047 * @see #setDefaultLocale 048 * @see #setDefaultTimeZone 049 */ 050 public FixedLocaleResolver() { 051 setDefaultLocale(Locale.getDefault()); 052 } 053 054 /** 055 * Create a FixedLocaleResolver that exposes the given locale. 056 * @param locale the locale to expose 057 */ 058 public FixedLocaleResolver(Locale locale) { 059 setDefaultLocale(locale); 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 FixedLocaleResolver(Locale locale, TimeZone timeZone) { 068 setDefaultLocale(locale); 069 setDefaultTimeZone(timeZone); 070 } 071 072 073 @Override 074 public Locale resolveLocale(HttpServletRequest request) { 075 Locale locale = getDefaultLocale(); 076 if (locale == null) { 077 locale = Locale.getDefault(); 078 } 079 return locale; 080 } 081 082 @Override 083 public LocaleContext resolveLocaleContext(HttpServletRequest request) { 084 return new TimeZoneAwareLocaleContext() { 085 @Override 086 @Nullable 087 public Locale getLocale() { 088 return getDefaultLocale(); 089 } 090 @Override 091 public TimeZone getTimeZone() { 092 return getDefaultTimeZone(); 093 } 094 }; 095 } 096 097 @Override 098 public void setLocaleContext( HttpServletRequest request, @Nullable HttpServletResponse response, 099 @Nullable LocaleContext localeContext) { 100 101 throw new UnsupportedOperationException("Cannot change fixed locale - use a different locale resolution strategy"); 102 } 103 104}