Java Example of Accessing a vSphere Automation Service

The example is based on the code in the LibraryCrud.java sample.

This example shows the steps for creating an authenticated session to the vSphere Automation Endpoint and creating the service stub for the Content Library API provider.

This example uses the steps described in the Access a vSphere Automation Service procedure.

Note: For a complete and up-to-date version of the sample code, see the vSphere Automation SDK Java samples at GitHub.
...

// Log in by using username/password
	  this.stubFactory = createApiStubFactory(server, httpConfig);

// Create a security context for username/password authentication
SecurityContext securityContext =
        SecurityContextFactory.createUserPassSecurityContext(
            username, password.toCharArray());

// Create a stub configuration with username/password security context
StubConfiguration stubConfig = new StubConfiguration(securityContext);

// Create a session stub by using the stub configuration.
Session session =
        this.stubFactory.createStub(Session.class, stubConfig);

// Log in and create a session
char[] sessionId = session.create();

// Initialize a session security context from the generated session id
SessionSecurityContext sessionSecurityContext =
        new SessionSecurityContext(sessionId);

// Update the stub configuration to use the session id
stubConfig.setSecurityContext(sessionSecurityContext);

/*
 * Create a stub for the session service using the authenticated
 * session
 */
this.sessionSvc =
        this.stubFactory.createStub(Session.class, stubConfig);

// Create service stubs for the Content Library service.
Library libraryService = stubFactory.createStub(Library.class, stubConfig);
	
// Invoke an operation of the Content Library service.
List<String> listContentLibraries = libraryService.list();