001/*
002 * Copyright 2002-2016 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.view.tiles3;
018
019import java.util.Locale;
020import javax.servlet.http.HttpServletRequest;
021
022import org.apache.tiles.locale.impl.DefaultLocaleResolver;
023import org.apache.tiles.request.Request;
024import org.apache.tiles.request.servlet.NotAServletEnvironmentException;
025import org.apache.tiles.request.servlet.ServletUtil;
026
027import org.springframework.web.servlet.support.RequestContextUtils;
028
029/**
030 * Tiles LocaleResolver adapter that delegates to a Spring
031 * {@link org.springframework.web.servlet.LocaleResolver}, exposing the
032 * DispatcherServlet-managed locale.
033 *
034 * <p>This adapter gets automatically registered by {@link TilesConfigurer}.
035 *
036 * @author Nicolas Le Bas
037 * @since 3.2
038 * @see org.apache.tiles.definition.UrlDefinitionsFactory#LOCALE_RESOLVER_IMPL_PROPERTY
039 */
040public class SpringLocaleResolver extends DefaultLocaleResolver {
041
042        @Override
043        public Locale resolveLocale(Request request) {
044                try {
045                        HttpServletRequest servletRequest = ServletUtil.getServletRequest(request).getRequest();
046                        if (servletRequest != null) {
047                                return RequestContextUtils.getLocale(servletRequest);
048                        }
049                }
050                catch (NotAServletEnvironmentException ex) {
051                        // ignore
052                }
053                return super.resolveLocale(request);
054        }
055
056}