Accessing the vSphere Web
Services HTTP Endpoint with JAX-WS
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.
- Create aTrustManagerclass to handle certificate checking.In this example we use aTrustManagerclass 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:
- ManagedObjectReferencefor theServiceInstance.
- VimServiceobject for access to the Web service.
- VimPortTypeobject for access to all of the methods defined in the vSphere API.
- ServiceContentfor access to the managed object services on the server.The following Java code fragment shows these variable declarations:ManagedObjectReference SVC_INST_REF 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 contextjavax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL");
- Create the session contextjavax.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 connectionjavax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
- Set the default host name verifier to enable the connection.HttpsURLConnection.setDefaultHostnameVerifier(hv);