Example usage for java.net ProxySelector ProxySelector

List of usage examples for java.net ProxySelector ProxySelector

Introduction

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

Prototype

ProxySelector

Source Link

Usage

From source file:org.wso2.extension.siddhi.device.utils.ClientUtils.java

public static OkHttpClient getSSLClient() {

    boolean isIgnoreHostnameVerification = Boolean
            .parseBoolean(System.getProperty("org.wso2" + ".ignoreHostnameVerification"));
    OkHttpClient okHttpClient;/*from  ww w .j  av a 2  s.  com*/
    final String proxyHost = System.getProperty("http.proxyHost");
    final String proxyPort = System.getProperty("http.proxyPort");
    final String nonProxyHostsValue = System.getProperty("http.nonProxyHosts");

    final ProxySelector proxySelector = new ProxySelector() {
        @Override
        public List<Proxy> select(URI uri) {
            List<Proxy> proxyList = new ArrayList<>();
            String host = uri.getHost();

            if (!StringUtils.isEmpty(host)) {
                if (host.startsWith(DEFAULT_HOST_IP) || host.startsWith(DEFAULT_HOST)
                        || StringUtils.isEmpty(nonProxyHostsValue)
                        || StringUtils.contains(nonProxyHostsValue, host) || StringUtils.isEmpty(proxyHost)
                        || StringUtils.isEmpty(proxyPort)) {
                    proxyList.add(Proxy.NO_PROXY);
                } else {
                    proxyList.add(new Proxy(Proxy.Type.HTTP,
                            new InetSocketAddress(proxyHost, Integer.parseInt(proxyPort))));
                }
            } else {
                log.error("Host is null. Host could not be empty or null");
            }
            return proxyList;
        }

        @Override
        public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    };

    X509TrustManager trustAllCerts = new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return new java.security.cert.X509Certificate[0];
        }

        public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
        }

        public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
        }
    };
    if (isIgnoreHostnameVerification) {
        okHttpClient = new OkHttpClient.Builder()
                .sslSocketFactory(getSimpleTrustedSSLSocketFactory(), trustAllCerts)
                .hostnameVerifier(new HostnameVerifier() {
                    @Override
                    public boolean verify(String s, SSLSession sslSession) {
                        return true;
                    }
                }).proxySelector(proxySelector).build();
        return okHttpClient;
    } else {
        SSLSocketFactory trustedSSLSocketFactory = getTrustedSSLSocketFactory();
        okHttpClient = new OkHttpClient.Builder().sslSocketFactory(trustedSSLSocketFactory)
                .proxySelector(proxySelector).build();
        return okHttpClient;
    }
}