static public Scheme createHttpsScheme()

in ch-httpclient-util/src/main/java/com/cloudhopper/httpclient/util/SchemeFactory.java [70:131]


    static public Scheme createHttpsScheme( File keystoreFile, 
					    String keystorePassword, 
					    File truststoreFile, 
					    String truststorePassword ) 
	throws NoSuchAlgorithmException, KeyStoreException, FileNotFoundException,
	       IOException, KeyManagementException, CertificateException, 
	       UnrecoverableKeyException
    {
	
	if( keystoreFile == null && truststoreFile == null ){
	    // To insure we don't break anything, if keystore and trust store is not specified, 
	    // call the legacy createHttpsScheme.
	    return createHttpsScheme();
	} else {
	    // Configure https scheme with a keystore to authenticate ourselves to the server
	    // and/or a truststore to verify the server's certificate.
	    KeyStore keystore = null;
	    if( keystoreFile != null ){
		keystore  = KeyStore.getInstance( KeyStore.getDefaultType() );        
		FileInputStream instream = new FileInputStream( keystoreFile ); 
		try {
		    // A null password is valid when the keystore does not have a password.
		    if( keystorePassword != null ){
			keystore.load(instream, keystorePassword.toCharArray());
		    } else {
			keystore.load(instream, null );
		    }
		} finally {
		    instream.close();
		}
		
	    }
	    KeyStore truststore = null;
	    if( truststoreFile != null ){
	        truststore = KeyStore.getInstance( KeyStore.getDefaultType() );        
		FileInputStream instream = new FileInputStream( truststoreFile ); 
		try {
		    // A null password is valid when the keystore does not have a password.
		    if( truststorePassword != null ){
			truststore.load(instream, truststorePassword.toCharArray());
		    } else {
			truststore.load(instream, null);
		    }
		} finally {
		    instream.close();
		}
	    }
	    // Not sure if identifing which params were passed in as null and calling the 
	    // appropriate constructor is necessary, because the Apache Docs don't describe
	    // what happens when we pass in null. Play it conservative rather than test the
	    // behavior. 
	    SSLSocketFactory socketFactory;
	    if( keystore != null && truststore != null ){
		socketFactory = new SSLSocketFactory( keystore, keystorePassword, truststore );
	    } else if( keystore != null ){
		socketFactory = new SSLSocketFactory( keystore, keystorePassword );
	    } else {
		socketFactory = new SSLSocketFactory( truststore );
	    }
	    return new Scheme("https", socketFactory, 443);
	} 
    }