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 194530
Collapse All | Expand All

(-)a/openide.util/apichanges.xml (+15 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="NetworkSettings">
55
        <api name="util"/>
56
        <summary><code>NetworkSettings</code> added</summary>
57
        <version major="8" minor="13"/>
58
        <date day="1" month="2" year="2011"/>
59
        <author login="jrechtacek"/>
60
        <compatibility addition="yes"/>
61
        <description>
62
            <p>
63
                Added new useful methods for getting Network Proxy for specified URI.
64
            </p>
65
        </description>
66
        <class package="org.openide.util" name="NetworkSettings"/>
67
        <issue number="194530"/>
68
    </change>
54
    <change id="ImageUtilities.URL">
69
    <change id="ImageUtilities.URL">
55
        <api name="util"/>
70
        <api name="util"/>
56
        <summary>ImageUtilities support "url" attribute</summary>
71
        <summary>ImageUtilities support "url" attribute</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.12
4
OpenIDE-Module-Specification-Version: 8.13
5
5
(-)e844a475ba5d (+151 lines)
Added Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2011 Sun Microsystems, Inc.
41
 */
42
package org.openide.util;
43
44
import java.net.InetSocketAddress;
45
import java.net.Proxy;
46
import java.net.ProxySelector;
47
import java.net.URI;
48
import java.util.List;
49
import java.util.logging.Level;
50
import java.util.logging.Logger;
51
import java.util.prefs.Preferences;
52
53
/** Useful static methods for getting Network Proxy required for make network
54
 * connection for specified resource.
55
 *
56
 * @since 8.13
57
 * @author Jiri Rechtacek
58
 */
59
public final class NetworkSettings {
60
61
    private static final String PROXY_AUTHENTICATION_USERNAME = "proxyAuthenticationUsername";
62
    private static final String USE_PROXY_AUTHENTICATION = "useProxyAuthentication";
63
    private static final Logger LOGGER = Logger.getLogger(NetworkSettings.class.getName());
64
65
    /** Returns the <code>hostname</code> part of network proxy address 
66
     * based on given URI to access the resource at.
67
     * Returns <code>null</code> for direct connection.
68
     * 
69
     * @param u The URI that a connection is required to
70
     * @return the hostname part of the Proxy address
71
     */
72
    public static String getProxyHost(URI u) {
73
        if (getPreferences() == null) {
74
            return null;
75
        }
76
        InetSocketAddress sa = analyzeProxy(u);
77
        return sa == null ? null : sa.getHostName();
78
    }
79
80
    /** Returns the <code>port</code> part of network proxy address 
81
     * based on given URI to access the resource at.
82
     * Returns <code>null</code> for direct connection.
83
     * 
84
     * @param u The URI that a connection is required to
85
     * @return the port part of the Proxy address
86
     */
87
    public static String getProxyPort(URI u) {
88
        if (getPreferences() == null) {
89
            return null;
90
        }
91
        InetSocketAddress sa = analyzeProxy(u);
92
        return sa == null ? null : Integer.toString(sa.getPort());
93
    }
94
95
    /** Returns the <code>username</code> for Proxy Authentication.
96
     * Returns <code>null</code> if no authentication required.
97
     * 
98
     * @param u The URI that a connection is required to
99
     * @return username for Proxy Authentication
100
     */
101
    public static String getAuthenticationUsername(URI u) {
102
        if (getPreferences() == null) {
103
            return null;
104
        }
105
        if (getPreferences().getBoolean(USE_PROXY_AUTHENTICATION, false)) {
106
            return getPreferences().get(PROXY_AUTHENTICATION_USERNAME, "");
107
        }
108
        return null;
109
    }
110
    
111
    /** Returns the <code>key</code> for reading password for Proxy Authentication.
112
     * Use {@link Keyring} for reading the password from the ring.
113
     * Returns <code>null</code> if no authentication required.
114
     * 
115
     * @param u The URI that a connection is required to
116
     * @return the key for reading password for Proxy Authentication from the ring
117
     */
118
    public static String getKeyForAuthenticationPassword(URI u) {
119
        if (getPreferences() == null) {
120
            return null;
121
        }
122
        if (getPreferences().getBoolean(USE_PROXY_AUTHENTICATION, false)) {
123
            return PROXY_AUTHENTICATION_USERNAME;
124
        }
125
        return null;
126
    }
127
128
    private static Preferences getPreferences() {
129
        return NbPreferences.root().node("org/netbeans/core"); // NOI18N
130
    }
131
    
132
    private static InetSocketAddress analyzeProxy(URI u) {
133
        List<Proxy> proxies = ProxySelector.getDefault().select(u);
134
        assert proxies != null : "ProxySelector cannot return null for " + u;
135
        assert ! proxies.isEmpty() : "ProxySelector cannot return empty list for " + u;
136
        Proxy p = proxies.get(0);
137
        if (Proxy.Type.DIRECT == p.type()) {
138
            // return null for DIRECT proxy
139
            return null;
140
        } else {
141
            if (p.address() instanceof InetSocketAddress) {
142
                // check is
143
                //assert ! ((InetSocketAddress) p.address()).isUnresolved() : p.address() + " must be resolved address.";
144
                return (InetSocketAddress) p.address();
145
            } else {
146
                LOGGER.log(Level.INFO, p.address() + " is not instanceof InetSocketAddress but " + p.address().getClass());
147
                return null;
148
            }
149
        }
150
    }
151
}
(-)e844a475ba5d (+119 lines)
Added Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2011 Sun Microsystems, Inc.
41
 */
42
43
package org.openide.util;
44
45
import java.net.InetSocketAddress;
46
import java.net.URISyntaxException;
47
import java.util.Collections;
48
import java.io.IOException;
49
import java.net.Proxy;
50
import java.net.ProxySelector;
51
import java.net.SocketAddress;
52
import java.net.URI;
53
import java.util.List;
54
import junit.framework.TestCase;
55
56
/**
57
 *
58
 * @author Jiri Rechtacek
59
 */
60
public class NetworkSettingsTest extends TestCase {
61
    private static ProxySelector defaultPS;
62
63
    public NetworkSettingsTest(String name) {
64
        super(name);
65
    }
66
67
    @Override
68
    public void setUp() {
69
        if (defaultPS == null) {
70
            defaultPS = ProxySelector.getDefault();
71
        }
72
        ProxySelector ps = new ProxySelector() {
73
74
            @Override
75
            public List<Proxy> select(URI uri) {
76
                if (uri == null) {
77
                    return Collections.singletonList(Proxy.NO_PROXY);
78
                }
79
                if (uri.toString().equals("http://localhost")) {
80
                    return Collections.singletonList(Proxy.NO_PROXY);
81
                } else if (uri.toString().startsWith("http://inner")) {
82
                    return Collections.singletonList(Proxy.NO_PROXY);
83
                } else {
84
                    return Collections.singletonList(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("corpcache.cache", 1234)));
85
                }
86
            }
87
88
            @Override
89
            public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
90
                throw new UnsupportedOperationException("Not supported yet.");
91
            }
92
        };
93
        ProxySelector.setDefault(ps);
94
    }
95
96
    @Override
97
    public void tearDown() {
98
        ProxySelector.setDefault(defaultPS);
99
    }
100
101
    public void testGetProxyForLocalhost() throws URISyntaxException {
102
        URI u = new URI("http://localhost");
103
        assertNull("NetworkSettings.getProxyHost() returns null for " + u, NetworkSettings.getProxyHost(u));
104
        assertNull("NetworkSettings.getProxyPort() returns null for " + u, NetworkSettings.getProxyPort(u));
105
    }
106
107
    public void testGetProxyForRemote() throws URISyntaxException {
108
        URI u = new URI("http://remove.org");
109
        assertEquals("Check NetworkSettings.getProxyHost() for " + u, "corpcache.cache", NetworkSettings.getProxyHost(u));
110
        assertEquals("Check NetworkSettings.getProxyPort() for " + u, "1234", NetworkSettings.getProxyPort(u));
111
    }
112
113
    public void testGetProxyForIntra() throws URISyntaxException {
114
        URI u = new URI("http://inner.private.web");
115
        assertNull("NetworkSettings.getProxyHost() returns null for " + u, NetworkSettings.getProxyHost(u));
116
        assertNull("NetworkSettings.getProxyPort() returns null for " + u, NetworkSettings.getProxyPort(u));
117
    }
118
119
}

Return to bug 194530