[i444] Support nonProxyHosts parameter in settings.xml

This commit is contained in:
Christian Galsterer
2016-01-25 20:30:37 +01:00
parent b45f9f514b
commit d023b2b2ff
3 changed files with 26 additions and 1 deletions

View File

@@ -165,6 +165,10 @@ public final class Settings {
* The properties key for the proxy password.
*/
public static final String PROXY_PASSWORD = "proxy.password";
/**
* The properties key for the non proxy hosts.
*/
public static final String PROXY_NON_PROXY_HOSTS = "proxy.nonproxyhosts";
/**
* The properties key for the connection timeout.
*/

View File

@@ -53,13 +53,15 @@ public final class URLConnectionFactory {
public static HttpURLConnection createHttpURLConnection(URL url) throws URLConnectionFailureException {
HttpURLConnection conn = null;
final String proxyUrl = Settings.getString(Settings.KEYS.PROXY_SERVER);
try {
if (proxyUrl != null) {
if (proxyUrl != null && !skipProxy(url)) {
final int proxyPort = Settings.getInt(Settings.KEYS.PROXY_PORT);
final SocketAddress address = new InetSocketAddress(proxyUrl, proxyPort);
final String username = Settings.getString(Settings.KEYS.PROXY_USERNAME);
final String password = Settings.getString(Settings.KEYS.PROXY_PASSWORD);
if (username != null && password != null) {
final Authenticator auth = new Authenticator() {
@Override
@@ -94,6 +96,24 @@ public final class URLConnectionFactory {
return conn;
}
/**
* Checks of for the given URL the proxy shall be used or not checking the nonProxyHosts configuration.
* @param url The URL to check.
* @return If the proxy shall be skip for the given URL or not.
*/
private static boolean skipProxy(URL url) {
boolean skip = false;
final String nonProxySettings = Settings.getString(Settings.KEYS.PROXY_NON_PROXY_HOSTS);
String[] nonProxyHosts = nonProxySettings.split(",");
for (int i = 0; i < nonProxyHosts.length; i++) {
if (url.getHost().matches(nonProxyHosts[i])) {
skip = true;
break;
}
}
return skip;
}
/**
* Utility method to create an HttpURLConnection. The use of a proxy here is optional as there may be cases where a proxy is
* configured but we don't want to use it (for example, if there's an internal repository configured)