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.util.HashMap;
19  import java.util.Map;
20  
21  import javax.net.ssl.SSLSocket;
22  
23  import org.apache.commons.httpclient.contrib.ssl.EasySSLProtocolSocketFactory;
24  import org.apache.commons.httpclient.params.HttpConnectionParams;
25  import org.apache.commons.ssl.KeyMaterial;
26  
27  import com.eviware.soapui.SoapUI;
28  import com.eviware.soapui.support.StringUtils;
29  
30  public class SoapUIEasySSLProtocolSocketFactory extends EasySSLProtocolSocketFactory
31  {
32  	private Map<String, EasySSLProtocolSocketFactory> factoryMap = new HashMap<String, EasySSLProtocolSocketFactory>();
33  
34  	public SoapUIEasySSLProtocolSocketFactory() throws Exception
35  	{
36  		super();
37  	}
38  
39  	@Override
40  	public Socket createSocket( String host, int port, InetAddress localAddress, int localPort,
41  			HttpConnectionParams params ) throws IOException
42  	{
43  		String sslConfig = ( String )params.getParameter( SoapUIHostConfiguration.SOAPUI_SSL_CONFIG );
44  
45  		if( StringUtils.isNullOrEmpty( sslConfig ) )
46  		{
47  			return enableSocket( ( SSLSocket )super.createSocket( host, port, localAddress, localPort, params ) );
48  		}
49  
50  		EasySSLProtocolSocketFactory factory = factoryMap.get( sslConfig );
51  		if( factory != null )
52  		{
53  			return enableSocket( ( SSLSocket )factory.createSocket( host, port, localAddress, localPort, params ) );
54  		}
55  		try
56  		{
57  			// try to create new factory for specified config
58  			factory = new EasySSLProtocolSocketFactory();
59  
60  			int ix = sslConfig.lastIndexOf( ' ' );
61  			String keyStore = sslConfig.substring( 0, ix );
62  			String pwd = sslConfig.substring( ix + 1 );
63  
64  			factory.setKeyMaterial( new KeyMaterial( keyStore, pwd.toCharArray() ) );
65  			factoryMap.put( sslConfig, factory );
66  
67  			return enableSocket( ( SSLSocket )factory.createSocket( host, port, localAddress, localPort, params ) );
68  		}
69  		catch( Exception gse )
70  		{
71  			SoapUI.logError( gse );
72  			return enableSocket( ( SSLSocket )super.createSocket( host, port, localAddress, localPort, params ) );
73  		}
74  	}
75  
76  	private Socket enableSocket( SSLSocket socket )
77  	{
78  		socket.getSession().invalidate();
79  
80  		String protocols = System.getProperty( "soapui.https.protocols" );
81  		String ciphers = System.getProperty( "soapui.https.ciphers" );
82  
83  		if( StringUtils.hasContent( protocols ) )
84  		{
85  			socket.setEnabledProtocols( protocols.split( "," ) );
86  		}
87  		else
88  		{
89  			socket.setEnabledProtocols( socket.getSupportedProtocols() );
90  		}
91  
92  		if( StringUtils.hasContent( ciphers ) )
93  		{
94  			socket.setEnabledCipherSuites( ciphers.split( "," ) );
95  		}
96  		else
97  		{
98  			socket.setEnabledCipherSuites( socket.getSupportedCipherSuites() );
99  		}
100 		return socket;
101 	}
102 }