ActionFilter

package util;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.regex.Pattern;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.MethodUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

public class ActionFilter implements Filter
{
    private ServletContext context;
    static Logger log = Logger.getLogger(ActionFilter.class);

    @Override
    public void init(FilterConfig cfg) throws ServletException
    {
        this.context = cfg.getServletContext();
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException,
            ServletException
    {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        RequestContext rc = RequestContext.begin(this.context, request, response);
        String url = request.getRequestURI();
        boolean flag = Pattern.compile("(.css|.js|.png|.gif|.jpg|.jpeg|.jsp|.html|.htm)$").matcher(url).find();
        url = url.substring(request.getContextPath().length() + 1);
        try
        {
            if (url.length() == 0 || flag)
            {
                chain.doFilter(RequestContext.getRequest(), RequestContext.getResponse());
                return;
            }
            else
            {
                long t1 = System.currentTimeMillis();
                log.debug("url=" + url);
                boolean ignore = Pattern.compile("(common/vc|common/login|common/logout)$").matcher(url).find();
                if (!ignore)
                {
                    Object user = request.getSession().getAttribute("user");
                    if (user == null)
                    {
                        String xhr = request.getHeader("x-requested-with");
                        if(!StringUtils.isBlank(xhr)){
                            response.setStatus(1111);
                        }else{              
                            response.getWriter().println("abc");
                        }
                        return;
                    }
                }
                String[] parts = StringUtils.split(url, '/');
                if (parts.length < 1)
                {
                    response.sendError(HttpServletResponse.SC_NOT_FOUND);
                    return;
                }
                String action_name = StringUtils.capitalize(parts[0]) + "Action";
                String action_method_name = (parts.length > 1) ? parts[1] : "execute";
                log.debug("action=" + action_name);
                log.debug("method=" + action_method_name);
                Class<?> cls = Class.forName("action." + action_name);
                MethodUtils.invokeMethod(cls.newInstance(), action_method_name, null);
                long t2 = System.currentTimeMillis();
                log.debug("handleTime=" + (t2 - t1) + "ms");
            }
        }
        catch (Exception e)
        {
            log.debug("Exception in action.");
            if (e instanceof ClassNotFoundException)
            {
                log.debug(e.getMessage() + " Not Found.");
            }
            else if (e instanceof NoSuchMethodException)
            {
                log.debug(e.getMessage());
            }
            else if (e instanceof InvocationTargetException)
            {
                Throwable t = e.getCause();
                log.debug(t.getMessage());
                String xhr = request.getHeader("x-requested-with");
                if(!StringUtils.isBlank(xhr)){
                    response.setStatus(9999);
                    response.getWriter().print(t.getMessage());
                }
            }
            else
            {
                throw new ServletException(e);
            }
        }
        finally
        {
            if (rc != null)
            {
                rc.end();
            }
        }
    }

    @Override
    public void destroy()
    {

    }
}

  

rzy
06-08 649
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值