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.
  1. 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; } }
  2. 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";
  3. 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 VimService vimService; VimPortType vimPort; ServiceContent serviceContent;
  4. 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; } };
  5. 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;
  6. Create the SSL context
    javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL");
  7. Create the session context
    javax.net.ssl.SSLSessionContext sslsc = sc.getServerSessionContext();
  8. Initialize the contexts; the session context takes the trust manager.
    sslsc.setSessionTimeout(0); sc.init(null, trustAllCerts, null);
  9. Use the default socket factory to create the socket for the secure connection
    javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
  10. Set the default host name verifier to enable the connection.
    HttpsURLConnection.setDefaultHostnameVerifier(hv);