X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;ds=sidebyside;f=src%2Fbe%2Fnikiroo%2Futils%2FProxy.java;fp=src%2Fbe%2Fnikiroo%2Futils%2FProxy.java;h=750b3eef6a37c874b69b485e8f30b9c41ca47946;hb=d46b7b96f94e88a776bcd2dfd756549ffb300cc9;hp=0000000000000000000000000000000000000000;hpb=c9994f27667bc421bcd448d39e55774fddf5c431;p=fanfix.git diff --git a/src/be/nikiroo/utils/Proxy.java b/src/be/nikiroo/utils/Proxy.java new file mode 100644 index 0000000..750b3ee --- /dev/null +++ b/src/be/nikiroo/utils/Proxy.java @@ -0,0 +1,150 @@ +package be.nikiroo.utils; + +import java.net.Authenticator; +import java.net.PasswordAuthentication; + +/** + * Simple proxy helper to select a default internet proxy. + * + * @author niki + */ +public class Proxy { + /** + * Use the proxy described by this string: + * + * Some examples: + * + * + * @param proxy + * the proxy + */ + static public void use(String proxy) { + if (proxy != null && !proxy.isEmpty()) { + String user = null; + String password = null; + int port = 8080; + + if (proxy.contains("@")) { + int pos = proxy.indexOf("@"); + user = proxy.substring(0, pos); + proxy = proxy.substring(pos + 1); + if (user.contains(":")) { + pos = user.indexOf(":"); + password = user.substring(pos + 1); + user = user.substring(0, pos); + } + } + + if (proxy.equals(":")) { + proxy = null; + } else if (proxy.contains(":")) { + int pos = proxy.indexOf(":"); + try { + port = Integer.parseInt(proxy.substring(0, pos)); + proxy = proxy.substring(pos + 1); + } catch (Exception e) { + } + } + + if (proxy == null) { + Proxy.useSystemProxy(user, password); + } else { + Proxy.useProxy(proxy, port, user, password); + } + } + } + + /** + * Use the system proxy. + */ + static public void useSystemProxy() { + useSystemProxy(null, null); + } + + /** + * Use the system proxy with the given login/password, for authenticated + * proxies. + * + * @param user + * the user name or login + * @param password + * the password + */ + static public void useSystemProxy(String user, String password) { + System.setProperty("java.net.useSystemProxies", "true"); + auth(user, password); + } + + /** + * Use the give proxy. + * + * @param host + * the proxy host name or IP address + * @param port + * the port to use + */ + static public void useProxy(String host, int port) { + useProxy(host, port, null, null); + } + + /** + * Use the given proxy with the given login/password, for authenticated + * proxies. + * + * @param user + * the user name or login + * @param password + * the password + * @param host + * the proxy host name or IP address + * @param port + * the port to use + * @param user + * the user name or login + * @param password + * the password + */ + static public void useProxy(String host, int port, String user, + String password) { + System.setProperty("http.proxyHost", host); + System.setProperty("http.proxyPort", Integer.toString(port)); + auth(user, password); + } + + /** + * Select the default authenticator for proxy requests. + * + * @param user + * the user name or login + * @param password + * the password + */ + static private void auth(final String user, final String password) { + if (user != null && password != null) { + Authenticator proxy = new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + if (getRequestorType() == RequestorType.PROXY) { + return new PasswordAuthentication(user, + password.toCharArray()); + } + return null; + } + }; + Authenticator.setDefault(proxy); + } + } +}