Topic
No replies
SystemAdmin
SystemAdmin
2768 Posts
ACCEPTED ANSWER

Pinned topic How to get content from HttpServletResponse

‏2009-08-18T13:26:39Z |
Hi All

I have written a filter on top of a Servlet. After doFilter() method, I want to get the content from the response object and save it inside cache. The content in the response is set by PrintWriter.println(xmlContent).

Here is the code snippet:




public 

void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 

throws IOException, ServletException 
{   

if (debug) 
{ log(
"ServletContainerFilter:doFilter()"); 
}   RequestWrapper wrappedRequest = 

new RequestWrapper((HttpServletRequest) request); ResponseWrapper wrappedResponse = 

new ResponseWrapper((HttpServletResponse) response);   

if (!doBeforeProcessing(wrappedRequest, wrappedResponse)) 
{ Throwable problem = 

null;   

try 
{ chain.doFilter(wrappedRequest, wrappedResponse); doAfterProcessing(wrappedRequest, wrappedResponse); 
} 

catch (Throwable t) 
{ 
// If an exception is thrown somewhere down the filter chain, 
// we still want to execute our after processing, and then 
// rethrow the problem after that. problem = t; t.printStackTrace(); 
// If there was a problem, we want to rethrow it if it is 
// a known type, otherwise log it. 

if (problem != 

null) 
{ 

if (problem 

instanceof ServletException) 
{ 

throw (ServletException) problem; 
} 

if (problem 

instanceof IOException) 
{ 

throw (IOException) problem; 
} sendProcessingError(problem, response); 
} 
} 
} 

return; 
}   

private 

void doAfterProcessing(HttpServletRequestWrapper request, HttpServletResponseWrapper response) 

throws IOException, ServletException 
{ 

if (debug) 
{ log(
"ServletContainerFilter:DoAfterProcessing"); 
} CacheManager cacheManager = CacheManager.getInstance(); String xmlContent = response.getContent(); System.out.println(
"XML Content just before saving in cache:\n" + xmlContent); cacheManager.save(getAbsoluteUrl(request), xmlContent); 
}


Please look at this line in the code snippet:
String xmlContent = response.getContent();

Right now, getContent() method is not available in HttpServletResponseWrapper class. But, I want a work around for this.

Thanks for showing your interest in this post.

Regards,
Sumved Shami