use of com.vmware.vim25.ServiceContent in project cloudstack by apache.
the class VmwareClient method connect.
/**
* Establishes session with the virtual center server.
*
* @throws Exception
* the exception
*/
public void connect(String url, String userName, String password) throws Exception {
svcInstRef.setType(SVC_INST_NAME);
svcInstRef.setValue(SVC_INST_NAME);
vimPort = vimService.getVimPort();
Map<String, Object> ctxt = ((BindingProvider) vimPort).getRequestContext();
ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url);
ctxt.put(BindingProvider.SESSION_MAINTAIN_PROPERTY, true);
ctxt.put("com.sun.xml.internal.ws.request.timeout", vCenterSessionTimeout);
ctxt.put("com.sun.xml.internal.ws.connect.timeout", vCenterSessionTimeout);
ServiceContent serviceContent = vimPort.retrieveServiceContent(svcInstRef);
// Extract a cookie. See vmware sample program com.vmware.httpfileaccess.GetVMFiles
@SuppressWarnings("unchecked") Map<String, List<String>> headers = (Map<String, List<String>>) ((BindingProvider) vimPort).getResponseContext().get(MessageContext.HTTP_RESPONSE_HEADERS);
List<String> cookies = headers.get("Set-cookie");
vimPort.login(serviceContent.getSessionManager(), userName, password, null);
if (cookies == null) {
// Get the cookie from the response header. See vmware sample program com.vmware.httpfileaccess.GetVMFiles
@SuppressWarnings("unchecked") Map<String, List<String>> responseHeaders = (Map<String, List<String>>) ((BindingProvider) vimPort).getResponseContext().get(MessageContext.HTTP_RESPONSE_HEADERS);
cookies = responseHeaders.get("Set-cookie");
if (cookies == null) {
String msg = "Login successful, but failed to get server cookies from url :[" + url + "]";
s_logger.error(msg);
throw new Exception(msg);
}
}
String cookieValue = cookies.get(0);
StringTokenizer tokenizer = new StringTokenizer(cookieValue, ";");
cookieValue = tokenizer.nextToken();
String pathData = "$" + tokenizer.nextToken();
serviceCookie = "$Version=\"1\"; " + cookieValue + "; " + pathData;
isConnected = true;
}
Aggregations