/**
   * Tries to uncompress the JavaScript code in the provided response.
   *
   * @param response the response to uncompress
   * @return a new response with uncompressed JavaScript code or the original response in case of
   *     failure
   */
  protected WebResponse uncompressJavaScript(final WebResponse response) {
    final WebRequest request = response.getWebRequest();
    final String scriptName = request.getUrl().toString();
    final String scriptSource = response.getContentAsString();

    // skip if it is already formatted? => TODO

    final ContextFactory factory = new ContextFactory();
    final ContextAction action =
        new ContextAction() {
          public Object run(final Context cx) {
            cx.setOptimizationLevel(-1);
            final Script script = cx.compileString(scriptSource, scriptName, 0, null);
            return cx.decompileScript(script, 4);
          }
        };

    try {
      final String decompileScript = (String) factory.call(action);
      final List<NameValuePair> responseHeaders =
          new ArrayList<NameValuePair>(response.getResponseHeaders());
      for (int i = responseHeaders.size() - 1; i >= 0; i--) {
        if ("content-encoding".equalsIgnoreCase(responseHeaders.get(i).getName())) {
          responseHeaders.remove(i);
        }
      }
      final WebResponseData wrd =
          new WebResponseData(
              decompileScript.getBytes(),
              response.getStatusCode(),
              response.getStatusMessage(),
              responseHeaders);
      return new WebResponse(
          wrd,
          response.getWebRequest().getUrl(),
          response.getWebRequest().getHttpMethod(),
          response.getLoadTime());
    } catch (final Exception e) {
      LOG.warn("Failed to decompress JavaScript response. Delivering as it.", e);
    }

    return response;
  }
  /**
   * 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());
  }