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