001: /*
002: * soapUI, copyright (C) 2005 Ole Matzura / eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.impl.wsdl.support.http;
014:
015: import java.net.MalformedURLException;
016: import java.net.URL;
017:
018: import org.apache.commons.httpclient.Credentials;
019: import org.apache.commons.httpclient.HostConfiguration;
020: import org.apache.commons.httpclient.HttpState;
021: import org.apache.commons.httpclient.UsernamePasswordCredentials;
022: import org.apache.commons.httpclient.auth.AuthScope;
023:
024: import com.eviware.soapui.SoapUI;
025: import com.eviware.soapui.model.settings.Settings;
026: import com.eviware.soapui.settings.ProxySettings;
027:
028: /**
029: * Utilities for setting proxy-servers corectly
030: *
031: * @author ole.matzura
032: */
033:
034: public class ProxyUtils {
035: public static HostConfiguration initProxySettings(
036: Settings settings, HttpState httpState,
037: HostConfiguration hostConfiguration, String urlString) {
038: // init proxy settings
039: if (hostConfiguration == null)
040: hostConfiguration = new HostConfiguration();
041:
042: // check system properties first
043: String proxyHost = System.getProperty("http.proxyHost");
044: String proxyPort = System.getProperty("http.proxyPort");
045:
046: if (proxyHost == null)
047: proxyHost = settings.getString(ProxySettings.HOST, null);
048:
049: if (proxyPort == null)
050: proxyPort = settings.getString(ProxySettings.PORT, null);
051:
052: if (proxyHost != null && proxyHost.length() > 0
053: && proxyPort != null && proxyPort.length() > 0) {
054: // check excludes
055: String[] excludes = settings.getString(
056: ProxySettings.EXCLUDES, "").split(",");
057:
058: try {
059: URL url = new URL(urlString);
060:
061: if (!excludes(excludes, url.getHost(), url.getPort())) {
062: hostConfiguration.setProxy(proxyHost, Integer
063: .parseInt(proxyPort));
064:
065: String proxyUsername = settings.getString(
066: ProxySettings.USERNAME, null);
067: String proxyPassword = settings.getString(
068: ProxySettings.PASSWORD, null);
069:
070: if (proxyUsername != null && proxyPassword != null) {
071: Credentials defaultcreds = new UsernamePasswordCredentials(
072: proxyUsername, proxyPassword);
073: httpState.setProxyCredentials(AuthScope.ANY,
074: defaultcreds);
075: }
076: }
077: } catch (MalformedURLException e) {
078: SoapUI.logError(e);
079: }
080:
081: }
082:
083: return hostConfiguration;
084: }
085:
086: public static boolean excludes(String[] excludes, String proxyHost,
087: int proxyPort) {
088: for (int c = 0; c < excludes.length; c++) {
089: String exclude = excludes[c].trim();
090: if (exclude.length() == 0)
091: continue;
092:
093: // check for port
094: int ix = exclude.indexOf(':');
095:
096: if (ix >= 0 && exclude.length() > ix + 1) {
097: String excludePort = exclude.substring(ix + 1);
098: if (proxyPort != -1
099: && excludePort
100: .equals(String.valueOf(proxyPort))) {
101: exclude = exclude.substring(0, ix);
102: } else {
103: continue;
104: }
105: }
106:
107: if (proxyHost.endsWith(exclude))
108: return true;
109: }
110:
111: return false;
112: }
113: }
|