Handle HEAD requests

Most containers handle a HEAD request the same as a GET request and so include all logic to build the page. While only the header is requested this could be a waste of resources.

To solve this a filter can be made that filters HEAD requests and stops further processing of the request.

package app.filters;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

public class HeadFilter implements Filter {
    private static final Logger LOGGER = Logger.getLogger(HeadFilter.class);

    @Override
    public void destroy() {
        // Do nothing
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        String method = ((HttpServletRequest) request).getMethod();
        LOGGER.debug("HeadFilter: dofilter with '" + method + "'");
        if (!method.equalsIgnoreCase("HEAD")) {
            chain.doFilter(request, response);
        }
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // Do nothing
    }
}

Because this filter needs to be the first filter in the chain it can't be annotated while then the order is not deterministic. Instead the filter must be configured in web.xml:

 <filter>
     <filter-name>headFilter</filter-name>
     <filter-class>com.deltalloyd.am.backmgr.filter.HeadFilter</filter-class>
 </filter>
 <filter-mapping>
     <filter-name>headFilter</filter-name>
     <url-pattern>/*</url-pattern>
 </filter-mapping>

 For HEAD requests authentication might not be needed. Put this in the same web.xml:

 <security-constraint>
     <web-resource-collection>
         <web-resource-name>Only HEAD</web-resource-name>
         <url-pattern>/</url-pattern>
         <http-method>HEAD</http-method>
     </web-resource-collection>
 </security-constraint>