最新ARI

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
            {
                boolean ignore = Pattern.compile("(common/vc|common/login|common/logout)$").matcher(url).find();
                if (!ignore)
                {
                    Object user = request.getSession().getAttribute("user");
                    if (user == null)
                    {
                        response.sendError(HttpServletResponse.SC_NOT_FOUND);
                        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";
                Class<?> cls = Class.forName("action." + action_name);
                MethodUtils.invokeMethod(cls.newInstance(), action_method_name, null);
            }
        }
        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());
            }
            else
            {
                throw new ServletException(e);
            }
            RequestContext.redirect("");
        }
        finally
        {
            if (rc != null)
            {
                rc.end();
            }
        }
    }

    @Override
    public void destroy()
    {

    }
}

 

你可能感兴趣的:(r)