package com.mtf.vega.interceptor;
import java.util.List;
import java.util.Properties;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import com.mtf.vega.model.Activity;
import com.mtf.vega.service.IActivityService;
import com.mtf.vega.util.Constants;
import com.mtf.vega.util.SecurityUtils;
import com.mtf.vega.util.SessionInfo;
/**
* 活跃度拦截器
*/
public class ActivityInterceptor implements HandlerInterceptor {
private static final Log logger = LogFactory.getLog(ActivityInterceptor.class);
private List<String> operationNodes;
@Autowired
private IActivityService activityService;
/**
* 设定需要记录的资源节点
* 例如: /workgroup/, /maintenance/, /admin/
*
* @param operationNodes 资源节点
*/
public void setOperationNodes(List<String> operationNodes) {
this.operationNodes = operationNodes;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object, Exception exception) throws Exception {
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object, ModelAndView modelAndView) throws Exception {
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
String requestUri = request.getRequestURI();
String contextPath = request.getContextPath();
String uri = requestUri.indexOf(".do") > 0 ? requestUri.substring(contextPath.length(), requestUri.indexOf(".do") + 3) : requestUri.substring(contextPath.length());
if (uri.contains("/do")) {
HttpSession session = SecurityUtils.getSession(request);
if (session != null) {
boolean isActivity = false;
Properties properties = (Properties) session.getServletContext().getAttribute(Constants.VEGA_GLOBAL);
if (properties != null && properties.containsKey("sys.activity")) {
String str = properties.getProperty("sys.activity");
if ("true".equalsIgnoreCase(str)) {
isActivity = true;
} else if ("yes".equalsIgnoreCase(str)) {
isActivity = true;
} else if ("y".equalsIgnoreCase(str)) {
isActivity = true;
} else if ("1".equals(str)) {
isActivity = true;
}
}
if (isActivity) {
SessionInfo sessionInfo = (SessionInfo) session.getAttribute(Constants.SESSION_INFO);
if (sessionInfo != null) {
boolean isHit = false;
if (this.operationNodes != null && this.operationNodes.size() > 0) {
for (String s : this.operationNodes) {
if (uri.contains(s)) {
isHit = true;
break;
}
}
}
if (isHit) {
Activity activity = new Activity();
activity.setUserId(sessionInfo.getUserId());
activity.setUserName(sessionInfo.getDisplayName());
activity.setUri(uri);
try {
this.activityService.advAdd(activity);
} catch (Exception e) {
logger.warn(e);
}
}
}
}
}
}
return true;
}
}
ActivityInterceptor
最新推荐文章于 2023-06-09 09:51:59 发布