ActivityInterceptor

/*
 * Copyright (c) 2013 LIAONING SHIDAI_WANHENG CO.,LTD. All Rights Reserved.
 * This work contains SHIDAI_WANHENG CO.,LTD.'s unpublished
 * proprietary information which may constitute a trade secret
 * and/or be confidential. This work may be used only for the
 * purposes for which it was provided, and may not be copied
 * or disclosed to others. Copyright notice is precautionary
 * only, and does not imply publication.
 *
 */
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;


/**
 * 活跃度拦截器
 *
 * @author Wade.Zhu
 * @version 1.0 2018-1-15 Wade.Zhu created.
 * @version <ver>
 */
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;
}
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yueyue____1/article/details/80690904
个人分类: Java_EE
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭