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