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. Set up a header handler to support adding the vCenter session cookie to the Storage Policy Server connection.
  4. Retrieve the
    PbmPort
    object for access to the Storage Policy API methods.
  5. Retrieve the request context and set the endpoint to the Storage Policy Server URL.
  6. Call the
    PbmRetrieveServiceContent
    method to establish the HTTP connection to the Storage Policy Server.
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());