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.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  		//	init proxy settings
    40  		if( hostConfiguration == null )
    41  			hostConfiguration = new HostConfiguration();
    42
    43  		// check system properties first
    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  			// check excludes
    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  			// check for port
    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 }