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