### Eclipse Workspace Patch 1.0 #P org.eclipse.ui.net Index: src/org/eclipse/ui/internal/net/Activator.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.ui.net/src/org/eclipse/ui/internal/net/Activator.java,v retrieving revision 1.2 diff -u -r1.2 Activator.java --- src/org/eclipse/ui/internal/net/Activator.java 20 Nov 2008 15:50:07 -0000 1.2 +++ src/org/eclipse/ui/internal/net/Activator.java 8 Jun 2010 14:53:52 -0000 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2008 IBM Corporation and others. + * Copyright (c) 2007, 2010 IBM Corporation and others. * 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 @@ -13,8 +13,15 @@ *******************************************************************************/ package org.eclipse.ui.internal.net; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; + +import org.eclipse.core.net.proxy.IProxyData; +import org.eclipse.core.net.proxy.IProxyService; import org.eclipse.ui.plugin.AbstractUIPlugin; import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceReference; /** * The activator class controls the plug-in life cycle @@ -27,6 +34,9 @@ // The shared instance private static Activator plugin; + // The bundle context + private BundleContext context; + /** * The constructor */ @@ -44,12 +54,31 @@ } public void start(BundleContext context) throws Exception { - super.start(context); + super.start(context); + this.context = context; } public void stop(BundleContext context) throws Exception { plugin = null; super.stop(context); + context = null; + } + + public IProxyData[] getProxyData(URL url) { + if (context == null || url == null) { + return null; + } + ServiceReference proxyServiceReference = context.getServiceReference(IProxyService.class.getName()); + IProxyService proxyService = (IProxyService) context.getService(proxyServiceReference); + try { + URI uri = url.toURI(); + return proxyService.select(uri); + } + catch (URISyntaxException e) { + // Nothing to do. + } finally { + context.ungetService(proxyServiceReference); + } + return null; } - } Index: src/org/eclipse/ui/internal/net/auth/NetAuthenticator.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.ui.net/src/org/eclipse/ui/internal/net/auth/NetAuthenticator.java,v retrieving revision 1.1 diff -u -r1.1 NetAuthenticator.java --- src/org/eclipse/ui/internal/net/auth/NetAuthenticator.java 26 Feb 2007 18:16:44 -0000 1.1 +++ src/org/eclipse/ui/internal/net/auth/NetAuthenticator.java 8 Jun 2010 14:53:52 -0000 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2007 IBM Corporation and others. + * Copyright (c) 2000, 2010 IBM Corporation and others. * 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 @@ -10,15 +10,101 @@ *******************************************************************************/ package org.eclipse.ui.internal.net.auth; -import java.net.*; +import java.net.Authenticator; +import java.net.InetAddress; +import java.net.PasswordAuthentication; +import java.net.URL; +import java.util.HashSet; +import java.util.Set; + +import org.eclipse.core.net.proxy.IProxyData; +import org.eclipse.ui.internal.net.Activator; public class NetAuthenticator extends Authenticator { /* + * This is class storing PasswordAuthentication credentials to enable comparing this data. + */ + private class ComparablePasswordAuthentication { + private final String userName; + private final String password; + public ComparablePasswordAuthentication(String userName, String password) { + this.userName = userName; + this.password = password; + } + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + getOuterType().hashCode(); + result = prime * result + + ((password == null) ? 0 : password.hashCode()); + result = prime * result + + ((userName == null) ? 0 : userName.hashCode()); + return result; + } + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + ComparablePasswordAuthentication other = (ComparablePasswordAuthentication) obj; + if (!getOuterType().equals(other.getOuterType())) + return false; + if (password == null) { + if (other.password != null) + return false; + } else if (!password.equals(other.password)) + return false; + if (userName == null) { + if (other.userName != null) + return false; + } else if (!userName.equals(other.userName)) + return false; + return true; + } + private NetAuthenticator getOuterType() { + return NetAuthenticator.this; + } + + } + + /* + * This set stores all used password credentials to avoid blocking by proxy + * server as a consequence of to many failed requests to proxy server. + * The access to the this set must be synchronized. + */ + private Set usedPasswordCredentials = new HashSet(); + + /* * @see Authenticator#getPasswordAuthentication() */ protected PasswordAuthentication getPasswordAuthentication() { - // String protocol = getRequestingProtocol(); + RequestorType requestorType = getRequestorType(); + if (requestorType == RequestorType.PROXY) { + URL url = getRequestingURL(); + IProxyData[] proxyDatas = Activator.getDefault().getProxyData(url); + if (proxyDatas != null && proxyDatas.length > 0) { + for (int i = 0; i < proxyDatas.length; i++) { + IProxyData iProxyData = proxyDatas[i]; + final String userName = iProxyData.getUserId(); + final String password = iProxyData.getPassword(); + ComparablePasswordAuthentication passwordAuthentication = + new ComparablePasswordAuthentication(userName, password); + synchronized (usedPasswordCredentials) { + if (!usedPasswordCredentials + .contains(passwordAuthentication)) { + usedPasswordCredentials.add(passwordAuthentication); + return new PasswordAuthentication(userName, + password.toCharArray()); + } + } + } + } + } + + // String protocol = getRequestingProtocol(); InetAddress address = getRequestingSite(); // can be null; // int port = getRequestingPort(); String prompt = getRequestingPrompt(); // realm or message, not documented that can be null