Using LoginByToken in Java
The code fragment in this section sets up the message handlers and calls the LoginByToken method to get the session cookie. The following sequence describes the steps and shows the corresponding objects and methods.
1 | Create a new HeaderHandlerResolver. Then set the message security handlers for cookie extraction and for inserting the SAML token and credentials in the SOAP header. | |
2 | Get the VIM port. | |
3 | Set the connection endpoint in the HTTP request context. | |
4 | Call the LoginByToken() method. The method invocation activates the
handlers to insert the elements into the message headers. The method returns a session cookie
that identifies the newly created session. | VimPortType.LoginByToken()
|
5 | Extract the cookie and save it for later use. | HeaderCookieExtractionHandler.getCookie() |
The following examples shows Java code that calls the LoginByToken() method.
Example: Using LoginByToken
/* * Create a handler resolver and add the handlers. * Create a cookie extraction handler and add it to the handler resolver. */ HeaderHandlerResolver handlerResolver = new HeaderHandlerResolver(); HeaderCookieExtractionHandler cookieExtractor = new HeaderCookieExtractionHandler(); handlerResolver.addHandler(cookieExtractor); handlerResolver.addHandler(new TimeStampHandler()); handlerResolver.addHandler(new SamlTokenHandler(token)); handlerResolver.addHandler(new WsSecuritySignatureAssertionHandler( userCert.getPrivateKey(), userCert.getUserCert(), Utils.getNodeProperty(token, "ID"))); vimService.setHandlerResolver(handlerResolver); /* * Create a handler resolver. * Set the VIM service handler resolver. */ vimService.setHandlerResolver(handlerResolver); /* * Get the Vim port; this call clears the request context. */ vimPort = vimService.getVimPort(); /* * Retrieve the request context and set the server URL. */ Map<String, Object> ctxt = ((BindingProvider) vimPort).getRequestContext(); ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, vcServerUrl); ctxt.put(BindingProvider.SESSION_MAINTAIN_PROPERTY, true); /* * Call LoginByToken. */ UserSession us = vimPort.loginByToken(serviceContent.getSessionManager(), null); /* * Save the HTTP cookie. */ String cookie = cookieExtractor.getCookie();