summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/network/ProxyConfiguration.java
blob: ecf800fe51b7af61de0985b8abdb79a9cb4cea19 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package org.openslx.network;

import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;

import org.apache.log4j.Logger;

import com.btr.proxy.search.wpad.WpadProxySearchStrategy;

/**
 * Class for configuring proxy settings system wide, if necessary.
 * 
 * @author bjoern
 * 
 */
public class ProxyConfiguration
{
	private static final Logger log = Logger.getLogger( ProxyConfiguration.class );

	public static void configProxy()
	{
		// Reset proxy settings first
		ProxySelector.setDefault( null );
		Authenticator.setDefault( null );

		// Configuring proxy settings. First read options from config file.
		ProxyProperties.load();
		String proxyConfiguration = ProxyProperties.getProxyConf();

		if ( proxyConfiguration.equals( "AUTO" ) || proxyConfiguration.isEmpty() ) {
			log.info( "Configuring proxy settings automatically..." );
			// Configuring proxy settings automatically.
			WpadProxySearchStrategy wPSS = new WpadProxySearchStrategy();
			try {
				ProxySelector pS = wPSS.getProxySelector();
				ProxySelector.setDefault( pS );
			} catch ( Throwable e ) {
				log.error( "Setting proxy configuration automatically failed.", e );
			}
			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.hasProxyAddress() ) {
				String proxyAddress = ProxyProperties.getProxyAddress();
				int proxyPort = ProxyProperties.getProxyPort();

				// Configure proxy.
				Proxy proxy = new Proxy( Proxy.Type.SOCKS, new InetSocketAddress( proxyAddress, proxyPort ) );
				StaticProxySelector sPS = new StaticProxySelector( proxy );
				ProxySelector.setDefault( sPS );

				if ( !ProxyProperties.hasProxyCredentials() ) {
					log.info( "Configuring proxy settings manually WITH authentication..." );
					// use Proxy with authentication.
					String proxyUname = ProxyProperties.getProxyUsername();
					String proxyPass = ProxyProperties.getProxyPassword();

					// Set authentication.
					StaticProxyAuthenticator sPA = new StaticProxyAuthenticator( proxyUname, proxyPass );
					Authenticator.setDefault( sPA );
				}
			}
		}

	}

}