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