View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.InetAddress;
16  import java.net.MalformedURLException;
17  import java.net.URL;
18  import java.net.UnknownHostException;
19  import java.util.regex.Matcher;
20  import java.util.regex.Pattern;
21  
22  import org.apache.commons.httpclient.Credentials;
23  import org.apache.commons.httpclient.HostConfiguration;
24  import org.apache.commons.httpclient.HttpState;
25  import org.apache.commons.httpclient.NTCredentials;
26  import org.apache.commons.httpclient.UsernamePasswordCredentials;
27  import org.apache.commons.httpclient.auth.AuthScope;
28  
29  import com.eviware.soapui.SoapUI;
30  import com.eviware.soapui.model.propertyexpansion.PropertyExpander;
31  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContext;
32  import com.eviware.soapui.model.settings.Settings;
33  import com.eviware.soapui.settings.ProxySettings;
34  import com.eviware.soapui.support.StringUtils;
35  
36  /***
37   * Utilities for setting proxy-servers correctly
38   * 
39   * @author ole.matzura
40   */
41  
42  public class ProxyUtils
43  {
44  	private static boolean proxyEnabled;
45  
46  	public static HostConfiguration initProxySettings(Settings settings, HttpState httpState,
47  			HostConfiguration hostConfiguration, String urlString, PropertyExpansionContext context)
48  	{
49  		if (ProxyUtils.isProxyEnabled())
50  		{
51  			// check system properties first
52  			String proxyHost = System.getProperty("http.proxyHost");
53  			String proxyPort = System.getProperty("http.proxyPort");
54  			if (proxyHost == null)
55  				proxyHost = PropertyExpander.expandProperties(context, settings.getString(ProxySettings.HOST, ""));
56  			if (proxyPort == null)
57  				proxyPort = PropertyExpander.expandProperties(context, settings.getString(ProxySettings.PORT, ""));
58  			if (!StringUtils.isNullOrEmpty(proxyHost) && !StringUtils.isNullOrEmpty(proxyPort))
59  			{
60  				// check excludes
61  				String[] excludes = PropertyExpander.expandProperties(context,
62  						settings.getString(ProxySettings.EXCLUDES, "")).split(",");
63  
64  				try
65  				{
66  					URL url = new URL(urlString);
67  
68  					if (!excludes(excludes, url.getHost(), url.getPort()))
69  					{
70  						hostConfiguration.setProxy(proxyHost, Integer.parseInt(proxyPort));
71  
72  						String proxyUsername = PropertyExpander.expandProperties(context, settings.getString(
73  								ProxySettings.USERNAME, null));
74  						String proxyPassword = PropertyExpander.expandProperties(context, settings.getString(
75  								ProxySettings.PASSWORD, null));
76  
77  						if (proxyUsername != null && proxyPassword != null)
78  						{
79  							Credentials proxyCreds = new UsernamePasswordCredentials(proxyUsername, proxyPassword == null ? ""
80  									: proxyPassword);
81  
82  							// check for nt-username
83  							int ix = proxyUsername.indexOf('//');
84  							if (ix > 0)
85  							{
86  								String domain = proxyUsername.substring(0, ix);
87  								if (proxyUsername.length() > ix + 1)
88  								{
89  									String user = proxyUsername.substring(ix + 1);
90  									proxyCreds = new NTCredentials(user, proxyPassword, proxyHost, domain);
91  								}
92  							}
93  
94  							httpState.setProxyCredentials(AuthScope.ANY, proxyCreds);
95  						}
96  					}
97  				}
98  				catch (MalformedURLException e)
99  				{
100 					SoapUI.logError(e);
101 				}
102 			}
103 		}
104 		return hostConfiguration;
105 	}
106 
107 	public static boolean excludes(String[] excludes, String proxyHost, int proxyPort)
108 	{
109 		for (int c = 0; c < excludes.length; c++)
110 		{
111 			String exclude = excludes[c].trim();
112 			if (exclude.length() == 0)
113 				continue;
114 
115 			// check for port
116 			int ix = exclude.indexOf(':');
117 
118 			if (ix >= 0 && exclude.length() > ix + 1)
119 			{
120 				String excludePort = exclude.substring(ix + 1);
121 				if (proxyPort != -1 && excludePort.equals(String.valueOf(proxyPort)))
122 				{
123 					exclude = exclude.substring(0, ix);
124 				}
125 				else
126 				{
127 					continue;
128 				}
129 			}
130 
131 			/*
132 			 * This will exclude addresses with wildcard *, too.
133 			 */
134 			// if( proxyHost.endsWith( exclude ) )
135 			// return true;
136 			String excludeIp = exclude.indexOf('*') >= 0 ? exclude : nslookup(exclude, true);
137 			String ip = nslookup(proxyHost, true);
138 			Pattern pattern = Pattern.compile(excludeIp);
139 			Matcher matcher = pattern.matcher(ip);
140 			Matcher matcher2 = pattern.matcher(proxyHost);
141 			if (matcher.find() || matcher2.find())
142 				return true;
143 		}
144 
145 		return false;
146 	}
147 
148 	private static String nslookup(String s, boolean ip)
149 	{
150 
151 		InetAddress host;
152 		String address;
153 
154 		// get the bytes of the IP address
155 		try
156 		{
157 			host = InetAddress.getByName(s);
158 			if (ip)
159 				address = host.getHostAddress();
160 			else
161 				address = host.getHostName();
162 		}
163 		catch (UnknownHostException ue)
164 		{
165 			return s; // no host
166 		}
167 
168 		return address;
169 
170 	} // end lookup
171 
172 	public static boolean isProxyEnabled()
173 	{
174 		return proxyEnabled;
175 	}
176 
177 	public static void setProxyEnabled(boolean proxyEnabled)
178 	{
179 		ProxyUtils.proxyEnabled = proxyEnabled;
180 	}
181 
182 }