Bundle: fix memory leak at init/reset
[fanfix.git] / Proxy.java
1 package be.nikiroo.utils;
2
3 import java.net.Authenticator;
4 import java.net.PasswordAuthentication;
5
6 /**
7 * Simple proxy helper to select a default internet proxy.
8 *
9 * @author niki
10 */
11 public class Proxy {
12 /**
13 * Use the proxy described by this string:
14 * <ul>
15 * <li><tt>((user(:pass)@)proxy:port)</tt></li>
16 * <li>System proxy is noted <tt>:</tt></li>
17 * </ul>
18 * Some examples:
19 * <ul>
20 * <li><tt></tt> → do not use any proxy</li>
21 * <li><tt>:</tt> → use the system proxy</li>
22 * <li><tt>user@prox.com</tt> → use the proxy "prox.com" with default port
23 * and user "user"</li>
24 * <li><tt>prox.com:8080</tt> → use the proxy "prox.com" on port 8080</li>
25 * <li><tt>user:pass@prox.com:8080</tt> → use "prox.com" on port 8080
26 * authenticated as "user" with password "pass"</li>
27 * <li><tt>user:pass@:</tt> → use the system proxy authenticated as user
28 * "user" with password "pass"</li>
29 * </ul>
30 *
31 * @param proxy
32 * the proxy
33 */
34 static public void use(String proxy) {
35 if (proxy != null && !proxy.isEmpty()) {
36 String user = null;
37 String password = null;
38 int port = 8080;
39
40 if (proxy.contains("@")) {
41 int pos = proxy.indexOf("@");
42 user = proxy.substring(0, pos);
43 proxy = proxy.substring(pos + 1);
44 if (user.contains(":")) {
45 pos = user.indexOf(":");
46 password = user.substring(pos + 1);
47 user = user.substring(0, pos);
48 }
49 }
50
51 if (proxy.equals(":")) {
52 proxy = null;
53 } else if (proxy.contains(":")) {
54 int pos = proxy.indexOf(":");
55 try {
56 port = Integer.parseInt(proxy.substring(0, pos));
57 proxy = proxy.substring(pos + 1);
58 } catch (Exception e) {
59 }
60 }
61
62 if (proxy == null) {
63 Proxy.useSystemProxy(user, password);
64 } else {
65 Proxy.useProxy(proxy, port, user, password);
66 }
67 }
68 }
69
70 /**
71 * Use the system proxy.
72 */
73 static public void useSystemProxy() {
74 useSystemProxy(null, null);
75 }
76
77 /**
78 * Use the system proxy with the given login/password, for authenticated
79 * proxies.
80 *
81 * @param user
82 * the user name or login
83 * @param password
84 * the password
85 */
86 static public void useSystemProxy(String user, String password) {
87 System.setProperty("java.net.useSystemProxies", "true");
88 auth(user, password);
89 }
90
91 /**
92 * Use the give proxy.
93 *
94 * @param host
95 * the proxy host name or IP address
96 * @param port
97 * the port to use
98 */
99 static public void useProxy(String host, int port) {
100 useProxy(host, port, null, null);
101 }
102
103 /**
104 * Use the given proxy with the given login/password, for authenticated
105 * proxies.
106 *
107 * @param user
108 * the user name or login
109 * @param password
110 * the password
111 * @param host
112 * the proxy host name or IP address
113 * @param port
114 * the port to use
115 * @param user
116 * the user name or login
117 * @param password
118 * the password
119 */
120 static public void useProxy(String host, int port, String user,
121 String password) {
122 System.setProperty("http.proxyHost", host);
123 System.setProperty("http.proxyPort", Integer.toString(port));
124 auth(user, password);
125 }
126
127 /**
128 * Select the default authenticator for proxy requests.
129 *
130 * @param user
131 * the user name or login
132 * @param password
133 * the password
134 */
135 static private void auth(final String user, final String password) {
136 if (user != null && password != null) {
137 Authenticator proxy = new Authenticator() {
138 @Override
139 protected PasswordAuthentication getPasswordAuthentication() {
140 if (getRequestorType() == RequestorType.PROXY) {
141 return new PasswordAuthentication(user,
142 password.toCharArray());
143 }
144 return null;
145 }
146 };
147 Authenticator.setDefault(proxy);
148 }
149 }
150 }