View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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 org.apache.commons.httpclient.contrib.ssl.EasySSLProtocolSocketFactory;
23  import org.apache.commons.httpclient.params.HttpConnectionParams;
24  import org.apache.commons.ssl.KeyMaterial;
25  
26  import com.eviware.soapui.SoapUI;
27  import com.eviware.soapui.support.StringUtils;
28  
29  public class SoapUIEasySSLProtocolSocketFactory extends EasySSLProtocolSocketFactory
30  {
31  	private Map<String,EasySSLProtocolSocketFactory> factoryMap = new HashMap<String, EasySSLProtocolSocketFactory>();
32  	
33  	public SoapUIEasySSLProtocolSocketFactory() throws GeneralSecurityException, IOException
34  	{
35  		super();
36  	}
37  
38  	@Override
39  	public Socket createSocket( String host, int port, InetAddress localAddress, int localPort, HttpConnectionParams params ) throws IOException
40  	{
41  		String sslConfig = ( String ) params.getParameter( SoapUIHostConfiguration.SOAPUI_SSL_CONFIG );
42  
43  		if( StringUtils.isNullOrEmpty( sslConfig ))
44  			return super.createSocket( host, port, localAddress, localPort, params );
45  		
46  		EasySSLProtocolSocketFactory factory = factoryMap.get( sslConfig );
47  		if( factory != null )
48  			return factory.createSocket( host, port, localAddress, localPort, params );
49  		
50  		try
51  		{
52  			// try to create new factory for specified config
53  			factory = new EasySSLProtocolSocketFactory();
54  			
55  			int ix = sslConfig.lastIndexOf( ' ' );
56  			String keyStore = sslConfig.substring( 0, ix );
57  			String pwd = sslConfig.substring( ix+1 );
58  			
59  			factory.setKeyMaterial( new KeyMaterial( keyStore, pwd.toCharArray() ) );
60  			factoryMap.put( sslConfig, factory );
61  			
62  			return factory.createSocket( host, port, localAddress, localPort, params );
63  		}
64  		catch( Exception gse )
65  		{
66  			SoapUI.logError( gse );
67  			return super.createSocket( host, port, localAddress, localPort, params );
68  		}
69  	}
70  }