Example usage for org.apache.http.impl.client DefaultHttpClient getConnectionManager

List of usage examples for org.apache.http.impl.client DefaultHttpClient getConnectionManager

Introduction

In this page you can find the example usage for org.apache.http.impl.client DefaultHttpClient getConnectionManager.

Prototype

public synchronized final ClientConnectionManager getConnectionManager() 

Source Link

Usage

From source file:org.anhonesteffort.flock.registration.HttpClientFactory.java

public DefaultHttpClient buildClient() throws RegistrationApiException {
    try {/*from  ww w. j a v a2 s .  co  m*/

        AssetManager assetManager = context.getAssets();
        InputStream keyStoreInputStream = assetManager.open("flock.store");
        KeyStore trustStore = KeyStore.getInstance("BKS");

        trustStore.load(keyStoreInputStream, "owsflock".toCharArray());

        SSLSocketFactory appSSLSocketFactory = new SSLSocketFactory(trustStore);
        DefaultHttpClient client = new DefaultHttpClient();
        SchemeRegistry schemeRegistry = client.getConnectionManager().getSchemeRegistry();
        Scheme httpsScheme = new Scheme("https", appSSLSocketFactory, 443);

        schemeRegistry.register(httpsScheme);

        return client;

    } catch (Exception e) {
        Log.e(getClass().getName(), "caught exception while constructing HttpClient client", e);
        throw new RegistrationApiException(
                "caught exception while constructing HttpClient client: " + e.toString());
    }
}

From source file:org.n52.oxf.util.web.ProxyAwareHttpClient.java

private ProxySelectorRoutePlanner createProxyPlanner() {
    DefaultHttpClient decoratedHttpClient = getHttpClientToDecorate();
    ClientConnectionManager connectionManager = decoratedHttpClient.getConnectionManager();
    SchemeRegistry schemeRegistry = connectionManager.getSchemeRegistry();
    ProxySelector defaultProxySelector = ProxySelector.getDefault();
    return new ProxySelectorRoutePlanner(schemeRegistry, defaultProxySelector);
}

From source file:org.broadinstitute.gatk.utils.help.ForumAPIUtils.java

private static String httpPost(String data, String URL) {
    try {/*  w ww  . j  a  v a  2s.c om*/

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost(URL);

        StringEntity input = new StringEntity(data);
        input.setContentType("application/json");
        postRequest.setEntity(input);

        HttpResponse response = httpClient.execute(postRequest);

        if (response.getStatusLine().getStatusCode() != 200) {
            throw new RuntimeException(
                    "Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));

        String output = "";
        String line;
        System.out.println("Output from Server .... \n");
        while ((line = br.readLine()) != null) {
            output += (line + '\n');
            System.out.println(line);
        }

        br.close();
        httpClient.getConnectionManager().shutdown();
        return output;

    } catch (MalformedURLException e) {

        e.printStackTrace();

    } catch (IOException e) {

        e.printStackTrace();

    }
    return null;
}

From source file:com.puppetlabs.geppetto.injectable.eclipse.impl.EclipseHttpClientProvider.java

@Override
public HttpClient get() {
    HttpParams params = new BasicHttpParams();
    if (connectonTimeout != null)
        HttpConnectionParams.setConnectionTimeout(params, connectonTimeout.intValue());
    if (soTimeout != null)
        HttpConnectionParams.setSoTimeout(params, soTimeout.intValue());

    DefaultHttpClient httpClient = new DefaultHttpClient(params);

    final SchemeRegistry schemeRegistry = httpClient.getConnectionManager().getSchemeRegistry();
    if (sslSocketFactory != null)
        schemeRegistry.register(new Scheme("https", 443, sslSocketFactory));

    httpClient.setRoutePlanner(new ProxiedRoutePlanner(schemeRegistry));
    for (IProxyData proxyData : Activator.getInstance().getProxyService().getProxyData()) {
        String user = proxyData.getUserId();
        String pwd = proxyData.getPassword();
        if (user != null || pwd != null)
            httpClient.getCredentialsProvider().setCredentials(
                    new AuthScope(proxyData.getHost(), proxyData.getPort()),
                    new UsernamePasswordCredentials(user, pwd));
    }//w  w  w.jav  a 2  s .c o  m
    return httpClient;
}

From source file:org.gw2InfoViewer.factories.HttpsConnectionFactory.java

public static HttpClient getHttpsClientWithProxy(Certificate[] sslCertificate, String proxyAddress,
        int proxyPort) {
    DefaultHttpClient httpClient;
    HttpHost proxy;//from  w ww  . j av  a 2 s  .c o m

    httpClient = new DefaultHttpClient();
    try {
        TrustManagerFactory tf = TrustManagerFactory.getInstance("X509");
        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
        ks.load(null);
        for (int i = 0; i < sslCertificate.length; i++) {
            ks.setCertificateEntry("StartCom" + i, sslCertificate[i]);
        }

        tf.init(ks);
        TrustManager[] tm = tf.getTrustManagers();

        SSLContext sslCon = SSLContext.getInstance("SSL");
        sslCon.init(null, tm, new SecureRandom());
        SSLSocketFactory socketFactory = new SSLSocketFactory(ks);
        Scheme sch = new Scheme("https", 443, socketFactory);

        proxy = new HttpHost(proxyAddress, proxyPort, "https");
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

        httpClient.getConnectionManager().getSchemeRegistry().register(sch);
    } catch (CertificateException | NoSuchAlgorithmException | KeyStoreException | IOException
            | KeyManagementException | UnrecoverableKeyException ex) {
        Logger.getLogger(HttpsConnectionFactory.class.getName()).log(Level.SEVERE, null, ex);
    }

    return httpClient;
}

From source file:com.google.api.client.http.apache.ApacheHttpTransportTest.java

private void checkDefaultHttpClient(DefaultHttpClient client) {
    HttpParams params = client.getParams();
    assertTrue(client.getConnectionManager() instanceof ThreadSafeClientConnManager);
    assertEquals(8192, params.getIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, -1));
    DefaultHttpRequestRetryHandler retryHandler = (DefaultHttpRequestRetryHandler) client
            .getHttpRequestRetryHandler();
    assertEquals(0, retryHandler.getRetryCount());
    assertFalse(retryHandler.isRequestSentRetryEnabled());
}

From source file:com.amalto.workbench.utils.ResourcesUtil.java

private static void postContent(String uri, HttpPost httppost) throws Exception, IOException {
    DefaultHttpClient httpclient = new DefaultHttpClient();

    httpclient.getCredentialsProvider().setCredentials(
            new AuthScope(getEndpointHost(uri), Integer.valueOf(getEndpointPort(uri))),
            new UsernamePasswordCredentials("admin", "talend"));//$NON-NLS-1$//$NON-NLS-2$

    log.info(Messages.ResourcesUtil_Loginfo + httppost.getRequestLine());
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();

    try {//from   w ww  . j a  v  a  2 s .  c  o  m
        Header[] headers = response.getAllHeaders();
        String responseString = null;
        if (response.getEntity() != null) {
            responseString = EntityUtils.toString(response.getEntity());
        }
    } finally {
        if (entity != null) {
            entity.consumeContent(); // release connection gracefully
        }
    }
    if (entity != null) {
        entity.consumeContent();
    }

    httpclient.getConnectionManager().shutdown();
}

From source file:org.wso2.carbon.appmgt.gateway.handlers.security.thrift.ThriftAuthClient.java

public ThriftAuthClient(String serverIP, String remoteServerPort, String webContextRoot)
        throws AuthenticationException {
    try {/*from  www .  j  a va 2 s . c  om*/
        TrustManager easyTrustManager = new X509TrustManager() {
            public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificates, String s)
                    throws java.security.cert.CertificateException {
            }

            public void checkServerTrusted(java.security.cert.X509Certificate[] x509Certificates, String s)
                    throws java.security.cert.CertificateException {
            }

            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };

        //skip host name verification
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, new TrustManager[] { easyTrustManager }, null);
        SSLSocketFactory sf = new SSLSocketFactory(sslContext);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        Scheme httpsScheme = new Scheme("https", sf, Integer.parseInt(remoteServerPort));

        DefaultHttpClient httpClient = new DefaultHttpClient();
        httpClient.getConnectionManager().getSchemeRegistry().register(httpsScheme);

        //If the webContextRoot is null or /
        if (webContextRoot == null || "/".equals(webContextRoot)) {
            //Assign it an empty value since it is part of the thriftServiceURL.
            webContextRoot = "";
        }
        String thriftServiceURL = "https://" + serverIP + ":" + remoteServerPort + webContextRoot + "/"
                + "thriftAuthenticator";
        client = new THttpClient(thriftServiceURL, httpClient);

    } catch (TTransportException e) {
        throw new AuthenticationException("Error in creating thrift authentication client..");
    } catch (Exception e) {
        throw new AuthenticationException("Error in creating thrift authentication client..");
    }
}

From source file:com.puppetlabs.puppetdb.javaclient.impl.DefaultModule.java

/**
 * Provides a HttpClient that is configured with the preferences of this module and the
 * injected <code>sslSocketFactory</code>.
 * //from  www  .j av  a 2 s.  c o  m
 * @param sslSocketFactory
 *            The injected SSL socket factory
 * @return The new HttpClient instance
 */
@Provides
public HttpClient provideHttpClient(SSLSocketFactory sslSocketFactory) {
    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, preferences.getConnectTimeout());
    HttpConnectionParams.setSoTimeout(params, preferences.getSoTimeout());

    DefaultHttpClient httpClient = new DefaultHttpClient(params);
    if (preferences.getCertPEM() != null)
        httpClient.getConnectionManager().getSchemeRegistry()
                .register(new Scheme("https", 443, sslSocketFactory));
    return httpClient;
}