Establishing a Session with Username and Password Credentials
You can specify username and password credentials to establish a session with a vCenter Server. This method of establishing a session has been deprecated as of vSphere 5.1. The following steps describe how a client application specifies username and password credentials for access to a vCenter Server.
1
2
Instantiate a local proxy object for reference to ServiceInstance. Use this local proxy object to retrieve the ServiceContent object from the server. ServiceContent contains a reference to the root folder for the inventory and references to the managed objects that provide the vSphere services.
3
4
5
6
Overview of a Java Sample Application
This section includes an example of a complete client application that demonstrates the basic client capability. The sample client application prints out the product name, server type, and product version to demonstrate that it is connected and able to retrieve information from the server.
While Example: Java Test Client Application is a complete client application that demonstrates the basic client capability, it uses a slightly different format than the Java sample files in the SDK\ directory. This example, and the Java samples that are included with your vSphere Web Service SDK, have been compiled using JAX-WS bindings.
Most of the vSphere Web Services SDK samples do not handle exceptions, and they accept all security certificates. Use the samples as examples for extracting the types of data you want to view, but do not use these security or exception techniques in your production applications.
Example: Java Test Client Application is a stand-alone application that accepts command-line arguments for the vSphere server name (DNS name or IP address), user name, and password.
To build a simple vSphere client application
1
import com.vmware.vim25.*;
2
import java.util.*;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.soap.SOAPFaultException;
3
public class TestClient {
4
Include the class variable declarations/definitions. Use a TrustManager class to accept all certificates, as shown in Accessing the HTTP Endpoint with JAX-WS. This is not appropriate for a production environment. Production code should implement certificate support.
5
6
Retrieve data from the vSphere or vCenter server. In this example, we are just going to print out the product name, server type, and product version to prove that the client is connected and working correctly.
System.out.println(serviceContent.getAbout().getFullName());
System.out.println("Server type is " + serviceContent.getAbout().getApiType());
System.out.println("API version is " + serviceContent.getAbout().getVersion());
7
Use the VimPort object to close the connection, as shown in Closing the Connection. Always close your server connections to maintain security.
Java Client Example
Example: Java Test Client Application shows the complete sample client application code, without the explanation steps.
Example: Java Test Client Application
import com.vmware.vim25.*;
import java.util.*;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.soap.SOAPFaultException;
 
public class TestClient {
 
// Authentication is handled by using a TrustManager and supplying
// a host name verifier method. (The host name verifier is declared
// in the main function.)
// See Example: Obtaining a Session Token - Code Fragments from VMPromoteDisks.java for more details.
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;
}
}
 
public static void main(String[] args) {
try {
// Server URL and credentials.
String serverName = args[0];
String userName = args[1];
String password = args[2];
String url = "https://"+serverName+"/sdk/vimService";
 
// Variables of the following types for access to the API methods
// and to the vSphere inventory.
// -- ManagedObjectReference for the ServiceInstance on the Server
// -- VimService for access to the vSphere Web service
// -- VimPortType for access to methods
// -- ServiceContent for access to managed object services
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;
}
};
// 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);
 
// Set up the manufactured managed object reference for the ServiceInstance
SVC_INST_REF.setType("ServiceInstance");
SVC_INST_REF.setValue("ServiceInstance");
 
// Create a VimService object to obtain a VimPort binding provider.
// The BindingProvider provides access to the protocol fields
// in request/response messages. Retrieve the request context
// which will be used for processing message requests.
vimService = new VimService();
vimPort = vimService.getVimPort();
Map<String, Object> ctxt = ((BindingProvider) vimPort).getRequestContext();
 
// Store the Server URL in the request context and specify true
// to maintain the connection between the client and server.
// The client API will include the Server's HTTP cookie in its
// requests to maintain the session. If you do not set this to true,
// the Server will start a new session with each request.
ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url);
ctxt.put(BindingProvider.SESSION_MAINTAIN_PROPERTY, true);
 
// Retrieve the ServiceContent object and login
serviceContent = vimPort.retrieveServiceContent(SVC_INST_REF);
vimPort.login(serviceContent.getSessionManager(),
userName,
password,
null);
 
// print out the product name, server type, and product version
System.out.println(serviceContent.getAbout().getFullName());
System.out.println("Server type is " + serviceContent.getAbout().getApiType());
System.out.println("API version is " + serviceContent.getAbout().getVersion());
 
// close the connection
vimPort.logout(serviceContent.getSessionManager());
} catch (Exception e) {
System.out.println(" Connect Failed ");
e.printStackTrace();
}
}//end main()
}// end class TestClient
 
Use the following command to compile the code for Example: Java Test Client Application, after you have saved it as a .java file:
c:>javac -classpath path-to-vim25.jar TestClient.java
Use the following command to execute the compiled class (binary) file:
c:>java -classpath path-to-vim25.jar TestClient web-service-url user-name user-password