Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Question from Servlet JSR expert group member

Thanks Jim,

I am about to post this response:

It is definitely possible to write an AspectJ aspect that provides the functionality of the servlet filter (and more). I'll explain how below. But note that the AOP solution can use advice to change behavior in the right place, instead of having to use decorators. In practice, this can be very helpful because if you are actually changing the output text with filters (e.g., if you are removing some of the output), using traditional servlet filters makes it challenging (e.g., flushing buffers so you can edit the stream properly). In other words, AOP is more powerful than interception and it's best used to take advantage of that rather than reinventing the wheel.

The key observation is that all of these operations are occuring in the control flow (context) of a Web request. AspectJ provides a cflow pointcut descriptor that you can use to pick out the HttpServlet that is handling this request and the HttpServletRequest argument to it. For example:

aspect FilterAspect {
    pointcut requestExec(HttpServlet servlet, HttpServletRequest request, HttpServletResponse response) :
        (execution(* HttpServlet.do*(..)) || execution(* service(..))) &&
        this(servlet) && args(request, response);

    pointcut responseOutputCall(HttpServletResponse response): 
        (call(* getOutputStream(..)) || call(* getWriter(..)) ||
        call(* setContentLength(..))) && target(response);

    pointcut responseOutputWithinRequest(HttpServlet servlet, 
        HttpServletRequest request, HttpServletResponse response) : 
        cflow(requestExec(servlet, request, *)) && responseOutputCall(response);
...
}

You could also use !cflowbelow(call(* RequestDispatcher+.*(..)) to not run your filter advice when a request dispatcher is on the stack. 

To limit the application of a filter to a particular Web app, you could apply your aspects to that particular app (either by compiling, linking after compile, or by using a classloader implementation). In general the Servlets running in Web apps belong the app themself (even if they are part of a library like Struts), so it's feasible to apply aspects to them locally. For JSP's, it can require some configuration (either precompiling, plugging in an adapter to the JSP process like Tomcat's Ant build, or using classloader hooks like BEA and soon Oracle offer).

While in some contexts it would be helpful to have a container that supported applying aspects to system classes but only for a single application, in this case you don't require it. I'd also point out that this is purely an implementation issue and not a fundamental problem with AspectJ (or AOP). 

Ron

Ron Bodkin
Chief Technology Officer
New Aspects of Software
m: (415) 509-2895

> ------------Original Message------------
> From: "Jim Z" <otugbox@xxxxxxxxxxx>
> To: aspectj-users@xxxxxxxxxxx
> Date: Fri, Jan-16-2004 7:25 AM
> Subject: [aspectj-users] Question from Servlet JSR expert group member
> 
> I noticed that Greg Wilkins (Jetty founder, Servlet JSR member) has some 
> interesting questions on his BLOG. I thought someone here might want to 
> address them -
> 
> http://www.mortbay.com/MB/log/gregw/?permalink=filtersVaspects.html&page=comments
> 
> _________________________________________________________________
> Scope out the new MSN Plus Internet Software — optimizes dial-up to the max! 
>    http://join.msn.com/?pgmarket=en-us&page=byoa/plus&ST=1
> 
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> http://dev.eclipse.org/mailman/listinfo/aspectj-users
> 
> 


Back to the top