View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.impl.wsdl.support.http;
14  
15  import java.io.IOException;
16  import java.net.InetAddress;
17  import java.net.Socket;
18  import java.security.GeneralSecurityException;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import javax.net.ssl.SSLSocket;
23  
24  import org.apache.commons.httpclient.contrib.ssl.EasySSLProtocolSocketFactory;
25  import org.apache.commons.httpclient.params.HttpConnectionParams;
26  import org.apache.commons.ssl.KeyMaterial;
27  
28  import com.eviware.soapui.SoapUI;
29  import com.eviware.soapui.support.StringUtils;
30  
31  public class SoapUIEasySSLProtocolSocketFactory extends EasySSLProtocolSocketFactory
32  {
33  	private Map<String, EasySSLProtocolSocketFactory> factoryMap = new HashMap<String, EasySSLProtocolSocketFactory>();
34  
35  	public SoapUIEasySSLProtocolSocketFactory() throws GeneralSecurityException, IOException
36  	{
37  		super();
38  	}
39  
40  	@Override
41  	public Socket createSocket( String host, int port, InetAddress localAddress, int localPort,
42  			HttpConnectionParams params ) throws IOException
43  	{
44  		String sslConfig = ( String )params.getParameter( SoapUIHostConfiguration.SOAPUI_SSL_CONFIG );
45  
46  		if( StringUtils.isNullOrEmpty( sslConfig ) )
47  		{
48  			return enableSocket( ( SSLSocket )super.createSocket( host, port, localAddress, localPort, params ) );
49  		}
50  
51  		EasySSLProtocolSocketFactory factory = factoryMap.get( sslConfig );
52  		if( factory != null )
53  		{
54  			return enableSocket( ( SSLSocket )factory.createSocket( host, port, localAddress, localPort, params ) );
55  		}
56  		try
57  		{
58  			// try to create new factory for specified config
59  			factory = new EasySSLProtocolSocketFactory();
60  
61  			int ix = sslConfig.lastIndexOf( ' ' );
62  			String keyStore = sslConfig.substring( 0, ix );
63  			String pwd = sslConfig.substring( ix + 1 );
64  
65  			factory.setKeyMaterial( new KeyMaterial( keyStore, pwd.toCharArray() ) );
66  			factoryMap.put( sslConfig, factory );
67  
68  			return enableSocket( ( SSLSocket )factory.createSocket( host, port, localAddress, localPort, params ) );
69  		}
70  		catch( Exception gse )
71  		{
72  			SoapUI.logError( gse );
73  			return enableSocket( ( SSLSocket )super.createSocket( host, port, localAddress, localPort, params ) );
74  		}
75  		}
76  
77  	private Socket enableSocket( SSLSocket socket )
78  	{
79  		socket.setEnabledProtocols( socket.getSupportedProtocols() );
80  		socket.setEnabledCipherSuites( socket.getSupportedCipherSuites() );
81  
82  		return socket;
83  	}
84  }