1
2
3
4
5
6
7
8
9
10
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 import com.eviware.soapui.support.components.BrowserComponent;
36
37 /***
38 * Utilities for setting proxy-servers correctly
39 *
40 * @author ole.matzura
41 */
42
43 public class ProxyUtils
44 {
45 private static boolean proxyEnabled = true;
46
47 public static HostConfiguration initProxySettings( Settings settings, HttpState httpState,
48 HostConfiguration hostConfiguration, String urlString, PropertyExpansionContext context )
49 {
50 boolean enabled = proxyEnabled;
51
52
53 String proxyHost = System.getProperty( "http.proxyHost" );
54 String proxyPort = System.getProperty( "http.proxyPort" );
55 if( proxyHost == null && enabled )
56 proxyHost = PropertyExpander.expandProperties( context, settings.getString( ProxySettings.HOST, "" ) );
57 if( proxyPort == null && proxyHost != null && enabled )
58 proxyPort = PropertyExpander.expandProperties( context, settings.getString( ProxySettings.PORT, "" ) );
59
60 if( !StringUtils.isNullOrEmpty( proxyHost ) && !StringUtils.isNullOrEmpty( proxyPort ) )
61 {
62
63 String[] excludes = PropertyExpander.expandProperties( context,
64 settings.getString( ProxySettings.EXCLUDES, "" ) ).split( "," );
65
66 try
67 {
68 URL url = new URL( urlString );
69
70 if( !excludes( excludes, url.getHost(), url.getPort() ) )
71 {
72 hostConfiguration.setProxy( proxyHost, Integer.parseInt( proxyPort ) );
73
74 String proxyUsername = PropertyExpander.expandProperties( context, settings.getString(
75 ProxySettings.USERNAME, null ) );
76 String proxyPassword = PropertyExpander.expandProperties( context, settings.getString(
77 ProxySettings.PASSWORD, null ) );
78
79 if( proxyUsername != null && proxyPassword != null )
80 {
81 Credentials proxyCreds = new UsernamePasswordCredentials( proxyUsername, proxyPassword == null ? ""
82 : proxyPassword );
83
84
85 int ix = proxyUsername.indexOf( '//' );
86 if( ix > 0 )
87 {
88 String domain = proxyUsername.substring( 0, ix );
89 if( proxyUsername.length() > ix + 1 )
90 {
91 String user = proxyUsername.substring( ix + 1 );
92 proxyCreds = new NTCredentials( user, proxyPassword, proxyHost, domain );
93 }
94 }
95
96 httpState.setProxyCredentials( AuthScope.ANY, proxyCreds );
97 }
98 }
99 }
100 catch( MalformedURLException e )
101 {
102 SoapUI.logError( e );
103 }
104 }
105
106 return hostConfiguration;
107 }
108
109 public static boolean excludes( String[] excludes, String proxyHost, int proxyPort )
110 {
111 for( int c = 0; c < excludes.length; c++ )
112 {
113 String exclude = excludes[c].trim();
114 if( exclude.length() == 0 )
115 continue;
116
117
118 int ix = exclude.indexOf( ':' );
119
120 if( ix >= 0 && exclude.length() > ix + 1 )
121 {
122 String excludePort = exclude.substring( ix + 1 );
123 if( proxyPort != -1 && excludePort.equals( String.valueOf( proxyPort ) ) )
124 {
125 exclude = exclude.substring( 0, ix );
126 }
127 else
128 {
129 continue;
130 }
131 }
132
133
134
135
136
137
138 String excludeIp = exclude.indexOf( '*' ) >= 0 ? exclude : nslookup( exclude, true );
139 String ip = nslookup( proxyHost, true );
140 Pattern pattern = Pattern.compile( excludeIp );
141 Matcher matcher = pattern.matcher( ip );
142 Matcher matcher2 = pattern.matcher( proxyHost );
143 if( matcher.find() || matcher2.find() )
144 return true;
145 }
146
147 return false;
148 }
149
150 private static String nslookup( String s, boolean ip )
151 {
152
153 InetAddress host;
154 String address;
155
156
157 try
158 {
159 host = InetAddress.getByName( s );
160 if( ip )
161 address = host.getHostAddress();
162 else
163 address = host.getHostName();
164 }
165 catch( UnknownHostException ue )
166 {
167 return s;
168 }
169
170 return address;
171
172 }
173
174 public static boolean isProxyEnabled()
175 {
176 return proxyEnabled;
177 }
178
179 public static void setProxyEnabled( boolean proxyEnabled )
180 {
181 ProxyUtils.proxyEnabled = proxyEnabled;
182 if( !SoapUI.isJXBrowserDisabled() )
183 BrowserComponent.updateProxy( proxyEnabled );
184 }
185
186 }