rename org.spearce.egit -> org.eclipse.egit and bump version to 0.5.0
[egit/imyousuf.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / EclipseAuthenticator.java
blob55e0205a0091ee194fb4d80e6a95c1748b89a499
1 /*******************************************************************************
2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * See LICENSE for the full license text, also available.
7 *******************************************************************************/
8 package org.eclipse.egit.ui;
10 import java.net.Authenticator;
11 import java.net.InetAddress;
12 import java.net.PasswordAuthentication;
13 import java.net.UnknownHostException;
15 import org.eclipse.core.net.proxy.IProxyData;
16 import org.eclipse.core.net.proxy.IProxyService;
18 class EclipseAuthenticator extends Authenticator {
19 private final IProxyService service;
21 EclipseAuthenticator(final IProxyService s) {
22 service = s;
25 @Override
26 protected PasswordAuthentication getPasswordAuthentication() {
27 final IProxyData[] data = service.getProxyData();
28 if (data == null)
29 return null;
30 for (final IProxyData d : data) {
31 if (d.getUserId() == null || d.getHost() == null)
32 continue;
33 if (d.getPort() == getRequestingPort() && hostMatches(d))
34 return auth(d);
36 return null;
39 private PasswordAuthentication auth(final IProxyData d) {
40 final String user = d.getUserId();
41 final String pass = d.getPassword();
42 final char[] passChar = pass != null ? pass.toCharArray() : new char[0];
43 return new PasswordAuthentication(user, passChar);
46 private boolean hostMatches(final IProxyData d) {
47 try {
48 final InetAddress dHost = InetAddress.getByName(d.getHost());
49 InetAddress rHost = getRequestingSite();
50 if (rHost == null)
51 rHost = InetAddress.getByName(getRequestingHost());
52 return dHost.equals(rHost);
53 } catch (UnknownHostException err) {
54 return false;