URLOpenSessionInViewFilter

/*
* Copyright Huawei Symantec Technologies Co.,Ltd. 2008-2009. All rights reserved.
*
*
*/

package com.huaweisymantec.core.utils;

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

import org.apache.commons.lang.StringUtils;
import org.springframework.orm.hibernate3.support.OpenSessionInViewFilter;

/**
* 缩窄OpenSessionInViewFilter的过滤范围,请求css、js、图片等静态内容时不创建Session连接.
*
* 在web.xml中可配置excludeSuffixs参数,多个后缀名以','分割.
*
*/
public class URLOpenSessionInViewFilter extends OpenSessionInViewFilter {

    /**
     * 需要过滤掉的url设定变量
     */
    private static final String EXCLUDE_SUFFIXS_NAME = "excludeSuffixs";

    /**
     * 需要过滤掉的url,不进行session绑定
     */
    private static final String[] DEFAULT_EXCLUDE_SUFFIXS = { ".js", ".css", ".jpg", ".gif" };

    /**
     * 需要session绑定的请求后缀
     */
    private static final String[] DEFAULT_INCLUDE_SUFFIXS = { ".action", ".do" };

    /**
     * 需要过滤掉的url设定变量数组
     */
    private static String[] excludeSuffixs = null;

    /**
     * 重载过滤控制函数,忽略特定后缀名的请求.
     */
    @Override
    protected boolean shouldNotFilter(final HttpServletRequest request) throws ServletException {
        String path = request.getServletPath();

        // 对必须INCLUDE的URL直接返回false,省略后面对EXCLUDE后缀名的判断.
        for (String suffix : DEFAULT_INCLUDE_SUFFIXS) {
            if (path.endsWith(suffix))
                return false;
        }

        for (String suffix : excludeSuffixs) {
            if (path.endsWith(suffix))
                return true;
        }

        return false;
    }

    /**
     * 初始化excludeSuffixs参数.
     */
    @Override
    protected void initFilterBean() throws ServletException {

        String excludeSuffixStr = getFilterConfig().getInitParameter(EXCLUDE_SUFFIXS_NAME);

        if (StringUtils.isNotBlank(excludeSuffixStr)) {
            excludeSuffixs = excludeSuffixStr.split(",");
            // 处理匹配字符串为".后缀名"
            for (int i = 0; i < excludeSuffixs.length; i++) {
                excludeSuffixs[i] = "." + excludeSuffixs[i];
            }
        } else {
            excludeSuffixs = DEFAULT_EXCLUDE_SUFFIXS;
        }
    }
}

你可能感兴趣的:(URLOpenSessionInViewFilter)