Skip to content

Commit

Permalink
Update WSClient for yavijava#38 and yavijava#123
Browse files Browse the repository at this point in the history
Attempt to fix issue yavijava#38 and yavijava#123 in order to reuse the SSLSocketFactory

Fixes yavijava#38
Fixes yavijava#123

Added test for the SSLSocketFactory initialization
  • Loading branch information
BigBeaule authored and Michael Rice committed Aug 19, 2015
1 parent 4df0869 commit 1ace5a2
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 24 deletions.
63 changes: 59 additions & 4 deletions src/intTest/java/com/vmware/vim25/ws/WSClientIntTest.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
package com.vmware.vim25.ws;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.Calendar;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLSocketFactory;

import com.vmware.vim25.*;
import org.apache.log4j.Logger;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.utility.LoadVcenterProps;
import com.vmware.vim25.ManagedObjectReference;
import com.vmware.vim25.ObjectContent;
import com.vmware.vim25.ObjectSpec;
import com.vmware.vim25.PropertyFilterSpec;
import com.vmware.vim25.PropertySpec;
import com.vmware.vim25.SelectionSpec;
import com.vmware.vim25.mo.ServiceInstance;
import com.vmware.vim25.mo.util.PropertyCollectorUtil;

import javax.net.ssl.HttpsURLConnection;

public class WSClientIntTest {

private static final Logger log = Logger.getLogger(WSClientIntTest.class);
Expand Down Expand Up @@ -46,7 +53,6 @@ public void setUp() throws Exception {
} catch (MalformedURLException e) {
e.printStackTrace();
}

if(si != null) {
wsClient = new WSClient(LoadVcenterProps.url, true);

Expand Down Expand Up @@ -140,6 +146,29 @@ public void testReqMarshall() {
}

/**
* This test will confirm that the internal SSL socket factory is initiate only once in the WSClient (Issue #38).
*/
@Test
public void testSSLSocketFactoryInitialization() throws Exception {
CustomWSClient client = new CustomWSClient(LoadVcenterProps.url, true);
Assert.assertEquals(1, createdSSLFactory);

try {
client.invoke("RetrieveProperties", buildGetHostsArgs(), "ObjectContent[]");
} catch (RemoteException e) {
}

Assert.assertEquals(1, createdSSLFactory);

try {
client.invoke("RetrieveProperties", buildGetHostsArgs(), "ObjectContent[]");
} catch (RemoteException e) {
}

Assert.assertEquals(1, createdSSLFactory);
}

/**
* This method will build the request payload.
*
* @return Argument[]
Expand Down Expand Up @@ -179,4 +208,30 @@ private Argument[] buildGetHostsArgs() {

return paras;
}

/**
* Counter for created factory in {@link CustomWSClient}.
*/
private int createdSSLFactory = 0;

/**
* This extension of the WSClient will create count the number of time the {@link SSLSocketFactory} was created.
*
* @author Francis Beaulé
*
*/
private class CustomWSClient extends WSClient {
public CustomWSClient(String serverUrl, boolean ignoreCert) throws MalformedURLException, IOException {
super(serverUrl, ignoreCert);
}

/**
* {@inheritDoc}
*/
@Override
protected SSLSocketFactory getSocketFactory(boolean ignoreCert) throws IOException {
++createdSSLFactory;
return super.getSocketFactory(ignoreCert);
}
}
}
41 changes: 21 additions & 20 deletions src/main/java/com/vmware/vim25/ws/WSClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWIS
import org.apache.log4j.Logger;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
Expand All @@ -52,22 +53,22 @@ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWIS
public class WSClient extends SoapClient {

private static final Logger log = Logger.getLogger(WSClient.class);
private boolean ignoreCert = false;
private final SSLSocketFactory sslSocketFactory;

XmlGen xmlGen = new XmlGenDom();

public WSClient(String serverUrl) throws MalformedURLException {
public WSClient(String serverUrl) throws MalformedURLException, IOException {
this(serverUrl, true);
}

public WSClient(String serverUrl, boolean ignoreCert) throws MalformedURLException {
public WSClient(String serverUrl, boolean ignoreCert) throws MalformedURLException, IOException {
if (serverUrl.endsWith("/")) {
serverUrl = serverUrl.substring(0, serverUrl.length() - 1);
}
log.trace("Creating WSClient to server URL: " + serverUrl);
log.trace("Ignore ssl: " + ignoreCert);
this.baseUrl = new URL(serverUrl);
this.ignoreCert = ignoreCert;
this.sslSocketFactory = getSocketFactory(ignoreCert);
}

public Object invoke(String methodName, Argument[] paras, String returnType) throws RemoteException {
Expand Down Expand Up @@ -115,22 +116,9 @@ public StringBuffer invokeAsString(String methodName, Argument[] paras) throws R
}

protected InputStream post(String soapMsg) throws IOException {
HttpURLConnection postCon;

if(ignoreCert && trustManager != null) {
log.warn("The option to ignore certs has been set along with a provided trust manager. This is not a valid scenario and the trust manager will be ignored.");
}

if (baseUrl.getProtocol().equalsIgnoreCase("https") && ignoreCert) {
postCon = (HttpsURLConnection) baseUrl.openConnection();
((HttpsURLConnection) postCon).setSSLSocketFactory(TrustAllSSL.getTrustContext().getSocketFactory());
} else if(baseUrl.getProtocol().equalsIgnoreCase("https") && !ignoreCert) {
postCon = (HttpsURLConnection) baseUrl.openConnection();
if(trustManager != null) {
((HttpsURLConnection) postCon).setSSLSocketFactory(CustomSSLTrustContextCreator.getTrustContext(trustManager).getSocketFactory());
}
} else {
postCon = (HttpURLConnection) baseUrl.openConnection();
HttpURLConnection postCon = (HttpURLConnection) baseUrl.openConnection();;
if (sslSocketFactory != null) {
((HttpsURLConnection) postCon).setSSLSocketFactory(sslSocketFactory);
}

log.trace("POST: " + soapAction);
Expand Down Expand Up @@ -206,4 +194,17 @@ protected OutputStreamWriter createOutputStreamWriter(OutputStream os) throws Un
return new OutputStreamWriter(os, "UTF8");
}

protected SSLSocketFactory getSocketFactory(boolean ignoreCert) throws IOException {
try {
return ignoreCert ? TrustAllSSL.getTrustContext().getSocketFactory() : null;
}
catch (NoSuchAlgorithmException e) {
log.debug("Unable to find suitable algorithm while attempting to communicate with remote server.", e);
throw new IOException("Unable to find suitable algorithm while attempting to communicate with remote server.", e);
}
catch (KeyManagementException e) {
log.debug("Key Management exception while attempting to communicate with remote server.", e);
throw new IOException("Key Management exception while attempting to communicate with remote server.", e);
}
}
}

0 comments on commit 1ace5a2

Please sign in to comment.