summaryrefslogtreecommitdiffstats
path: root/src/main/java/com/btr/proxy/selector/whitelist/ProxyBypassListSelector.java
blob: 81e2a7d114e7d576b2b5e862631c42fc6fc43707 (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
73
74
75
76
77
78
79
80
81
82
83
84
package com.btr.proxy.selector.whitelist;

import java.io.IOException;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.util.List;

import com.btr.proxy.util.ProxyUtil;
import com.btr.proxy.util.UriFilter;

/*****************************************************************************
 * Special purpose ProxySelector used as Facade on top of a normal ProxySelector.
 * A wrapper that will first check the URI against a white list and if it matches
 * it will return DIRECT else it will pass the URI to an delegate for inspection.
 *
 * @author Bernd Rosstauscher (proxyvole@rosstauscher.de) Copyright 2009
 ****************************************************************************/

public class ProxyBypassListSelector extends ProxySelector {
	
	private ProxySelector delegate;
	private List<UriFilter> whiteListFilter;
	
	
	/*************************************************************************
	 * Constructor
	 * @param whiteListFilter a list of filters for whitelist URLs.
	 * @param proxySelector the proxy selector to use.
	 ************************************************************************/
	
	public ProxyBypassListSelector(List<UriFilter> whiteListFilter, ProxySelector proxySelector) {
		super();
		if (whiteListFilter == null) {
			throw new NullPointerException("Whitelist must not be null.");
		}
		if (proxySelector == null) {
			throw new NullPointerException("ProxySelector must not be null.");
		}
		
		this.delegate = proxySelector;
		this.whiteListFilter = whiteListFilter;
	}


	/*************************************************************************
	 * Constructor
	 * @param whiteList a list of filters for whitelist URLs as comma/space separated string.
	 * @param proxySelector the proxy selector to use.
	 ************************************************************************/
	
	public ProxyBypassListSelector(String whiteList, ProxySelector proxySelector) {
		this(new DefaultWhiteListParser().parseWhiteList(whiteList), proxySelector);
	}
	
	/*************************************************************************
	 * connectFailed
	 * @see java.net.ProxySelector#connectFailed(java.net.URI, java.net.SocketAddress, java.io.IOException)
	 ************************************************************************/
	@Override
	public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
		this.delegate.connectFailed(uri, sa, ioe);
	}

	/*************************************************************************
	 * select
	 * @see java.net.ProxySelector#select(java.net.URI)
	 ************************************************************************/

	@Override
	public List<Proxy> select(URI uri) {

		// If in white list, use DIRECT connection. 
		for (UriFilter filter : this.whiteListFilter) {
			if (filter.accept(uri)) {
				return ProxyUtil.noProxyList();
			}
		}
		
		return this.delegate.select(uri);
	}

}