Kent, that's a great solution, but in order to adhere to RFC 2616 shouldn't you also prevent the response from being written to the client, as the StackOverflow guys details?
"The HEAD method is identical to GET except that the server MUST NOT
return a message-body in the response. "
What do you think about:
Code:
if ("HEAD".equals(request.getMethod())) {
filterChain.doFilter(new HttpMethodRequestWrapper("GET", request),
new NullHttpServletResponseWrapper(response));
} else {
filterChain.doFilter(request, response);
}
Code:
private static class NullHttpServletResponseWrapper extends HttpServletResponseWrapper {
private static final PrintWriter writer = new PrintWriter(new NullOutputStream());
private static final ServletOutputStream out = new ServletOutputStream() {
@Override
public void write(int b) throws IOException {
//noop
}
@Override
public void write(byte[] b) throws IOException {
//noop
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
//noop
}
@Override
public void flush() throws IOException {
//noop
}
};
private NullHttpServletResponseWrapper(HttpServletResponse response) {
super(response);
}
@Override
public ServletOutputStream getOutputStream() throws IOException {
return out;
}
@Override
public PrintWriter getWriter() throws IOException {
return writer;
}
}