This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 201662
Collapse All | Expand All

(-)a/o.n.core/nbproject/project.xml (-1 / +1 lines)
Lines 160-166 Link Here
160
                    <build-prerequisite/>
160
                    <build-prerequisite/>
161
                    <compile-dependency/>
161
                    <compile-dependency/>
162
                    <run-dependency>
162
                    <run-dependency>
163
                        <specification-version>8.0</specification-version>
163
                        <specification-version>8.17</specification-version>
164
                    </run-dependency>
164
                    </run-dependency>
165
                </dependency>
165
                </dependency>
166
                <dependency>
166
                <dependency>
(-)a/o.n.core/src/org/netbeans/core/NbAuthenticator.java (-1 / +26 lines)
Lines 44-49 Link Here
44
package org.netbeans.core;
44
package org.netbeans.core;
45
45
46
import java.net.PasswordAuthentication;
46
import java.net.PasswordAuthentication;
47
import java.net.URL;
47
import java.util.logging.Level;
48
import java.util.logging.Level;
48
import java.util.logging.Logger;
49
import java.util.logging.Logger;
49
import java.util.prefs.Preferences;
50
import java.util.prefs.Preferences;
Lines 51-56 Link Here
51
import org.openide.DialogDisplayer;
52
import org.openide.DialogDisplayer;
52
import org.openide.util.NbBundle;
53
import org.openide.util.NbBundle;
53
import org.openide.util.NbPreferences;
54
import org.openide.util.NbPreferences;
55
import org.openide.util.NetworkSettings;
54
56
55
/** Global password protected sites Authenticator for IDE
57
/** Global password protected sites Authenticator for IDE
56
 *
58
 *
Lines 61-67 Link Here
61
    private static final long TIMEOUT = 3000;
63
    private static final long TIMEOUT = 3000;
62
    private static long lastTry = 0;
64
    private static long lastTry = 0;
63
65
64
65
    private NbAuthenticator() {
66
    private NbAuthenticator() {
66
        Preferences proxySettingsNode = NbPreferences.root().node("/org/netbeans/core"); //NOI18N
67
        Preferences proxySettingsNode = NbPreferences.root().node("/org/netbeans/core"); //NOI18N
67
        assert proxySettingsNode != null;
68
        assert proxySettingsNode != null;
Lines 73-78 Link Here
73
        }
74
        }
74
    }
75
    }
75
76
77
    static void install4test() {
78
        setDefault(new NbAuthenticator());
79
    }
80
76
    @Override
81
    @Override
77
    protected PasswordAuthentication getPasswordAuthentication() {
82
    protected PasswordAuthentication getPasswordAuthentication() {
78
        Logger.getLogger(NbAuthenticator.class.getName()).log(Level.FINER, "Authenticator.getPasswordAuthentication() with prompt " + this.getRequestingPrompt()); //NOI18N
83
        Logger.getLogger(NbAuthenticator.class.getName()).log(Level.FINER, "Authenticator.getPasswordAuthentication() with prompt " + this.getRequestingPrompt()); //NOI18N
Lines 85-90 Link Here
85
                if (getRequestingProtocol().startsWith("SOCKS")&&(ProxySettings.getAuthenticationUsername().length()>0)) { //NOI18N
90
                if (getRequestingProtocol().startsWith("SOCKS")&&(ProxySettings.getAuthenticationUsername().length()>0)) { //NOI18N
86
                    return new java.net.PasswordAuthentication(ProxySettings.getAuthenticationUsername(), ProxySettings.getAuthenticationPassword());
91
                    return new java.net.PasswordAuthentication(ProxySettings.getAuthenticationUsername(), ProxySettings.getAuthenticationPassword());
87
                }
92
                }
93
                PasswordAuthentication auth = getAuthenticationFromURL();
94
                if (auth != null) {
95
                    return auth;
96
                }
97
                if (NetworkSettings.isAuthenticationDialogSuppressed()) {
98
                    return null;
99
                }
88
                NbAuthenticatorPanel ui = new NbAuthenticatorPanel(getRequestingPrompt());
100
                NbAuthenticatorPanel ui = new NbAuthenticatorPanel(getRequestingPrompt());
89
                Object result = DialogDisplayer.getDefault().notify(
101
                Object result = DialogDisplayer.getDefault().notify(
90
                        new DialogDescriptor(ui, NbBundle.getMessage(NbAuthenticator.class, "CTL_Authentication"))); //NOI18N
102
                        new DialogDescriptor(ui, NbBundle.getMessage(NbAuthenticator.class, "CTL_Authentication"))); //NOI18N
Lines 101-104 Link Here
101
        return null;
113
        return null;
102
    }
114
    }
103
115
116
    private PasswordAuthentication getAuthenticationFromURL() {
117
        URL u = this.getRequestingURL();
118
        if (u != null) {
119
            String auth = u.getUserInfo();
120
            if (auth != null) {
121
                int i = auth.indexOf(':');
122
                String user = (i == -1) ? auth : auth.substring(0, i);
123
                String pwd = (i == -1) ? "" : auth.substring(i + 1);
124
                return new PasswordAuthentication(user, pwd.toCharArray());
125
            }
126
        }
127
        return null;
128
    }
104
}
129
}
(-)a/o.n.core/src/org/netbeans/core/ProxySettings.java (+79 lines)
Lines 45-61 Link Here
45
package org.netbeans.core;
45
package org.netbeans.core;
46
46
47
import java.net.InetAddress;
47
import java.net.InetAddress;
48
import java.net.InetSocketAddress;
49
import java.net.Proxy;
50
import java.net.ProxySelector;
51
import java.net.URI;
48
import java.net.UnknownHostException;
52
import java.net.UnknownHostException;
49
import java.util.HashSet;
53
import java.util.HashSet;
54
import java.util.List;
50
import java.util.Locale;
55
import java.util.Locale;
51
import java.util.Set;
56
import java.util.Set;
52
import java.util.StringTokenizer;
57
import java.util.StringTokenizer;
58
import java.util.logging.Level;
59
import java.util.logging.Logger;
53
import java.util.prefs.PreferenceChangeListener;
60
import java.util.prefs.PreferenceChangeListener;
54
import java.util.prefs.Preferences;
61
import java.util.prefs.Preferences;
55
import org.netbeans.api.keyring.Keyring;
62
import org.netbeans.api.keyring.Keyring;
56
import org.openide.util.NbBundle;
63
import org.openide.util.NbBundle;
57
import org.openide.util.NbPreferences;
64
import org.openide.util.NbPreferences;
65
import org.openide.util.NetworkSettings;
66
import org.openide.util.Parameters;
58
import org.openide.util.Utilities;
67
import org.openide.util.Utilities;
68
import org.openide.util.lookup.ServiceProvider;
59
69
60
/**
70
/**
61
 *
71
 *
Lines 95-100 Link Here
95
    /** Proxy PAC file manually set. */
105
    /** Proxy PAC file manually set. */
96
    public static final int MANUAL_SET_PAC = 4;
106
    public static final int MANUAL_SET_PAC = 4;
97
    
107
    
108
    private static final Logger LOGGER = Logger.getLogger(ProxySettings.class.getName());
109
    
98
    private static Preferences getPreferences() {
110
    private static Preferences getPreferences() {
99
        return NbPreferences.forModule (ProxySettings.class);
111
        return NbPreferences.forModule (ProxySettings.class);
100
    }
112
    }
Lines 437-440 Link Here
437
        }
449
        }
438
    }
450
    }
439
    
451
    
452
    private static InetSocketAddress analyzeProxy(URI uri) {
453
        Parameters.notNull("uri", uri);
454
        List<Proxy> proxies = ProxySelector.getDefault().select(uri);
455
        assert proxies != null : "ProxySelector cannot return null for " + uri;
456
        assert !proxies.isEmpty() : "ProxySelector cannot return empty list for " + uri;
457
        Proxy p = proxies.get(0);
458
        if (Proxy.Type.DIRECT == p.type()) {
459
            // return null for DIRECT proxy
460
            return null;
461
        } else {
462
            if (p.address() instanceof InetSocketAddress) {
463
                // check is
464
                //assert ! ((InetSocketAddress) p.address()).isUnresolved() : p.address() + " must be resolved address.";
465
                return (InetSocketAddress) p.address();
466
            } else {
467
                LOGGER.log(Level.INFO, p.address() + " is not instanceof InetSocketAddress but " + p.address().getClass());
468
                return null;
469
            }
470
        }
471
    }
472
473
    @ServiceProvider(service = NetworkSettings.ProxyCredentialsProvider.class, position = 1000)
474
    public static class NbProxyCredentialsProvider extends NetworkSettings.ProxyCredentialsProvider {
475
476
        @Override
477
        public String getProxyHost(URI u) {
478
            if (getPreferences() == null) {
479
                return null;
480
            }
481
            InetSocketAddress sa = analyzeProxy(u);
482
            return sa == null ? null : sa.getHostName();
483
        }
484
485
        @Override
486
        public String getProxyPort(URI u) {
487
            if (getPreferences() == null) {
488
                return null;
489
            }
490
            InetSocketAddress sa = analyzeProxy(u);
491
            return sa == null ? null : Integer.toString(sa.getPort());
492
        }
493
494
        @Override
495
        protected String getProxyUserName(URI u) {
496
            if (getPreferences() == null) {
497
                return null;
498
            }
499
            return ProxySettings.getAuthenticationUsername();
500
        }
501
502
        @Override
503
        protected char[] getProxyPassword(URI u) {
504
            if (getPreferences() == null) {
505
                return null;
506
            }
507
            return ProxySettings.getAuthenticationPassword();
508
        }
509
510
        @Override
511
        protected boolean isProxyAuthentication(URI u) {
512
            if (getPreferences() == null) {
513
                return false;
514
            }
515
            return getPreferences().getBoolean(USE_PROXY_AUTHENTICATION, false);
516
        }
517
518
    }
440
}
519
}
(-)a/openide.util/apichanges.xml (+42 lines)
Lines 51-56 Link Here
51
    <apidef name="actions">Actions API</apidef>
51
    <apidef name="actions">Actions API</apidef>
52
</apidefs>
52
</apidefs>
53
<changes>
53
<changes>
54
    <change id="ProxyCredentialsProvider">
55
        <api name="util"/>
56
        <summary>SPI <code>NetworkSettings.ProxyCredentialsProvider</code> added</summary>
57
        <version major="8" minor="17"/>
58
        <date day="20" month="9" year="2011"/>
59
        <author login="jrechtacek"/>
60
        <compatibility addition="yes"/>
61
        <description>
62
            <p>
63
                Most of the proxy and network credentials are currently read
64
                from a default NetBeans storage. This may not be ideal
65
                for other Platform aplications storing the settings in a different
66
                way or computing them dynamically. 
67
                A SPI <a href="@TOP@/org/openide/util/NetworkSettings.ProxyCredentialsProvider.html">
68
                NetworkSettings.ProxyCredentialsProvider</a> allows NetBeans Platform
69
                users to provide proxy and network credentials separately.
70
                See <a href="http://wiki.netbeans.org/Authenticator">http://wiki.netbeans.org/Authenticator</a>
71
            </p>
72
        </description>
73
        <class package="org.openide.util" name="NetworkSettings"/>
74
        <issue number="201662"/>
75
    </change>
76
    <change id="SuppressAuthentication">
77
        <api name="util"/>
78
        <summary><code>NetworkSettings.suppressAuthenticationDialog</code> added</summary>
79
        <version major="8" minor="17"/>
80
        <date day="20" month="9" year="2011"/>
81
        <author login="jrechtacek"/>
82
        <compatibility addition="yes"/>
83
        <description>
84
            <p>
85
                Some plugins needs a way to suppress <a href="http://download.oracle.com/javase/6/docs/api/java/net/Authenticator.html">
86
                    Authenticator</a> without asking
87
                user a question about the credentials. 
88
                Invoke <a href="@TOP@/org/openide/util/NetworkSettings.html#suppressAuthenticationDialog(java.util.concurrent.Callable)">
89
                suppressAuthenticationDialog</a> with a block of code where authentication dialog will be suppressed.                
90
                See <a href="http://wiki.netbeans.org/Authenticator">http://wiki.netbeans.org/Authenticator</a>
91
            </p>
92
        </description>
93
        <class package="org.openide.util" name="NetworkSettings"/>
94
        <issue number="201662"/>
95
    </change>
54
    <change id="NetworkSettings">
96
    <change id="NetworkSettings">
55
        <api name="util"/>
97
        <api name="util"/>
56
        <summary><code>NetworkSettings</code> added</summary>
98
        <summary><code>NetworkSettings</code> added</summary>
(-)a/openide.util/manifest.mf (-1 / +1 lines)
Lines 1-5 Link Here
1
Manifest-Version: 1.0
1
Manifest-Version: 1.0
2
OpenIDE-Module: org.openide.util
2
OpenIDE-Module: org.openide.util
3
OpenIDE-Module-Localizing-Bundle: org/openide/util/Bundle.properties
3
OpenIDE-Module-Localizing-Bundle: org/openide/util/Bundle.properties
4
OpenIDE-Module-Specification-Version: 8.16
4
OpenIDE-Module-Specification-Version: 8.17
5
5
(-)a/openide.util/src/org/openide/util/NetworkSettings.java (-55 / +107 lines)
Lines 41-54 Link Here
41
 */
41
 */
42
package org.openide.util;
42
package org.openide.util;
43
43
44
import java.net.InetSocketAddress;
45
import java.net.Proxy;
46
import java.net.ProxySelector;
47
import java.net.URI;
44
import java.net.URI;
48
import java.util.List;
45
import java.util.concurrent.Callable;
49
import java.util.logging.Level;
50
import java.util.logging.Logger;
46
import java.util.logging.Logger;
51
import java.util.prefs.Preferences;
52
47
53
/** Useful static methods for getting Network Proxy required for make network
48
/** Useful static methods for getting Network Proxy required for make network
54
 * connection for specified resource.
49
 * connection for specified resource.
Lines 58-66 Link Here
58
 */
53
 */
59
public final class NetworkSettings {
54
public final class NetworkSettings {
60
55
61
    private static final String PROXY_AUTHENTICATION_USERNAME = "proxyAuthenticationUsername";
62
    private static final String PROXY_AUTHENTICATION_PASSWORD = "proxyAuthenticationPassword";
56
    private static final String PROXY_AUTHENTICATION_PASSWORD = "proxyAuthenticationPassword";
63
    private static final String USE_PROXY_AUTHENTICATION = "useProxyAuthentication";
64
    private static final Logger LOGGER = Logger.getLogger(NetworkSettings.class.getName());
57
    private static final Logger LOGGER = Logger.getLogger(NetworkSettings.class.getName());
65
58
66
    /** Returns the <code>hostname</code> part of network proxy address 
59
    /** Returns the <code>hostname</code> part of network proxy address 
Lines 68-81 Link Here
68
     * Returns <code>null</code> for direct connection.
61
     * Returns <code>null</code> for direct connection.
69
     * 
62
     * 
70
     * @param u The URI that a connection is required to
63
     * @param u The URI that a connection is required to
71
     * @return the hostname part of the Proxy address
64
     * @return the hostname part of the Proxy address or <code>null</code>
72
     */
65
     */
73
    public static String getProxyHost(URI u) {
66
    public static String getProxyHost(URI u) {
74
        if (getPreferences() == null) {
67
        ProxyCredentialsProvider provider = Lookup.getDefault().lookup(ProxyCredentialsProvider.class);
75
            return null;
68
        //assert provider != null : "At least once provider found in lookup " + Lookup.getDefault();
76
        }
69
        LOGGER.warning("No ProxyCredentialsProvider found in lookup " + Lookup.getDefault() + " thus no proxy information will provide!");
77
        InetSocketAddress sa = analyzeProxy(u);
70
        return provider == null ? null : provider.getProxyHost(u);
78
        return sa == null ? null : sa.getHostName();
79
    }
71
    }
80
72
81
    /** Returns the <code>port</code> part of network proxy address 
73
    /** Returns the <code>port</code> part of network proxy address 
Lines 83-153 Link Here
83
     * Returns <code>null</code> for direct connection.
75
     * Returns <code>null</code> for direct connection.
84
     * 
76
     * 
85
     * @param u The URI that a connection is required to
77
     * @param u The URI that a connection is required to
86
     * @return the port part of the Proxy address
78
     * @return the port part of the Proxy address or <code>null</code>
87
     */
79
     */
88
    public static String getProxyPort(URI u) {
80
    public static String getProxyPort(URI u) {
89
        if (getPreferences() == null) {
81
        ProxyCredentialsProvider provider = Lookup.getDefault().lookup(ProxyCredentialsProvider.class);
90
            return null;
82
        //assert provider != null : "At least once provider found in lookup " + Lookup.getDefault();
91
        }
83
        LOGGER.warning("No ProxyCredentialsProvider found in lookup " + Lookup.getDefault() + " thus no proxy information will provide!");
92
        InetSocketAddress sa = analyzeProxy(u);
84
        return provider == null ? null : provider.getProxyPort(u);
93
        return sa == null ? null : Integer.toString(sa.getPort());
94
    }
85
    }
95
86
96
    /** Returns the <code>username</code> for Proxy Authentication.
87
    /** Returns the <code>username</code> for Proxy Authentication.
97
     * Returns <code>null</code> if no authentication required.
88
     * Returns <code>null</code> if no authentication required.
98
     * 
89
     * 
99
     * @param u The URI that a connection is required to
90
     * @param u The URI that a connection is required to
100
     * @return username for Proxy Authentication
91
     * @return username for Proxy Authentication or <code>null</code>
101
     */
92
     */
102
    public static String getAuthenticationUsername(URI u) {
93
    public static String getAuthenticationUsername(URI u) {
103
        if (getPreferences() == null) {
94
        ProxyCredentialsProvider provider = Lookup.getDefault().lookup(ProxyCredentialsProvider.class);
95
        //assert provider != null : "At least once provider found in lookup " + Lookup.getDefault();
96
        LOGGER.warning("No ProxyCredentialsProvider found in lookup " + Lookup.getDefault() + " thus no proxy information will provide!");
97
        if (provider.isProxyAuthentication(u)) {
98
            return provider == null ? null : provider.getProxyUserName(u);
99
        } else {
104
            return null;
100
            return null;
105
        }
101
        }
106
        if (getPreferences().getBoolean(USE_PROXY_AUTHENTICATION, false)) {
107
            return getPreferences().get(PROXY_AUTHENTICATION_USERNAME, "");
108
        }
109
        return null;
110
    }
102
    }
111
    
103
112
    /** Returns the <code>key</code> for reading password for Proxy Authentication.
104
    /** Returns the <code>key</code> for reading password for Proxy Authentication.
113
     * Use {@link Keyring} for reading the password from the ring.
105
     * Use {@link org.netbeans.api.Keyring} for reading the password from the ring.
114
     * Returns <code>null</code> if no authentication required.
106
     * Returns <code>null</code> if no authentication required.
115
     * 
107
     * 
116
     * @param u The URI that a connection is required to
108
     * @param u The URI that a connection is required to
117
     * @return the key for reading password for Proxy Authentication from the ring
109
     * @return the key for reading password for Proxy Authentication from the ring or <code>null</code>
118
     */
110
     */
119
    public static String getKeyForAuthenticationPassword(URI u) {
111
    public static String getKeyForAuthenticationPassword(URI u) {
120
        if (getPreferences() == null) {
112
        ProxyCredentialsProvider provider = Lookup.getDefault().lookup(ProxyCredentialsProvider.class);
113
        //assert provider != null : "At least once provider found in lookup " + Lookup.getDefault();
114
        LOGGER.warning("No ProxyCredentialsProvider found in lookup " + Lookup.getDefault() + " thus no proxy information will provide!");
115
        if (provider.isProxyAuthentication(u)) {
116
            return provider == null ? null : PROXY_AUTHENTICATION_PASSWORD;
117
        } else {
121
            return null;
118
            return null;
122
        }
119
        }
123
        if (getPreferences().getBoolean(USE_PROXY_AUTHENTICATION, false)) {
120
    }
124
            return PROXY_AUTHENTICATION_PASSWORD;
121
    private static ThreadLocal<Boolean> authenticationDialogSuppressed = new ThreadLocal<Boolean>();
122
123
    /** Suppress asking user a question about the authentication credentials while
124
     * running <code>blockOfCode</code>. It's a contract with NetBeans implementation
125
     * of <a href="http://download.oracle.com/javase/6/docs/api/java/net/Authenticator.html">Authenticator</a>.
126
     * Tn the case a system is using other Authenticator implementation, it must call {@link #isAuthenticationDialogSuppressed} method. 
127
     * 
128
     * @param blockOfCode {@link Callable} containing code which will be executed while authentication is suppressed
129
     * @return a result of calling of <code>blockOfCode</code> and may throw an exception.
130
     * @throws Exception 
131
     * @see #isAuthenticationDialogSuppressed
132
     * @since 8.17
133
     */
134
    public static <R> R suppressAuthenticationDialog(Callable<R> blockOfCode) throws Exception {
135
        try {
136
            authenticationDialogSuppressed.set(Boolean.TRUE);
137
            return blockOfCode.call();
138
        } finally {
139
            authenticationDialogSuppressed.remove();
125
        }
140
        }
126
        return null;
127
    }
141
    }
128
142
129
    private static Preferences getPreferences() {
143
    /** A utility method for implementations of <a href="http://download.oracle.com/javase/6/docs/api/java/net/Authenticator.html">Authenticator</a>
130
        return NbPreferences.root().node("org/netbeans/core"); // NOI18N
144
     * to suppress asking users a authentication question while running code posted
145
     * in {@link #authenticationDialogSuppressed}.
146
     * 
147
     * @return true while running code posted in {@link #authenticationDialogSuppressed} method.
148
     * @since 8.17
149
     * @see #authenticationDialogSuppressed
150
     */
151
    public static boolean isAuthenticationDialogSuppressed() {
152
        return Boolean.TRUE.equals(authenticationDialogSuppressed.get());
131
    }
153
    }
132
    
154
133
    private static InetSocketAddress analyzeProxy(URI uri) {
155
    /** A SPI abstract class {@link NetworkSettings.ProxyCredentialsProvider} allows
134
        Parameters.notNull("uri", uri);
156
     * NetBeans Platform users to provide own proxy and network credentials separately.
135
        List<Proxy> proxies = ProxySelector.getDefault().select(uri);
157
     * 
136
        assert proxies != null : "ProxySelector cannot return null for " + uri;
158
     * @see <a href="http://wiki.netbeans.org/Authenticator">http://wiki.netbeans.org/Authenticator</a>
137
        assert ! proxies.isEmpty() : "ProxySelector cannot return empty list for " + uri;
159
     * @author Jiri Rechtacek, Ondrej Vrabec
138
        Proxy p = proxies.get(0);
160
     * @since 8.17
139
        if (Proxy.Type.DIRECT == p.type()) {
161
     */
140
            // return null for DIRECT proxy
162
    public static abstract class ProxyCredentialsProvider {
141
            return null;
163
142
        } else {
164
        /** Returns the <code>username</code> for Proxy Authentication.
143
            if (p.address() instanceof InetSocketAddress) {
165
         * Returns <code>null</code> if no authentication required.
144
                // check is
166
         * 
145
                //assert ! ((InetSocketAddress) p.address()).isUnresolved() : p.address() + " must be resolved address.";
167
         * @param u The URI that a connection is required to
146
                return (InetSocketAddress) p.address();
168
         * @return username for Proxy Authentication
147
            } else {
169
         */
148
                LOGGER.log(Level.INFO, p.address() + " is not instanceof InetSocketAddress but " + p.address().getClass());
170
        protected abstract String getProxyUserName(URI u);
149
                return null;
171
150
            }
172
        /** Returns the <code>password</code> for Proxy Authentication.
151
        }
173
         * Returns <code>null</code> if no authentication required.
174
         * 
175
         * @param u The URI that a connection is required to
176
         * @return password for Proxy Authentication
177
         */
178
        protected abstract char[] getProxyPassword(URI u);
179
180
        /** Returns <code>true</code> if Proxy Authentication is required.
181
         * 
182
         * @param u The URI that a connection is required to
183
         * @return <code>true</code> if authentication required.
184
         */
185
        protected abstract boolean isProxyAuthentication(URI u);
186
187
        /** Returns the <code>hostname</code> part of network proxy address 
188
         * based on given URI to access the resource at.
189
         * Returns <code>null</code> for direct connection.
190
         * 
191
         * @param u The URI that a connection is required to
192
         * @return the hostname part of the Proxy address or <code>null</code>
193
         */
194
        protected abstract String getProxyHost(URI u);
195
196
        /** Returns the <code>port</code> part of network proxy address 
197
         * based on given URI to access the resource at.
198
         * Returns <code>null</code> for direct connection.
199
         * 
200
         * @param u The URI that a connection is required to
201
         * @return the port part of the Proxy address or <code>null</code>
202
         */
203
        protected abstract String getProxyPort(URI u);
152
    }
204
    }
153
}
205
}
(-)a/openide.util/test/unit/src/org/openide/util/NetworkSettingsTest.java (-3 / +158 lines)
Lines 39-63 Link Here
39
 *
39
 *
40
 * Portions Copyrighted 2011 Sun Microsystems, Inc.
40
 * Portions Copyrighted 2011 Sun Microsystems, Inc.
41
 */
41
 */
42
43
package org.openide.util;
42
package org.openide.util;
44
43
45
import java.net.InetSocketAddress;
44
import java.net.InetSocketAddress;
45
import java.net.PasswordAuthentication;
46
import java.net.URISyntaxException;
46
import java.net.URISyntaxException;
47
import java.net.UnknownHostException;
47
import java.util.Collections;
48
import java.util.Collections;
48
import java.io.IOException;
49
import java.io.IOException;
50
import java.net.Authenticator;
51
import java.net.Inet4Address;
49
import java.net.Proxy;
52
import java.net.Proxy;
50
import java.net.ProxySelector;
53
import java.net.ProxySelector;
51
import java.net.SocketAddress;
54
import java.net.SocketAddress;
52
import java.net.URI;
55
import java.net.URI;
53
import java.util.List;
56
import java.util.List;
57
import java.util.concurrent.Callable;
54
import junit.framework.TestCase;
58
import junit.framework.TestCase;
59
import org.netbeans.junit.MockServices;
60
import org.openide.util.NetworkSettings.ProxyCredentialsProvider;
55
61
56
/**
62
/**
57
 *
63
 *
58
 * @author Jiri Rechtacek
64
 * @author Jiri Rechtacek, Ondrej Vrabec
59
 */
65
 */
60
public class NetworkSettingsTest extends TestCase {
66
public class NetworkSettingsTest extends TestCase {
67
61
    private static ProxySelector defaultPS;
68
    private static ProxySelector defaultPS;
62
69
63
    public NetworkSettingsTest(String name) {
70
    public NetworkSettingsTest(String name) {
Lines 91-96 Link Here
91
            }
98
            }
92
        };
99
        };
93
        ProxySelector.setDefault(ps);
100
        ProxySelector.setDefault(ps);
101
        MockServices.setServices(MyProxyCredentialsProvider.class);
94
    }
102
    }
95
103
96
    @Override
104
    @Override
Lines 105-111 Link Here
105
    }
113
    }
106
114
107
    public void testGetProxyForRemote() throws URISyntaxException {
115
    public void testGetProxyForRemote() throws URISyntaxException {
108
        URI u = new URI("http://remove.org");
116
        URI u = new URI("http://remote.org");
109
        assertEquals("Check NetworkSettings.getProxyHost() for " + u, "corpcache.cache", NetworkSettings.getProxyHost(u));
117
        assertEquals("Check NetworkSettings.getProxyHost() for " + u, "corpcache.cache", NetworkSettings.getProxyHost(u));
110
        assertEquals("Check NetworkSettings.getProxyPort() for " + u, "1234", NetworkSettings.getProxyPort(u));
118
        assertEquals("Check NetworkSettings.getProxyPort() for " + u, "1234", NetworkSettings.getProxyPort(u));
111
    }
119
    }
Lines 116-119 Link Here
116
        assertNull("NetworkSettings.getProxyPort() returns null for " + u, NetworkSettings.getProxyPort(u));
124
        assertNull("NetworkSettings.getProxyPort() returns null for " + u, NetworkSettings.getProxyPort(u));
117
    }
125
    }
118
126
127
    public void testIsAuthenticationDialogNotSuppressed() throws Exception {
128
        final boolean[] suppressed = new boolean[1];
129
        Authenticator.setDefault(new Authenticator() {
130
131
            @Override
132
            protected PasswordAuthentication getPasswordAuthentication() {
133
                suppressed[0] = NetworkSettings.isAuthenticationDialogSuppressed();
134
                return super.getPasswordAuthentication();
135
            }
136
        });
137
138
        Authenticator.requestPasswordAuthentication("localhost", Inet4Address.getLocalHost(), 1234, "http", null, "http");
139
        assertFalse(suppressed[0]);
140
    }
141
142
    public void testIsAuthenticationDialogSuppressed() throws Exception {
143
        final boolean[] suppressed = new boolean[1];
144
        Authenticator.setDefault(new Authenticator() {
145
146
            @Override
147
            protected PasswordAuthentication getPasswordAuthentication() {
148
                suppressed[0] = NetworkSettings.isAuthenticationDialogSuppressed();
149
                return super.getPasswordAuthentication();
150
            }
151
        });
152
153
        Callable<Void> callable = new Callable<Void>() {
154
155
            @Override
156
            public Void call() throws Exception {
157
                Authenticator.requestPasswordAuthentication("localhost", Inet4Address.getLocalHost(), 1234, "http", null, "http");
158
                return null;
159
            }
160
        };
161
        NetworkSettings.suppressAuthenticationDialog(callable);
162
        assertTrue(suppressed[0]);
163
    }
164
165
    @SuppressWarnings("SleepWhileInLoop")
166
    public void testIsAuthenticationDialogSuppressedExclusive() throws InterruptedException, UnknownHostException {
167
        final boolean[] suppressed = new boolean[1];
168
        Authenticator.setDefault(new Authenticator() {
169
170
            @Override
171
            protected PasswordAuthentication getPasswordAuthentication() {
172
                suppressed[0] = NetworkSettings.isAuthenticationDialogSuppressed();
173
                return super.getPasswordAuthentication();
174
            }
175
        });
176
177
        final boolean[] barrier = new boolean[2];
178
        Thread t = new Thread(new Runnable() {
179
180
            @Override
181
            public void run() {
182
                Callable<Void> callable = new Callable<Void>() {
183
184
                    @Override
185
                    public Void call() throws Exception {
186
                        barrier[0] = true;
187
                        while (!barrier[1]) {
188
                            Thread.sleep(100);
189
                        }
190
                        Authenticator.requestPasswordAuthentication("localhost", Inet4Address.getLocalHost(), 1234, "http", null, "http");
191
                        return null;
192
                    }
193
                };
194
                try {
195
                    NetworkSettings.suppressAuthenticationDialog(callable);
196
                } catch (Exception ex) {
197
                    Exceptions.printStackTrace(ex);
198
                }
199
            }
200
        });
201
        t.start();
202
        while (!barrier[0]) {
203
            Thread.sleep(100);
204
        }
205
        Authenticator.requestPasswordAuthentication("localhost", Inet4Address.getLocalHost(), 1234, "http", null, "http");
206
        assertFalse(suppressed[0]);
207
        barrier[1] = true;
208
        t.join();
209
        assertTrue(suppressed[0]);
210
    }
211
    
212
    public void testNoProxyCredentialsProviderFound() throws URISyntaxException {
213
        MockServices.setServices();
214
        URI localURI = new URI("http://localhost");
215
        assertNull("NetworkSettings.getProxyHost() returns null for " + localURI, NetworkSettings.getProxyHost(localURI));
216
        assertNull("NetworkSettings.getProxyPort() returns null for " + localURI, NetworkSettings.getProxyPort(localURI));
217
        URI remoteURI = new URI("http://remove.org");
218
        assertNull("NetworkSettings.getProxyHost() returns null for " + remoteURI, NetworkSettings.getProxyHost(localURI));
219
        assertNull("NetworkSettings.getProxyHost() returns null for " + remoteURI, NetworkSettings.getProxyHost(localURI));
220
        URI intraURI = new URI("http://inner.private.web");
221
        assertNull("NetworkSettings.getProxyHost() returns null for " + intraURI, NetworkSettings.getProxyHost(intraURI));
222
        assertNull("NetworkSettings.getProxyPort() returns null for " + intraURI, NetworkSettings.getProxyPort(intraURI));
223
    }
224
    
225
    public static class MyProxyCredentialsProvider extends ProxyCredentialsProvider {
226
227
        @Override
228
        protected String getProxyUserName(URI u) {
229
            throw new UnsupportedOperationException("Not supported yet.");
230
        }
231
232
        @Override
233
        protected char[] getProxyPassword(URI u) {
234
            throw new UnsupportedOperationException("Not supported yet.");
235
        }
236
237
        @Override
238
        protected boolean isProxyAuthentication(URI u) {
239
            return false;
240
        }
241
242
        @Override
243
        protected String getProxyHost(URI u) {
244
            InetSocketAddress sa = analyzeProxy(u);
245
            return sa == null ? null : sa.getHostName();
246
        }
247
248
        @Override
249
        protected String getProxyPort(URI u) {
250
            InetSocketAddress sa = analyzeProxy(u);
251
            return sa == null ? null : Integer.toString(sa.getPort());
252
        }
253
    }
254
255
    private static InetSocketAddress analyzeProxy(URI uri) {
256
        Parameters.notNull("uri", uri);
257
        List<Proxy> proxies = ProxySelector.getDefault().select(uri);
258
        assert proxies != null : "ProxySelector cannot return null for " + uri;
259
        assert !proxies.isEmpty() : "ProxySelector cannot return empty list for " + uri;
260
        Proxy p = proxies.get(0);
261
        if (Proxy.Type.DIRECT == p.type()) {
262
            // return null for DIRECT proxy
263
            return null;
264
        } else {
265
            if (p.address() instanceof InetSocketAddress) {
266
                // check is
267
                //assert ! ((InetSocketAddress) p.address()).isUnresolved() : p.address() + " must be resolved address.";
268
                return (InetSocketAddress) p.address();
269
            } else {
270
                return null;
271
            }
272
        }
273
    }
119
}
274
}

Return to bug 201662