@SuppressWarnings("rawtypes") private void makeConnection(String url, String username, String password, boolean trustEveryone) throws RuntimeFaultFaultMsg, InvalidLocaleFaultMsg, InvalidLoginFaultMsg, KeyManagementException, NoSuchAlgorithmException { vimService = new VimService(); vimPort = vimService.getVimPort(); populateContextMap(url, username, password); if (Boolean.TRUE.equals(trustEveryone)) { DisableSecurity.trustEveryone(); } serviceContent = vimPort.retrieveServiceContent(this.getServiceInstanceReference()); userSession = vimPort.login(serviceContent.getSessionManager(), username, password, null); }
/** * Creates a session with the server using username and password * * @param server hostname or ip address of the server to log in to * @param username username for login * @param password password for login */ public void loginByUsernameAndPassword( String server, String username, String password) { try { String vimSdkUrl = "https://" + server + VIM_PATH; /* * Create a VimService object to obtain a VimPort binding provider. * The BindingProvider provides access to the protocol fields * in request/response messages. Retrieve the request context * which will be used for processing message requests. */ this.vimService = new VimService(); this.vimPort = vimService.getVimPort(); Map<String, Object> ctxt = ((BindingProvider) vimPort).getRequestContext(); /* * Store the Server URL in the request context and specify true * to maintain the connection between the client and server. * The client API will include the Server's HTTP cookie in its * requests to maintain the session. If you do not set this to * true, the Server will start a new session with each request. */ ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, vimSdkUrl); ctxt.put(BindingProvider.SESSION_MAINTAIN_PROPERTY, true); // Retrieve the ServiceContent object and login this.serviceContent = vimPort.retrieveServiceContent(SVC_INST_REF); this.vimPort.login( serviceContent.getSessionManager(), username, password, null); } catch (Exception e) { e.printStackTrace(); } }
/** * Establish a connection to the vCenter. */ public void connect() throws Exception { // FIXME what to do? HostnameVerifier hv = new HostnameVerifier() { @Override public boolean verify(String urlHostName, SSLSession session) { return true; } }; int numFailedLogins = 0; boolean repeatLogin = true; while (repeatLogin) { try { HttpsURLConnection.setDefaultHostnameVerifier(hv); VimService vimService = new VimService(); VimPortType vimPort = vimService.getVimPort(); Map<String, Object> ctxt = ((BindingProvider) vimPort) .getRequestContext(); ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url); ctxt.put(BindingProvider.SESSION_MAINTAIN_PROPERTY, Boolean.TRUE); ManagedObjectReference morSvcInstance = new ManagedObjectReference(); morSvcInstance.setType("ServiceInstance"); morSvcInstance.setValue("ServiceInstance"); ServiceContent serviceContent = vimPort .retrieveServiceContent(morSvcInstance); vimPort.login(serviceContent.getSessionManager(), user, password, null); connection = new ServiceConnection(vimPort, serviceContent); LOG.debug("Established connection to vSphere. URL: " + url + ", UserId: " + user); repeatLogin = false; } catch (Exception e) { LOG.error("Failed to establish connection to vSphere. URL: " + url + ", UserId: " + user, e); if (numFailedLogins > 2) { throw e; } numFailedLogins++; repeatLogin = true; try { Thread.sleep(3000); } catch (@SuppressWarnings("unused") InterruptedException ex) { Thread.currentThread().interrupt(); } } } }
public VimService getVimService() { return this.vimService; }
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); }
VimService getVimService();