Restoring the vCenter
Server Session Cookie in a Java Client
After you log in, you must restore the
standard vCenter Server 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.
1 | Restore the default message
handler. The handlers used for
LoginByToken() are not
used in subsequent calls to the vSphere
API. | VimService.setHandlerResolver()
|
2 | Get the VIM
port. | ![]() |
3 | Set the connection endpoint in the
HTTP request context. | ![]() |
4 | Set the HTTP request header
(vCenter Server session
cookie). | RequestContext.get()
RequestContext.put() |
The following example shows Java code that
restores the vCenter Server session. This code requires the vCenter Server URL
and the cookie and default handler that were retrieved before login. See
Sample Code for a Java Client to the Web Services SDK.
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);