I need to add some audit logs to some business methods that are not in the web tier. One of the requirements are to store in this audit logs the user that executed the business method and the IP of the remote terminal. I decided to use AspectJ to Audit all this methods to avoid contaminating them with no bussiness code. (Note: I’m not using Spring, this is an old Struts 1 webapp)
public void myMethod(Object param, HttpRequest request**){
//Bussiness code that never use request param
}
@After("bla bla matches myMethod")
public void myAdvice(JoinPoint jp){
Object request = (HttpServletRequest)jp.getArgs()[1];
//bla bla using the request to get user and IP
}
So my question is, does anyone have any suggestion to obtain in a better or elegant way the Servlet Request in the Advice? In fact, myMethod does not contain Audit code (that is really good) but now have a second parammeter that is not used directly by the method and can become confusing to other developers. Please do not recomend me to advice the Servlet instead the business method.
Logging the IP from
myMethodalready ties the method to the web tier (or at least a tier with a meaningful IP address). AnHttpServletRequestisn’t appropriate, obviously, but an implementation providing “useful” information about the context of the call would be.If the goal is to remove any extra parameter to
myMethod, my first thought would be to create aRequestProcessorthat stores the “useful” info in aThreadLocal, which would be used by the logging aspect.