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 |
} |