org.apache.http.impl.conn.BasicClientConnectionManager Java Examples

The following examples show how to use org.apache.http.impl.conn.BasicClientConnectionManager. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SocketFactoryHttpClientFactory.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Override
public DefaultHttpClient create(final HttpMethod method, final URI uri) {
  final TrustStrategy acceptTrustStrategy = new TrustStrategy() {
    @Override
    public boolean isTrusted(final X509Certificate[] certificate, final String authType) {
      return true;
    }
  };

  final SchemeRegistry registry = new SchemeRegistry();
  try {
    final SSLSocketFactory ssf =
            new SSLSocketFactory(acceptTrustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    registry.register(new Scheme(uri.getScheme(), uri.getPort(), ssf));
  } catch (Exception e) {
    throw new ODataRuntimeException(e);
  }

  final DefaultHttpClient httpClient = new DefaultHttpClient(new BasicClientConnectionManager(registry));
  httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, USER_AGENT);

  return httpClient;
}
 
Example #2
Source File: DatabricksRestClientImpl425.java    From databricks-rest-client with Apache License 2.0 5 votes vote down vote up
protected void initClient(DatabricksServiceFactory.Builder builder) {
  try {

    SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
    sslContext.init(null, null, new SecureRandom());

    SSLSocketFactory sf = new SSLSocketFactory(sslContext);
    Scheme httpsScheme = new Scheme("https", HTTPS_PORT, sf);
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(httpsScheme);
    ClientConnectionManager cm = new BasicClientConnectionManager(schemeRegistry);

    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, builder.getConnectionTimeout());
    HttpConnectionParams.setSoTimeout(params, builder.getSoTimeout());

    DefaultHttpClient defaultHttpClient = new DefaultHttpClient(cm, params);
    defaultHttpClient.setHttpRequestRetryHandler(retryHandler);

    // set authorization header if token base
    if (isNotEmpty(builder.getToken())) {
      isTokenAuth = true;
      authToken = builder.getToken();

    } else if (isNotEmpty(builder.getUsername()) && isNotEmpty(builder.getPassword())) {
      defaultHttpClient.getCredentialsProvider().setCredentials(
          new AuthScope(host, HTTPS_PORT),
          new UsernamePasswordCredentials(builder.getUsername(), builder.getPassword()));
    }

    client = new AutoRetryHttpClient(defaultHttpClient, retryStrategy);

  } catch (Exception e) {
    logger.error("", e);
  }

  url = String.format("https://%s/api/%s", host, apiVersion);
  mapper = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
}
 
Example #3
Source File: UnsafeClientConstructor.java    From reef with Apache License 2.0 5 votes vote down vote up
@Override
public CloseableHttpClient newInstance() {
  try {
    final SSLSocketFactory socketFactory = new SSLSocketFactory(this.getSSLContext());
    socketFactory.setHostnameVerifier(new UnsafeHostNameVerifier());
    final SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("https", 443, socketFactory));
    final ClientConnectionManager clientConnectionManager = new BasicClientConnectionManager(schemeRegistry);
    return new DefaultHttpClient(clientConnectionManager);
  } catch (final KeyManagementException | NoSuchAlgorithmException ex) {
    throw new RuntimeException("Unable to instantiate HTTP Client", ex);
  }
}