Example usage for javax.net.ssl SSLPeerUnverifiedException printStackTrace

List of usage examples for javax.net.ssl SSLPeerUnverifiedException printStackTrace

Introduction

In this page you can find the example usage for javax.net.ssl SSLPeerUnverifiedException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:net.sf.taverna.cagrid.activity.CaGridActivity.java

/**
 * This static block is needed in case some of the caGrid services require
 * https which is more than likely and needs to be executed before we start
 * loading caGrid services or otherwise some of these services will fail.
 * Some caGrid services requiring https have a weird CN in their server
 * certificates - instead of CN=<HOSTNAME> they have CN="host/"+<HOSTNAME>,
 * i.e. string "host/" prepended so we have to tell Java's SSL to accept
 * these hostnames as well. This is not very good at is sets this hostname
 * verifier across all https connections created in the JVM from now on, but
 * solves the problem with such caGrid services.
 * /*from w ww.  j  a v  a 2 s.c  om*/
 */
protected static void setHostNameVerifier() {
    HostnameVerifier hv = new HostnameVerifier() {
        public boolean verify(String hostName, SSLSession session) {
            String hostNameFromCertificate = null;
            try {
                hostNameFromCertificate = session.getPeerPrincipal().getName().substring(3,
                        session.getPeerPrincipal().getName().indexOf(','));
            } catch (SSLPeerUnverifiedException e) {
                e.printStackTrace();
                return false;
            }
            logger.info("Hostname verifier: host from url: " + hostName + " vs. host from certificate: "
                    + hostNameFromCertificate);
            //return (hostName.equals(hostNameFromCertificate) || ("host/"+hostName).equals(hostNameFromCertificate));
            //force no-verification, dangerous!!!
            System.out.println(hostName + "\nis using a certificate issued to:\n " + hostNameFromCertificate);
            return true;
        }
    };
    HttpsURLConnection.setDefaultHostnameVerifier(hv);
}

From source file:com.ct855.util.HttpsClientUtil.java

private void print_https_cert(HttpsURLConnection con) {

    if (con != null) {

        try {//  ww w  .j  a  va2s  .c  o  m

            System.out.println("Response Code : " + con.getResponseCode());
            System.out.println("Cipher Suite : " + con.getCipherSuite());
            System.out.println("\n");

            Certificate[] certs = con.getServerCertificates();
            for (Certificate cert : certs) {
                System.out.println("Cert Type : " + cert.getType());
                System.out.println("Cert Hash Code : " + cert.hashCode());
                System.out.println("Cert Public Key Algorithm : " + cert.getPublicKey().getAlgorithm());
                System.out.println("Cert Public Key Format : " + cert.getPublicKey().getFormat());
                System.out.println("\n");
            }

        } catch (SSLPeerUnverifiedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}