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