VaadinServlet : setContentLength

HI,

I’ve just spent a “fun” four days tackling a very very weird issue : various different machines throughout the company would have trouble accessing a Vaadin 7 application - irrespective of Browser type, OS etc; the faiilure simply manifested iteself in the javascript for the widget failing to download (after a 20s timeout on Chrome & FF)

Through sheer bloody luck and graft, we found out that simply adding a content length to static resources served via the VaadinServlet fixed the issue.

I think that some interaction between a virus checker, proxy server & browser - in some scenarios, with the wind blowing in a particular direction :wink: - caused an intervening proxy server to demand a content-length header and wait until it got one. This only affected some client configurations.

a) Is there any good reason not to add the content length in the VaadinServlet?
b) If there isn’t any good reason, could this be added to the core VaadinServlet?

Cheers,

Charles.

public class CSAVaadinServlet extends VaadinServlet {
  @Override
  protected void writeStaticResourceResponse(HttpServletRequest request, HttpServletResponse response, URL resourceUrl) throws IOException {
    URLConnection connection = null;
    try {
      connection = resourceUrl.openConnection();
      int length = connection.getContentLength();
      if (length > 0) {
        response.setContentLength(length);
      }
    } catch (Throwable e) {
      if (connection != null) {
        try {
          // Explicitly close the input stream to prevent it from remaining hanging
          // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4257700
          InputStream is = connection.getInputStream();
          if (is != null) {
            is.close();
          }
        } catch (Throwable close) {
          getLogger().log(Level.INFO, "Error closing URLConnection input stream", e);
        }
      }
    }

    super.writeStaticResourceResponse(request, response, resourceUrl);
  }

  private static final Logger getLogger() {
    return Logger.getLogger(CSAVaadinServlet.class.getName());
  }
}

Based on some discussions, there should not be any reason not to add the Content-Length header.

I created
ticket #11699
about this.

Great : Thanks!