I have a JSF (IceFaces 1.8.2) website. I need the following functionality: When a specific JSF page is displayed, I would like to store its HTML code in a database. I tried to use Servlet filters to capture the code (using a HttpResponseWrapper object as it is written on the SUN webbage and in a lot of tutorials), but the response was always empty. Can somebody help me?
Here is the doFilter:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
Wrapper responseWrapper = new Wrapper((HttpServletResponse)response);
chain.doFilter(request, responseWrapper);
//I just want the output on stdout at the moment...
System.out.println(responseWrapper.toString());
PrintWriter out = response.getWriter();
out.write(responseWrapper.toString());
}
And here is the Wrapper:
public class Wrapper extends HttpServletResponseWrapper{
private CharArrayWriter writer;
public Wrapper(HttpServletResponse response){
super(response);
writer = new CharArrayWriter();
}
public PrintWriter getWriter(){
return (new PrintWriter(writer));
}
public String toString(){
return writer.toString();
}
public char[] toCharArray(){
return (writer.toCharArray());
}
}
And the web.xml:
<filter>
<filter-name>MyFilter</filter-name>
<filter-class>foo.myfilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>*.iface</url-pattern>
</filter-mapping>
Perhaps JSF sends a redirect, or writes the response using
response.getOutputStream()? Try setting break points in all methods ofHttpServletResponseWrapperto discover which methods JSF invokes.