From b1641d24dad8f1d79b282b4bafff9b52c21986b1 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 18 Nov 2014 15:48:30 +0100 Subject: Reset proxy settings furst when configuring proxy --- .../org/openslx/network/ProxyConfiguration.java | 18 +++++++--- .../java/org/openslx/network/ProxyProperties.java | 38 ++++++++++++++-------- 2 files changed, 38 insertions(+), 18 deletions(-) (limited to 'src/main/java/org/openslx/network') diff --git a/src/main/java/org/openslx/network/ProxyConfiguration.java b/src/main/java/org/openslx/network/ProxyConfiguration.java index cfdcdb9..e4956fa 100644 --- a/src/main/java/org/openslx/network/ProxyConfiguration.java +++ b/src/main/java/org/openslx/network/ProxyConfiguration.java @@ -22,9 +22,14 @@ public class ProxyConfiguration public static void configProxy() { + // Reset proxy settings first + ProxySelector.setDefault( null ); + Authenticator.setDefault( null ); + // Configuring proxy settings. First read options from config file. String proxyConfiguration = ProxyProperties.getProxyConf(); - if ( ( proxyConfiguration.equals( "AUTO" ) ) || ( proxyConfiguration.equals( "" ) ) ) { + + if ( proxyConfiguration.equals( "AUTO" ) || proxyConfiguration.isEmpty() ) { log.info( "Configuring proxy settings automatically..." ); // Configuring proxy settings automatically. WpadProxySearchStrategy wPSS = new WpadProxySearchStrategy(); @@ -34,10 +39,13 @@ public class ProxyConfiguration } catch ( ProxyException e ) { log.error( "Setting proxy configuration automatically failed.", e ); } - } else if ( proxyConfiguration.equals( "YES" ) ) { + return; + } + + if ( proxyConfiguration.equals( "YES" ) ) { // Take the proxy settings from config file. // First check if one of the following necessary options might not be set. - if ( ProxyProperties.checkProxySettings() ) { + if ( ProxyProperties.hasProxyAddress() ) { String proxyAddress = ProxyProperties.getProxyAddress(); int proxyPort = ProxyProperties.getProxyPort(); @@ -46,7 +54,7 @@ public class ProxyConfiguration StaticProxySelector sPS = new StaticProxySelector( proxy ); ProxySelector.setDefault( sPS ); - if ( ! ( ProxyProperties.getProxyUsername().equals( "" ) ) && ! ( ProxyProperties.getProxyPassword().equals( "" ) ) ) { + if ( !ProxyProperties.hasProxyCredentials() ) { log.info( "Configuring proxy settings manually WITH authentication..." ); // use Proxy with authentication. String proxyUname = ProxyProperties.getProxyUsername(); @@ -58,5 +66,7 @@ public class ProxyConfiguration } } } + } + } diff --git a/src/main/java/org/openslx/network/ProxyProperties.java b/src/main/java/org/openslx/network/ProxyProperties.java index 6675f3a..7e03023 100644 --- a/src/main/java/org/openslx/network/ProxyProperties.java +++ b/src/main/java/org/openslx/network/ProxyProperties.java @@ -13,19 +13,19 @@ public class ProxyProperties { private static Logger log = Logger.getLogger( ProxyProperties.class ); private static final Properties properties = new Properties(); - + // Getting the proxy settings from config file stored in // "/opt/openslx/proxy/conf". public static String getProxyConf() { return properties.getProperty( "PROXY_CONF" ); } - + public static String getProxyAddress() { return properties.getProperty( "PROXY_ADDR" ); } - + public static String getProxyUsername() { return properties.getProperty( "PROXY_USERNAME" ); @@ -35,7 +35,7 @@ public class ProxyProperties { return properties.getProperty( "PROXY_PASSWORD" ); } - + // Integers // public static int getProxyPort() { @@ -50,24 +50,34 @@ public class ProxyProperties try { // Load all entries of the config file into properties stream = new InputStreamReader( - new FileInputStream("/opt/openslx/proxy/config"), StandardCharsets.UTF_8); - properties.load(stream); + new FileInputStream( "/opt/openslx/proxy/config" ), StandardCharsets.UTF_8 ); + properties.load( stream ); stream.close(); - } catch (IOException e) { - log.error("Could not load proxy properties from '/opt/openslx/proxy/conf'. Exiting."); + } catch ( IOException e ) { + log.error( "Could not load proxy properties from '/opt/openslx/proxy/conf'. Exiting." ); System.exit( 2 ); } finally { Util.streamClose( stream ); } } - + /** * Check proxy settings for being not empty. - * @return + * + * @return true if address and port are set */ - public static boolean checkProxySettings() { - return ( - (getProxyAddress() != "") && - (getProxyPort() != 0)); + public static boolean hasProxyAddress() + { + return !getProxyAddress().isEmpty() && getProxyPort() != 0; + } + + /** + * Check if a username or password is configured. + * + * @return true if either username or password (or both) are set + */ + public static boolean hasProxyCredentials() + { + return !getProxyUsername().isEmpty() || !getProxyPassword().isEmpty(); } } -- cgit v1.2.3-55-g7522