View Javadoc

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