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()
{
}
}