I am trying to implement the vSphere 5 API VIX so that it will work in the appliance. When using the existing URL class the SSL connection fails with InternalError: HTTP GET error : java.security.cert.CertificateException: No subject alternative DNS name matching foo found.
I tried everything I can think of to get around the class name collision.
I even tried to use this code, without any luck.
//javascript
var javaimp = JavaImporter();
//javaimp.importPackage(Packages.com.csc.vmware.orchestrator.vix);
javaimp.importPackage(Packages.com.csc.vmware.orchestrator.vix);
with(javaimp) {
var ft = FileTransfer;
System.debug(ft);
System.debug(typeof ft);
}
//Java, created as jar and put into \VMware\Orchestrator\app-server\bin
package com.csc.vmware.orchestrator.vix;
import java.net.*;
import java.io.*;
import javax.net.ssl.*;
//http://exampledepot.com/egs/javax.net.ssl/TrustAll.html
public class FileTransfer {
@SuppressWarnings("deprecation")
public static boolean uploadFile(String destUrl, String file) {
java.net.URL url;
javax.net.ssl.HttpsURLConnection httpsConn;
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
}
};
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}
catch (Exception e) {
e.printStackTrace();
}
try {
url = new URL(destUrl);
try {
httpsConn = (javax.net.ssl.HttpsURLConnection) url.openConnection();
httpsConn.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
// TODO Auto-generated method stub
return false;
}
});
httpsConn.setDoInput(true);
httpsConn.setDoOutput(true);
DataOutputStream out = new DataOutputStream(httpsConn.getOutputStream());
out.writeBytes(file);
out.flush();
out.close();
DataInputStream input = new DataInputStream(httpsConn.getInputStream());
while( null != (input.readLine())) {
//blah
}
input.close();
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
}
If anyone has any suggestions I would appreciate it.
Thanks,
Joe
Its not what I wanted to do but it worked. I used the Orchestrator SDK and "Create a skeleton vCenter Orchestrator plug-in project". Being lazy I just added the code I needed as static under
sampleplugin-model/src/main/java -> com.vmware.sample.model -> Sample.java
Then added it as a plugin. Back to orchestrator the following code was used.
Its no where near perfect but it works.