/* * Define the client side of the test. * * If the server prematurely exits, serverReady will be set to true * to avoid infinite hangs. */ void doClientSide() throws Exception { /* * Wait for server to get started. */ while (!serverReady) { Thread.sleep(50); } // Send HTTP POST request to server URL url = new URL("https://localhost:" + serverPort); HttpsURLConnection.setDefaultHostnameVerifier(new NameVerifier()); HttpsURLConnection http = (HttpsURLConnection) url.openConnection(); http.setDoOutput(true); http.setRequestMethod("POST"); PrintStream ps = new PrintStream(http.getOutputStream()); try { ps.println(postMsg); ps.flush(); if (http.getResponseCode() != 200) { throw new RuntimeException("test Failed"); } } finally { ps.close(); http.disconnect(); closeReady = true; } }
/** Creates a new URL to use as the basis of a connection. */ public MsgRpcImpl( String username, String password, String host, int port, boolean ssl, boolean debugf) throws MalformedURLException { if (ssl) { // Install the all-trusting trust manager & HostnameVerifier try { SSLContext sc = SSLContext.getInstance("SSL"); sc.init( null, new TrustManager[] { new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted( java.security.cert.X509Certificate[] certs, String authType) {} public void checkServerTrusted( java.security.cert.X509Certificate[] certs, String authType) {} } }, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); HttpsURLConnection.setDefaultHostnameVerifier( new HostnameVerifier() { public boolean verify(String string, SSLSession ssls) { return true; } }); } catch (Exception e) { } u = new URL("https", host, port, "/api/1.0/"); } else { u = new URL("http", host, port, "/api/1.0/"); } /* login to msf server */ Object[] params = new Object[] {username, password}; Map results = exec("auth.login", params); /* save the temp token (lasts for 5 minutes of inactivity) */ rpcToken = results.get("token").toString(); /* generate a non-expiring token and use that */ params = new Object[] {rpcToken}; results = exec("auth.token_generate", params); rpcToken = results.get("token").toString(); }
/* * Define the client side of the test. * * If the server prematurely exits, serverReady will be set to true * to avoid infinite hangs. */ void doClientSide() throws Exception { /* * Wait for server to get started. */ while (!serverReady) { Thread.sleep(50); } HttpsURLConnection.setDefaultHostnameVerifier(new NameVerifier()); URL url = new URL("https://" + "localhost:" + serverPort + "/etc/hosts"); URLConnection urlc = url.openConnection(); if (!(urlc instanceof javax.net.ssl.HttpsURLConnection)) { throw new Exception("URLConnection ! instanceof javax.net.ssl.HttpsURLConnection"); } BufferedReader in = null; try { in = new BufferedReader(new InputStreamReader(urlc.getInputStream())); String inputLine; System.out.print("Client reading... "); while ((inputLine = in.readLine()) != null) System.out.println(inputLine); System.out.println("Cipher Suite: " + ((HttpsURLConnection) urlc).getCipherSuite()); Certificate[] certs = ((HttpsURLConnection) urlc).getServerCertificates(); for (int i = 0; i < certs.length; i++) { System.out.println(certs[0]); } in.close(); } catch (SSLException e) { if (in != null) in.close(); throw e; } System.out.println("Client reports: SUCCESS"); }
/** * Establishes session with the virtual center server. * * @throws Exception the exception */ private static void connect() throws Exception { HostnameVerifier hv = new HostnameVerifier() { public boolean verify(String urlHostName, SSLSession session) { return true; } }; trustAllHttpsCertificates(); HttpsURLConnection.setDefaultHostnameVerifier(hv); SVC_INST_REF.setType(SVC_INST_NAME); SVC_INST_REF.setValue(SVC_INST_NAME); vimService = new VimService(); vimPort = vimService.getVimPort(); Map<String, Object> ctxt = ((BindingProvider) vimPort).getRequestContext(); ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url); ctxt.put(BindingProvider.SESSION_MAINTAIN_PROPERTY, true); serviceContent = vimPort.retrieveServiceContent(SVC_INST_REF); boolean isVCApi = checkApiType(serviceContent); if (!isVCApi) { System.out.println("Virtual Center is not supported"); System.exit(0); } headers = (Map) ((BindingProvider) vimPort) .getResponseContext() .get(MessageContext.HTTP_RESPONSE_HEADERS); vimPort.login(serviceContent.getSessionManager(), userName, password, null); isConnected = true; rootRef = serviceContent.getRootFolder(); propCollectorRef = serviceContent.getPropertyCollector(); }