Spring Profiling/Audit using MVC interceptors

Hi Readers,

recently i got requirement from customer that they want to audit Spring web application services. In simple terms they want to know which of services used more and less. Requirement is simple,just by writing one Spring MVC web interceptor we can scan all the Http requests and write audit logic but here is the actual problem that they want us to audit only few of the services not all other services,it means i have to figure out the methods which need be audited in interceptor.

To achieve i did following  steps

  1. Define custom annotation.
  2. Use this annotation at spring MVC controller method.
  3. Write own interceptor,access called method and class level annotations in it and if expected annotation present then write audit logic

STEP 1:  Define @ApplicationAudit custom annotation.

package com.myjavarecipes.common.controller

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD) //can use in method only.
public @interface AppicationAudit{
	
	String value();
}

STEP 2:Add this custom annotation in Spring controller method addAccount().

package com.myjavarecipes.common.controller

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/accountService")
public class HelloWorldController{

@ApplicationAudit("ADDACCOUNT")
@RequestMapping(value="/addAccount",method = RequestMethod.POST)
public ModelAndView addAccount(){
 
  ModelAndView model = new ModelAndView("AddAccount");
		 
   return model;
  }

@RequestMapping(value="/getAccount",method=RequestMethod.GET)
public ModelAndView getAccount(){

 ModelAndView model=new ModelAndView("getAccount");

 //implementation goes here

 return model;
}

}
 STEP 3: Add Spring MVC interceptor entry in spring context xml.
<mvc:interceptors>
  <bean class="com.myjavarecipes.interceptor.AuditInterceptor" />
</mvc:interceptors>
STEP 4:  write own AuditInterceptor spring web mvc interceptor,in it’s methods preHandle,postHandle,afterCompletion identify called method controller class and method has expected annotation if it present write audit logic.
package com.myjavarecipes.interceptor;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.stereotype.Controller;
import com.myjavarecipes.common.controller.ApplicationAudit; 

public class AuditInterceptor implements HandlerInterceptor { 

@Override 
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { 
System.out.println("Pre-handle"); 
HandlerMethod hm=(HandlerMethod)handler; 
Method method=hm.getMethod(); if(method.getDeclaringClass().isAnnotationPresent(Controller.class)){
 if(method.isAnnotationPresent(ApplicationAudit.class))
{ 
System.out.println(method.getAnnotation(ApplicationAudit.class).value()); 
request.setAttribute("STARTTIME",System.currentTimemillis());
 }
} 
return true; 
} 

@Override 
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
 System.out.println("Post-handle"); 
HandlerMethod hm=(HandlerMethod)handler;
 Method method=hm.getMethod();
if(method.getDeclaringClass().isAnnotationPresent(Controller.class)){

if(method.isAnnotationPresent(ApplicationAudit.class))
{ System.out.println(method.getAnnotation(ApplicationAudit.class).value()); request.setAttribute("ENDTIME",System.currentTimemillis());  }
 }
} 

@Override 
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { 
System.out.println("After completion handle"); HandlerMethod hm=(HandlerMethod)handler; 
Method method=hm.getMethod(); if(method.isAnnotationPresent(ApplicationAudit.class))
{ System.out.println(method.getAnnotation(ApplicationAudit.class).value()); 
System.out.println("Total Took:"+((Long)request.getAttribute("ENDTIME")-(Long)request.getAttribute("STARTTIME")));  
 }  
 } 
}

Leave a Reply

WordPress spam blocked by CleanTalk.