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