/**
   * Saves the response content in the temp dir and adds it to the summary page.
   *
   * @param response the response to save
   * @param request the request used to get the response
   * @throws IOException if a problem occurs writing the file
   */
  protected void saveResponse(final WebResponse response, final WebRequest request)
      throws IOException {
    counter_++;
    final String extension = chooseExtension(response.getContentType());
    final File f = createFile(request.getUrl(), extension);
    final InputStream input = response.getContentAsStream();
    final OutputStream output = new FileOutputStream(f);
    int length = 0;
    try {
      length = IOUtils.copy(input, output);
    } catch (final EOFException e) {
      // ignore
    } finally {
      IOUtils.closeQuietly(input);
      IOUtils.closeQuietly(output);
    }

    final URL url = response.getWebRequest().getUrl();
    LOG.info("Created file " + f.getAbsolutePath() + " for response " + counter_ + ": " + url);

    final StringBuilder buffer = new StringBuilder();
    buffer.append("tab[tab.length] = {code: " + response.getStatusCode() + ", ");
    buffer.append("fileName: '" + f.getName() + "', ");
    buffer.append("contentType: '" + response.getContentType() + "', ");
    buffer.append("method: '" + request.getHttpMethod().name() + "', ");
    if (request.getHttpMethod() == HttpMethod.POST
        && request.getEncodingType() == FormEncodingType.URL_ENCODED) {
      buffer.append(
          "postParameters: " + nameValueListToJsMap(request.getRequestParameters()) + ", ");
    }
    buffer.append("url: '" + escapeJSString(url.toString()) + "', ");
    buffer.append("loadTime: " + response.getLoadTime() + ", ");
    buffer.append("responseSize: " + length + ", ");
    buffer.append("responseHeaders: " + nameValueListToJsMap(response.getResponseHeaders()));
    buffer.append("};\n");
    appendToJSFile(buffer.toString());
  }