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;
  }
Exemple #2
0
  public XMLizable send(
      String soapAction,
      QName requestElement,
      XMLizable request,
      QName responseElement,
      Class responseType)
      throws ConnectionException {

    long startTime = System.currentTimeMillis();

    try {
      boolean firstTime = true;
      while (true) {
        try {
          Transport transport = newTransport(config);
          OutputStream out = transport.connect(url, soapAction);
          sendRequest(out, request, requestElement);
          InputStream in = transport.getContent();
          XMLizable result;
          result = receive(transport, responseElement, responseType, in);
          return result;
        } catch (SessionTimedOutException se) {
          if (config.getSessionRenewer() == null || !firstTime) {
            throw (ConnectionException) se.getCause();
          } else {
            SessionRenewer.SessionRenewalHeader sessionHeader =
                config.getSessionRenewer().renewSession(config);
            if (sessionHeader != null) {
              addHeader(sessionHeader.name, sessionHeader.headerElement);
            }
          }
        }
        firstTime = false;
      }
    } catch (SocketTimeoutException e) {
      long timeTaken = System.currentTimeMillis() - startTime;
      throw new ConnectionException(
          "Request to "
              + url
              + " timed out. TimeTaken="
              + timeTaken
              + " ConnectionTimeout="
              + config.getConnectionTimeout()
              + " ReadTimeout="
              + config.getReadTimeout(),
          e);

    } catch (IOException e) {
      throw new ConnectionException("Failed to send request to " + url, e);
    }
  }