Restoring the vCenter Server Session Cookie

After you log in, you must restore the standard vCenter session context.
The code fragment in this section restores the default message handler and the session cookie. As the cookie handler has been replaced by the default handler, the client resets the session cookie by calling request context methods to access the context fields directly. The following sequence describes these steps and shows the corresponding objects and methods.
Restore the default message handler. The handlers used for
LoginByToken
are not used in subsequent calls to the vSphere API.
VimService.setHandlerResolver ( )
Get the VIM port.
Set the connection endpoint in the HTTP request context.
Set the HTTP request header (vCenter session cookie).
RequestContext.get ()
RequestContext.put ( )
The following example shows Java code that restores the vCenter session. This code requires the vCenter URL and the cookie and default handler that were retrieved before login. See LoginByToken Sample Code.
Restoring the vCenter Server Session
/* * Reset the default handler. This overwrites the existing handlers, effectively removing them. */ vimService.setHandlerResolver(defaultHandler); vimPort = vimService.getVimPort(); /* * Restore the connection endpoint in the request context. */ // Set the validated session cookie and set it in the header for once, // JAXWS will maintain that cookie for all the subsequent requests Map<String, Object> ctxt = ((BindingProvider) vimPort).getRequestContext(); ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, vcServerUrl); ctxt.put(BindingProvider.SESSION_MAINTAIN_PROPERTY, true); /* * Reset the cookie in the request context. */ 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(cookie)); ctxt.put(MessageContext.HTTP_REQUEST_HEADERS, headers);