在spring配置文件中配置拦截器
<mvc:interceptors>
<bean class="aaaHandlerInterceptor"/>
</mvc:interceptors>
public class aaaHandlerInterceptor implements HandlerInterceptor {
@Autowiredprivate LogsService logsService;
private static List<List<LogsOuter>> logsList = null;
private static int logsPoint = 0;
private static final int listSize = 1;
private static final int everyListSize = 1;
private static final int accessRecordSize = 1;
/**
* 主要用于统计访问次数和点击率
*
* @param request
* @param response
* @param object
* @return
* @throws Exception
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
String ip = request.getRemoteHost();
if (StringUtils.isBlank(ip))
return true;
if (0 < logsService.queryIPCount(ip))
return true;
LogsOuter logs = new LogsOuter();
String uri = request.getRequestURI();//返回请求行中的资源名称
saveAccessRecord(ip, uri);
Cookie[] cookies = request.getCookies();
String cookieString = "";
if (null != cookies && 0 < cookies.length) {
for (Cookie cookie : cookies) {
if (StringUtils.isBlank(cookieString)) {
cookieString += cookie.getName() + ":" + cookie.getValue();
} else {
cookieString += ";" + cookie.getName() + ":" + cookie.getValue();
}
}
}
logs.setUri(StringUtils.isNotBlank(uri) ? uri : "");
logs.setIp(null != request.getRemoteHost() ? request.getRemoteHost() : "");
logs.setCookies(cookieString);
logs.setReferer(null != request.getHeader("Referer") ? request.getHeader("Referer") : "");
if (null == logsList)
logsList = new ArrayList<List<LogsOuter>>(listSize);
if (0 == logsList.size()) {
for (int i = 0; i < listSize; i++)
logsList.add(new ArrayList<LogsOuter>());
}
logsList.get(logsPoint % listSize).add(logs);
if (everyListSize == logsList.get(logsPoint % listSize).size()) {
logsService.insertLogs(logsList.get(logsPoint % listSize));
logsList.get(logsPoint % listSize).clear();
}
logsPoint++;
if (listSize * everyListSize == logsPoint) {
logsPoint = 0;
}
return true;
}
private void saveAccessRecord(String ip, String uri) {
List<AccessRecord> accessRecordList = null;
String focusId = "";
AccessRecord accessRecord = null;
if (uri.matches("^\\/hotel\\/\\d+\\.html$")) {
if (null == accessRecord)
accessRecord = new AccessRecord();
accessRecord.setSourceType(GoutripUtils.HOTEL);
Pattern pattern = Pattern.compile("^\\/hotel\\/(\\d+)\\.html$");
Matcher matcher = pattern.matcher(uri);
while (matcher.find())
focusId = matcher.group(1);
if (focusId.matches("\\d+"))
accessRecord.setFocusId(Integer.parseInt(focusId));
} else if (uri.matches("^\\/route\\/\\d+\\.html$")) {
if (null == accessRecord)
accessRecord = new AccessRecord();
accessRecord.setSourceType(GoutripUtils.ROUTE);
Pattern pattern = Pattern.compile("^\\/route\\/(\\d+)\\.html$");
Matcher matcher = pattern.matcher(uri);
while (matcher.find())
focusId = matcher.group(1);
if (focusId.matches("\\d+"))
accessRecord.setFocusId(Integer.parseInt(focusId));
} else if (uri.matches("^\\/news\\/\\d+\\.html$")) {
if (null == accessRecord)
accessRecord = new AccessRecord();
accessRecord.setSourceType(GoutripUtils.NEWS);
Pattern pattern = Pattern.compile("^\\/news\\/(\\d+)\\.html$");
Matcher matcher = pattern.matcher(uri);
while (matcher.find())
focusId = matcher.group(1);
if (focusId.matches("\\d+"))
accessRecord.setFocusId(Integer.parseInt(focusId));
} else if (uri.matches("^\\/visa\\/\\d+\\.html$")) {
if (null == accessRecord)
accessRecord = new AccessRecord();
accessRecord.setSourceType(GoutripUtils.VISA);
Pattern pattern = Pattern.compile("^\\/visa\\/(\\d+)\\.html$");
Matcher matcher = pattern.matcher(uri);
while (matcher.find())
focusId = matcher.group(1);
if (focusId.matches("\\d+"))
accessRecord.setFocusId(Integer.parseInt(focusId));
}
if (null == accessRecordList) {
accessRecordList = new ArrayList<AccessRecord>();
}
if (null != accessRecord) {
accessRecord.setIp(ip);
accessRecordList.add(accessRecord);
}
if (accessRecordList.size() >= accessRecordSize) {
logsService.insertAccessRecord(accessRecordList);
}
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) throws Exception {
}