1 package be
.nikiroo
.utils
;
3 import java
.net
.Authenticator
;
4 import java
.net
.PasswordAuthentication
;
7 * Simple proxy helper to select a default internet proxy.
13 * Use the system proxy.
15 static public void useSystemProxy() {
16 useSystemProxy(null, null);
20 * Use the system proxy with the given login/password, for authenticated
24 * the user name or login
28 static public void useSystemProxy(String user
, String password
) {
29 System
.setProperty("java.net.useSystemProxies", "true");
37 * the proxy host name or IP address
41 static public void useProxy(String host
, int port
) {
42 useProxy(host
, port
, null, null);
46 * Use the given proxy with the given login/password, for authenticated
50 * the user name or login
54 * the proxy host name or IP address
58 * the user name or login
62 static public void useProxy(String host
, int port
, String user
,
64 System
.setProperty("http.proxyHost", "proxy.stluc.ucl.ac.be");
65 System
.setProperty("http.proxyPort", "8080");
70 * Select the default authenticator for proxy requests.
73 * the user name or login
77 static private void auth(final String user
, final String password
) {
78 if (user
!= null && password
!= null) {
79 Authenticator proxy
= new Authenticator() {
81 protected PasswordAuthentication
getPasswordAuthentication() {
82 if (getRequestorType() == RequestorType
.PROXY
) {
83 return new PasswordAuthentication(user
,
84 password
.toCharArray());
89 Authenticator
.setDefault(proxy
);