summaryrefslogtreecommitdiffstats
path: root/src/main/java/com/btr/proxy/selector/fixed/FixedProxySelector.java
blob: 79e0866856a89121045f391b4c5dc709bf10f731 (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
85
86
87
88
89
90
91
92
93
package com.btr.proxy.selector.fixed;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Proxy.Type;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/*****************************************************************************
 * This proxy selector is configured with a fixed proxy. This proxy will be 
 * returned for all URIs passed to the select method.
 *
 * @author Bernd Rosstauscher (proxyvole@rosstauscher.de) Copyright 2009
 ****************************************************************************/

public class FixedProxySelector extends ProxySelector {
	
	private List<Proxy> proxyList;
	
	
	/*************************************************************************
	 * Constructor
	 * @param proxy the proxy to use.
	 ************************************************************************/
	
	public FixedProxySelector(Proxy... proxy) {
		super();
		if (proxy.length == 0)
			throw new IllegalArgumentException("Empty list was passed");
		this.proxyList = Collections.unmodifiableList(Arrays.asList(proxy));
		if (this.proxyList.contains(null))
			throw new NullPointerException("List conains NULL proxy");
	}
	
	/*************************************************************************
	 * Constructor
	 * @param proxyHost the host name or IP address of the proxy to use.
	 * @param proxyPort the port of the proxy.
	 ************************************************************************/
	
	public FixedProxySelector(Proxy.Type type, String proxyHost, int proxyPort) {
		this(new Proxy(type, 
				InetSocketAddress.createUnresolved(proxyHost, proxyPort)));
	}

	/*************************************************************************
	 * 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) {
		final List<Proxy> list = this.proxyList;
		if (list.size() < 2)
			return;
		if (!list.get(0).address().equals(sa))
			return; // We only care if the failing one is the first one in list
		List<Proxy> newList = new ArrayList<>(list.size());
		Proxy broken = null;
		for (Proxy p : list) {
			if (p.address().equals(sa)) {
				broken = p;
			} else {
				newList.add(p);
			}
		}
		if (broken != null) {
			newList.add(broken);
		}
		this.proxyList = Collections.unmodifiableList(newList);
	}

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

	@Override
	public List<Proxy> select(URI uri) {
		return this.proxyList;
	}

	public Type getType() {
		return this.proxyList.get(0).type();
	}

}