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
) {
26 protected PasswordAuthentication
getPasswordAuthentication() {
27 final IProxyData
[] data
= service
.getProxyData();
30 for (final IProxyData d
: data
) {
31 if (d
.getUserId() == null || d
.getHost() == null)
33 if (d
.getPort() == getRequestingPort() && hostMatches(d
))
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
) {
48 final InetAddress dHost
= InetAddress
.getByName(d
.getHost());
49 InetAddress rHost
= getRequestingSite();
51 rHost
= InetAddress
.getByName(getRequestingHost());
52 return dHost
.equals(rHost
);
53 } catch (UnknownHostException err
) {