package com.ciweb.common.base;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.apache.struts2.StrutsStatics;
import com..ciweb.common.enums.NameSpace;
import com.ciweb.common.util.StringTools;
import com..ciweb.service.LoginInfoService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
/**
*
* 登陆拦截器
*
* @author chenze
* @version [版本号, 2011-10-10]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class ValidateAuthority implements Interceptor
{
/**
* 注释内容
*/
private static final long serialVersionUID = 1L;
private static final Logger log =
Logger.getLogger(ValidateAuthority.class);
private LoginInfoService loginInfoService;
/**
* {@inheritDoc}
*/
@Override
public void destroy()
{
}
/**
* {@inheritDoc}
*/
@Override
public void init()
{
}
/**
* {@inheritDoc}
*/
@Override
public String intercept(ActionInvocation invocation)
throws Exception
{
ActionContext actionContext = invocation.getInvocationContext();
HttpServletRequest request =
(HttpServletRequest)actionContext.get(StrutsStatics.HTTP_REQUEST);
// HttpServletResponse response = (HttpServletResponse)actionContext.get(StrutsStatics.HTTP_RESPONSE);
StringBuilder url = new StringBuilder();
url.append(request.getScheme() + "://" + request.getServerName() + ":"
+ request.getServerPort() + request.getContextPath()
+ request.getServletPath() + "?");
HttpSession session = request.getSession();
//刷新计数器
if (session != null && session.isNew())
{
loginInfoService.modifyCount();
}
Map m = request.getParameterMap();
Set set = m.keySet();
for (Object key : set)
{
Object o = ((Object[])m.get(key))[0];
if (o.getClass().getName().equals("java.lang.String"))
{
url.append(key + "=" + StringTools.toUTF8(o.toString()) + "&");
}
}
request.getSession().setAttribute("url", url);
//正则表达式来判断URL的模块
this.regNameSpace(url.toString());
log.info("请求地址:" + url);
String servletPath = request.getServletPath();
//kindeditor图片或文件下载免验证
if ("/kindfileDown.action".equals(servletPath)
|| "/project/projectFindAll!findAll.action".equals(servletPath)
|| "/admin/askright/project.action".equals(servletPath))
{
return invocation.invoke();
}
Cookie[] cookies = request.getCookies();
String result = "login";
if (cookies != null && cookies.length > 0)
{
for (Cookie c : cookies)
{
if ("user_id".equals(c.getName())
&& StringTools.isNotEmpty(c.getValue()))
{
result = "index";
}
}
}
if ("login".equals(result))
{
return "login";
}
else
{
// response.sendRedirect(path);
return invocation.invoke();
}
}
private void regNameSpace(String url)
{
// 根据正则表达式取到命名空间的模块
String reg = "/(\\w+)(/\\w+)(\\.action)(?!.*vpar=)";
Pattern p = Pattern.compile(reg);
Matcher mt = p.matcher(url);
String nameSpace = "";
if (mt.find())
{
nameSpace = mt.group(1);
}
//判断 ciweb模块
if (NameSpace.ABILITY.equals(nameSpace))
{
loginInfoService.modifyStatistics(nameSpace);
}
else if (NameSpace.ASKRIGHT.equals(nameSpace))
{
loginInfoService.modifyStatistics(nameSpace);
}
else if (NameSpace.ASKRIGHTMANAGE.equals(nameSpace))
{
loginInfoService.modifyStatistics(nameSpace);
}
else if (NameSpace.BACKSTAGE.equals(nameSpace))
{
loginInfoService.modifyStatistics(nameSpace);
}
else if (NameSpace.CALCULATE.equals(nameSpace))
{
loginInfoService.modifyStatistics(nameSpace);
}
else if (NameSpace.CIRULE.equals(nameSpace))
{
loginInfoService.modifyStatistics(nameSpace);
}
else if (NameSpace.DATADISPLAY.equals(nameSpace))
{
loginInfoService.modifyStatistics(nameSpace);
}
else if (NameSpace.HELPAPPLY.equals(nameSpace))
{
loginInfoService.modifyStatistics(nameSpace);
}
else if (NameSpace.HELPAPPLY.equals(nameSpace))
{
loginInfoService.modifyStatistics(nameSpace);
}
else if (NameSpace.INTEGRITY.equals(nameSpace))
{
loginInfoService.modifyStatistics(nameSpace);
}
else if (NameSpace.ISSUE.equals(nameSpace))
{
loginInfoService.modifyStatistics(nameSpace);
}
else if (NameSpace.LTEMANAGE.equals(nameSpace))
{
loginInfoService.modifyStatistics(nameSpace);
}
else if (NameSpace.MAILGROUP.equals(nameSpace))
{
loginInfoService.modifyStatistics(nameSpace);
}
else if (NameSpace.MAIN.equals(nameSpace))
{
loginInfoService.modifyStatistics(nameSpace);
}
else if (NameSpace.PARAM.equals(nameSpace))
{
loginInfoService.modifyStatistics(nameSpace);
}
else if (NameSpace.PROJECT.equals(nameSpace))
{
loginInfoService.modifyStatistics(nameSpace);
}
else if (NameSpace.REVIEW.equals(nameSpace))
{
loginInfoService.modifyStatistics(nameSpace);
}
else if (NameSpace.TRACE.equals(nameSpace))
{
loginInfoService.modifyStatistics(nameSpace);
}
else if (NameSpace.TRAIN.equals(nameSpace))
{
loginInfoService.modifyStatistics(nameSpace);
}
else if (NameSpace.VOTE.equals(nameSpace))
{
loginInfoService.modifyStatistics(nameSpace);
}
else if (!"".equals(nameSpace))
{
loginInfoService.modifyStatistics(NameSpace.OTHER);
}
}
/**
* <初始化session>
* <功能详细描述>
* @param session session
* @param nameSpace 命名空间
* @see [类、类#方法、类#成员]
*/
private void initSession(HttpSession session, String nameSpace)
{
if (session.getAttribute(nameSpace) == null)
{
session.setAttribute(nameSpace, 0);
}
}
public void setLoginInfoService(LoginInfoService loginInfoService)
{
this.loginInfoService = loginInfoService;
}
}