Example usage for javax.net.ssl HttpsURLConnection setDefaultHostnameVerifier

List of usage examples for javax.net.ssl HttpsURLConnection setDefaultHostnameVerifier

Introduction

In this page you can find the example usage for javax.net.ssl HttpsURLConnection setDefaultHostnameVerifier.

Prototype

public static void setDefaultHostnameVerifier(HostnameVerifier v) 

Source Link

Document

Sets the default HostnameVerifier inherited by a new instance of this class.

Usage

From source file:org.jumpmind.symmetric.transport.TransportManagerFactory.java

public static void initHttps(final String httpSslVerifiedServerNames, boolean allowSelfSignedCerts) {
    try {//from ww w.  j  a v a2 s.c o  m
        if (!StringUtils.isBlank(httpSslVerifiedServerNames)) {
            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
                public boolean verify(String s, SSLSession sslsession) {
                    boolean verified = false;
                    if (!StringUtils.isBlank(httpSslVerifiedServerNames)) {
                        if (httpSslVerifiedServerNames
                                .equalsIgnoreCase(Constants.TRANSPORT_HTTPS_VERIFIED_SERVERS_ALL)) {
                            verified = true;
                        } else {
                            String[] names = httpSslVerifiedServerNames.split(",");
                            for (String string : names) {
                                if (s != null && s.equals(string.trim())) {
                                    verified = true;
                                    break;
                                }
                            }
                        }
                    }
                    return verified;
                }
            });
        }

        if (allowSelfSignedCerts) {
            HttpsURLConnection.setDefaultSSLSocketFactory(createSelfSignedSocketFactory());
        }

    } catch (GeneralSecurityException ex) {
        throw new SecurityException(ex);
    }

}

From source file:com.emobc.android.utils.HttpUtils.java

/**
 * Take a client address is https by default if
 * @param https/*from  w  w w  .  ja v  a  2 s. co  m*/
 * @return
 */
public static DefaultHttpClient getHttpClient(boolean https) {
    if (https) {
        HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;

        DefaultHttpClient client = new DefaultHttpClient();

        SchemeRegistry registry = new SchemeRegistry();
        SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
        socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
        registry.register(new Scheme("https", socketFactory, 443));
        SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
        DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());

        // Set verifier      
        HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
        return httpClient;
    }
    return new DefaultHttpClient();
}

From source file:com.gistandard.androidbase.http.extension.VolleyEx.java

/**
 * Creates a default instance of the worker pool and calls {@link RequestQueue#start()} on it.
 *
 * @param context A {@link Context} to use for creating the cache dir.
 * @param stack An {@link HttpStack} to use for the network, or null for default.
 * @return A started {@link RequestQueue} instance.
 *///from w w w . ja v  a  2 s.  c  o  m
public static RequestQueue newRequestQueue(Context context, HttpStack stack) {
    File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);

    String userAgent = "volley/0";
    try {
        String packageName = context.getPackageName();
        PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
        userAgent = packageName + "/" + info.versionCode;
    } catch (NameNotFoundException e) {
    }

    if (stack == null) {
        if (Build.VERSION.SDK_INT >= 9) {
            try {
                // allow all ssl connection
                HttpsURLConnection.setDefaultHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
                stack = new HurlStack(null, new SSLSocketFactoryEx());
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            // Prior to Gingerbread, HttpUrlConnection was unreliable.
            // See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html
            stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
        }
    }

    Network network = new BasicNetwork(stack);

    RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network);
    queue.start();

    return queue;
}

From source file:com.bytelightning.opensource.pokerface.HelloWorldScriptTest.java

@BeforeClass
public static void setUpBeforeClass() throws Exception {
    PrevSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();
    PrevHostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier();

    proxy = new PokerFace();
    XMLConfiguration conf = new XMLConfiguration();
    conf.load(ProxySpecificTest.class.getResource("/HelloWorldTestConfig.xml"));
    proxy.config(conf);//ww  w.j ava  2s  .c o  m
    boolean started = proxy.start();
    Assert.assertTrue("Successful proxy start", started);

    SSLContext sc = SSLContext.getInstance("TLS");
    TrustManager[] trustAllCertificates = { new X509TrustAllManager() };
    sc.init(null, trustAllCertificates, new SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
        @Override
        public boolean verify(String hostname, SSLSession session) {
            return true; // Just allow them all.
        }
    });

}

From source file:org.wso2.developerstudio.eclipse.platform.ui.utils.SSLUtils.java

/**
 * Initialize the ssl context with the custom trust manager 
 *   1. setup https access to the created ssl context
 *   2. setup hostname verifier//w  w  w . jav a2 s  .com
 * @throws NoSuchAlgorithmException
 * @throws KeyManagementException
 */
public static void init() throws NoSuchAlgorithmException, KeyManagementException {
    if (sslCtx == null) {
        sslCtx = SSLContext.getInstance("SSL");
        sslCtx.init(null, new TrustManager[] { getCustomTrustManager() }, null);
        HttpsURLConnection.setDefaultSSLSocketFactory(sslCtx.getSocketFactory());
        HostnameVerifier allHostsValid = new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        };
        HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
    }
}

From source file:org.wso2.carbon.dynamic.client.web.proxy.util.DCRProxyUtils.java

public static DefaultHttpClient getHttpsClient() {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    // Setup the HTTPS settings to accept any certificate.
    HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;

    SchemeRegistry registry = new SchemeRegistry();
    SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
    socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
    registry.register(new Scheme(Constants.RemoteServiceProperties.DYNAMIC_CLIENT_SERVICE_PROTOCOL,
            socketFactory, DCRProxyUtils.getServerHTTPSPort()));
    SingleClientConnManager mgr = new SingleClientConnManager(httpClient.getParams(), registry);
    httpClient = new DefaultHttpClient(mgr, httpClient.getParams());

    // Set verifier
    HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
    return httpClient;
}

From source file:eu.codebits.plasmas.services.PollingService.java

@Override
protected void onHandleIntent(Intent intent) {
    originalIntent = intent;/* www.j  av a 2  s .co  m*/
    //Log.d(TAG, "Starting polling task");

    if (trustingHostnameVerifier != null) {
        HttpsURLConnection.setDefaultHostnameVerifier(trustingHostnameVerifier);
    }
    if (PlayerService.alerts == null) {
        new PollingTask().execute(intent.getData().toString());
    } else {
        Log.i(TAG, "Skipped polling until alerts flush");
    }
}

From source file:sit.web.client.HTTPTrustHelper.java

public static void init(String securityAlgorithm, KeyManager[] kms, TrustManager[] tms) {
    // Install the all-trusting trust manager
    try {/*from   w  w w  .j  a v  a  2  s  .co  m*/
        SSLContext sc = SSLContext.getInstance(securityAlgorithm);
        sc.init(kms, tms, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception ex) {
        Logger.getLogger(HttpHelper.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
    }

    HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

        @Override
        public boolean verify(String string, SSLSession ssls) {
            return true;
        }
    });
    HttpsURLConnection.setFollowRedirects(true);
}

From source file:camp.pixels.signage.services.PollingService.java

@Override
protected void onHandleIntent(Intent intent) {
    originalIntent = intent;/*w ww. j  a v a 2s .co m*/
    Log.d(TAG, "Starting polling task");

    if (trustingHostnameVerifier != null) {
        HttpsURLConnection.setDefaultHostnameVerifier(trustingHostnameVerifier);
    }
    if (PlayerService.alerts == null) {
        new PollingTask().execute(intent.getData().toString());
    } else {
        Log.i(TAG, "Skipped polling until alerts flush");
    }
}

From source file:net.sf.jsignpdf.ssl.SSLInitializer.java

public static final void init() throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException,
        CertificateException, IOException {
    if (Constants.RELAX_SSL_SECURITY) {
        LOGGER.debug("Relaxing SSL security.");

        //Details for the properties - http://docs.oracle.com/javase/7/docs/technotes/guides/security/jsse/JSSERefGuide.html
        //Workaround for http://sourceforge.net/tracker/?func=detail&atid=1037906&aid=3491269&group_id=216921
        System.setProperty("jsse.enableSNIExtension", "false");

        //just in case...
        System.setProperty("sun.security.ssl.allowUnsafeRenegotiation", "true");
        System.setProperty("sun.security.ssl.allowLegacyHelloMessages", "true");

        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }/*from w  w  w  . j  a  v  a 2s.  c o m*/
        });
    }

    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, TRUST_MANAGERS, null);

    HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
}