package de.fred4jupiter.fredbet.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.ThreadContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class ExecutionTimeInterceptor extends HandlerInterceptorAdapter {

	private static final Logger LOG = LoggerFactory.getLogger(ExecutionTimeInterceptor.class);

	private static final String START_TIME_ATTRIBUTE_NAME = "startTime";

	// before the actual handler will be executed
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		long startTime = System.currentTimeMillis();
		request.setAttribute(START_TIME_ATTRIBUTE_NAME, startTime);

		final String requestURI = request.getRequestURI();

		ThreadContext.put("mdc.requestURI", requestURI);
		return true;
	}

	// after the handler is executed
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
			throws Exception {
		long startTime = (Long) request.getAttribute(START_TIME_ATTRIBUTE_NAME);
		long endTime = System.currentTimeMillis();
		long executeTime = endTime - startTime;

		final String requestURI = request.getRequestURI();

		ThreadContext.put("mdc.executionTime", "" + executeTime);

		LOG.debug("requestURI={}, executionTime={}", requestURI, executeTime);

		ThreadContext.clearMap();
	}
}

@@ -35,102 +35,8 @@
 b;%0A%0A
-import javax.servlet.http.HttpServletRequest;%0Aimport javax.servlet.http.HttpServletResponse;%0A%0A
 impo
@@ -174,28 +174,39 @@
 servlet.
-ModelAndView
+AsyncHandlerInterceptor
 ;%0Aimport
@@ -242,41 +242,114 @@
 let.
-handler.HandlerInterceptorAdapter
+ModelAndView;%0A%0Aimport javax.servlet.http.HttpServletRequest;%0Aimport javax.servlet.http.HttpServletResponse
 ;%0A%0Ap
@@ -389,16 +389,24 @@
 tor 
-extend
+implement
 s 
+Async
 Hand
@@ -423,20 +423,16 @@
 ptor
-Adapter %7B%0A%0A%09
+ %7B%0A%0A    
 priv
@@ -519,17 +519,20 @@
 lass);%0A%0A
-%09
+    
 private 
@@ -593,17 +593,20 @@
 Time%22;%0A%0A
-%09
+    
 // befor
@@ -639,25 +639,42 @@
 be executed%0A
-%09
+    @Override%0A    
 public boole
@@ -760,37 +760,26 @@
 andler) 
-throws Exception %7B%0A%09%09
+%7B%0A        
 long sta
@@ -815,18 +815,24 @@
 llis();%0A
-%09%09
+        
 request.
@@ -884,18 +884,24 @@
 Time);%0A%0A
-%09%09
+        
 final St
@@ -936,34 +936,40 @@
 tRequestURI();%0A%0A
-%09%09
+        
 ThreadContext.pu
@@ -1001,18 +1001,24 @@
 stURI);%0A
-%09%09
+        
 return t
@@ -1026,13 +1026,19 @@
 ue;%0A
-%09%7D%0A%0A%09
+    %7D%0A%0A    
 // a
@@ -1066,17 +1066,34 @@
 xecuted%0A
-%09
+    @Override%0A    
 public v
@@ -1212,11 +1212,20 @@
 ew)%0A
-%09%09%09
+            
 thro
@@ -1239,18 +1239,24 @@
 ption %7B%0A
-%09%09
+        
 long sta
@@ -1320,18 +1320,24 @@
 _NAME);%0A
-%09%09
+        
 long end
@@ -1371,18 +1371,24 @@
 llis();%0A
-%09%09
+        
 long exe
@@ -1420,18 +1420,24 @@
 tTime;%0A%0A
-%09%09
+        
 final St
@@ -1476,26 +1476,32 @@
 uestURI();%0A%0A
-%09%09
+        
 ThreadContex
@@ -1547,18 +1547,24 @@
 Time);%0A%0A
-%09%09
+        
 LOG.debu
@@ -1631,10 +1631,16 @@
 );%0A%0A
-%09%09
+        
 Thre
@@ -1665,9 +1665,12 @@
 ();%0A
-%09
+    
 %7D%0A%7D%0A

update removing deprecation