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:com.geoxp.oss.client.OSSClient.java

/**
 * Get an HttpClient able to use a the Java configured proxyHost/Port if needed
 *
 * @returns HttpClient/*  ww w  .  ja  v a2 s  . co  m*/
 */
private static HttpClient newHttpClient() {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner(
            httpclient.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault());
    httpclient.setRoutePlanner(routePlanner);
    return httpclient;
}

From source file:ua.pp.msk.cliqr.GetProcessorImpl.java

private void init(URL targetURL, String user, String password) throws ClientSslException {
    this.targetUrl = targetURL;
    logger.debug("Initializing " + this.getClass().getName() + " with target URL " + targetURL.toString());
    HttpHost htHost = new HttpHost(targetUrl.getHost(), targetUrl.getPort(), targetUrl.getProtocol());

    AuthCache aCache = new BasicAuthCache();
    BasicScheme basicAuth = new BasicScheme();
    aCache.put(htHost, basicAuth);/*from ww w  .ja  v a2 s  .c o m*/

    UsernamePasswordCredentials creds = new UsernamePasswordCredentials(user, password);
    BasicCredentialsProvider cProvider = new BasicCredentialsProvider();
    cProvider.setCredentials(new AuthScope(htHost), creds);
    logger.debug("Credential provider: " + cProvider.toString());

    context = new BasicHttpContext();
    ClientContextConfigurer cliCon = new ClientContextConfigurer(context);
    cliCon.setCredentialsProvider(cProvider);
    context.setAttribute(ClientContext.AUTH_CACHE, aCache);
    SSLSocketFactory sslConnectionSocketFactory = null;
    try {
        sslConnectionSocketFactory = new SSLSocketFactory(new TrustSelfSignedStrategy(),
                new CliQrHostnameVerifier());
    } catch (KeyManagementException ex) {
        logger.error("Cannot manage secure keys", ex);
        throw new ClientSslException("Cannot manage secure keys", ex);
    } catch (KeyStoreException ex) {
        logger.error("Cannot build SSL context due to KeyStore error", ex);
        throw new ClientSslException("Cannot build SSL context due to KeyStore error", ex);
    } catch (NoSuchAlgorithmException ex) {
        logger.error("Unsupported security algorithm", ex);
        throw new ClientSslException("Unsupported security algorithm", ex);
    } catch (UnrecoverableKeyException ex) {
        logger.error("Unrecoverable key", ex);
        throw new ClientSslException("Unrecoverrable key", ex);
    }

    DefaultHttpClient defClient = new DefaultHttpClient();
    defClient.setRedirectStrategy(new CliQrRedirectStrategy());
    defClient.setCredentialsProvider(cProvider);
    Scheme https = new Scheme("https", 443, sslConnectionSocketFactory);
    defClient.getConnectionManager().getSchemeRegistry().register(https);
    defClient.setTargetAuthenticationStrategy(new TargetAuthenticationStrategy());
    client = defClient;
}

From source file:org.deegree.protocol.ows.http.OwsHttpClientImpl.java

@Override
public OwsHttpResponse doGet(URL endPoint, Map<String, String> params, Map<String, String> headers)
        throws IOException {

    OwsHttpResponseImpl response = null;
    URI query = null;//from  w w w.  java2s  .co m
    try {
        URL normalizedEndpointUrl = normalizeGetUrl(endPoint);
        StringBuilder sb = new StringBuilder(normalizedEndpointUrl.toString());
        boolean first = true;
        if (params != null) {
            for (Entry<String, String> param : params.entrySet()) {
                if (!first) {
                    sb.append('&');
                } else {
                    first = false;
                }
                sb.append(URLEncoder.encode(param.getKey(), "UTF-8"));
                sb.append('=');
                sb.append(URLEncoder.encode(param.getValue(), "UTF-8"));
            }
        }

        query = new URI(sb.toString());
        HttpGet httpGet = new HttpGet(query);
        DefaultHttpClient httpClient = getInitializedHttpClient(endPoint);
        LOG.debug("Performing GET request: " + query);
        HttpResponse httpResponse = httpClient.execute(httpGet);
        response = new OwsHttpResponseImpl(httpResponse, httpClient.getConnectionManager(), sb.toString());
    } catch (Throwable e) {
        e.printStackTrace();
        String msg = "Error performing GET request on '" + query + "': " + e.getMessage();
        throw new IOException(msg);
    }
    return response;
}

From source file:ua.pp.msk.gradle.http.Client.java

private void init(URL targetURL, String user, String password) throws ClientSslException {
    this.targetUrl = targetURL;
    logger.debug("Initializing " + this.getClass().getName() + " with target URL " + targetURL.toString());
    HttpHost htHost = new HttpHost(targetUrl.getHost(), targetUrl.getPort(), targetUrl.getProtocol());

    AuthCache aCache = new BasicAuthCache();
    BasicScheme basicAuth = new BasicScheme();
    aCache.put(htHost, basicAuth);/*from  w  w w.  ja  v  a 2s  .c  o  m*/

    UsernamePasswordCredentials creds = new UsernamePasswordCredentials(user, password);
    BasicCredentialsProvider cProvider = new BasicCredentialsProvider();
    cProvider.setCredentials(new AuthScope(htHost), creds);
    logger.debug("Credential provider: " + cProvider.toString());

    context = new BasicHttpContext();
    ClientContextConfigurer cliCon = new ClientContextConfigurer(context);
    cliCon.setCredentialsProvider(cProvider);
    context.setAttribute(ClientContext.AUTH_CACHE, aCache);
    SSLSocketFactory sslConnectionSocketFactory = null;
    try {
        sslConnectionSocketFactory = new SSLSocketFactory(new TrustSelfSignedStrategy(),
                new NexusHostnameVerifier());
    } catch (KeyManagementException ex) {
        logger.error("Cannot manage secure keys", ex);
        throw new ClientSslException("Cannot manage secure keys", ex);
    } catch (KeyStoreException ex) {
        logger.error("Cannot build SSL context due to KeyStore error", ex);
        throw new ClientSslException("Cannot build SSL context due to KeyStore error", ex);
    } catch (NoSuchAlgorithmException ex) {
        logger.error("Unsupported security algorithm", ex);
        throw new ClientSslException("Unsupported security algorithm", ex);
    } catch (UnrecoverableKeyException ex) {
        logger.error("Unrecoverable key", ex);
        throw new ClientSslException("Unrecoverrable key", ex);
    }

    DefaultHttpClient defClient = new DefaultHttpClient();
    defClient.setRedirectStrategy(new NexusRedirectStrategy());
    defClient.setCredentialsProvider(cProvider);
    Scheme https = new Scheme("https", 443, sslConnectionSocketFactory);
    defClient.getConnectionManager().getSchemeRegistry().register(https);
    defClient.setTargetAuthenticationStrategy(new TargetAuthenticationStrategy());
    client = defClient;
}

From source file:ua.pp.msk.cliqr.PostProcessorImpl.java

private void init(URL url, String user, String password) throws ClientSslException {
    this.targetUrl = url;
    HttpHost htHost = new HttpHost(targetUrl.getHost(), targetUrl.getPort(), targetUrl.getProtocol());

    BasicAuthCache aCache = new BasicAuthCache();
    BasicScheme basicAuth = new BasicScheme(ChallengeState.TARGET);
    aCache.put(htHost, basicAuth);//from  ww  w .j av a 2s  .c  om

    UsernamePasswordCredentials creds = new UsernamePasswordCredentials(user, password);
    BasicCredentialsProvider cProvider = new BasicCredentialsProvider();
    cProvider.setCredentials(new AuthScope(htHost), creds);
    logger.debug("Credential provider: " + cProvider.toString());

    context = new BasicHttpContext();
    ClientContextConfigurer cliCon = new ClientContextConfigurer(context);
    context.setAttribute(ClientContext.AUTH_CACHE, aCache);
    //context.setAuthCache(aCache);
    cliCon.setCredentialsProvider(cProvider);
    //context.setCredentialsProvider(cProvider);
    SSLSocketFactory sslSocketFactory = null;
    try {
        //SSLContext trustySslContext = SSLContextBuilder.create().loadTrustMaterial( new TrustSelfSignedStrategy()).build();
        //sslConnectionSocketFactory = new SSLConnectionSocketFactory(trustySslContext, new CliQrHostnameVerifier());
        sslSocketFactory = new SSLSocketFactory(new TrustSelfSignedStrategy(), new CliQrHostnameVerifier());
    } catch (KeyManagementException ex) {
        logger.error("Cannot manage secure keys", ex);
        throw new ClientSslException("Cannot manage secure keys", ex);
    } catch (KeyStoreException ex) {
        logger.error("Cannot build SSL context due to KeyStore error", ex);
        throw new ClientSslException("Cannot build SSL context due to KeyStore error", ex);
    } catch (NoSuchAlgorithmException ex) {
        logger.error("Unsupported security algorithm", ex);
        throw new ClientSslException("Unsupported security algorithm", ex);
    } catch (UnrecoverableKeyException ex) {
        logger.error("Unrecoverable key", ex);
        throw new ClientSslException("Unrecoverrable key", ex);
    }

    DefaultHttpClient defClient = new DefaultHttpClient();
    defClient.setRedirectStrategy(new CliQrRedirectStrategy());
    defClient.setCredentialsProvider(cProvider);
    Scheme https = new Scheme("https", 443, sslSocketFactory);
    defClient.getConnectionManager().getSchemeRegistry().register(https);
    defClient.setTargetAuthenticationStrategy(new TargetAuthenticationStrategy());
    client = defClient;
}

From source file:com.jivesoftware.jivesdk.impl.auth.jiveauth.JiveSignatureValidatorImpl.java

private HttpClient createSubjectVerifierHttpClient(@Nonnull String url) throws Exception {
    int targetPort = new URL(url).getPort();
    if (targetPort == -1) {
        if (url.startsWith(ServerConstants.HTTPS_PREFIX)) {
            targetPort = 443;/*ww  w .  ja v  a 2s  .c  o  m*/
        } else {
            targetPort = 80;
        }
    }

    DefaultHttpClient httpClient = new DefaultHttpClient();
    String subject = configuration
            .getProperty(JiveSDKConfig.ServiceConfiguration.SIGNATURE_URL_CERTIFICATE_SUBJECT_NAME);
    SchemeSocketFactory sslf = SubjectVerifierSocketFactory.createSocketFactory(subject);
    ClientConnectionManager connectionManager = httpClient.getConnectionManager();
    SchemeRegistry schemeRegistry = connectionManager.getSchemeRegistry();
    schemeRegistry.register(new Scheme(ServerConstants.HTTPS, targetPort, sslf));
    return httpClient;
}

From source file:org.deviceconnect.message.http.event.HttpEventManager.java

@Override
protected HttpResponse execute(final HttpUriRequest request) throws IOException {
    DefaultHttpClient client = new DefaultHttpClient();
    HttpResponse response = client.execute(request);
    HttpResponse retRes = copyResponse(response);
    client.getConnectionManager().shutdown();
    return retRes;
}

From source file:net.oddsoftware.android.feedscribe.data.Downloader.java

private HttpClient createHttpClient() {
    // use apache http client lib to set parameters from feedStatus
    DefaultHttpClient client = new DefaultHttpClient();

    // set up proxy handler
    ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner(
            client.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault());
    client.setRoutePlanner(routePlanner);

    return client;
}

From source file:de.mendelson.comm.as2.client.HTMLPanel.java

/**Sets a new page to the viewer and handles the Stack update
 *@param url URL to move to/*  w w w .  ja v  a 2 s .co m*/
 */
public Header[] setURL(String urlStr, String userAgent, File fallbackOnError) {
    Header[] header = new Header[0];
    try {
        //post for header data
        HttpPost filePost = new HttpPost(new URL(urlStr).toExternalForm());
        HttpParams params = filePost.getParams();
        HttpProtocolParams.setUserAgent(params, userAgent);
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpResponse httpResponse = httpClient.execute(filePost);
        header = httpResponse.getAllHeaders();
        int status = httpResponse.getStatusLine().getStatusCode();
        if (status != HttpServletResponse.SC_OK) {
            throw new Exception("HTTP " + status);
        }
        httpClient.getConnectionManager().shutdown();
        //now get for body data
        this.jEditorPane.setPage(new URL(urlStr));
    } catch (Exception e) {
        e.printStackTrace();
        try {
            this.setPage(fallbackOnError);
        } catch (Exception ex) {
            //nop
        }
    }
    return (header);
}

From source file:org.codegist.crest.io.http.HttpClientFactoryTest.java

@Test
public void createWithOneShouldCreateDefaultHttpClient() throws Exception {

    DefaultHttpClient expected = mock(DefaultHttpClient.class);
    ProxySelectorRoutePlanner planner = mock(ProxySelectorRoutePlanner.class);
    ClientConnectionManager clientConnectionManager = mock(ClientConnectionManager.class);
    SchemeRegistry schemeRegistry = mock(SchemeRegistry.class);
    ProxySelector proxySelector = mock(ProxySelector.class);

    when(expected.getConnectionManager()).thenReturn(clientConnectionManager);
    when(clientConnectionManager.getSchemeRegistry()).thenReturn(schemeRegistry);

    mockStatic(ProxySelector.class);
    when(ProxySelector.getDefault()).thenReturn(proxySelector);

    whenNew(DefaultHttpClient.class).withNoArguments().thenReturn(expected);
    whenNew(ProxySelectorRoutePlanner.class).withArguments(schemeRegistry, proxySelector).thenReturn(planner);

    HttpClient actual = HttpClientFactory.create(crestConfig, getClass());
    assertSame(expected, actual);/*  w w  w .  java 2  s .co m*/

    verify(expected).setRoutePlanner(planner);
}