/*
 * Copyright (C) 2008 feilong
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.feilong.spring.web.servlet.interceptor.i18n;

import static com.feilong.core.CharsetType.UTF8;
import static com.feilong.core.Validator.isNullOrEmpty;

import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;

import com.feilong.servlet.http.RequestUtil;

/**
 * 如果直接使用 {@link org.springframework.web.servlet.i18n.LocaleChangeInterceptor} ,而参数中传入了 不存在的/不支持的 locale 调用
 * {@link org.springframework.util.StringUtils#parseLocaleString(String)}会报错
 *
 * @author <a href="http://feitianbenyue.iteye.com/">feilong</a>
 * @since 1.0.9
 */
public class SupportLocaleChangeInterceptor extends LocaleChangeInterceptor{

    /** The Constant log. */
    private static final Logger LOGGER = LoggerFactory.getLogger(SupportLocaleChangeInterceptor.class);

    /** The support locales. */
    private List<String>        supportLocales;

    //---------------------------------------------------------------

    /*
     * (non-Javadoc)
     * 
     * @see org.springframework.web.servlet.i18n.LocaleChangeInterceptor#preHandle(javax.servlet.http.HttpServletRequest,
     * javax.servlet.http.HttpServletResponse, java.lang.Object)
     */
    @Override
    public boolean preHandle(HttpServletRequest request,HttpServletResponse response,Object handler) throws ServletException{
        if (!(handler instanceof HandlerMethod)){
            LOGGER.warn(
                            "request info:[{}],not [HandlerMethod],handler is [{}],What ghost~~,",
                            RequestUtil.getRequestFullURL(request, UTF8),
                            handler.getClass().getName());
            return true;
        }

        //---------------------------------------------------------------

        boolean canHandle = isSupport(request);
        if (canHandle){
            super.preHandle(request, response, handler);
        }
        //不管支不支持  都return true
        return true;
    }

    /**
     * Checks if is support.
     *
     * @param request
     *            the request
     * @return true, if checks if is support
     * @since 1.0.9
     */
    private boolean isSupport(HttpServletRequest request){
        if (isNullOrEmpty(supportLocales)){
            LOGGER.warn("SupportLocaleChangeInterceptor's supportLocales is isNullOrEmpty,you can direct use LocaleChangeInterceptor");
            return true; //如果isNotNullOrEmpty supportLocales,那么 就是个普通的  LocaleChangeInterceptor
        }

        String newLocaleValue = request.getParameter(getParamName());
        boolean contains = supportLocales.contains(newLocaleValue);
        if (!contains){
            LOGGER.warn("SupportLocaleChangeInterceptor's supportLocales:[{}] not contains [{}]", supportLocales, newLocaleValue);
        }
        return contains; //是否属于支持的locale
    }

    //---------------------------------------------------------------

    /**
     * 设置 support locales.
     *
     * @param supportLocales
     *            the supportLocales to set
     */
    public void setSupportLocales(List<String> supportLocales){
        this.supportLocales = supportLocales;
    }
}
@@ -1143,16 +1143,48 @@
 l;%0A%0A/**%0A
+ * %E6%94%AF%E6%8C%81%E8%AF%AD%E8%A8%80%E7%9A%84 change %E6%8B%A6%E6%88%AA%E5%99%A8.%0A * %0A * %3Cp%3E%0A
  * %E5%A6%82%E6%9E%9C%E7%9B%B4%E6%8E%A5%E4%BD%BF
@@ -1257,17 +1257,16 @@
 tor%7D
- 
 ,%E8%80%8C%E5%8F%82%E6%95%B0%E4%B8%AD%E4%BC%A0%E5%85%A5%E4%BA%86
  %E4%B8%8D%E5%AD%98%E5%9C%A8
@@ -1261,17 +1261,22 @@
 ,%E8%80%8C%E5%8F%82%E6%95%B0%E4%B8%AD%E4%BC%A0%E5%85%A5%E4%BA%86
- 
+%3Ccode%3E
 %E4%B8%8D%E5%AD%98%E5%9C%A8%E7%9A%84/%E4%B8%8D%E6%94%AF%E6%8C%81
@@ -1283,16 +1283,23 @@
 %E7%9A%84 locale
+%3C/code%3E
  %E8%B0%83%E7%94%A8%0A * %7B
@@ -1371,16 +1371,24 @@
 ng)%7D%E4%BC%9A%E6%8A%A5%E9%94%99%0A
+ * %3C/p%3E%0A
  *%0A * @a
@@ -1447,16 +1447,48 @@
 ong%3C/a%3E%0A
+ * @see LocaleChangeInterceptor%0A
  * @sinc
@@ -1723,27 +1723,13 @@
 /** 
-The support locales
+%E6%94%AF%E6%8C%81%E7%9A%84%E8%AF%AD%E8%A8%80
 . */
@@ -2633,25 +2633,24 @@
 -----------%0A
-%0A
         bool
@@ -2831,24 +2831,95 @@
 rue;%0A    %7D%0A%0A
+    //---------------------------------------------------------------%0A%0A
     /**%0A    
@@ -3251,19 +3251,16 @@
 Locales 
-is 
 isNullOr
@@ -3269,16 +3269,22 @@
 pty,you 
+maybe 
 can dire
@@ -3380,17 +3380,16 @@
 cales,%E9%82%A3%E4%B9%88
- 
 %E5%B0%B1%E6%98%AF%E4%B8%AA%E6%99%AE%E9%80%9A%E7%9A%84  
@@ -3435,69 +3435,320 @@
     
-String newLocaleValue = request.getParameter(getParamName());
+//---------------------------------------------------------------%0A        String newLocaleValue = request.getParameter(getParamName());%0A        //since 1.12.6%0A        if (isNullOrEmpty(newLocaleValue))%7B%0A            return true;%0A        %7D%0A%0A        //---------------------------------------------------------------
 %0A   

优化 SupportLocaleChangeInterceptor null 的日志 fix #90