NetworkSettings.ProxyCredentialsProvider
added+ Most of the proxy and network credentials are currently read + from a default NetBeans storage. This may not be ideal + for other Platform aplications storing the settings in a different + way or computing them dynamically. + A SPI + NetworkSettings.ProxyCredentialsProvider allows NetBeans Platform + users to provide proxy and network credentials separately. + See http://wiki.netbeans.org/Authenticator +
+NetworkSettings.suppressAuthenticationDialog
added+ Some plugins needs a way to suppress + Authenticator without asking + user a question about the credentials. + Invoke + suppressAuthenticationDialog with a block of code where authentication dialog will be suppressed. + See http://wiki.netbeans.org/Authenticator +
+NetworkSettings
addedhostname
part of network proxy address
@@ -68,14 +61,13 @@
* Returns null
for direct connection.
*
* @param u The URI that a connection is required to
- * @return the hostname part of the Proxy address
+ * @return the hostname part of the Proxy address or null
*/
public static String getProxyHost(URI u) {
- if (getPreferences() == null) {
- return null;
- }
- InetSocketAddress sa = analyzeProxy(u);
- return sa == null ? null : sa.getHostName();
+ ProxyCredentialsProvider provider = Lookup.getDefault().lookup(ProxyCredentialsProvider.class);
+ //assert provider != null : "At least once provider found in lookup " + Lookup.getDefault();
+ LOGGER.warning("No ProxyCredentialsProvider found in lookup " + Lookup.getDefault() + " thus no proxy information will provide!");
+ return provider == null ? null : provider.getProxyHost(u);
}
/** Returns the port
part of network proxy address
@@ -83,71 +75,131 @@
* Returns null
for direct connection.
*
* @param u The URI that a connection is required to
- * @return the port part of the Proxy address
+ * @return the port part of the Proxy address or null
*/
public static String getProxyPort(URI u) {
- if (getPreferences() == null) {
- return null;
- }
- InetSocketAddress sa = analyzeProxy(u);
- return sa == null ? null : Integer.toString(sa.getPort());
+ ProxyCredentialsProvider provider = Lookup.getDefault().lookup(ProxyCredentialsProvider.class);
+ //assert provider != null : "At least once provider found in lookup " + Lookup.getDefault();
+ LOGGER.warning("No ProxyCredentialsProvider found in lookup " + Lookup.getDefault() + " thus no proxy information will provide!");
+ return provider == null ? null : provider.getProxyPort(u);
}
/** Returns the username
for Proxy Authentication.
* Returns null
if no authentication required.
*
* @param u The URI that a connection is required to
- * @return username for Proxy Authentication
+ * @return username for Proxy Authentication or null
*/
public static String getAuthenticationUsername(URI u) {
- if (getPreferences() == null) {
+ ProxyCredentialsProvider provider = Lookup.getDefault().lookup(ProxyCredentialsProvider.class);
+ //assert provider != null : "At least once provider found in lookup " + Lookup.getDefault();
+ LOGGER.warning("No ProxyCredentialsProvider found in lookup " + Lookup.getDefault() + " thus no proxy information will provide!");
+ if (provider.isProxyAuthentication(u)) {
+ return provider == null ? null : provider.getProxyUserName(u);
+ } else {
return null;
}
- if (getPreferences().getBoolean(USE_PROXY_AUTHENTICATION, false)) {
- return getPreferences().get(PROXY_AUTHENTICATION_USERNAME, "");
- }
- return null;
}
-
+
/** Returns the key
for reading password for Proxy Authentication.
- * Use {@link Keyring} for reading the password from the ring.
+ * Use {@link org.netbeans.api.Keyring} for reading the password from the ring.
* Returns null
if no authentication required.
*
* @param u The URI that a connection is required to
- * @return the key for reading password for Proxy Authentication from the ring
+ * @return the key for reading password for Proxy Authentication from the ring or null
*/
public static String getKeyForAuthenticationPassword(URI u) {
- if (getPreferences() == null) {
+ ProxyCredentialsProvider provider = Lookup.getDefault().lookup(ProxyCredentialsProvider.class);
+ //assert provider != null : "At least once provider found in lookup " + Lookup.getDefault();
+ LOGGER.warning("No ProxyCredentialsProvider found in lookup " + Lookup.getDefault() + " thus no proxy information will provide!");
+ if (provider.isProxyAuthentication(u)) {
+ return provider == null ? null : PROXY_AUTHENTICATION_PASSWORD;
+ } else {
return null;
}
- if (getPreferences().getBoolean(USE_PROXY_AUTHENTICATION, false)) {
- return PROXY_AUTHENTICATION_PASSWORD;
+ }
+ private static ThreadLocalblockOfCode
. It's a contract with NetBeans implementation
+ * of Authenticator.
+ * Tn the case a system is using other Authenticator implementation, it must call {@link #isAuthenticationDialogSuppressed} method.
+ *
+ * @param blockOfCode {@link Callable} containing code which will be executed while authentication is suppressed
+ * @return a result of calling of blockOfCode
and may throw an exception.
+ * @throws Exception
+ * @see #isAuthenticationDialogSuppressed
+ * @since 8.17
+ */
+ public static username
for Proxy Authentication.
+ * Returns null
if no authentication required.
+ *
+ * @param u The URI that a connection is required to
+ * @return username for Proxy Authentication
+ */
+ protected abstract String getProxyUserName(URI u);
+
+ /** Returns the password
for Proxy Authentication.
+ * Returns null
if no authentication required.
+ *
+ * @param u The URI that a connection is required to
+ * @return password for Proxy Authentication
+ */
+ protected abstract char[] getProxyPassword(URI u);
+
+ /** Returns true
if Proxy Authentication is required.
+ *
+ * @param u The URI that a connection is required to
+ * @return true
if authentication required.
+ */
+ protected abstract boolean isProxyAuthentication(URI u);
+
+ /** Returns the hostname
part of network proxy address
+ * based on given URI to access the resource at.
+ * Returns null
for direct connection.
+ *
+ * @param u The URI that a connection is required to
+ * @return the hostname part of the Proxy address or null
+ */
+ protected abstract String getProxyHost(URI u);
+
+ /** Returns the port
part of network proxy address
+ * based on given URI to access the resource at.
+ * Returns null
for direct connection.
+ *
+ * @param u The URI that a connection is required to
+ * @return the port part of the Proxy address or null
+ */
+ protected abstract String getProxyPort(URI u);
}
}
--- a/openide.util/test/unit/src/org/openide/util/NetworkSettingsTest.java Tue Sep 13 10:54:44 2011 +0200
+++ a/openide.util/test/unit/src/org/openide/util/NetworkSettingsTest.java Wed Sep 14 12:00:14 2011 +0200
@@ -39,25 +39,32 @@
*
* Portions Copyrighted 2011 Sun Microsystems, Inc.
*/
-
package org.openide.util;
import java.net.InetSocketAddress;
+import java.net.PasswordAuthentication;
import java.net.URISyntaxException;
+import java.net.UnknownHostException;
import java.util.Collections;
import java.io.IOException;
+import java.net.Authenticator;
+import java.net.Inet4Address;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.util.List;
+import java.util.concurrent.Callable;
import junit.framework.TestCase;
+import org.netbeans.junit.MockServices;
+import org.openide.util.NetworkSettings.ProxyCredentialsProvider;
/**
*
- * @author Jiri Rechtacek
+ * @author Jiri Rechtacek, Ondrej Vrabec
*/
public class NetworkSettingsTest extends TestCase {
+
private static ProxySelector defaultPS;
public NetworkSettingsTest(String name) {
@@ -91,6 +98,7 @@
}
};
ProxySelector.setDefault(ps);
+ MockServices.setServices(MyProxyCredentialsProvider.class);
}
@Override
@@ -105,7 +113,7 @@
}
public void testGetProxyForRemote() throws URISyntaxException {
- URI u = new URI("http://remove.org");
+ URI u = new URI("http://remote.org");
assertEquals("Check NetworkSettings.getProxyHost() for " + u, "corpcache.cache", NetworkSettings.getProxyHost(u));
assertEquals("Check NetworkSettings.getProxyPort() for " + u, "1234", NetworkSettings.getProxyPort(u));
}
@@ -116,4 +124,151 @@
assertNull("NetworkSettings.getProxyPort() returns null for " + u, NetworkSettings.getProxyPort(u));
}
+ public void testIsAuthenticationDialogNotSuppressed() throws Exception {
+ final boolean[] suppressed = new boolean[1];
+ Authenticator.setDefault(new Authenticator() {
+
+ @Override
+ protected PasswordAuthentication getPasswordAuthentication() {
+ suppressed[0] = NetworkSettings.isAuthenticationDialogSuppressed();
+ return super.getPasswordAuthentication();
+ }
+ });
+
+ Authenticator.requestPasswordAuthentication("localhost", Inet4Address.getLocalHost(), 1234, "http", null, "http");
+ assertFalse(suppressed[0]);
+ }
+
+ public void testIsAuthenticationDialogSuppressed() throws Exception {
+ final boolean[] suppressed = new boolean[1];
+ Authenticator.setDefault(new Authenticator() {
+
+ @Override
+ protected PasswordAuthentication getPasswordAuthentication() {
+ suppressed[0] = NetworkSettings.isAuthenticationDialogSuppressed();
+ return super.getPasswordAuthentication();
+ }
+ });
+
+ Callable