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