summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/network/ProxyConfiguration.java
blob: cfdcdb97dec3532daa465a7a79d0b348a00cb53f (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
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;
import com.btr.proxy.util.ProxyException;

/**
 * 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()
	{
		// Configuring proxy settings. First read options from config file.
		String proxyConfiguration = ProxyProperties.getProxyConf();
		if ( ( proxyConfiguration.equals( "AUTO" ) ) || ( proxyConfiguration.equals( "" ) ) ) {
			log.info( "Configuring proxy settings automatically..." );
			// Configuring proxy settings automatically.
			WpadProxySearchStrategy wPSS = new WpadProxySearchStrategy();
			try {
				ProxySelector pS = wPSS.getProxySelector();
				ProxySelector.setDefault( pS );
			} catch ( ProxyException e ) {
				log.error( "Setting proxy configuration automatically failed.", e );
			}
		} else 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() ) {
				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.getProxyUsername().equals( "" ) ) && ! ( ProxyProperties.getProxyPassword().equals( "" ) ) ) {
					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 );
				}
			}
		}
	}
}