View | Details | Raw Unified | Return to bug 257443 | Differences between
and this patch

Collapse All | Expand All

(-)plugin.xml (+10 lines)
Lines 2-7 Link Here
2
<?eclipse version="3.2"?>
2
<?eclipse version="3.2"?>
3
<plugin>
3
<plugin>
4
   <extension-point id="authenticator" name="Authenticator" schema="schema/authenticator.exsd"/>
4
   <extension-point id="authenticator" name="Authenticator" schema="schema/authenticator.exsd"/>
5
   <extension-point id="proxyProvider" name="ProxyProvider" schema="schema/proxyProvider.exsd"/>
5
   <extension
6
   <extension
6
         point="org.eclipse.equinox.preferences.preferences">
7
         point="org.eclipse.equinox.preferences.preferences">
7
      <initializer
8
      <initializer
Lines 11-15 Link Here
11
            class="org.eclipse.core.internal.net.PreferenceModifyListener">
12
            class="org.eclipse.core.internal.net.PreferenceModifyListener">
12
      </modifier>
13
      </modifier>
13
   </extension>
14
   </extension>
15
   <extension
16
         point="org.eclipse.core.net.proxyProvider">
17
      <proxyProvider
18
            class="org.eclipse.core.internal.net.proxy.DirectProxyProvider">
19
      </proxyProvider>
20
      <proxyProvider
21
            class="org.eclipse.core.internal.net.proxy.ManualProxyProvider">
22
      </proxyProvider>
23
   </extension>
14
24
15
</plugin>
25
</plugin>
(-)schema/proxyProvider.exsd (+102 lines)
Added Link Here
1
<?xml version='1.0' encoding='UTF-8'?>
2
<!-- Schema file written by PDE -->
3
<schema targetNamespace="org.eclipse.core.net" xmlns="http://www.w3.org/2001/XMLSchema">
4
<annotation>
5
      <appInfo>
6
         <meta.schema plugin="org.eclipse.core.net" id="proxyProvider" name="ProxyProvider"/>
7
      </appInfo>
8
      <documentation>
9
         [Enter description of this extension point.]
10
      </documentation>
11
   </annotation>
12
13
   <element name="extension">
14
      <annotation>
15
         <appInfo>
16
            <meta.element />
17
         </appInfo>
18
      </annotation>
19
      <complexType>
20
         <sequence>
21
            <element ref="proxyProvider" minOccurs="1" maxOccurs="unbounded"/>
22
         </sequence>
23
         <attribute name="point" type="string" use="required">
24
            <annotation>
25
               <documentation>
26
                  
27
               </documentation>
28
            </annotation>
29
         </attribute>
30
         <attribute name="id" type="string">
31
            <annotation>
32
               <documentation>
33
                  
34
               </documentation>
35
            </annotation>
36
         </attribute>
37
         <attribute name="name" type="string">
38
            <annotation>
39
               <documentation>
40
                  
41
               </documentation>
42
               <appInfo>
43
                  <meta.attribute translatable="true"/>
44
               </appInfo>
45
            </annotation>
46
         </attribute>
47
      </complexType>
48
   </element>
49
50
   <element name="proxyProvider">
51
      <complexType>
52
         <attribute name="class" type="string" use="required">
53
            <annotation>
54
               <documentation>
55
                  
56
               </documentation>
57
               <appInfo>
58
                  <meta.attribute kind="java" basedOn="org.eclipse.core.internal.net.AbstractProxyProvider:"/>
59
               </appInfo>
60
            </annotation>
61
         </attribute>
62
      </complexType>
63
   </element>
64
65
   <annotation>
66
      <appInfo>
67
         <meta.section type="since"/>
68
      </appInfo>
69
      <documentation>
70
         [Enter the first release in which this extension point appears.]
71
      </documentation>
72
   </annotation>
73
74
   <annotation>
75
      <appInfo>
76
         <meta.section type="examples"/>
77
      </appInfo>
78
      <documentation>
79
         [Enter extension point usage example here.]
80
      </documentation>
81
   </annotation>
82
83
   <annotation>
84
      <appInfo>
85
         <meta.section type="apiinfo"/>
86
      </appInfo>
87
      <documentation>
88
         [Enter API information here.]
89
      </documentation>
90
   </annotation>
91
92
   <annotation>
93
      <appInfo>
94
         <meta.section type="implementation"/>
95
      </appInfo>
96
      <documentation>
97
         [Enter information about supplied implementation of this extension point.]
98
      </documentation>
99
   </annotation>
100
101
102
</schema>
(-)src/org/eclipse/core/internal/net/AbstractProxyProvider.java (+4 lines)
Lines 20-25 Link Here
20
 */
20
 */
21
public abstract class AbstractProxyProvider {
21
public abstract class AbstractProxyProvider {
22
22
23
	public abstract String getName();
24
	
25
	public abstract String getLocalizedName();
26
	
23
	/**
27
	/**
24
	 * Returns proxies to use with the given URI. Returns empty array
28
	 * Returns proxies to use with the given URI. Returns empty array
25
	 * when there is no appropriate proxy.
29
	 * when there is no appropriate proxy.
(-)src/org/eclipse/core/internal/net/Activator.java (+2 lines)
Lines 49-54 Link Here
49
	private static final String PROP_REGISTER_SERVICE = "org.eclipse.net.core.enableProxyService"; //$NON-NLS-1$
49
	private static final String PROP_REGISTER_SERVICE = "org.eclipse.net.core.enableProxyService"; //$NON-NLS-1$
50
50
51
	public static final String PT_AUTHENTICATOR = "authenticator"; //$NON-NLS-1$
51
	public static final String PT_AUTHENTICATOR = "authenticator"; //$NON-NLS-1$
52
	
53
	public static final String PT_PROXYPROVIDER = "proxyProvider"; //$NON-NLS-1$
52
54
53
	private BundleContext bundleContext;
55
	private BundleContext bundleContext;
54
56
(-)src/org/eclipse/core/internal/net/ProxyData.java (-4 / +15 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2007, 2009 IBM Corporation and others.
2
 * Copyright (c) 2007, 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 25-31 Link Here
25
25
26
	public ProxyData(String type, String host, int port,
26
	public ProxyData(String type, String host, int port,
27
			boolean requiresAuthentication, String source) {
27
			boolean requiresAuthentication, String source) {
28
		this.type = type;
28
		this.type = resolveType(type);
29
		this.host = host;
29
		this.host = host;
30
		this.port = port;
30
		this.port = port;
31
		this.requiresAuthentication = requiresAuthentication;
31
		this.requiresAuthentication = requiresAuthentication;
Lines 33-39 Link Here
33
	}
33
	}
34
34
35
	public ProxyData(String type) {
35
	public ProxyData(String type) {
36
		this.type = type;
36
		this.type = resolveType(type);
37
	}
37
	}
38
38
39
	public String getHost() {
39
	public String getHost() {
Lines 57-63 Link Here
57
	}
57
	}
58
	
58
	
59
	public void setType(String type) {
59
	public void setType(String type) {
60
		this.type = type;
60
		this.type = resolveType(type);
61
	}
61
	}
62
62
63
	public void setHost(String host) {
63
	public void setHost(String host) {
Lines 184-188 Link Here
184
			return false;
184
			return false;
185
		return true;
185
		return true;
186
	}
186
	}
187
	
188
	private String resolveType(String proxyType) {
189
		if (IProxyData.HTTP_PROXY_TYPE.equalsIgnoreCase(proxyType)) {
190
			return IProxyData.HTTP_PROXY_TYPE;
191
		} else if (IProxyData.HTTPS_PROXY_TYPE.equalsIgnoreCase(proxyType)) {
192
			return IProxyData.HTTPS_PROXY_TYPE;
193
		} else if (IProxyData.SOCKS_PROXY_TYPE.equalsIgnoreCase(proxyType)) {
194
			return IProxyData.SOCKS_PROXY_TYPE;
195
		}
196
		throw new IllegalArgumentException("Incorrect proxy data type."); //$NON-NLS-1$
197
	}
187
198
188
}
199
}
(-)src/org/eclipse/core/internal/net/ProxyManager.java (-114 / +79 lines)
Lines 16-22 Link Here
16
import java.util.ArrayList;
16
import java.util.ArrayList;
17
import java.util.Arrays;
17
import java.util.Arrays;
18
import java.util.List;
18
import java.util.List;
19
import java.util.Map;
19
import java.util.Properties;
20
import java.util.Properties;
21
import java.util.TreeMap;
20
22
21
import org.eclipse.core.net.proxy.IProxyChangeEvent;
23
import org.eclipse.core.net.proxy.IProxyChangeEvent;
22
import org.eclipse.core.net.proxy.IProxyChangeListener;
24
import org.eclipse.core.net.proxy.IProxyChangeListener;
Lines 42-51 Link Here
42
	static final String PREF_NON_PROXIED_HOSTS = "nonProxiedHosts"; //$NON-NLS-1$
44
	static final String PREF_NON_PROXIED_HOSTS = "nonProxiedHosts"; //$NON-NLS-1$
43
	static final String PREF_ENABLED = "proxiesEnabled"; //$NON-NLS-1$
45
	static final String PREF_ENABLED = "proxiesEnabled"; //$NON-NLS-1$
44
	static final String PREF_OS = "systemProxiesEnabled"; //$NON-NLS-1$
46
	static final String PREF_OS = "systemProxiesEnabled"; //$NON-NLS-1$
47
	static final String PREF_CUSTOM_PROVIDER = "customProxyProvider"; //$NON-NLS-1$
45
	
48
	
46
	private static IProxyService proxyManager;
49
	private static IProxyService proxyManager;
47
	
50
48
	private AbstractProxyProvider nativeProxyProvider;
51
	private Map providers;
49
	
52
	
50
	private PreferenceManager preferenceManager;
53
	private PreferenceManager preferenceManager;
51
	
54
	
Lines 58-73 Link Here
58
		};
61
		};
59
62
60
	private ProxyManager() {
63
	private ProxyManager() {
61
		try {
64
		providers = new TreeMap();
62
			nativeProxyProvider = (AbstractProxyProvider) Class.forName(
63
					"org.eclipse.core.net.ProxyProvider").newInstance(); //$NON-NLS-1$
64
		} catch (ClassNotFoundException e) {
65
			// no class found
66
		} catch (Exception e) {
67
			Activator.logInfo("Problems occured during the proxy provider initialization.", e); //$NON-NLS-1$
68
		}
69
		preferenceManager = Activator.getInstance().getPreferenceManager();
65
		preferenceManager = Activator.getInstance().getPreferenceManager();
70
	}
66
	}
67
	
68
	private void initializeProxyProviders() {
69
		IExtension[] extensions = RegistryFactory.getRegistry().getExtensionPoint(Activator.ID, Activator.PT_PROXYPROVIDER).getExtensions();
70
		if (extensions.length == 0) {
71
			return;
72
		}
73
		for (int i = 0; i < extensions.length; i++) {
74
			IConfigurationElement[] configs = extensions[i].getConfigurationElements();
75
			for (int j = 0; j < configs.length; j++) {
76
				try {
77
					Object object = configs[j].createExecutableExtension("class"); //$NON-NLS-1$
78
					if (object instanceof AbstractProxyProvider) {
79
						AbstractProxyProvider provider = (AbstractProxyProvider) object;
80
						if (!providers.containsKey(provider.getName())) {
81
							providers.put(provider.getName(), provider);
82
						}
83
					}
84
				} catch (CoreException e) {
85
					Activator.logError("Cannot initialize proxy provider", e); //$NON-NLS-1$
86
				}
87
			}
88
		}
89
	}
90
	
91
	AbstractProxyProvider getProviderByName(String name) {
92
		AbstractProxyProvider provider = (AbstractProxyProvider) providers.get(name);
93
		if (provider == null) {
94
			throw new IllegalArgumentException("Provider not supported"); //$NON-NLS-1$
95
		}
96
		return provider;
97
	}
98
	
99
	String getCustomProxyProvider() {
100
		String provider = preferenceManager.getString(PreferenceManager.ROOT, PREF_CUSTOM_PROVIDER);
101
		if (provider != null && providers.containsKey(provider)) {
102
			return provider;
103
		}
104
		if (hasSystemProxies()) {
105
			return "Native"; //$NON-NLS-1$
106
		}
107
		return null;
108
	}
71
109
72
	/**
110
	/**
73
	 * Return the proxy manager.
111
	 * Return the proxy manager.
Lines 123-136 Link Here
123
		System.arraycopy(nonProxiedHosts, 0, result, 0, nonProxiedHosts.length );
161
		System.arraycopy(nonProxiedHosts, 0, result, 0, nonProxiedHosts.length );
124
		return result;
162
		return result;
125
	}
163
	}
126
164
	
127
	public String[] getNativeNonProxiedHosts() {
128
		if (hasSystemProxies()) {
129
			return nativeProxyProvider.getNonProxiedHosts();
130
		}
131
		return new String[0];
132
	}
133
134
	/* (non-Javadoc)
165
	/* (non-Javadoc)
135
	 * @see org.eclipse.core.net.IProxyManager#setNonProxiedHosts(java.lang.String[])
166
	 * @see org.eclipse.core.net.IProxyManager#setNonProxiedHosts(java.lang.String[])
136
	 */
167
	 */
Lines 168-198 Link Here
168
			ProxyType type = proxies[i];
199
			ProxyType type = proxies[i];
169
			result[i] = type.getProxyData(ProxyType.VERIFY_EQUAL);
200
			result[i] = type.getProxyData(ProxyType.VERIFY_EQUAL);
170
		}
201
		}
171
		return resolveType(result);
202
		return result;
172
	}
173
174
	public IProxyData[] getNativeProxyData() {
175
		if (hasSystemProxies()) {
176
			return resolveType(nativeProxyProvider.getProxyData());
177
		}
178
		return new IProxyData[0];
179
	}
203
	}
180
204
181
	public void setProxyData(IProxyData[] proxies) {
205
	public void setProxyData(IProxyData[] proxies) {
182
		checkMigrated();
206
		checkMigrated();
183
		doSetProxyData(proxies);
184
	}
185
	
186
	private void doSetProxyData(IProxyData[] proxyDatas) {
187
		IProxyData[] oldData = getProxyData();
207
		IProxyData[] oldData = getProxyData();
188
		String[] hosts = getNonProxiedHosts();
208
		String[] hosts = getNonProxiedHosts();
189
		IProxyData[] changedProxies = internalSetProxyData(proxyDatas);
209
		IProxyData[] changedProxies = internalSetProxyData(proxies);
190
		if (changedProxies.length > 0) {
210
		if (changedProxies.length > 0) {
191
			IProxyChangeEvent event = new ProxyChangeEvent(IProxyChangeEvent.PROXY_SERVICE_ENABLEMENT_CHANGE, hosts, hosts, oldData, changedProxies);
211
			IProxyChangeEvent event = new ProxyChangeEvent(IProxyChangeEvent.PROXY_SERVICE_ENABLEMENT_CHANGE, hosts, hosts, oldData, changedProxies);
192
			fireChange(event);
212
			fireChange(event);
193
		}
213
		}
194
	}
214
	}
195
215
	
196
	private IProxyData[] internalSetProxyData(IProxyData[] proxyDatas) {
216
	private IProxyData[] internalSetProxyData(IProxyData[] proxyDatas) {
197
		List result = new ArrayList();
217
		List result = new ArrayList();
198
		for (int i = 0; i < proxyDatas.length; i++) {
218
		for (int i = 0; i < proxyDatas.length; i++) {
Lines 266-271 Link Here
266
	}
286
	}
267
287
268
	public void initialize() {
288
	public void initialize() {
289
		initializeProxyProviders();
269
		checkMigrated();
290
		checkMigrated();
270
		preferenceManager.addPreferenceChangeListener(PreferenceManager.ROOT, this);
291
		preferenceManager.addPreferenceChangeListener(PreferenceManager.ROOT, this);
271
		// Now initialize each proxy type
292
		// Now initialize each proxy type
Lines 278-284 Link Here
278
299
279
	public IProxyData getProxyData(String type) {
300
	public IProxyData getProxyData(String type) {
280
		checkMigrated();
301
		checkMigrated();
281
		return resolveType(internalGetProxyData(type, ProxyType.VERIFY_EQUAL));
302
		return internalGetProxyData(type, ProxyType.VERIFY_EQUAL);
282
	}
303
	}
283
304
284
	private IProxyData internalGetProxyData(String type, int verifySystemProperties) {
305
	private IProxyData internalGetProxyData(String type, int verifySystemProperties) {
Lines 293-320 Link Here
293
314
294
	public IProxyData[] getProxyDataForHost(String host) {
315
	public IProxyData[] getProxyDataForHost(String host) {
295
		checkMigrated();
316
		checkMigrated();
296
		if (!internalIsProxiesEnabled()) {
297
			return new IProxyData[0];
298
		}
299
		URI uri = tryGetURI(host);
317
		URI uri = tryGetURI(host);
300
		if (uri == null) {
318
		return select(uri);
301
			return new IProxyData[0];
302
		}
303
		if (hasSystemProxies() && isSystemProxiesEnabled()) {
304
			return resolveType(nativeProxyProvider.select(uri));
305
		}
306
307
		if (isHostFiltered(uri))
308
			return new IProxyData[0];
309
		IProxyData[] data = getProxyData();
310
		List result = new ArrayList();
311
		for (int i = 0; i < data.length; i++) {
312
			IProxyData proxyData = data[i];
313
			if (proxyData.getHost() != null)
314
				result.add(proxyData);
315
		}
316
		IProxyData ret[] = (IProxyData[]) result.toArray(new IProxyData[result.size()]);
317
		return resolveType(ret);
318
	}
319
	}
319
320
320
	public static URI tryGetURI(String host) {
321
	public static URI tryGetURI(String host) {
Lines 329-374 Link Here
329
		}
330
		}
330
	}
331
	}
331
332
332
	private boolean isHostFiltered(URI uri) {
333
		String[] filters = getNonProxiedHosts();
334
		for (int i = 0; i < filters.length; i++) {
335
			String filter = filters[i];
336
			if (matchesFilter(uri.getHost(), filter))
337
				return true;
338
		}
339
		return false;
340
	}
341
342
	private boolean matchesFilter(String host, String filter) {
343
		StringMatcher matcher = new StringMatcher(filter, true, false);
344
		return matcher.match(host);
345
	}
346
347
	/* (non-Javadoc)
333
	/* (non-Javadoc)
348
	 * @see org.eclipse.net.core.IProxyManager#getProxyDataForHost(java.lang.String, java.lang.String)
334
	 * @see org.eclipse.net.core.IProxyManager#getProxyDataForHost(java.lang.String, java.lang.String)
349
	 */
335
	 */
350
	public IProxyData getProxyDataForHost(String host, String type) {
336
	public IProxyData getProxyDataForHost(String host, String type) {
351
		checkMigrated();
337
		checkMigrated();
352
		if (!internalIsProxiesEnabled()) {
338
		try {
339
			URI uri = new URI(type, "//" + host, null); //$NON-NLS-1$
340
			IProxyData[] proxyDatas = select(uri);
341
			return proxyDatas.length > 0 ? proxyDatas[0] : null;
342
		} catch (URISyntaxException e) {
353
			return null;
343
			return null;
354
		}
344
		}
355
		if (hasSystemProxies() && isSystemProxiesEnabled())
356
			try {
357
				URI uri = new URI(type, "//" + host, null); //$NON-NLS-1$
358
				IProxyData[] proxyDatas = nativeProxyProvider.select(uri);
359
				return proxyDatas.length > 0 ? resolveType(proxyDatas[0]) : null;
360
			} catch (URISyntaxException e) {
361
				return null;
362
			}
363
			
364
		IProxyData[] data = getProxyDataForHost(host);
365
		for (int i = 0; i < data.length; i++) {
366
			IProxyData proxyData = data[i];
367
			if (proxyData.getType().equalsIgnoreCase(type)
368
					&& proxyData.getHost() != null)
369
				return resolveType(proxyData);
370
		}
371
		return null;
372
	}
345
	}
373
	
346
	
374
	private void registerAuthenticator() {
347
	private void registerAuthenticator() {
Lines 411-417 Link Here
411
	}
384
	}
412
385
413
	public void preferenceChange(PreferenceChangeEvent event) {
386
	public void preferenceChange(PreferenceChangeEvent event) {
414
		if (event.getKey().equals(PREF_ENABLED) || event.getKey().equals(PREF_OS)) {
387
		if (event.getKey().equals(PREF_ENABLED) || event.getKey().equals(PREF_OS) || event.getKey().equals(PREF_CUSTOM_PROVIDER)) {
415
			checkMigrated();
388
			checkMigrated();
416
			internalSetEnabled(preferenceManager.getBoolean(PreferenceManager.ROOT, PREF_ENABLED),
389
			internalSetEnabled(preferenceManager.getBoolean(PreferenceManager.ROOT, PREF_ENABLED),
417
					preferenceManager.getBoolean(PreferenceManager.ROOT, PREF_OS));
390
					preferenceManager.getBoolean(PreferenceManager.ROOT, PREF_OS));
Lines 419-425 Link Here
419
	}
392
	}
420
393
421
	public boolean hasSystemProxies() {
394
	public boolean hasSystemProxies() {
422
		return nativeProxyProvider != null;
395
		return providers.containsKey("Native") ? true : false; //$NON-NLS-1$
423
	}
396
	}
424
397
425
	public boolean isSystemProxiesEnabled() {
398
	public boolean isSystemProxiesEnabled() {
Lines 438-473 Link Here
438
	}
411
	}
439
412
440
	public IProxyData[] select(URI uri) {
413
	public IProxyData[] select(URI uri) {
441
		IProxyData data = getProxyDataForHost(uri.getHost(), uri.getScheme());
414
		if (uri == null) {
442
		if (data != null) {
415
			throw new IllegalArgumentException("Illegal argument value: " + uri); //$NON-NLS-1$
443
			return resolveType(new IProxyData[] { data });
444
		}
416
		}
445
		return new IProxyData[0];
417
		if ("file".equals(uri.getScheme())) { //$NON-NLS-1$
446
	}
418
			return new IProxyData[0];
447
448
	public IProxyData resolveType(IProxyData data) {
449
		if (data == null) {
450
			return null;
451
		}
419
		}
452
		ProxyData d = (ProxyData) data;
420
		if (uri.getHost() == null) {
453
		if (d.getType().equalsIgnoreCase(IProxyData.HTTP_PROXY_TYPE)) {
421
			uri = tryGetURI(uri.toString());
454
			d.setType(IProxyData.HTTP_PROXY_TYPE);
455
		} else if (d.getType().equalsIgnoreCase(IProxyData.HTTPS_PROXY_TYPE)) {
456
			d.setType(IProxyData.HTTPS_PROXY_TYPE);
457
		} else if (d.getType().equalsIgnoreCase(IProxyData.SOCKS_PROXY_TYPE)) {
458
			d.setType(IProxyData.SOCKS_PROXY_TYPE);
459
		}
422
		}
460
		return d;
423
		return internalIsProxiesEnabled() ? getProviderByName(getProxyProvider()).select(uri) : new IProxyData[0];
461
	}
424
	}
462
425
463
	public IProxyData[] resolveType(IProxyData[] data) {
426
	public void setProxyProvider(String name) {
464
		if (data == null) {
427
		preferenceManager.putString(PreferenceManager.ROOT, PREF_CUSTOM_PROVIDER, getProviderByName(name).getName());
465
			return null;
428
	}
466
		}
429
	
467
		for (int i = 0; i < data.length; i++) {
430
	public String getProxyProvider() {
468
			resolveType(data[i]);
431
		return ProxySelector.getDefaultProvider();
469
		}
432
	}
470
		return data;
433
434
	public String[] getAvailableProxyProviders() {
435
		return (String[]) providers.keySet().toArray(new String[0]);
471
	}
436
	}
472
437
473
}
438
}
(-)src/org/eclipse/core/internal/net/ProxySelector.java (-80 / +45 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2008, 2009 IBM Corporation and others.
2
 * Copyright (c) 2008, 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 11-19 Link Here
11
package org.eclipse.core.internal.net;
11
package org.eclipse.core.internal.net;
12
12
13
import org.eclipse.core.net.proxy.IProxyData;
13
import org.eclipse.core.net.proxy.IProxyData;
14
import org.eclipse.core.net.proxy.IProxyService;
15
import org.eclipse.core.runtime.Assert;
14
import org.eclipse.core.runtime.Assert;
16
import org.eclipse.core.runtime.CoreException;
17
15
18
/**
16
/**
19
 * This class adapts ProxyManager to add additional layer of providers on its
17
 * This class adapts ProxyManager to add additional layer of providers on its
Lines 22-96 Link Here
22
public class ProxySelector {
20
public class ProxySelector {
23
21
24
	private static final String DIRECT_PROVIDER = "Direct"; //$NON-NLS-1$
22
	private static final String DIRECT_PROVIDER = "Direct"; //$NON-NLS-1$
25
	private static final String ECLIPSE_PROVIDER = "Manual"; //$NON-NLS-1$
23
	private static final String MANUAL_PROVIDER = "Manual"; //$NON-NLS-1$
26
	private static final String NATIVE_PROVIDER = "Native"; //$NON-NLS-1$
24
	
25
	private static ProxyManager manager;
26
	
27
	static {
28
		manager = (ProxyManager) ProxyManager.getProxyManager();
29
	}
27
30
28
	public static String[] getProviders() {
31
	public static String[] getProviders() {
29
		return new String[] { DIRECT_PROVIDER, ECLIPSE_PROVIDER,
32
		return manager.getAvailableProxyProviders();
30
				NATIVE_PROVIDER };
31
	}
33
	}
32
34
33
	public static String unlocalizeProvider(String name) {
35
	public static String unlocalizeProvider(String name) {
34
		if (Messages.ProxySelector_0.equals(name)) {
36
		String[] providers = manager.getAvailableProxyProviders();
35
			return DIRECT_PROVIDER;
37
		for (int i = 0; i < providers.length; i++) {
36
		} else if (Messages.ProxySelector_1.equals(name)) {
38
			AbstractProxyProvider provider = manager.getProviderByName(providers[i]);
37
			return ECLIPSE_PROVIDER;
39
			if (provider.getLocalizedName().equals(name)) {
38
		} else if (Messages.ProxySelector_2.equals(name)) {
40
				return provider.getName();
39
			return NATIVE_PROVIDER;
41
			}
40
		}
42
		}
41
		Assert.isTrue(false);
43
		Assert.isTrue(false);
42
		return null;
44
		return null;
43
	}
45
	}
44
46
45
	public static String localizeProvider(String name) {
47
	public static String localizeProvider(String name) {
46
		if (DIRECT_PROVIDER.equals(name)) {
48
		AbstractProxyProvider proxyProvider = manager.getProviderByName(name);
47
			return Messages.ProxySelector_0;
49
		String localizedName = proxyProvider.getLocalizedName();
48
		} else if (ECLIPSE_PROVIDER.equals(name)) {
50
		if (localizedName != null) {
49
			return Messages.ProxySelector_1;
51
			return localizedName;
50
		} else if (NATIVE_PROVIDER.equals(name)) {
51
			return Messages.ProxySelector_2;
52
		}
52
		}
53
		Assert.isTrue(false);
53
		Assert.isTrue(false);
54
		return null;
54
		return null;
55
	}
55
	}
56
56
57
	public static String getDefaultProvider() {
57
	public static String getDefaultProvider() {
58
		IProxyService service = ProxyManager.getProxyManager();
58
		if (!manager.isProxiesEnabled()) {
59
		if (!service.isProxiesEnabled()) {
60
			return DIRECT_PROVIDER;
59
			return DIRECT_PROVIDER;
61
		} else if (service.isProxiesEnabled()
60
		} 
62
				&& !service.isSystemProxiesEnabled()) {
61
		if (!manager.isSystemProxiesEnabled()) {
63
			return ECLIPSE_PROVIDER;
62
			return MANUAL_PROVIDER;
64
		}
63
		}
65
		return NATIVE_PROVIDER;
64
		String provider = manager.getCustomProxyProvider();
65
		return provider != null ? provider : DIRECT_PROVIDER;
66
	}
66
	}
67
67
68
	public static void setActiveProvider(String provider) {
68
	public static void setActiveProvider(String provider) {
69
		IProxyService service = ProxyManager.getProxyManager();
70
		if (provider.equals(DIRECT_PROVIDER)) {
69
		if (provider.equals(DIRECT_PROVIDER)) {
71
			service.setProxiesEnabled(false);
70
			manager.setProxiesEnabled(false);
72
			service.setSystemProxiesEnabled(false);
71
			manager.setSystemProxiesEnabled(false);
73
		} else if (provider.equals(ECLIPSE_PROVIDER)) {
72
		} else if (provider.equals(MANUAL_PROVIDER)) {
74
			service.setProxiesEnabled(true);
73
			manager.setProxiesEnabled(true);
75
			service.setSystemProxiesEnabled(false);
74
			manager.setSystemProxiesEnabled(false);
76
		} else if (provider.equals(NATIVE_PROVIDER)) {
77
			service.setProxiesEnabled(true);
78
			service.setSystemProxiesEnabled(true);
79
		} else {
75
		} else {
80
			throw new IllegalArgumentException("Provider not supported"); //$NON-NLS-1$
76
			manager.setProxiesEnabled(true);
77
			manager.setSystemProxiesEnabled(true);
78
			manager.setProxyProvider(provider);
81
		}
79
		}
80
		
82
	}
81
	}
83
82
	
84
	public static ProxyData[] getProxyData(String provider) {
83
	public static ProxyData[] getProxyData(String provider) {
85
		ProxyManager manager = (ProxyManager) ProxyManager.getProxyManager();
84
		AbstractProxyProvider proxyProvider = manager.getProviderByName(provider);
86
		if (provider.equals(DIRECT_PROVIDER)) {
85
		return castArray(proxyProvider.getProxyData());
87
			return new ProxyData[0];
88
		} else if (provider.equals(ECLIPSE_PROVIDER)) {
89
			return castArray(manager.getProxyData());
90
		} else if (provider.equals(NATIVE_PROVIDER)) {
91
			return castArray(manager.getNativeProxyData());
92
		}
93
		throw new IllegalArgumentException("Provider not supported"); //$NON-NLS-1$
94
	}
86
	}
95
87
96
	private static ProxyData[] castArray(IProxyData data[]) {
88
	private static ProxyData[] castArray(IProxyData data[]) {
Lines 100-114 Link Here
100
	}
92
	}
101
93
102
	public static void setProxyData(String provider, ProxyData proxies[]) {
94
	public static void setProxyData(String provider, ProxyData proxies[]) {
103
		if (provider.equals(ECLIPSE_PROVIDER)) {
95
		if (canSetProxyData(provider)) {
104
			IProxyService service = ProxyManager.getProxyManager();
96
			manager.setProxyData(proxies);
105
			try {
106
				service.setProxyData(proxies);
107
			} catch (CoreException e) {
108
				// Should never occur since ProxyManager does not
109
				// declare CoreException to be thrown
110
				throw new RuntimeException(e);
111
			}
112
		} else {
97
		} else {
113
			throw new IllegalArgumentException(
98
			throw new IllegalArgumentException(
114
					"Provider does not support setting proxy data"); //$NON-NLS-1$ 
99
					"Provider does not support setting proxy data"); //$NON-NLS-1$ 
Lines 116-149 Link Here
116
	}
101
	}
117
102
118
	public static boolean canSetProxyData(String provider) {
103
	public static boolean canSetProxyData(String provider) {
119
		if (provider.equals(ECLIPSE_PROVIDER)) {
104
		return provider.equals(MANUAL_PROVIDER);
120
			return true;
121
		}
122
		return false;
123
	}
105
	}
124
106
125
	public static String[] getBypassHosts(String provider) {
107
	public static String[] getBypassHosts(String provider) {
126
		ProxyManager manager = (ProxyManager) ProxyManager.getProxyManager();
108
		AbstractProxyProvider proxyProvider = manager.getProviderByName(provider);
127
		if (provider.equals(DIRECT_PROVIDER)) {
109
		return proxyProvider.getNonProxiedHosts();
128
			return new String[0];
129
		} else if (provider.equals(ECLIPSE_PROVIDER)) {
130
			return manager.getNonProxiedHosts();
131
		} else if (provider.equals(NATIVE_PROVIDER)) {
132
			return manager.getNativeNonProxiedHosts();
133
		}
134
		throw new IllegalArgumentException("Provider not supported"); //$NON-NLS-1$ 
135
	}
110
	}
136
111
137
	public static void setBypassHosts(String provider, String hosts[]) {
112
	public static void setBypassHosts(String provider, String hosts[]) {
138
		if (provider.equals(ECLIPSE_PROVIDER)) {
113
		if (canSetBypassHosts(provider)) {
139
			IProxyService service = ProxyManager.getProxyManager();
114
			manager.setNonProxiedHosts(hosts);
140
			try {
141
				service.setNonProxiedHosts(hosts);
142
			} catch (CoreException e) {
143
				// Should never occur since ProxyManager does not
144
				// declare CoreException to be thrown
145
				throw new RuntimeException(e);
146
			}
147
		} else {
115
		} else {
148
			throw new IllegalArgumentException(
116
			throw new IllegalArgumentException(
149
					"Provider does not support setting bypass hosts"); //$NON-NLS-1$ 
117
					"Provider does not support setting bypass hosts"); //$NON-NLS-1$ 
Lines 151-160 Link Here
151
	}
119
	}
152
120
153
	public static boolean canSetBypassHosts(String provider) {
121
	public static boolean canSetBypassHosts(String provider) {
154
		if (provider.equals(ECLIPSE_PROVIDER)) {
122
		return provider.equals(MANUAL_PROVIDER);
155
			return true;
156
		}
157
		return false;
158
	}
123
	}
159
124
160
}
125
}
(-)src/org/eclipse/core/internal/net/WindowsProxyProvider.java (+10 lines)
Lines 18-23 Link Here
18
18
19
public class WindowsProxyProvider extends AbstractProxyProvider {
19
public class WindowsProxyProvider extends AbstractProxyProvider {
20
20
21
	private static String NAME = "Native"; //$NON-NLS-1$
22
	
21
	private static final String LIBRARY_NAME = "jWinHttp-1.0.0"; //$NON-NLS-1$
23
	private static final String LIBRARY_NAME = "jWinHttp-1.0.0"; //$NON-NLS-1$
22
24
23
	private static boolean jWinHttpLoaded = false;
25
	private static boolean jWinHttpLoaded = false;
Lines 44-49 Link Here
44
		}
46
		}
45
	}
47
	}
46
	
48
	
49
	public String getName() {
50
		return NAME;
51
	}
52
	
53
	public String getLocalizedName() {
54
		return Messages.ProxySelector_2;
55
	}
56
	
47
	public IProxyData[] select(URI uri) {
57
	public IProxyData[] select(URI uri) {
48
		IProxyData[] proxies = new IProxyData[0];
58
		IProxyData[] proxies = new IProxyData[0];
49
		if (jWinHttpLoaded) {
59
		if (jWinHttpLoaded) {
(-)src/org/eclipse/core/internal/net/proxy/DirectProxyProvider.java (+43 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.core.internal.net.proxy;
12
13
import java.net.URI;
14
15
import org.eclipse.core.internal.net.AbstractProxyProvider;
16
import org.eclipse.core.internal.net.Messages;
17
import org.eclipse.core.net.proxy.IProxyData;
18
19
public class DirectProxyProvider extends AbstractProxyProvider {
20
21
	private static String NAME = "Direct"; //$NON-NLS-1$
22
	
23
	public String getName() {
24
		return NAME;
25
	}
26
27
	public String getLocalizedName() {
28
		return Messages.ProxySelector_0;
29
	}
30
	
31
	public IProxyData[] select(URI uri) {
32
		return new IProxyData[0];
33
	}
34
35
	protected IProxyData[] getProxyData() {
36
		return new IProxyData[0];
37
	}
38
39
	protected String[] getNonProxiedHosts() {
40
		return new String[0];
41
	}
42
43
}
(-)src/org/eclipse/core/internal/net/proxy/ManualProxyProvider.java (+79 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.core.internal.net.proxy;
12
13
import java.net.URI;
14
import java.util.ArrayList;
15
import java.util.List;
16
17
import org.eclipse.core.internal.net.AbstractProxyProvider;
18
import org.eclipse.core.internal.net.Messages;
19
import org.eclipse.core.internal.net.ProxyManager;
20
import org.eclipse.core.internal.net.StringMatcher;
21
import org.eclipse.core.net.proxy.IProxyData;
22
23
public class ManualProxyProvider extends AbstractProxyProvider {
24
25
	private static String NAME = "Manual"; //$NON-NLS-1$
26
	
27
	public String getName() {
28
		return NAME;
29
	}
30
	
31
	public String getLocalizedName() {
32
		return Messages.ProxySelector_1;
33
	}
34
	
35
	public IProxyData[] select(URI uri) {
36
		String[] nonProxyHosts = getNonProxiedHosts();
37
		if (nonProxyHosts != null) {
38
			String host = uri.getHost();
39
			for (int npIndex = 0; npIndex < nonProxyHosts.length; npIndex++) {
40
				if (matchesFilter(host, nonProxyHosts[npIndex])) {
41
 					return new IProxyData[0];
42
 				}
43
			}
44
		}
45
		
46
		IProxyData[] data = getProxyData();
47
		List result = new ArrayList();
48
		for (int i = 0; i < data.length; i++) {
49
			IProxyData proxyData = data[i];
50
			if (proxyData.getHost() != null)
51
				result.add(proxyData);
52
		}
53
		data = (IProxyData[]) result.toArray(new IProxyData[result.size()]);
54
		if (uri.getScheme() == null) {
55
			return data;
56
		}
57
		for (int i = 0; i < data.length; i++) {
58
			IProxyData proxyData = data[i];
59
			if (proxyData.getType().equalsIgnoreCase(uri.getScheme()) && proxyData.getHost() != null) {
60
				return new IProxyData[] { proxyData };
61
			}	
62
		}
63
		return new IProxyData[0];
64
	}
65
66
	protected IProxyData[] getProxyData() {
67
		return ProxyManager.getProxyManager().getProxyData();
68
	}
69
70
	protected String[] getNonProxiedHosts() {
71
		return ProxyManager.getProxyManager().getNonProxiedHosts();
72
	}
73
	
74
	private boolean matchesFilter(String host, String filter) {
75
		StringMatcher matcher = new StringMatcher(filter, true, false);
76
		return matcher.match(host);
77
	}
78
79
}
(-)src/org/eclipse/core/internal/net/proxy/unix/UnixProxyProvider.java (+11 lines)
Lines 19-24 Link Here
19
19
20
import org.eclipse.core.internal.net.AbstractProxyProvider;
20
import org.eclipse.core.internal.net.AbstractProxyProvider;
21
import org.eclipse.core.internal.net.Activator;
21
import org.eclipse.core.internal.net.Activator;
22
import org.eclipse.core.internal.net.Messages;
22
import org.eclipse.core.internal.net.Policy;
23
import org.eclipse.core.internal.net.Policy;
23
import org.eclipse.core.internal.net.ProxyData;
24
import org.eclipse.core.internal.net.ProxyData;
24
import org.eclipse.core.internal.net.StringMatcher;
25
import org.eclipse.core.internal.net.StringMatcher;
Lines 27-32 Link Here
27
28
28
public class UnixProxyProvider extends AbstractProxyProvider {
29
public class UnixProxyProvider extends AbstractProxyProvider {
29
30
31
	private static String NAME = "Native"; //$NON-NLS-1$
32
	
30
	private static final String LIBRARY_GCONF2 = "gconf-2"; //$NON-NLS-1$
33
	private static final String LIBRARY_GCONF2 = "gconf-2"; //$NON-NLS-1$
31
34
32
	private static final String LIBRARY_NAME = "gnomeproxy-1.0.0"; //$NON-NLS-1$
35
	private static final String LIBRARY_NAME = "gnomeproxy-1.0.0"; //$NON-NLS-1$
Lines 49-54 Link Here
49
		// Nothing to initialize
52
		// Nothing to initialize
50
	}
53
	}
51
	
54
	
55
	public String getName() {
56
		return NAME;
57
	}
58
	
59
	public String getLocalizedName() {
60
		return Messages.ProxySelector_2;
61
	}
62
	
52
	public IProxyData[] select(URI uri) {
63
	public IProxyData[] select(URI uri) {
53
		String[] nonProxyHosts = getNonProxiedHosts();
64
		String[] nonProxyHosts = getNonProxiedHosts();
54
		if (nonProxyHosts != null) {
65
		if (nonProxyHosts != null) {
(-)src/org/eclipse/core/net/proxy/IProxyService.java (-1 / +40 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2007, 2008 IBM Corporation and others.
2
 * Copyright (c) 2007, 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 260-263 Link Here
260
	 * @param listener a change listener
260
	 * @param listener a change listener
261
	 */
261
	 */
262
	void removeProxyChangeListener(IProxyChangeListener listener);
262
	void removeProxyChangeListener(IProxyChangeListener listener);
263
	
264
	/**
265
	 * <p>
266
	 * Sets proxy provider that will be used to provide proxy data for methods:
267
	 * <li>{@link #select(URI)}</li>
268
	 * <li>{@link #getProxyDataForHost(String)}</li>
269
	 * <li>{@link #getProxyDataForHost(String, String)}</li>
270
	 * </p>
271
	 * <p>
272
	 * To use provider which was set using this method, 
273
	 * {@link #isProxiesEnabled()} and {@link #isSystemProxiesEnabled()}
274
	 * have to return <code>true</code>. Otherwise, value set by calling this method will be ignored. 
275
	 * </p>
276
	 * @param name - proxy provider's name. 
277
	 * To get all available providers call {@link #getAvailableProxyProviders()}
278
	 * @throws IllegalArgumentException - if there is no proxy provider with the given name
279
	 */
280
	void setProxyProvider(String name) throws IllegalArgumentException;
281
	
282
	/**
283
	 * Returns name of a current proxy provider which was set by calling {@link #setProxyProvider(String)}.
284
	 * This method returns:
285
	 * <li><code>Direct</code> - if {@link #isProxiesEnabled()} returns <code>false</code></li>
286
	 * <li><code>Manual</code> - if {@link #isProxiesEnabled()} returns <code>true</code> and {@link #isSystemProxiesEnabled()} returns <code>false</code></li>
287
	 * <li><code>Custom</code> - if {@link #isProxiesEnabled()} returns <code>true</code> and {@link #isSystemProxiesEnabled()} returns <code>true</code>, 
288
	 * where <code>Custom</code> is a provider set by calling {@link #setProxyProvider(String)}. 
289
	 * If there was no call to this method it returns <code>Native</code> provider. 
290
	 * If Native is unavailable, it returns <code>Direct</code>.</li>
291
	 * @return name of a current proxy provider
292
	 */
293
	String getProxyProvider();
294
	
295
	/**
296
	 * Returns all available proxy providers. Name is a unique identifier of each proxy provider.
297
	 * This method can be used to retrive providers which can be set using {@link #setProxyProvider(String)}
298
	 * New proxy provider can be added by extending org.eclipse.core.net.proxyProvider extension point.
299
	 * @return array of available providers names
300
	 */
301
	String[] getAvailableProxyProviders();
263
}
302
}
(-)build.properties (-2 / +3 lines)
Lines 1-5 Link Here
1
###############################################################################
1
###############################################################################
2
# Copyright (c) 2008 IBM Corporation and others.
2
# Copyright (c) 2008, 2010 IBM Corporation and others.
3
# All rights reserved. This program and the accompanying materials
3
# All rights reserved. This program and the accompanying materials
4
# are made available under the terms of the Eclipse Public License v1.0
4
# are made available under the terms of the Eclipse Public License v1.0
5
# which accompanies this distribution, and is available at
5
# which accompanies this distribution, and is available at
Lines 13-19 Link Here
13
               about.html,\
13
               about.html,\
14
               META-INF/,\
14
               META-INF/,\
15
               bin/,\
15
               bin/,\
16
               jWinHttp-1.0.0.dll
16
               jWinHttp-1.0.0.dll,\
17
               fragment.xml
17
generateSourceBundle=false
18
generateSourceBundle=false
18
src.includes = about.html
19
src.includes = about.html
19
source.. = src/
20
source.. = src/
(-)fragment.xml (+11 lines)
Added Link Here
1
<?xml version="1.0" encoding="UTF-8"?>
2
<?eclipse version="3.4"?>
3
<fragment>
4
   <extension
5
         point="org.eclipse.core.net.proxyProvider">
6
      <proxyProvider
7
            class="org.eclipse.core.internal.net.WindowsProxyProvider">
8
      </proxyProvider>
9
   </extension>
10
11
</fragment>
(-)src/org/eclipse/core/net/ProxyProvider.java (-16 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 compeople AG and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 * 	compeople AG (Stefan Liebig) - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.core.net;
12
13
import org.eclipse.core.internal.net.WindowsProxyProvider;
14
15
public class ProxyProvider extends WindowsProxyProvider {
16
}
(-)META-INF/MANIFEST.MF (-1 / +1 lines)
Lines 1-7 Link Here
1
Manifest-Version: 1.0
1
Manifest-Version: 1.0
2
Bundle-ManifestVersion: 2
2
Bundle-ManifestVersion: 2
3
Bundle-Name: Net Tests Plug-in
3
Bundle-Name: Net Tests Plug-in
4
Bundle-SymbolicName: org.eclipse.core.tests.net
4
Bundle-SymbolicName: org.eclipse.core.tests.net;singleton:=true
5
Bundle-Version: 1.2.100.qualifier
5
Bundle-Version: 1.2.100.qualifier
6
Bundle-Activator: org.eclipse.core.tests.net.Activator
6
Bundle-Activator: org.eclipse.core.tests.net.Activator
7
Bundle-Vendor: Eclipse.org
7
Bundle-Vendor: Eclipse.org
(-)plugin.xml (+11 lines)
Added Link Here
1
<?xml version="1.0" encoding="UTF-8"?>
2
<?eclipse version="3.4"?>
3
<plugin>
4
   <extension
5
         point="org.eclipse.core.net.proxyProvider">
6
      <proxyProvider
7
            class="org.eclipse.core.tests.net.CustomProxyProvider">
8
      </proxyProvider>
9
   </extension>
10
11
</plugin>
(-)src/org/eclipse/core/tests/net/CustomProxyProvider.java (+77 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.core.tests.net;
12
13
import java.net.URI;
14
15
import org.eclipse.core.internal.net.AbstractProxyProvider;
16
import org.eclipse.core.internal.net.ProxyData;
17
import org.eclipse.core.internal.net.StringMatcher;
18
import org.eclipse.core.net.proxy.IProxyData;
19
20
public class CustomProxyProvider extends AbstractProxyProvider {
21
22
	private static String NAME = "Test"; //$NON-NLS-1$
23
	
24
	private IProxyData[] proxies = {
25
			new ProxyData(IProxyData.HTTP_PROXY_TYPE, "customHttp", 1, false, NAME),
26
			new ProxyData(IProxyData.HTTPS_PROXY_TYPE, "customHttps", 2, false, NAME),
27
			new ProxyData(IProxyData.SOCKS_PROXY_TYPE, "customSocks", 3, false, NAME),
28
	};
29
	
30
	public String getName() {
31
		return NAME;
32
	}
33
34
	public String getLocalizedName() {
35
		return NAME;
36
	}
37
	
38
	public IProxyData[] select(URI uri) {
39
		String[] nonProxyHosts = getNonProxiedHosts();
40
		if (nonProxyHosts != null) {
41
			String host = uri.getHost();
42
			for (int npIndex = 0; npIndex < nonProxyHosts.length; npIndex++) {
43
				if (matchesFilter(host, nonProxyHosts[npIndex])) {
44
 					return new IProxyData[0];
45
 				}
46
			}
47
		}
48
		
49
		String scheme = uri.getScheme();
50
		if (scheme != null) {
51
			if (scheme.equalsIgnoreCase(IProxyData.HTTP_PROXY_TYPE)) {
52
				return new IProxyData[] { proxies[0] };
53
			}
54
			if (scheme.equalsIgnoreCase(IProxyData.HTTPS_PROXY_TYPE)) {
55
				return new IProxyData[] { proxies[1] };	
56
			}
57
			if (scheme.equalsIgnoreCase(IProxyData.SOCKS_PROXY_TYPE)) {
58
				return new IProxyData[] { proxies[2] };
59
			}
60
		}
61
		return proxies;
62
	}
63
64
	protected IProxyData[] getProxyData() {
65
		return proxies;
66
	}
67
68
	protected String[] getNonProxiedHosts() {
69
		return new String[] { "customBypass" };
70
	}
71
	
72
	private boolean matchesFilter(String host, String filter) {
73
		StringMatcher matcher = new StringMatcher(filter, true, false);
74
		return matcher.match(host);
75
	}
76
77
}
(-)src/org/eclipse/core/tests/net/NetTest.java (+90 lines)
Lines 15-20 Link Here
15
import java.util.ArrayList;
15
import java.util.ArrayList;
16
import java.util.Arrays;
16
import java.util.Arrays;
17
import java.util.HashMap;
17
import java.util.HashMap;
18
import java.util.List;
18
import java.util.Map;
19
import java.util.Map;
19
import java.util.Properties;
20
import java.util.Properties;
20
21
Lines 431-436 Link Here
431
432
432
		this.getProxyManager().setNonProxiedHosts(oldHosts);
433
		this.getProxyManager().setNonProxiedHosts(oldHosts);
433
	}
434
	}
435
	
436
	public void testCustomProxyProvider() {
437
		List providers = Arrays.asList(getProxyManager().getAvailableProxyProviders());
438
		assertEquals(4, providers.size());
439
		assertTrue(providers.contains("Direct"));
440
		assertTrue(providers.contains("Manual"));
441
		assertTrue(providers.contains("Native"));
442
		assertTrue(providers.contains("Test"));
443
	}
444
	
445
	public void testSetCustomProxyProvider() throws CoreException {
446
		setDataTest(IProxyData.HTTP_PROXY_TYPE);
447
		setDataTest(IProxyData.HTTPS_PROXY_TYPE);
448
		setDataTest(IProxyData.SOCKS_PROXY_TYPE);
449
		
450
		IProxyService service = getProxyManager();
451
		service.setProxyProvider("Test");
452
		
453
		setSystemProxiesEnabled(false);
454
		setProxiesEnabled(false);
455
		assertEquals("Direct", service.getProxyProvider());
456
		validateSystemProperties(false);
457
		
458
		setSystemProxiesEnabled(false);
459
		setProxiesEnabled(true);
460
		assertEquals("Manual", service.getProxyProvider());
461
		validateSystemProperties(true);
462
		
463
		setSystemProxiesEnabled(true);
464
		setProxiesEnabled(true);
465
		assertEquals("Test", service.getProxyProvider());
466
		
467
		setSystemProxiesEnabled(false);
468
		setProxiesEnabled(false);
469
		assertEquals("Direct", service.getProxyProvider());
470
		validateSystemProperties(false);
471
	}
472
	
473
	public void testSelectCustomProxyProvider() throws CoreException, URISyntaxException {	
474
		IProxyService service = getProxyManager();
475
		service.setProxyProvider("Test");
476
		setSystemProxiesEnabled(true);
477
		
478
		IProxyData[] proxies = service.select(new URI("eclipse.org"));
479
		assertEquals(3, proxies.length);
480
		
481
		IProxyData[] httpProxy = service.select(new URI("http://eclipse.org"));
482
		assertEquals(1, httpProxy.length);
483
		assertEquals(proxies[0], httpProxy[0]);
484
		
485
		IProxyData[] httpsProxy = service.select(new URI("https://eclipse.org"));
486
		assertEquals(1, httpsProxy.length);
487
		assertEquals(proxies[1], httpsProxy[0]);
488
		
489
		IProxyData[] socksProxy = service.select(new URI("socks://eclipse.org"));
490
		assertEquals(1, socksProxy.length);
491
		assertEquals(proxies[2], socksProxy[0]);
492
	}
493
	
494
	public void testChangeCustomProxyProvider() throws CoreException, URISyntaxException {	
495
		IProxyService service = getProxyManager();
496
		service.setProxyProvider("Test");
497
		setSystemProxiesEnabled(true);
498
		
499
		IProxyData[] proxies = service.select(new URI("eclipse.org"));
500
		assertEquals(3, proxies.length);
501
		
502
		service.setProxyProvider("Native");
503
		proxies = service.select(new URI("eclipse.org"));
504
		assertEquals(0, proxies.length);
505
	}
506
	
507
	public void testBypassProxyProvider() throws CoreException, URISyntaxException {	
508
		IProxyService service = getProxyManager();
509
		service.setProxyProvider("Test");
510
		setSystemProxiesEnabled(true);
511
		
512
		IProxyData[] proxies = service.select(new URI("customBypass"));
513
		assertEquals(0, proxies.length);
514
		
515
		IProxyData[] httpProxy = service.select(new URI("http://customBypass"));
516
		assertEquals(0, httpProxy.length);
517
		
518
		IProxyData[] httpsProxy = service.select(new URI("https://customBypass"));
519
		assertEquals(0, httpsProxy.length);
520
		
521
		IProxyData[] socksProxy = service.select(new URI("socks://customBypass"));
522
		assertEquals(0, socksProxy.length);
523
	}
434
524
435
	private void validateSystemProperties(boolean present) {
525
	private void validateSystemProperties(boolean present) {
436
		validateProperty("http.proxySet", "true", present);
526
		validateProperty("http.proxySet", "true", present);

Return to bug 257443