/**
   * Create a http client
   *
   * @return
   * @throws Exception
   */
  private static BayeuxClient makeClient() throws Exception {
    HttpClient httpClient = new HttpClient();
    httpClient.setConnectTimeout(connectionTimeout);
    httpClient.setTimeout(readTimeout); // TODO optional parameters
    httpClient.start();

    SoapLoginUtil.login(httpClient, userName, password);
    final String sessionId = SoapLoginUtil.getSessionId();
    String endpoint = SoapLoginUtil.getEndpoint();
    System.out.println("Login successful!\nEndpoint: " + endpoint + "\nSessionid=" + sessionId);

    Map<String, Object> options = new HashMap<String, Object>();
    options.put(ClientTransport.TIMEOUT_OPTION, readTimeout);
    LongPollingTransport transport =
        new LongPollingTransport(options, httpClient) {

          @Override
          protected void customize(ContentExchange exchange) {
            super.customize(exchange);
            exchange.addRequestHeader("Authorization", "OAuth " + sessionId);
          }
        };

    BayeuxClient client = new BayeuxClient(salesforceStreamingEndpoint(endpoint), transport);
    if (useCookies) establishCookies(client, userName, sessionId);
    return client;
  }
Example #2
0
  private static HttpClient httpClient() throws Exception {
    if (_httpClient == null) {
      _httpClient = new HttpClient();
      _httpClient.setConnectorType(CONNECTOR_SELECT_CHANNEL);
      _httpClient.setConnectTimeout(TIMEOUT_CONNECT);
      _httpClient.start();
    }

    return _httpClient;
  }
  // TODO: client.start() could potentially have some side effects.
  // Need to understand more of what's going on under the hood.
  @Provides
  HttpClient provideHttpClient() {
    LOGGER.info("Providing http client for connection name " + connectionName);

    HttpClient client = new HttpClient();
    client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
    client.setConnectTimeout(timeoutOption);
    client.setTimeout(timeoutOption);

    return client;
  }
Example #4
0
  /**
   * Create the new Jetty client connector.
   *
   * @param jaxrsClient JAX-RS client instance, for which the connector is created.
   * @param config client configuration.
   */
  JettyConnector(final Client jaxrsClient, final Configuration config) {

    final SSLContext sslContext = getSslContext(jaxrsClient, config);
    final SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.setSslContext(sslContext);
    this.client = new HttpClient(sslContextFactory);

    final Object connectTimeout = config.getProperties().get(ClientProperties.CONNECT_TIMEOUT);
    if (connectTimeout != null
        && connectTimeout instanceof Integer
        && (Integer) connectTimeout > 0) {
      client.setConnectTimeout((Integer) connectTimeout);
    }
    final Object threadPoolSize =
        config.getProperties().get(ClientProperties.ASYNC_THREADPOOL_SIZE);
    if (threadPoolSize != null
        && threadPoolSize instanceof Integer
        && (Integer) threadPoolSize > 0) {
      final String name = HttpClient.class.getSimpleName() + "@" + hashCode();
      final QueuedThreadPool threadPool = new QueuedThreadPool((Integer) threadPoolSize);
      threadPool.setName(name);
      client.setExecutor(threadPool);
    }
    Boolean disableCookies =
        (Boolean) config.getProperties().get(JettyClientProperties.DISABLE_COOKIES);
    disableCookies = (disableCookies != null) ? disableCookies : false;

    final AuthenticationStore auth = client.getAuthenticationStore();
    final Object basicAuthProvider =
        config.getProperty(JettyClientProperties.PREEMPTIVE_BASIC_AUTHENTICATION);
    if (basicAuthProvider != null && (basicAuthProvider instanceof BasicAuthentication)) {
      auth.addAuthentication((BasicAuthentication) basicAuthProvider);
    }

    final Object proxyUri = config.getProperties().get(ClientProperties.PROXY_URI);
    if (proxyUri != null) {
      final URI u = getProxyUri(proxyUri);
      final ProxyConfiguration proxyConfig = client.getProxyConfiguration();
      proxyConfig.getProxies().add(new HttpProxy(u.getHost(), u.getPort()));
    }

    if (disableCookies) {
      client.setCookieStore(new HttpCookieStore.Empty());
    }

    try {
      client.start();
    } catch (final Exception e) {
      throw new ProcessingException("Failed to start the client.", e);
    }
    this.cookieStore = client.getCookieStore();
  }
Example #5
0
 public static void setHTTPClientConnectTimeout(int timeout) {
   _httpClient.setConnectTimeout(timeout);
 }
Example #6
0
  @Override
  public void init() throws Exception {
    super.init();

    if (client == null) {
      client = new HttpClient(new SslContextFactory());
      client.setExecutor(new ExecutorThreadPool(threadPool));
      // configure timeout if set
      if (connectTimeout != -1) {
        client.setConnectTimeout(connectTimeout);
      }
      if (idleTimeout != -1) {
        client.setIdleTimeout(idleTimeout);
      }
      client.setMaxConnectionsPerDestination(maxConnectionsPerDestination);
      client.setMaxRequestsQueuedPerDestination(maxRequestsQueuedPerDestination);

      // Configure SSL - if relevant
      if (transportSSLEnabled) {
        KeyStoreManagement keyStore =
            KeyStoreManagement.getKeyStoreManagement(
                httpsKeystoreType, httpsKeystore, httpsKeyPassword);
        if (jmxControl != null && keyStore != null) {
          jmxControl.registerMBean(
              "CoUGAR:name=AsyncHttpClientKeyStore,beanName=" + beanName, keyStore);
        }
        KeyStoreManagement trustStore =
            KeyStoreManagement.getKeyStoreManagement(
                httpsTruststoreType, httpsTruststore, httpsTrustPassword);
        if (jmxControl != null) {
          jmxControl.registerMBean(
              "CoUGAR:name=AsyncHttpClientTrustStore,beanName=" + beanName, trustStore);
        }
        if (trustStore == null) {
          throw new IllegalStateException(
              "This configuration ostensibly supports TLS, yet doesn't provide valid truststore configuration");
        }

        final SslContextFactory sslContextFactory = client.getSslContextFactory();

        com.betfair.cougar.netutil.SslContextFactory factory =
            new com.betfair.cougar.netutil.SslContextFactory();
        factory.setTrustManagerFactoryKeyStore(trustStore.getKeyStore());
        if (keyStore != null) {
          factory.setKeyManagerFactoryKeyStore(keyStore.getKeyStore());
          factory.setKeyManagerFactoryKeyStorePassword(httpsKeyPassword);
        }
        SSLContext context = factory.newInstance();

        if (hostnameVerificationDisabled) {
          context.getDefaultSSLParameters().setEndpointIdentificationAlgorithm(null);
          LOGGER.warn(
              "CRITICAL SECURITY CHECKS ARE DISABLED: server SSL certificate hostname "
                  + "verification is turned off.");
        } else {
          context.getDefaultSSLParameters().setEndpointIdentificationAlgorithm("https");
        }

        sslContextFactory.setSslContext(context);
      }
      client.start();
      clientCreated = true;
    }

    metrics = new JettyTransportMetrics();

    if (jmxControl != null) {
      jmxControl.registerMBean("CoUGAR:name=AsyncHttpClientExecutable,beanName=" + beanName, this);
    }
  }