Establish a Connection with the VMware Storage Policy Server
Use the session cookie from the vCenter Server session to establish the Storage Policy session. The session cookie represents the authenticated vCenter Server session, which is based on the SSO token.
The following code fragments establish connections both with the vCenter Server and the Storage Policy Server. These examples are based on the BasicConnection sample which is located in the Storage Policy SDK connection sample directory:
SDK/spbm/java/JAXWS/samples/com/vmware/spbm/connection/BasicConnection.java
The BasicConnection sample uses an instance of the LoginByTokenSample class. See vCenter LoginByToken Example. The LoginByToken example saves the HTTP cookie produced during the intial connection sequence and then restores the cookie after the vCenter Server connection has been established. Although the LoginByToken example creates a vCenter Server connection, the BasicConnection sample establishes its own connection with the vCenter Server. A different implementation might integrate those capabilities to reduce the number of vCenter Server connections.
Server URLs
The BasicConnection sample creates connections to three VMware Servers.
In the example configuration, the SSO and Storage Policy Servers are located on the same system as the vCenter Server. In other configurations, the SSO Server may be located on a different server.
https://server-name|IPaddress/sdk/vimService
https://server-name|IPaddress/sts/STSService
https://server-name|IPaddress/pbm
Establish the vCenter Session Connection for the Local Instance
The following code fragment sets up the HTTP connection with the vCenter Server.
1
Retrieve the VimPort interface. This provides access to the vSphere API methods.
2
3
4
Call the RetrieveServiceContent method to establish the HTTP connection with the vCenter Server.
Example: vCenter Server Connection
// 1. Retrieve the VimPort interface.
vimService = new VimService();
vimPort = vimService.getVimPort();
 
// 2. Retrieve the request context and set the vCenter Server endpoint.
Map<String, Object> ctxt = ((BindingProvider) vimPort).getRequestContext();
ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, vcurl.toString());
ctxt.put(BindingProvider.SESSION_MAINTAIN_PROPERTY, true);
 
// 3. Put the extracted vCenter session cookie into the VimPortType request header.
Map<String, List<String>> headers =
(Map<String, List<String>>) ctxt .get(MessageContext.HTTP_REQUEST_HEADERS);
if (headers == null) {
headers = new HashMap<String, List<String>>();
}
headers.put("Cookie", Arrays.asList(cookieVal));
ctxt.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
 
// 4. Retrieve the vCenter Server service content. (Establishes the HTTP connection)
vimServiceContent = vimPort.retrieveServiceContent(this.getVimServiceInstanceReference());
Create the Storage Policy Server Connection
The following code fragment uses a vCenter session cookie to create a Storage Policy Server session.
1
Extract the actual cookie value from the name=value expression in the cookie string obtained from the vCenter session connection.
2
Create a PbmService object.
3
4
Retrieve the PbmPort object for access to the Storage Policy API methods.
5
6
Call the PbmRetrieveServiceContent method to establish the HTTP connection to the Storage Policy Server.
Example: Storage Policy Server Connection
// 1. Set the extracted cookie in the PbmPortType
//
// Need to extract only the cookie value
String[] tokens = cookieVal.split(";");
tokens = tokens[0].split("=");
String extractedCookie = tokens[1];
 
// 2. Create a PbmService object.
pbmService = new PbmService();
 
// 3. Setting the header resolver for adding the VC session cookie to the
// requests for authentication
HeaderHandlerResolver headerResolver = new HeaderHandlerResolver();
headerResolver.addHandler(new VcSessionHandler(extractedCookie));
pbmService.setHandlerResolver(headerResolver);
 
// 4. Retrieve the PbmPort object for access to the Storage Policy API
pbmPort = pbmService.getPbmPort();
 
// 5. Set the Storage Policy Server endpoint
Map<String, Object> pbmCtxt = ((BindingProvider) pbmPort).getRequestContext();
pbmCtxt.put(BindingProvider.SESSION_MAINTAIN_PROPERTY, true);
pbmCtxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, spbmurl.toString());
 
// 6. Retrieve the service content (creates the connection)
pbmServiceContent = pbmPort.pbmRetrieveServiceContent(getPbmServiceInstanceReference());