Example usage for java.security UnrecoverableKeyException printStackTrace

List of usage examples for java.security UnrecoverableKeyException printStackTrace

Introduction

In this page you can find the example usage for java.security UnrecoverableKeyException printStackTrace.

Prototype

public void printStackTrace(PrintStream s) 

Source Link

Document

Prints this throwable and its backtrace to the specified print stream.

Usage

From source file:org.jenkinsci.plugins.stashNotifier.StashNotifier.java

/**
 * Returns the HttpClient through which the REST call is made. Uses an
 * unsafe TrustStrategy in case the user specified a HTTPS URL and
 * set the ignoreUnverifiedSSLPeer flag.
 * //  w  w  w .ja v a2 s.co  m
 * @param logger   the logger to log messages to
 * @return         the HttpClient
 */
private HttpClient getHttpClient(PrintStream logger) {
    HttpClient client = null;
    boolean ignoreUnverifiedSSL = ignoreUnverifiedSSLPeer;
    DescriptorImpl descriptor = getDescriptor();
    if (!ignoreUnverifiedSSL) {
        ignoreUnverifiedSSL = descriptor.isIgnoreUnverifiedSsl();
    }
    if (getStashServerBaseUrl().startsWith("https") && ignoreUnverifiedSSL) {
        // add unsafe trust manager to avoid thrown
        // SSLPeerUnverifiedException
        try {
            TrustStrategy easyStrategy = new TrustStrategy() {
                public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    return true;
                }
            };

            SSLSocketFactory sslSocketFactory = new SSLSocketFactory(easyStrategy);
            SchemeRegistry schemeRegistry = new SchemeRegistry();
            schemeRegistry.register(new Scheme("https", 443, sslSocketFactory));
            ClientConnectionManager connectionManager = new SingleClientConnManager(schemeRegistry);
            client = new DefaultHttpClient(connectionManager);
        } catch (NoSuchAlgorithmException nsae) {
            logger.println("Couldn't establish SSL context:");
            nsae.printStackTrace(logger);
        } catch (KeyManagementException kme) {
            logger.println("Couldn't initialize SSL context:");
            kme.printStackTrace(logger);
        } catch (KeyStoreException kse) {
            logger.println("Couldn't initialize SSL context:");
            kse.printStackTrace(logger);
        } catch (UnrecoverableKeyException uke) {
            logger.println("Couldn't initialize SSL context:");
            uke.printStackTrace(logger);
        } finally {
            if (client == null) {
                logger.println("Trying with safe trust manager, instead!");
                client = new DefaultHttpClient();
            }
        }
    } else {
        client = new DefaultHttpClient();
    }

    ProxyConfiguration proxy = Jenkins.getInstance().proxy;
    if (proxy != null && !proxy.name.isEmpty() && !proxy.name.startsWith("http")) {
        SchemeRegistry schemeRegistry = client.getConnectionManager().getSchemeRegistry();
        schemeRegistry.register(new Scheme("http", proxy.port, new PlainSocketFactory()));
        client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(proxy.name, proxy.port));
    }

    return client;
}