@Override
  public void filterAbstractHttpRequest(SubmitContext context, AbstractHttpRequest<?> httpRequest) {
    Settings settings = httpRequest.getSettings();
    String compressionAlg = settings.getString(HttpSettings.REQUEST_COMPRESSION, "None");
    if (!"None".equals(compressionAlg)) {
      try {
        ExtendedHttpMethod method =
            (ExtendedHttpMethod) context.getProperty(BaseHttpRequestTransport.HTTP_METHOD);
        if (method instanceof HttpEntityEnclosingRequest) {
          HttpEntity requestEntity = ((HttpEntityEnclosingRequest) method).getEntity();
          if (requestEntity != null) {
            ByteArrayOutputStream tempOut = new ByteArrayOutputStream();
            requestEntity.writeTo(tempOut);

            byte[] compressedData =
                CompressionSupport.compress(compressionAlg, tempOut.toByteArray());
            ((HttpEntityEnclosingRequest) method).setEntity(new ByteArrayEntity(compressedData));
          }
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
Пример #2
0
  public synchronized InputStream load(String url) throws Exception {
    if (!PathUtils.isHttpPath(url)) {
      try {
        File file = new File(url.replace('/', File.separatorChar));
        if (file.exists()) url = file.toURI().toURL().toString();
      } catch (Exception e) {
      }
    }

    if (urlCache.containsKey(url)) {
      setNewBaseURI(url);
      return new ByteArrayInputStream(urlCache.get(url));
    }

    if (url.startsWith("file:")) {
      return handleFile(url);
    }

    log.debug("Getting wsdl component from [" + url + "]");

    createGetMethod(url);

    if (aborted) return null;

    LoaderWorker worker = new LoaderWorker();
    if (useWorker) worker.start();
    else worker.construct();

    while (!aborted && !finished) {
      Thread.sleep(200);
    }

    // wait for method to catch up - required in unit tests..
    // limited looping to 10 loops because of eclipse plugin which entered
    // endless loop without it
    int counter = 0;
    byte[] content = null;

    if (httpResponse != null && httpResponse.getEntity() != null) {
      content = EntityUtils.toByteArray(new BufferedHttpEntity(httpResponse.getEntity()));
    }

    while (!aborted && content == null && counter < 10) {
      Thread.sleep(200);
      counter++;
    }

    if (aborted) {
      throw new Exception("Load of url [" + url + "] was aborted");
    } else {
      if (content != null) {
        String compressionAlg = HttpClientSupport.getResponseCompressionType(httpResponse);
        if (compressionAlg != null)
          content = CompressionSupport.decompress(compressionAlg, content);

        urlCache.put(url, content);
        String newUrl = getMethod.getURI().toString();
        if (!url.equals(newUrl)) log.info("BaseURI was redirected to [" + newUrl + "]");
        setNewBaseURI(newUrl);
        urlCache.put(newUrl, content);
        return new ByteArrayInputStream(content);
      } else {
        throw new Exception(
            "Failed to load url; "
                + url
                + ", "
                + (httpResponse != null ? httpResponse.getStatusLine().getStatusCode() : 0)
                + " - "
                + (httpResponse != null ? httpResponse.getStatusLine().getReasonPhrase() : ""));
      }
    }
  }