Search in sources :

Example 1 with ServiceContent

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;
}
Also used : StringTokenizer(java.util.StringTokenizer) ServiceContent(com.vmware.vim25.ServiceContent) ArrayList(java.util.ArrayList) List(java.util.List) BindingProvider(javax.xml.ws.BindingProvider) Map(java.util.Map) WebServiceException(javax.xml.ws.WebServiceException)

Aggregations

ServiceContent (com.vmware.vim25.ServiceContent)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 StringTokenizer (java.util.StringTokenizer)1 BindingProvider (javax.xml.ws.BindingProvider)1 WebServiceException (javax.xml.ws.WebServiceException)1