001/******************************************************************************* 002 * Copyright 2017 The MIT Internet Trust Consortium 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 * http://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/** 017 * 018 */ 019package org.mitre.openid.connect.config; 020 021import java.util.Locale; 022import java.util.TimeZone; 023 024import javax.servlet.http.HttpServletRequest; 025import javax.servlet.http.HttpServletResponse; 026 027import org.springframework.beans.factory.annotation.Autowired; 028import org.springframework.context.i18n.LocaleContext; 029import org.springframework.context.i18n.TimeZoneAwareLocaleContext; 030import org.springframework.web.servlet.i18n.AbstractLocaleContextResolver; 031 032/** 033 * 034 * Resolve the server's locale from the injected ConfigurationPropertiesBean. 035 * 036 * @author jricher 037 * 038 */ 039public class ConfigurationBeanLocaleResolver extends AbstractLocaleContextResolver { 040 041 @Autowired 042 private ConfigurationPropertiesBean config; 043 044 @Override 045 protected Locale getDefaultLocale() { 046 if (config.getLocale() != null) { 047 return config.getLocale(); 048 } else { 049 return super.getDefaultLocale(); 050 } 051 } 052 053 @Override 054 public LocaleContext resolveLocaleContext(HttpServletRequest request) { 055 return new TimeZoneAwareLocaleContext() { 056 @Override 057 public Locale getLocale() { 058 return getDefaultLocale(); 059 } 060 @Override 061 public TimeZone getTimeZone() { 062 return getDefaultTimeZone(); 063 } 064 }; 065 } 066 067 @Override 068 public void setLocaleContext(HttpServletRequest request, HttpServletResponse response, LocaleContext localeContext) { 069 throw new UnsupportedOperationException("Cannot change fixed locale - use a different locale resolution strategy"); 070 } 071 072}