1.5

VMware, Inc. 25
Chapter 3 Using vCenter Chargeback with a Billing System
FakeSSLCertificateSocketFactory Class
ThecodeexamplesusethefollowingclasstoaccessresourcesovertheHTTPSprotocol.
/**
* Helper class to accept self-signed certificate.
*/
public class FakeSSLCertificateSocketFactory implements SecureProtocolSocketFactory {
private SSLContext sslContext;
public FakeSSLCertificateSocketFactory() throws NoSuchAlgorithmException,
KeyManagementException {
sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, new TrustManager[] {new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] ax509certificate, String s) throws
CertificateException {
// Allow.
}
@Override
public void checkServerTrusted(X509Certificate[] ax509certificate, String s) throws
CertificateException {
// Allow.
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}}, null);
}
@Override
public Socket createSocket(String s, int i) throws IOException, UnknownHostException {
return sslContext.getSocketFactory().createSocket(s, i);
}
@Override
public Socket createSocket(String s, int i, InetAddress inetaddress, int j) throws
IOException, UnknownHostException {
return sslContext.getSocketFactory().createSocket(s, i, inetaddress, j);
}
@Override
public Socket createSocket(String s, int i, InetAddress inetaddress, int j,
HttpConnectionParams httpconnectionparams) throws IOException,
UnknownHostException, ConnectTimeoutException {
return sslContext.getSocketFactory().createSocket(s, i, inetaddress, j);
}
@Override
public Socket createSocket(Socket socket, String s, int i, boolean flag) throws IOException,
UnknownHostException {
return sslContext.getSocketFactory().createSocket(socket, s, i, flag);
}
}