/*******************************************************************************
 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
 *
 * 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
 *******************************************************************************/
package org.eclipse.egit.ui;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.net.proxy.IProxyData;
import org.eclipse.core.net.proxy.IProxyService;

class EclipseProxySelector extends ProxySelector {
	private final IProxyService service;

	EclipseProxySelector(final IProxyService s) {
		service = s;
	}

	@Override
	public List<Proxy> select(final URI uri) {
		final ArrayList<Proxy> r = new ArrayList<Proxy>();
		final String host = uri.getHost();

		if (host != null) {
			String type = IProxyData.SOCKS_PROXY_TYPE;
			if ("http".equals(uri.getScheme())) //$NON-NLS-1$
				type = IProxyData.HTTP_PROXY_TYPE;
			else if ("ftp".equals(uri.getScheme())) //$NON-NLS-1$
				type = IProxyData.HTTP_PROXY_TYPE;
			else if ("https".equals(uri.getScheme())) //$NON-NLS-1$
				type = IProxyData.HTTPS_PROXY_TYPE;

			final IProxyData data = service.getProxyDataForHost(host, type);
			if (data != null) {
				if (IProxyData.HTTP_PROXY_TYPE.equals(data.getType()))
					addProxy(r, Proxy.Type.HTTP, data);
				else if (IProxyData.HTTPS_PROXY_TYPE.equals(data.getType()))
					addProxy(r, Proxy.Type.HTTP, data);
				else if (IProxyData.SOCKS_PROXY_TYPE.equals(data.getType()))
					addProxy(r, Proxy.Type.SOCKS, data);
			}
		}
		if (r.isEmpty())
			r.add(Proxy.NO_PROXY);
		return r;
	}

	private void addProxy(final ArrayList<Proxy> r, final Proxy.Type type,
			final IProxyData d) {
		try {
			r.add(new Proxy(type, new InetSocketAddress(InetAddress.getByName(d
					.getHost()), d.getPort())));
		} catch (UnknownHostException uhe) {
			// Oh well.
		}
	}

	@Override
	public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
		// Don't tell Eclipse.
	}
}

@@ -694,16 +694,52 @@
 et.URI;%0A
+import java.net.URISyntaxException;%0A
 import j
@@ -1573,17 +1573,93 @@
 Y_TYPE;%0A
-%0A
+%09%09%09try %7B%0A%09%09%09%09URI queryUri = new URI(type, %22//%22 + host, null); //$NON-NLS-1$%0A%09
 %09%09%09final
@@ -1669,21 +1669,28 @@
 roxyData
+%5B%5D
  data
+Array
  = servi
@@ -1696,68 +1696,70 @@
 ice.
-getProxyDataForHost(host, type);%0A%09%09%09if (data != null
+select(queryUri);%0A%09%09%09%09for (IProxyData data : dataArray
 ) %7B%0A%09%09%09%09
 if (
@@ -1754,16 +1754,17 @@
 ) %7B%0A%09%09%09%09
+%09
 if (IPro
@@ -1802,32 +1802,33 @@
 ata.getType()))%0A
+%09
 %09%09%09%09%09addProxy(r,
@@ -1848,32 +1848,33 @@
 TTP, data);%0A%09%09%09%09
+%09
 else if (IProxyD
@@ -1910,32 +1910,33 @@
 ata.getType()))%0A
+%09
 %09%09%09%09%09addProxy(r,
@@ -1960,24 +1960,25 @@
  data);%0A%09%09%09%09
+%09
 else if (IPr
@@ -2027,24 +2027,25 @@
 pe()))%0A%09%09%09%09%09
+%09
 addProxy(r, 
@@ -2069,16 +2069,87 @@
  data);%0A
+%09%09%09%09%7D%0A%09%09%09%7D catch (URISyntaxException e) %7B%0A%09%09%09%09// just add nothing to r%0A
 %09%09%09%7D%0A%09%09%7D

Remove last deprecation warning from EGit UI