Esempio n. 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;
  }
Esempio n. 2
0
  @Override
  public void post() throws IOException, ParameterLoadException {
    DefaultHttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(endpoint);
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(Arrays.asList(pairs));

    int proxyPort = config.getInt(Config.PROXY_PORT);
    String proxyHostName = config.getString(Config.PROXY_HOST);
    String proxyUser = config.getString(Config.PROXY_USERNAME);
    String proxyPassword = config.getString(Config.PROXY_PASSWORD);
    String ntlmDomain = config.getString(Config.PROXY_NTLM_DOMAIN);
    proxyHostName = proxyHostName != null ? proxyHostName.trim() : "";
    proxyUser = proxyUser != null ? proxyUser.trim() : "";
    proxyPassword = proxyPassword != null ? proxyPassword.trim() : "";
    ntlmDomain = ntlmDomain != null ? ntlmDomain.trim() : "";

    post.addHeader("User-Agent", VersionInfo.info());
    post.setEntity(entity);

    // proxy
    if (proxyHostName.length() > 0) {
      InetSocketAddress proxyAddress = new InetSocketAddress(proxyHostName, proxyPort);
      HttpHost proxyHost = new HttpHost(proxyAddress.getHostName(), proxyAddress.getPort(), "http");
      AuthScope scope =
          new AuthScope(proxyAddress.getHostName(), proxyAddress.getPort(), null, null);
      Credentials credentials = new UsernamePasswordCredentials(proxyUser, proxyPassword);

      if (ntlmDomain.length() > 0) {
        credentials =
            new NTCredentials(
                proxyUser,
                proxyPassword,
                InetAddress.getLocalHost().getCanonicalHostName(),
                ntlmDomain);
      }

      client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxyHost);
      client.getCredentialsProvider().setCredentials(scope, credentials);
    }

    try {
      if (ntlmDomain.length() > 0) {
        // need to send a HEAD request to trigger NTLM authentication
        HttpHead head = new HttpHead("http://salesforce.com");
        client.execute(head);
        head.releaseConnection();
      }
      HttpResponse response = client.execute(post);

      successful = response.getStatusLine().getStatusCode() < 400;
      statusCode = response.getStatusLine().getStatusCode();
      reasonPhrase = response.getStatusLine().getReasonPhrase();

      // copy input stream data into a new input stream because releasing the connection will close
      // the input stream
      ByteArrayOutputStream bOut = new ByteArrayOutputStream();
      IOUtils.copy(response.getEntity().getContent(), bOut);
      input = new ByteArrayInputStream(bOut.toByteArray());

      if (response.containsHeader("Content-Encoding")
          && response.getHeaders("Content-Encoding")[0].getValue().equals("gzip")) {
        input = new GZIPInputStream(input);
      }

    } finally {
      post.releaseConnection();
    }
  }