Showing Google’s Web Accelerator the door using Java / ServletFilter

The guys over at 37 Signals posted an explanation of why they think the Google Web Accelerator is a bad idea and a link to a page which shows how you can disable the Google Web Accelerator by analyzing HTTP headers using Ruby. I’m don’t really care to discuss why you think the GWA is good or bad, but in case you think it’s bad, here’s the same thing implemented as a ServletFilter (here’s the same thing for ColdFusion):

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
  throws IOException, ServletException {
  HttpServletRequest httpreq = (HttpServletRequest)req;
  HttpServletResponse httpres = (HttpServletResponse)res;
  if (httpreq.getHeader("X-moz") != null &&
    httpreq.getHeader("X-moz").equalsIgnoreCase("prefetch")) {
    httpres.sendError(HttpServletResponse.SC_FORBIDDEN);
  } else {
    chain.doFilter(req, res);
  }
}

Add the following to your web.xml:

<filter>
  <filter-name>gwa</filter-name>
  <filter-class>com.mycompany.web.filters.GWAFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>gwa</filter-name>
  <url-pattern>/</url-pattern>
</filter-mapping>

and you’re good.

One thought on “Showing Google’s Web Accelerator the door using Java / ServletFilter”

Leave a Reply

Your email address will not be published. Required fields are marked *