Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.penglecode.common.web.interceptor;
  
  import java.util.Arrays;
  import java.util.HashMap;
  import java.util.Map;
 
 
 
Http请求日志记录拦截器

Author(s):
pengpeng
Version:
1.0
Date:
2014年10月16日 下午9:21:35
 
 
 	private static final Pattern messageSourceCodePattern = Pattern.compile("\\$\\{([a-zA-Z0-9_.]+)\\}");
 	
 	private static final Logger logger = LoggerFactory.getLogger(AbstractHttpRequestLoggingInterceptor.class);
 	
 	private static final UrlPathHelper urlPathHelper = new UrlPathHelper();
 	
 	private static final ExecutorService httpRequestLoggingExecutor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2, new NamedThreadFactory("HTTP-REQUEST-LOGGING-EXECUTE-WORKER-"));
 	
 	private static final ThreadLocal<HttpRequestLogger<Object>> httpRequestLoggerThreadLocal = new NamedThreadLocal<HttpRequestLogger<Object>>("httpRequestLogger ThreadLocal");

请求进入处理器方法之前调用
 
 	public boolean preHandle(HttpServletRequest requestHttpServletResponse responseObject handlerthrows Exception {
 		try {
 			String currentRequestURI = .getRequestUri(request);
 			Map<String,String[]> originalParamMap = request.getParameterMap();
 			Map<String,StringparamMap = new HashMap<String,String>();
 			if(originalParamMap != null && !originalParamMap.isEmpty()){
 				for(Map.Entry<StringString[]> entry : originalParamMap.entrySet()){
 					paramMap.put(entry.getKey(), getStringParameterValue(entry.getValue()));
 				}
 			}
 			HttpRequestLogger<ObjecthttpRequestLogger = new HttpRequestLogger<Object>();
 			httpRequestLogger.setOptBeginMillis(System.currentTimeMillis());
 			httpRequestLogger.setUri(currentRequestURI);
 			httpRequestLogger.setOptTime(DateTimeUtils.formatNow());
 			httpRequestLogger.setMethod(request.getMethod());
 			httpRequestLogger.setOptUser(getOptUser(request));
 			httpRequestLogger.setParams(paramMap);
 			httpRequestLogger.setClientIpAddr(NetUtils.getRemoteIpAddr(request));
 			httpRequestLogger.setServerIpAddr(NetUtils.getLocalIpAddr(request));
 			httpRequestLogger.setOptEndMillis(null);
 			httpRequestLogger.setProcessTime1(null);
 			httpRequestLogger.setProcessTime2(null);
 			httpRequestLogger.setAsynRequest(HttpUtils.isAsynRequest(request));
 			.set(httpRequestLogger);
 			.debug(">>> do request logging[preHandle] : " + httpRequestLogger);
 			if(handler != null && handler instanceof HandlerMethod){
 				HandlerMethod handlerMethod = (HandlerMethodhandler;
 				HttpRequestLogging httpRequestLoggingAnnotation = handlerMethod.getMethodAnnotation(HttpRequestLogging.class);
 				if(httpRequestLoggingAnnotation != null){
 					String title = httpRequestLoggingAnnotation.title();
 					if(!StringUtils.isEmpty(title)){
 						if(matcher.find()){
 							title = getMessage(matcher.group(1));
 						}
 					}
 					if(StringUtils.isEmpty(title)){
 						title = getMessage("logging.title.default");
 					}
 					httpRequestLogger.setTitle(title);
 				}
 			}
 		} catch (Exception e) {
 		}
 		return true;
 	}

Action方法执行完毕以后调用
	public void postHandle(HttpServletRequest requestHttpServletResponse responseObject handlerModelAndView modelAndViewthrows Exception {
		if (httpRequestLogger != null) {
			try {
				httpRequestLogger.setOptEndMillis(System.currentTimeMillis());
				httpRequestLogger.setProcessTime1(httpRequestLogger.getOptEndMillis() - httpRequestLogger.getOptBeginMillis());
				httpRequestLogger.setLoggingCompleted(true);
				.debug(">>> do request logging[postHandle] : " + httpRequestLogger);
				if(handler != null && handler instanceof HandlerMethod){
					HandlerMethod handlerMethod = (HandlerMethodhandler;
					HttpRequestLogging httpRequestLoggingAnnotation = handlerMethod.getMethodAnnotation(HttpRequestLogging.class);
					if(httpRequestLoggingAnnotation != null && httpRequestLoggingAnnotation.persistIntoDatabase() && httpRequestLogger.getOptUser() != null){
					}
				}
finally {
			}
		}
	}

渲染页面完成以后调用
	public void afterCompletion(HttpServletRequest requestHttpServletResponse responseObject handlerException exthrows Exception {
	}
	public void destroy() throws Exception {
	}

获取操作人的LoginUser对象

Parameters:
request
Returns:
	protected abstract <T> T getOptUser(HttpServletRequest request);

从国际化资源文件中获取message

Parameters:
code
Returns:
	protected abstract String getMessage(String code);

创建HttpRequestLogger处理器(如将日志写入数据库、日志文件等等)

Returns:
	protected abstract <T> AbstractHttpRequestLoggerHandler<T> createHttpRequestLoggerHandler(HttpRequestLogger<T> httpRequestLogger);
	protected String getStringParameterValue(String[] values){
		if(values == null){
			return null;
		}else{
			return values.length == 1 ? values[0] : Arrays.toString(values);
		}
	}
New to GrepCode? Check out our FAQ X