ifeilong / feilong-spring

:gem:focus on spring
Apache License 2.0
4 stars 12 forks source link

想办法 把 SupportLocaleChangeInterceptor (handler instanceof HandlerMethod) 部分去掉 #85

Closed venusdrogon closed 6 years ago

venusdrogon commented 6 years ago

想办法 把 SupportLocaleChangeInterceptor (handler instanceof HandlerMethod) 部分去掉

venusdrogon commented 6 years ago

public class SupportLocaleChangeInterceptor extends AbstractHandlerMethodInterceptorAdapter{

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

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

    /**
     * Default name of the locale specification parameter: "locale".
     * 
     * @since 1.12.7
     */
    public static final String  DEFAULT_PARAM_NAME = "locale";

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

    /** 支持的语言. */
    private List<String>        supportLocales;

    /**
     * The param name.
     * 
     * @since 1.12.7
     */
    private String              paramName          = DEFAULT_PARAM_NAME;

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

    /*
     * (non-Javadoc)
     * 
     * @see org.springframework.web.servlet.i18n.LocaleChangeInterceptor#preHandle(javax.servlet.http.HttpServletRequest,
     * javax.servlet.http.HttpServletResponse, java.lang.Object)
     */
    @Override
    public boolean doPreHandle(HttpServletRequest request,HttpServletResponse response,HandlerMethod handlerMethod){
        boolean canHandle = isSupport(request);
        if (canHandle){
            handler(request, response);
        }
        //不管支不支持  都return true
        return true;
    }

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

    /**
     * copy from {@link LocaleChangeInterceptor}.
     *
     * @param request
     *            the request
     * @param response
     *            the response
     * @return true, if successful
     * @since 1.12.7
     */
    private boolean handler(HttpServletRequest request,HttpServletResponse response){
        LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
        if (localeResolver == null){
            throw new IllegalStateException("No LocaleResolver found: not in a DispatcherServlet request?");
        }
        String newLocale = request.getParameter(this.paramName);
        localeResolver.setLocale(request, response, StringUtils.parseLocaleString(newLocale));
        // Proceed in any case.
        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 isNullOrEmpty,you maybe can direct use LocaleChangeInterceptor");
            return true; //如果isNotNullOrEmpty supportLocales,那么就是个普通的  LocaleChangeInterceptor
        }

        //---------------------------------------------------------------
        String newLocaleValue = request.getParameter(paramName);
        //since 1.12.6
        if (isNullOrEmpty(newLocaleValue)){
            return true;
        }

        //---------------------------------------------------------------
        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;
    }

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

    /**
     * Set the name of the parameter that contains a locale specification in a locale change request.
     * 
     * <p>
     * Default is "locale".
     * </p>
     *
     * @param paramName
     *            the new param name
     * @since 1.12.7
     */
    public void setParamName(String paramName){
        this.paramName = paramName;
    }

}

代码比较简单, 挪过来了

venusdrogon commented 6 years ago

02:42:48 INFO  (AbstractHandlerMethodInterceptorAdapter.java:98) postConstruct() - [com.feilong.spring.web.servlet.interceptor.i18n.SupportLocaleChangeInterceptor] field's value map:
[    {
        "paramName": "locale",
        "supportLocales": "[in_ID, en_US]"
    }]