Search in sources :

Example 1 with AllCertificatesTrustManager

use of io.fabric8.utils.ssl.AllCertificatesTrustManager in project fabric8 by fabric8io.

the class URLUtils method prepareForSSL.

/**
 * Prepares an url connection for authentication if necessary.
 *
 * @param connection the connection to be prepared
 * @return the prepared conection
 */
public static URLConnection prepareForSSL(final URLConnection connection) {
    NullArgumentException.validateNotNull(connection, "url connection cannot be null");
    URLConnection conn = connection;
    if (conn instanceof JarURLConnection) {
        try {
            conn = ((JarURLConnection) connection).getJarFileURL().openConnection();
            conn.connect();
        } catch (IOException e) {
            throw new RuntimeException("Could not prepare connection for HTTPS.", e);
        }
    }
    if (conn instanceof HttpsURLConnection) {
        try {
            SSLContext ctx = SSLContext.getInstance("SSLv3");
            ctx.init(null, new TrustManager[] { new AllCertificatesTrustManager() }, null);
            ((HttpsURLConnection) conn).setSSLSocketFactory(ctx.getSocketFactory());
        } catch (KeyManagementException e) {
            throw new RuntimeException("Could not prepare connection for HTTPS.", e);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("Could not prepare connection for HTTPS.", e);
        }
    }
    return connection;
}
Also used : JarURLConnection(java.net.JarURLConnection) IOException(java.io.IOException) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) KeyManagementException(java.security.KeyManagementException) AllCertificatesTrustManager(io.fabric8.utils.ssl.AllCertificatesTrustManager)

Example 2 with AllCertificatesTrustManager

use of io.fabric8.utils.ssl.AllCertificatesTrustManager in project fabric8 by jboss-fuse.

the class URLUtils method prepareForSSL.

/**
 * Prepares an url connection for authentication if necessary.
 *
 * @param connection the connection to be prepared
 * @return the prepared conection
 */
public static URLConnection prepareForSSL(final URLConnection connection) {
    NullArgumentException.validateNotNull(connection, "url connection cannot be null");
    URLConnection conn = connection;
    if (conn instanceof JarURLConnection) {
        try {
            conn = ((JarURLConnection) connection).getJarFileURL().openConnection();
            conn.connect();
        } catch (IOException e) {
            throw new RuntimeException("Could not prepare connection for HTTPS.", e);
        }
    }
    if (conn instanceof HttpsURLConnection) {
        try {
            SSLContext ctx = SSLContext.getInstance("SSLv3");
            ctx.init(null, new TrustManager[] { new AllCertificatesTrustManager() }, null);
            ((HttpsURLConnection) conn).setSSLSocketFactory(ctx.getSocketFactory());
        } catch (KeyManagementException e) {
            throw new RuntimeException("Could not prepare connection for HTTPS.", e);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("Could not prepare connection for HTTPS.", e);
        }
    }
    return connection;
}
Also used : JarURLConnection(java.net.JarURLConnection) IOException(java.io.IOException) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) KeyManagementException(java.security.KeyManagementException) AllCertificatesTrustManager(io.fabric8.utils.ssl.AllCertificatesTrustManager)

Aggregations

AllCertificatesTrustManager (io.fabric8.utils.ssl.AllCertificatesTrustManager)2 IOException (java.io.IOException)2 JarURLConnection (java.net.JarURLConnection)2 URLConnection (java.net.URLConnection)2 KeyManagementException (java.security.KeyManagementException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)2 SSLContext (javax.net.ssl.SSLContext)2