/*******************************************************************************
 * 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();

		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.
	}
}

@@ -1182,16 +1182,39 @@
 ost();%0A%0A
+%09%09if (host != null) %7B%0A%09
 %09%09String
@@ -1250,16 +1250,17 @@
 Y_TYPE;%0A
+%09
 %09%09if (%22h
@@ -1298,32 +1298,33 @@
 /$NON-NLS-1$%0A%09%09%09
+%09
 type = IProxyDat
@@ -1334,32 +1334,33 @@
 TTP_PROXY_TYPE;%0A
+%09
 %09%09else if (%22ftp%22
@@ -1391,32 +1391,33 @@
 ) //$NON-NLS-1$%0A
+%09
 %09%09%09type = IProxy
@@ -1440,16 +1440,17 @@
 TYPE;%0A%09%09
+%09
 else if 
@@ -1500,16 +1500,17 @@
 S-1$%0A%09%09%09
+%09
 type = I
@@ -1538,16 +1538,17 @@
 _TYPE;%0A%0A
+%09
 %09%09final 
@@ -1608,16 +1608,17 @@
 ype);%0A%09%09
+%09
 if (data
@@ -1629,16 +1629,17 @@
 null) %7B%0A
+%09
 %09%09%09if (I
@@ -1684,32 +1684,33 @@
 getType()))%0A%09%09%09%09
+%09
 addProxy(r, Prox
@@ -1721,32 +1721,33 @@
 pe.HTTP, data);%0A
+%09
 %09%09%09else if (IPro
@@ -1786,32 +1786,33 @@
 ata.getType()))%0A
+%09
 %09%09%09%09addProxy(r, 
@@ -1834,24 +1834,25 @@
 , data);%0A%09%09%09
+%09
 else if (IPr
@@ -1896,24 +1896,25 @@
 getType()))%0A
+%09
 %09%09%09%09addProxy
@@ -1942,16 +1942,21 @@
  data);%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%09if

Fix potential NPE in EclipseProxySelector