Back Forum Reply New

Interceptor modifying response...?

Hello,

I'm in the process of writing our first new application using spring and I am hung up on a feature all of our other applications implement.  We show a javascript dialog box for communicating alerts/errors with the end user.  This dialog box is triggered by our controller setting a value in the user object for the current session.  If a value is present, we embed javascript into the HTML response.  This embedding is done via a servlet filter.

The goal in the end is to allow controllers and any other necessary layer to communicate messages to the end user without having to worry about the view technology.  However, I don't want to worry about rendering the communication in my JSP, it should happen automatically.

For clarity's sake, here is what we do in the filter:Code:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

fromServletRequest hRequest = (fromServletRequest) request;
PrintWriter out = response.getWriter();
CharResponseWrapper wrapper = new CharResponseWrapper((fromServletResponse)response);
chain.doFilter(request, wrapper);

User user = (User) request.getAttribute(quot;userquot;);

if(user.haveError()) {
int start = 0;
int end = 0;
CharArrayWriter caw = new CharArrayWriter();

start = wrapperString.indexOf(quot;gt;quot;, wrapperString.indexOf(quot;lt;bodyquot;)) + 1;
end = start + 1;

caw.write(wrapper.toString().substring(0, start));
// write javascript code
caw.write(OUR_CUSTOM_JAVASCRIPT);
// write new head tag
caw.write(wrapper.toString().substring(end));

response.setContentLength(caw.toString().length());
out.write(caw.toString());
} else {
out.write(wrapper.toString());
}
out.close();
}
Essentially, I wrap the response before any servlet or jsp starts writing to it.  Then after everyone in the chain is done, we check the response for the body tag and insert javascript if there is an error flag in the user object.

Now, I am having trouble recreating this in Spring Web MVC.  I've subclassed a HandlerInterceptorAdaptor and overridden the afterCompletion() method.  I got that far and then realized I couldn't wrap the response before people could modify it.  Then I checked into preHandle().  However, it doesn't look like I would be able to wrap the response there.

So my first question is, is there a way to do what I am trying to do in Spring Web MVC?  If not, then what would be the recommended way?  Has anybody out there tried to do something similiar to this?

Thanks for the help!

I am also interested in a solution to a similar problem.
My servlet filter is generating pdf from XML source using an XSL stylesheet to format it into FO and calling FOP to perform the actual rendering.
I am interested into making it an Interceptor but I am clueless for the time being.
In a last resort I will make a custom view of it but it complicates a bit my design.

What's wrong with the ServletFilter approach?  I think for modifying the response object itself it is probably always better to use a filter than a HandlerInterceptor.  HandlerInterceptors are better for modifying the request / view attributes and for modifying the Handler chain dynamically.

I guess because I didn't realize that you actually *could* configure a ServletFilter in the Spring context.  I just did a search and found references to a DelegatingFilterProxy that would enable this.

I guess that would be the way to go then, huh?

I am generating different XMLs according to the outcome of the validation.
1) If it is successful, the controller routes to the quot;sucessquot; view that generates the XML that is expected by the ServletFilter.  That means the configured XSL matches and FOP is able to perform the correct translation into PDF.
2) If the validation fails, the controller routes to an quot;errorquot; view that generates an XML that does not validate against the same XML Schema as the one generated in 1).  Moreover, I don't want that my XML error messages to be transcoded into PDF at all.

That's mainly why the ServletFilter approach doesn't fit here.
In the general case, I want PDF to be generated but, in all the other cases, I just want the plain XML error message.  Either, I modify the filter to handle the error case by inspecting a given attribute in request, or I modify the XSL to handle the error XML message.  I don't feel comfortable with both approach as it does not do exactly what I want and, from my point of view, a ServletFilter should be kept generic, reusable... hence stateless.

Just for info, my controller acts as a service to a front-end in another application server.

I solved my issue by moving the logic code from the servlet filter into a custom Spring view which better suits my needs.

What do you think of the direction I took ?
¥
Back Forum Reply New