Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b1e139f912e64b88ca0434b72ecded03d040f20e (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*******************************************************************************
 * Copyright (c) 2008, 2009 compeople AG and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * 	compeople AG (Stefan Liebig) - initial API and implementation
 *  IBM Corporation - handling URI without a scheme by select(URI, List) (bug 246065)
 *  IBM Corporation - Add proxy providers layer on the top of ProxyManager (bug 255616)
 *******************************************************************************/
package org.eclipse.core.internal.net.proxy.win32.winhttp;

import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.core.internal.net.ProxyData;
import org.eclipse.core.net.proxy.IProxyData;

/**
 * StaticProxyConfig wraps certain information of WinHttpCurrentIEProxyConfig,
 * i.e. the Windows specific list of proxies and the proxy bypass list.
 */
public class StaticProxyConfig {

	private static final String[] KNOWN_TYPES = {"HTTP", "HTTPS", "FTP", "GOPHER"}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
	private List universalProxies = new ArrayList();
	private Map protocolSpecificProxies = new HashMap();
	private ProxyBypass proxyBypass;

	/**
	 * @param proxiesString
	 * @param proxyBypassString
	 */
	public StaticProxyConfig(String proxiesString, String proxyBypassString) {
		ProxyProviderUtil.fillProxyLists(proxiesString, universalProxies,
				protocolSpecificProxies);
		proxyBypass = new ProxyBypass(proxyBypassString);
	}

	/**
	 * Select the static proxies for the given uri and add it to the given list
	 * of proxies.<br>
	 * This respects also the proxy bypass definition.
	 *
	 * @param uri
	 * @param proxies
	 */
	public void select(URI uri, List proxies) {
		if (proxyBypass.bypassProxyFor(uri))
			return;

		if (!protocolSpecificProxies.isEmpty()) {
			if (uri.getScheme() != null) {
				List protocolProxies = (List) protocolSpecificProxies.get(uri
						.getScheme().toUpperCase());
				if (protocolProxies == null)
					return;
				proxies.addAll(protocolProxies);
			} else {
				Iterator it = protocolSpecificProxies.values().iterator();
				while (it.hasNext()) {
					List protocolProxies = (List) it.next();
					if (protocolProxies == null)
						return;
					proxies.addAll(protocolProxies);
				}
			}
		} else {
			IProxyData[] data = getUniversalProxiesData();
			if (uri.getScheme() != null) {
				for (int i = 0; i < data.length; i++) {
					if (uri.getScheme().equalsIgnoreCase(data[i].getType())) {
						proxies.add(data[i]);
					}
				}
			} else {
				for (int i = 0; i < data.length; i++) {
					proxies.add(data[i]);
				}
			}
		}
	}

	public IProxyData[] getProxyData() {
		IProxyData[] data = getUniversalProxiesData();
		if (data.length > 0)
			return data;
		List proxies = new ArrayList();
		Iterator it = protocolSpecificProxies.values().iterator();
		while (it.hasNext()) {
			List protocolProxies = (List) it.next();
			if (protocolProxies != null) {
				proxies.addAll(protocolProxies);
			}
		}
		return (IProxyData[]) proxies.toArray(new IProxyData[0]);
	}

	private IProxyData[] getUniversalProxiesData() {
		if (universalProxies.size() == 0) {
			return new IProxyData[0];
		}
		IProxyData[] data = new IProxyData[KNOWN_TYPES.length];
		ProxyData universal = (ProxyData) universalProxies.get(0);
		for (int i = 0; i < KNOWN_TYPES.length; i++) {
			ProxyData newData = new ProxyData(KNOWN_TYPES[i], universal
					.getHost(), universal.getPort(), universal
					.isRequiresAuthentication(), universal.getSource());
			newData.setUserid(universal.getUserId());
			newData.setPassword(universal.getPassword());
			data[i] = newData;
		}
		return data;
	}

	public String[] getNonProxiedHosts() {
		return proxyBypass.getNonProxiedHosts();
	}

}

Back to the top