예제 #1
0
  private static HttpURLConnection createConnection(
      ConnectorConfig config,
      URL url,
      HashMap<String, String> httpHeaders,
      boolean enableCompression)
      throws IOException {

    if (config.isTraceMessage()) {
      config
          .getTraceStream()
          .println(
              "WSC: Creating a new connection to "
                  + url
                  + " Proxy = "
                  + config.getProxy()
                  + " username "
                  + config.getProxyUsername());
    }

    HttpURLConnection connection = (HttpURLConnection) url.openConnection(config.getProxy());
    connection.addRequestProperty("User-Agent", VersionInfo.info());

    /*
     * Add all the client specific headers here
     */
    if (config.getHeaders() != null) {
      for (Entry<String, String> ent : config.getHeaders().entrySet()) {
        connection.setRequestProperty(ent.getKey(), ent.getValue());
      }
    }

    if (enableCompression && config.isCompression()) {
      connection.addRequestProperty("Content-Encoding", "gzip");
      connection.addRequestProperty("Accept-Encoding", "gzip");
    }

    if (config.getProxyUsername() != null) {
      String token = config.getProxyUsername() + ":" + config.getProxyPassword();
      String auth = "Basic " + new String(Base64.encode(token.getBytes()));
      connection.addRequestProperty("Proxy-Authorization", auth);
      connection.addRequestProperty("Https-Proxy-Authorization", auth);
    }

    if (httpHeaders != null) {
      for (Map.Entry<String, String> entry : httpHeaders.entrySet()) {
        connection.addRequestProperty(entry.getKey(), entry.getValue());
      }
    }

    if (config.getReadTimeout() != 0) {
      connection.setReadTimeout(config.getReadTimeout());
    }

    if (config.getConnectionTimeout() != 0) {
      connection.setConnectTimeout(config.getConnectionTimeout());
    }

    return connection;
  }
예제 #2
0
  private OutputStream wrapOutput(OutputStream output, boolean enableCompression)
      throws IOException {
    if (config.getMaxRequestSize() > 0) {
      output = new LimitingOutputStream(config.getMaxRequestSize(), output);
    }

    // when we are writing a zip file we don't bother with compression
    if (enableCompression && config.isCompression()) {
      output = new GZIPOutputStream(output);
    }

    if (config.isTraceMessage()) {
      output = new TeeOutputStream(output);
    }

    if (config.hasMessageHandlers()) {
      output = new MessageHandlerOutputStream(output);
    }

    return output;
  }