Search in sources :

Example 1 with VimService

use of com.vmware.vim25.VimService in project opennms by OpenNMS.

the class VmwareViJavaAccess method setTimeout.

/**
 * Sets the timeout for server connections.
 *
 * @param timeout the timeout to be used for connecting
 * @return true, if the operation was successful
 */
public boolean setTimeout(int timeout) {
    if (m_serviceInstance != null) {
        ServerConnection serverConnection = m_serviceInstance.getServerConnection();
        if (serverConnection != null) {
            VimPortType vimService = serverConnection.getVimService();
            if (vimService != null) {
                Client client = vimService.getWsc();
                if (client != null) {
                    client.setConnectTimeout(timeout);
                    client.setReadTimeout(timeout);
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : ServerConnection(com.vmware.vim25.mo.ServerConnection) Client(com.vmware.vim25.ws.Client) CIMClient(org.sblim.wbem.client.CIMClient) VimPortType(com.vmware.vim25.VimPortType)

Example 2 with VimService

use of com.vmware.vim25.VimService in project photon-model by vmware.

the class BasicConnection method _connect.

@SuppressWarnings("unchecked")
private void _connect() throws RuntimeFaultFaultMsg, InvalidLocaleFaultMsg, InvalidLoginFaultMsg, com.vmware.pbm.RuntimeFaultFaultMsg {
    if (this.token != null) {
        /* This lock exists because header handlers are set per VimService.
            LoginByToken and normal login with username and password are using different sets of
            header handlers. If the correct handlers are not set, the connection will fail */
        synchronized (BasicConnection.class) {
            HandlerResolver defaultResolver = getVimService().getHandlerResolver();
            HeaderHandlerResolver handlerResolver = new HeaderHandlerResolver();
            handlerResolver.addHandler(new TimeStampHandler());
            handlerResolver.addHandler(new SamlTokenExtractionHandler());
            try {
                handlerResolver.addHandler(new SamlTokenHandler(SamlUtils.createSamlDocument(this.token).getDocumentElement()));
                getVimService().setHandlerResolver(handlerResolver);
            } catch (ParserConfigurationException | SAXException | IOException e) {
                throw new RuntimeFaultFaultMsg("Unable to authenticate", e);
            }
            this.vimPort = getVimService().getVimPort();
            updateBindingProvider(getBindingsProvider(), this.uri.toString());
            this.serviceContent = this.vimPort.retrieveServiceContent(this.getServiceInstanceReference());
            try {
                this.userSession = this.vimPort.loginByToken(this.serviceContent.getSessionManager(), null);
            } finally {
                getVimService().setHandlerResolver(defaultResolver);
            }
        }
    } else {
        this.vimPort = getVimService().getVimPort();
        updateBindingProvider(getBindingsProvider(), this.uri.toString());
        this.serviceContent = this.vimPort.retrieveServiceContent(this.getServiceInstanceReference());
        this.userSession = this.vimPort.login(this.serviceContent.getSessionManager(), this.username, this.password, null);
    }
    this.headers = (Map<String, List<String>>) getBindingsProvider().getResponseContext().get(MessageContext.HTTP_RESPONSE_HEADERS);
    // Need to extract only the cookie value
    List<String> cookieHeaders = this.headers.getOrDefault(HttpHeaderNames.SET_COOKIE.toString(), Collections.EMPTY_LIST);
    if (cookieHeaders.isEmpty()) {
        throw new RuntimeFaultFaultMsg("Failure in connecting to server, no session cookie found");
    }
    String cookieVal = cookieHeaders.get(0);
    String[] tokens = cookieVal.split(";");
    tokens = tokens[0].split("=");
    String extractedCookie = tokens[1];
    // PbmPortType
    this.pbmService = new PbmService();
    // Setting the header resolver for adding the VC session cookie to the
    // requests for authentication
    HeaderHandlerResolver headerResolver = new HeaderHandlerResolver();
    headerResolver.addHandler(new VcSessionHandler(extractedCookie));
    this.pbmService.setHandlerResolver(headerResolver);
    this.pbmPort = this.pbmService.getPbmPort();
    updateBindingProvider((BindingProvider) this.pbmPort, this.getSpbmURL().toString());
    this.pbmServiceContent = this.pbmPort.pbmRetrieveServiceContent(this.getPbmServiceInstanceReference());
}
Also used : IOException(java.io.IOException) RuntimeFaultFaultMsg(com.vmware.vim25.RuntimeFaultFaultMsg) SAXException(org.xml.sax.SAXException) HandlerResolver(javax.xml.ws.handler.HandlerResolver) PbmService(com.vmware.pbm.PbmService) List(java.util.List) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 3 with VimService

use of com.vmware.vim25.VimService in project cloudstack by apache.

the class VMwareUtil method getVMwareConnection.

public static VMwareConnection getVMwareConnection(LoginInfo loginInfo) throws Exception {
    trustAllHttpsCertificates();
    HostnameVerifier hv = new HostnameVerifier() {

        @Override
        public boolean verify(String urlHostName, SSLSession session) {
            return true;
        }
    };
    HttpsURLConnection.setDefaultHostnameVerifier(hv);
    ManagedObjectReference serviceInstanceRef = new ManagedObjectReference();
    final String serviceInstanceName = "ServiceInstance";
    serviceInstanceRef.setType(serviceInstanceName);
    serviceInstanceRef.setValue(serviceInstanceName);
    VimService vimService = new VimService();
    VimPortType vimPortType = vimService.getVimPort();
    Map<String, Object> ctxt = ((BindingProvider) vimPortType).getRequestContext();
    ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "https://" + loginInfo.getHost() + "/sdk");
    ctxt.put(BindingProvider.SESSION_MAINTAIN_PROPERTY, true);
    ServiceContent serviceContent = vimPortType.retrieveServiceContent(serviceInstanceRef);
    vimPortType.login(serviceContent.getSessionManager(), loginInfo.getUsername(), loginInfo.getPassword(), null);
    return new VMwareConnection(vimPortType, serviceContent);
}
Also used : VimService(com.vmware.vim25.VimService) SSLSession(javax.net.ssl.SSLSession) ServiceContent(com.vmware.vim25.ServiceContent) BindingProvider(javax.xml.ws.BindingProvider) VimPortType(com.vmware.vim25.VimPortType) HostnameVerifier(javax.net.ssl.HostnameVerifier) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Aggregations

VimPortType (com.vmware.vim25.VimPortType)2 PbmService (com.vmware.pbm.PbmService)1 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)1 RuntimeFaultFaultMsg (com.vmware.vim25.RuntimeFaultFaultMsg)1 ServiceContent (com.vmware.vim25.ServiceContent)1 VimService (com.vmware.vim25.VimService)1 ServerConnection (com.vmware.vim25.mo.ServerConnection)1 Client (com.vmware.vim25.ws.Client)1 IOException (java.io.IOException)1 List (java.util.List)1 HostnameVerifier (javax.net.ssl.HostnameVerifier)1 SSLSession (javax.net.ssl.SSLSession)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 BindingProvider (javax.xml.ws.BindingProvider)1 HandlerResolver (javax.xml.ws.handler.HandlerResolver)1 CIMClient (org.sblim.wbem.client.CIMClient)1 SAXException (org.xml.sax.SAXException)1