@Override
public void doFilter(HttpServletRequest request, HttpServletResponse response, HttpSession session, FilterChain chain) throws ServletException,
IOException {
BufferedHttpServletResponse bufferedResponse = new BufferedHttpServletResponse(response);
// By default don't buffer, code has to activate this explicitly.
bufferedResponse.setPassThrough(true);
request.setAttribute(BUFFERED_RESPONSE, bufferedResponse);
try {
chain.doFilter(request, bufferedResponse);
} finally {
if (!bufferedResponse.isPassThrough()) {
// TODO: output stream support
response.getWriter().write(bufferedResponse.getBufferAsString());
}
}
}