View Javadoc

1   /*
2    *  soapUI, copyright (C) 2005 Ole Matzura / 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.net.MalformedURLException;
16  import java.net.URL;
17  
18  import org.apache.commons.httpclient.Credentials;
19  import org.apache.commons.httpclient.HostConfiguration;
20  import org.apache.commons.httpclient.HttpState;
21  import org.apache.commons.httpclient.UsernamePasswordCredentials;
22  import org.apache.commons.httpclient.auth.AuthScope;
23  
24  import com.eviware.soapui.model.settings.Settings;
25  import com.eviware.soapui.settings.ProxySettings;
26  
27  public class ProxyUtils
28  {
29  	public static HostConfiguration initProxySettings( Settings settings, HttpState httpState, HostConfiguration hostConfiguration, 
30  				String urlString )
31  	{
32  		//	init proxy settings
33  		if( hostConfiguration == null )
34  			hostConfiguration = new HostConfiguration();
35  		
36  		// check system properties first
37  		String proxyHost = System.getProperty( "http.proxyHost" );
38  	   String proxyPort = System.getProperty( "http.proxyPort" );
39  	   
40  	   if( proxyHost == null )
41  	   	proxyHost = settings.getString(ProxySettings.HOST, null);
42  	   
43  	   if( proxyPort == null )
44  	   	proxyPort = settings.getString(ProxySettings.PORT, null);
45  	
46  		if (proxyHost != null && proxyHost.length() > 0 && proxyPort != null && proxyPort.length() > 0)
47  		{
48  			// check excludes
49  			String[] excludes = settings.getString(ProxySettings.EXCLUDES, "").split( "," );
50  			
51  			try
52  			{
53  				URL url = new URL( urlString );
54  				
55  				if( !excludes( excludes, url.getHost(), url.getPort() ) )
56  				{
57  					hostConfiguration.setProxy(proxyHost, Integer.parseInt(proxyPort));
58  		
59  					String proxyUsername = settings.getString(ProxySettings.USERNAME, null);
60  					String proxyPassword = settings.getString(ProxySettings.PASSWORD, null);
61  		
62  					if (proxyUsername != null && proxyPassword != null )
63  					{
64  						Credentials defaultcreds = new UsernamePasswordCredentials(proxyUsername, proxyPassword);
65  						httpState.setProxyCredentials(AuthScope.ANY, defaultcreds);
66  					}
67  				}
68  			}
69  			catch( MalformedURLException e )
70  			{
71  				e.printStackTrace();
72  			}
73  			
74  		}
75  		
76  		return hostConfiguration;
77  	}
78  	
79  	public static boolean excludes( String [] excludes, String proxyHost, int proxyPort )
80  	{
81  		for( int c = 0; c < excludes.length; c++ )
82  		{
83  			String exclude = excludes[c].trim();
84  			if( exclude.length() == 0 )
85  				continue;
86  			
87  			// check for port
88  			int ix = exclude.indexOf( ':' );
89  			
90  			if( ix >= 0 && exclude.length() > ix+1 )
91  			{
92  				String excludePort = exclude.substring( ix+1 );
93  				if( proxyPort != -1 && excludePort.equals( String.valueOf( proxyPort )))
94  				{
95  					exclude = exclude.substring( 0, ix );
96  				}
97  				else
98  				{
99  					continue;
100 				}
101 			}
102 			
103 			if( proxyHost.endsWith( exclude ))
104 				return true;
105 		}
106 		
107 		return false;
108 	}
109 }