VMware Cloud Community
jcp0wermac
Enthusiast
Enthusiast

InitiateFileTransferToGuest, URL and java.net.URL

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.

//example 1
var net = new java.net
net["URL(java.lang.String)"](javaFqdnUrl);

//example 2
var url = new java.net.URL
url["newInstance(java.lang.String)"](javaFqdnUrl);
//example 3
var classUrl = JavaImporter();
classUrl.importPackage(Packages.java.net);
classUrl.importClass(Packages.java.net.URL);
var url = classUrl.URL(javaFqdnUrl);

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

0 Kudos
1 Reply
jcp0wermac
Enthusiast
Enthusiast

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.

var sdkCon = sourceVM.sdkConnection;
var gom = sdkCon.guestOperationsManager;
var authmgr = gom.authManager;
var filemgr = gom.fileManager;
var passAuth = new VcNamePasswordAuthentication() ;
var fileAttr = new VcGuestFileAttributes() ;
var sysprepMime = sysprep.getContentAsMimeAttachment();
var contentSize = sysprep.contentSize;
var sysprepString = sysprepMime.content;
passAuth.username = "username";
passAuth.password = "password";
passAuth.interactiveSession = false;
System.debug(authmgr);
try {
authmgr.validateCredentialsInGuest(sourceVM,passAuth);
}
catch(err) {
System.debug(err);
}
var path = "c:\\temp2";
var createDirs = false;
try {
filemgr.makeDirectoryInGuest(sourceVM,passAuth,path, createDirs);
var urltemp = filemgr.initiateFileTransferToGuest(sourceVM , passAuth , "c:\\temp2\\temp.txt" , fileAttr , contentSize , true);
fqdnUrl = urltemp.replace("10.0.0.4" , "esx001");

Sample.uploadFile(fqdnUrl,sysprepString );

}
catch(err) {
System.debug(err);
}

Its no where near perfect but it works.

0 Kudos