The steps for accessing any HTTP endpoint with JAX-WS bindings include the vSphere Web Services SDK Server URL, vSphere server object, and variables.
These steps are listed at the beginning of Obtaining a Session Token - Code Fragments from VMPromoteDisks.java.
Procedure
- Create a
TrustManager
class to handle certificate checking.In this example we use a
TrustManager
class to accept all certificates. This is not appropriate for a production environment. Production code should implement certificate support.private static class TrustAllTrustManager implements javax.net.ssl.TrustManager, javax.net.ssl.X509TrustManager { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } public boolean isServerTrusted( java.security.cert.X509Certificate[] certs) { return true; } public boolean isClientTrusted(java.security.cert.X509Certificate[] certs) { return true; } public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) throws java.security.cert.CertificateException { return; } public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) throws java.security.cert.CertificateException { return; } }
- Include the Server URL and credentials as arguments in the main method:
public static void main(String[] args) { try { String serverName = args[0]; String userName = args[1]; String password = args[2]; String url = "https://"+serverName+"/sdk/vimService";
- Declare variables of the following types for access to vSphere server objects:
-
ManagedObjectReference for the ServiceInstance.
-
VimService object for access to the Web service.
-
VimPortType object for access to all of the methods defined in the vSphere API.
-
ServiceContent for access to the managed object services on the server.
The following Java code fragment shows these variable declarations:
ManagedObjectReference SVC_INST_REF = new ManagedObjectReference(); VimService vimService; VimPortType vimPort; ServiceContent serviceContent;
-
- Declare a host name verifier that will automatically enable the connection. The host name verifier is invoked during the SSL handshake.
HostnameVerifier hv = new HostnameVerifier() { public boolean verify(String urlHostName, SSLSession session) { return true; } };
- Instantiate the trust manager object.
// Create the trust manager. javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1]; javax.net.ssl.TrustManager tm = new TrustAllTrustManager(); trustAllCerts[0] = tm;
- Create the SSL context
javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL");
- Create the session context
javax.net.ssl.SSLSessionContext sslsc = sc.getServerSessionContext();
- Initialize the contexts; the session context takes the trust manager.
sslsc.setSessionTimeout(0); sc.init(null, trustAllCerts, null);
- Use the default socket factory to create the socket for the secure connection
javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
- Set the default host name verifier to enable the connection.
HttpsURLConnection.setDefaultHostnameVerifier(hv);