Это идеальный вариант использования для Apache Commons IO CountingOutputStream
.Вам нужно создать Filter
, который использует HttpServletResponseWrapper
, чтобы заменить OutputStream
ответа этим, а также заменит Writer
, который должен обернуть OutputStream
.Затем возьмите экземпляр HttpServletResponseWrapper
в области запроса, чтобы вы могли получить getByteCount()
из CountingOutputStream
.
Вот начальный пример CountingFilter
:
public class CountingFilter implements Filter {
@Override
public void init(FilterConfig arg0) throws ServletException {
// NOOP.
}
@Override
public void doFilter(ServletRequest request, final ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletResponse httpres = (HttpServletResponse) response;
CountingServletResponse counter = new CountingServletResponse(httpres);
HttpServletRequest httpreq = (HttpServletRequest) request;
httpreq.setAttribute("counter", counter);
chain.doFilter(request, counter);
counter.flushBuffer(); // Push the last bits containing HTML comment.
}
@Override
public void destroy() {
// NOOP.
}
}
CountingServletResponse
:
public class CountingServletResponse extends HttpServletResponseWrapper {
private final long startTime;
private final CountingServletOutputStream output;
private final PrintWriter writer;
public CountingServletResponse(HttpServletResponse response) throws IOException {
super(response);
startTime = System.nanoTime();
output = new CountingServletOutputStream(response.getOutputStream());
writer = new PrintWriter(output, true);
}
@Override
public ServletOutputStream getOutputStream() throws IOException {
return output;
}
@Override
public PrintWriter getWriter() throws IOException {
return writer;
}
@Override
public void flushBuffer() throws IOException {
writer.flush();
}
public long getElapsedTime() {
return System.nanoTime() - startTime;
}
public long getByteCount() throws IOException {
flushBuffer(); // Ensure that all bytes are written at this point.
return output.getByteCount();
}
}
CountingServletOutputStream
:
public class CountingServletOutputStream extends ServletOutputStream {
private final CountingOutputStream output;
public CountingServletOutputStream(ServletOutputStream output) {
this.output = new CountingOutputStream(output);
}
@Override
public void write(int b) throws IOException {
output.write(b);
}
@Override
public void flush() throws IOException {
output.flush();
}
public long getByteCount() {
return output.getByteCount();
}
}
Вы можете использовать его на любой (даже не JSF) странице следующим образом:
<!DOCTYPE html>
<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Counting demo</title>
</h:head>
<h:body>
<h1>Hello World</h1>
</h:body>
</html>
<!-- page size: #{counter.byteCount / 1000}KB -->
<!-- render time: #{counter.elapsedTime / 1000000}ms -->