Example usage for java.net ProxySelector getDefault

List of usage examples for java.net ProxySelector getDefault

Introduction

In this page you can find the example usage for java.net ProxySelector getDefault.

Prototype

public static ProxySelector getDefault() 

Source Link

Document

Gets the system-wide proxy selector.

Usage

From source file:jetbrains.teamcilty.github.api.impl.HttpClientWrapperImpl.java

public HttpClientWrapperImpl()
        throws UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
    final String serverVersion = ServerVersionHolder.getVersion().getDisplayVersion();

    final HttpParams ps = new BasicHttpParams();

    DefaultHttpClient.setDefaultHttpParams(ps);
    final int timeout = TeamCityProperties.getInteger("teamcity.github.http.timeout", 300 * 1000);
    HttpConnectionParams.setConnectionTimeout(ps, timeout);
    HttpConnectionParams.setSoTimeout(ps, timeout);
    HttpProtocolParams.setUserAgent(ps, "JetBrains TeamCity " + serverVersion);

    final SchemeRegistry schemaRegistry = SchemeRegistryFactory.createDefault();
    final SSLSocketFactory sslSocketFactory = new SSLSocketFactory(new TrustStrategy() {
        public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            return !TeamCityProperties.getBoolean("teamcity.github.verify.ssl.certificate");
        }//from w w  w  . j  a  va  2  s  . c  o  m
    });
    schemaRegistry.register(new Scheme("https", 443, sslSocketFactory));

    final DefaultHttpClient httpclient = new DefaultHttpClient(new ThreadSafeClientConnManager(schemaRegistry),
            ps);

    setupProxy(httpclient);

    httpclient.setRoutePlanner(new ProxySelectorRoutePlanner(
            httpclient.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault()));
    httpclient.addRequestInterceptor(new RequestAcceptEncoding());
    httpclient.addResponseInterceptor(new ResponseContentEncoding());
    httpclient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(3, true));

    myClient = httpclient;
}

From source file:wirschauenplugin.OmdbConnection.java

/**
 * Creates a new OmdbConnection with the Proxy Settings from the System Properties.
 *//* w w w.  j  av a  2s.  com*/
public OmdbConnection() {
    super();
    mHttpClient = new DefaultHttpClient();
    //the proxy settings for the tvbrowser are saved in the system properties. see tvbrowser.TVBrowser.updateProxySettings().
    //the ProxySelectorRoutePlanner will take these into account.
    ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner(
            mHttpClient.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault());
    mHttpClient.setRoutePlanner(routePlanner);
}

From source file:com.floragunn.searchguard.HeaderAwareJestClientFactory.java

protected HttpRoutePlanner getRoutePlanner() {
    return new SystemDefaultRoutePlanner(ProxySelector.getDefault());
}

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

@Test
public void createWithMoreThanOneShouldCreateDefaultHttpClientWithConnectionManagerSetup() throws Exception {
    DefaultHttpClient expected = mock(DefaultHttpClient.class);
    ProxySelectorRoutePlanner planner = mock(ProxySelectorRoutePlanner.class);
    ClientConnectionManager clientConnectionManager = mock(ClientConnectionManager.class);
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    ProxySelector proxySelector = mock(ProxySelector.class);
    BasicHttpParams httpParams = mock(BasicHttpParams.class);
    ConnPerRouteBean routeBean = mock(ConnPerRouteBean.class);
    PlainSocketFactory plainSocketFactory = mock(PlainSocketFactory.class);
    SSLSocketFactory sslSocketFactory = mock(SSLSocketFactory.class);
    Scheme plainScheme = new Scheme("http", plainSocketFactory, 80);
    Scheme sslScheme = new Scheme("https", sslSocketFactory, 443);
    ThreadSafeClientConnManager threadSafeClientConnManager = mock(ThreadSafeClientConnManager.class);

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

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

    mockStatic(PlainSocketFactory.class);
    when(PlainSocketFactory.getSocketFactory()).thenReturn(plainSocketFactory);

    mockStatic(SSLSocketFactory.class);
    when(SSLSocketFactory.getSocketFactory()).thenReturn(sslSocketFactory);

    whenNew(SchemeRegistry.class).withNoArguments().thenReturn(schemeRegistry);
    whenNew(Scheme.class).withArguments("http", plainSocketFactory, 80).thenReturn(plainScheme);
    whenNew(Scheme.class).withArguments("https", sslSocketFactory, 443).thenReturn(sslScheme);
    whenNew(ThreadSafeClientConnManager.class).withArguments(httpParams, schemeRegistry)
            .thenReturn(threadSafeClientConnManager);
    whenNew(ConnPerRouteBean.class).withArguments(2).thenReturn(routeBean);
    whenNew(BasicHttpParams.class).withNoArguments().thenReturn(httpParams);
    whenNew(DefaultHttpClient.class).withArguments(threadSafeClientConnManager, httpParams)
            .thenReturn(expected);/*from   ww w . ja va2  s. co m*/
    whenNew(ProxySelectorRoutePlanner.class).withArguments(schemeRegistry, proxySelector).thenReturn(planner);

    when(crestConfig.getConcurrencyLevel()).thenReturn(2);
    HttpClient actual = HttpClientFactory.create(crestConfig, getClass());
    assertSame(expected, actual);

    verify(expected).setRoutePlanner(planner);
    verify(httpParams).setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    verify(httpParams).setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, routeBean);
    verify(httpParams).setIntParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 2);
    assertSame(plainScheme, schemeRegistry.getScheme("http"));
    assertSame(sslScheme, schemeRegistry.getScheme("https"));
}

From source file:uk.co.tfd.sm.proxy.ProxyClientServiceImpl.java

private HttpClient getHttpClient() {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    if (useJreProxy) {
        ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner(
                httpclient.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault());
        httpclient.setRoutePlanner(routePlanner);
    }//from  www .ja v  a2  s. co  m
    return httpclient;
}

From source file:jetbrains.buildServer.commitPublisher.github.api.impl.HttpClientWrapperImpl.java

public HttpClientWrapperImpl()
        throws UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
    final String serverVersion = ServerVersionHolder.getVersion().getDisplayVersion();

    final HttpParams ps = new BasicHttpParams();

    DefaultHttpClient.setDefaultHttpParams(ps);
    final int timeout = TeamCityProperties.getInteger("teamcity.github.http.timeout", 300 * 1000);
    HttpConnectionParams.setConnectionTimeout(ps, timeout);
    HttpConnectionParams.setSoTimeout(ps, timeout);
    HttpProtocolParams.setUserAgent(ps, "JetBrains TeamCity " + serverVersion);

    final SchemeRegistry schemaRegistry = SchemeRegistryFactory.createDefault();
    final SSLSocketFactory sslSocketFactory = new SSLSocketFactory(new TrustStrategy() {
        public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            return !TeamCityProperties.getBoolean("teamcity.github.verify.ssl.certificate");
        }// w ww . j  a va2s.co  m
    }) {
        @Override
        public Socket connectSocket(int connectTimeout, Socket socket, HttpHost host,
                InetSocketAddress remoteAddress, InetSocketAddress localAddress, HttpContext context)
                throws IOException {
            if (socket instanceof SSLSocket) {
                try {
                    PropertyUtils.setProperty(socket, "host", host.getHostName());
                } catch (Exception ex) {
                    LOG.warn(String.format(
                            "A host name is not passed to SSL connection for the purpose of supporting SNI due to the following exception: %s",
                            ex.toString()));
                }
            }
            return super.connectSocket(connectTimeout, socket, host, remoteAddress, localAddress, context);
        }
    };
    schemaRegistry.register(new Scheme("https", 443, sslSocketFactory));

    final DefaultHttpClient httpclient = new DefaultHttpClient(new ThreadSafeClientConnManager(schemaRegistry),
            ps);

    setupProxy(httpclient);

    httpclient.setRoutePlanner(new ProxySelectorRoutePlanner(
            httpclient.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault()));
    httpclient.addRequestInterceptor(new RequestAcceptEncoding());
    httpclient.addResponseInterceptor(new ResponseContentEncoding());
    httpclient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(3, true));

    myClient = httpclient;
}