Debian Codegraph @

src/main/java/capsule/SystemProxySelector.java

raw symbols SCIP
1 package capsule;
2
3 import java.util.Map;
4 import java.util.Properties;
5
6 import org.eclipse.aether.repository.Proxy;
7 import org.eclipse.aether.repository.ProxySelector;
8 import org.eclipse.aether.repository.RemoteRepository;
9 import org.eclipse.aether.util.repository.AuthenticationBuilder;
10 import org.eclipse.aether.util.repository.DefaultProxySelector;
11
12 /**
13 * A proxy selector that uses proxy configuration defined
14 * in the system environment configuration and Java
15 * system properties.
16 *
17 *
18 * @author Paolo Di Tommaso <[email protected]>
19 * @author [email protected]
20 */
21 public class SystemProxySelector implements ProxySelector {
22 private static final int LOG_NONE = 0;
23 private static final int LOG_QUIET = 1;
24 private static final int LOG_VERBOSE = 2;
25 private static final int LOG_DEBUG = 3;
26 private static final String LOG_PREFIX = "CAPSULE: ";
27 private final int logLevel;
28
29 private final DefaultProxySelector target = new DefaultProxySelector();
30 private final Map<String, String> env;
31 private final Properties props;
32 private int count;
33
34 /**
35 * Create a proxy selector object looking for proxy settings in the environment
36 * configurations and the Java system properties. It checks the following properties:
37 *
38 * <li>http.proxyHost (Java system property) </li>
39 * <li>https.proxyHost (Java system property) </li>
40 * <li>http_proxy (environment variable)</li>
41 * <li>https_proxy (environment variable)</li>
42 * <li>HTTP_PROXY (environment variable)</li>
43 * <li>HTTPS_PROXY (environment variable)</li>
44 *
45 * @param logLevel Capsule logging level
46 */
47 public SystemProxySelector(int logLevel) {
48 this(System.getenv(), System.getProperties(), logLevel);
49 }
50
51 @SuppressWarnings("OverridableMethodCallInConstructor")
52 SystemProxySelector(Map env, Properties props, int logLevel) {
53 this.env = env;
54 this.props = props;
55 this.logLevel = logLevel;
56 init();
57 }
58
59 @Override
60 public Proxy getProxy(RemoteRepository repository) {
61 return target.getProxy(repository);
62 }
63
64 protected void init() {
65 addProxyFromProperties("http");
66 addProxyFromProperties("https");
67
68 addProxyFromEnv("http");
69 addProxyFromEnv("https");
70
71 addProxyFromEnv("HTTP");
72 addProxyFromEnv("HTTPS");
73 }
74
75 protected void addProxyFromEnv(String type) {
76 checkValidProtocol(type);
77
78 String key = type + (isUpper(type) ? "_PROXY" : "_proxy");
79 String[] items = parseProxy(env.get(key), "http".equals(type) ? "80" : "443");
80 if (items == null)
81 return;
82
83 String[] credentials = parseCredentials(env.get(key));
84
85 String noProxy = env.get(isUpper(type) ? "NO_PROXY" : "no_proxy");
86
87 if (noProxy != null)
88 noProxy = noProxy.replace(',', '|');
89
90 Proxy proxy;
91 if (credentials != null) {
92 AuthenticationBuilder authenticationBuilder = new AuthenticationBuilder();
93 authenticationBuilder.addUsername(credentials[0]);
94 if (credentials[1] != null)
95 authenticationBuilder.addPassword(credentials[1]);
96
97 proxy = new Proxy(type.toLowerCase(), items[0], Integer.parseInt(items[1]), authenticationBuilder.build());
98 } else {
99 proxy = new Proxy(type.toLowerCase(), items[0], Integer.parseInt(items[1]));
100 }
101 target.add(proxy, noProxy);
102 count++;
103
104 // dump proxy information to logger
105 if (isLogging(LOG_VERBOSE))
106 log(LOG_VERBOSE, String.format("Adding `%s` proxy: %s [from system environment]", type, proxy));
107
108 }
109
110 protected void addProxyFromProperties(String type) {
111 checkValidProtocol(type);
112
113 String host = props.getProperty(type + ".proxyHost");
114 if (host == null || host.isEmpty())
115 return; // nothing to do
116
117 String port = props.getProperty(type + ".proxyPort");
118 if (port == null || port.isEmpty())
119 port = "http".equals(type) ? "80" : "443";
120
121 String nonProxy = props.getProperty(type + ".nonProxyHosts");
122 String username = props.getProperty(type + ".proxyUser");
123 String password = props.getProperty(type + ".proxyPassword");
124
125 Proxy proxy;
126 if (username != null) {
127 AuthenticationBuilder authenticationBuilder = new AuthenticationBuilder();
128 authenticationBuilder.addUsername(username);
129 if (password != null)
130 authenticationBuilder.addPassword(password);
131
132 proxy = new Proxy(type, host, Integer.parseInt(port), authenticationBuilder.build());
133 } else {
134 proxy = new Proxy(type, host, Integer.parseInt(port));
135 }
136 // append this proxy to the target selector
137 target.add(proxy, nonProxy);
138 count++;
139
140 // dump proxy information to logger
141 if (isLogging(LOG_VERBOSE))
142 log(LOG_VERBOSE, String.format("Adding `%s` proxy: %s [from Java system properties]", type, proxy));
143 }
144
145 /**
146 * Given a proxy URL returns a two element arrays containing the user name and the password. The second component
147 * of the array is null if no password is specified.
148 *
149 * @param url The proxy host URL.
150 * @return An array containing the user name and the password or null when none are present or the url is empty.
151 */
152 static String[] parseCredentials(String url) {
153 String[] result = new String[2];
154
155 if (url == null || url.isEmpty())
156 return null;
157
158 int p = url.indexOf("://");
159 if (p != -1)
160 url = url.substring(p + 3);
161
162 if ((p = url.indexOf('@')) != -1) {
163 String credentials = url.substring(0, p);
164
165 if ((p = credentials.indexOf(':')) != -1) {
166 result[0] = credentials.substring(0, p);
167 result[1] = credentials.substring(p + 1);
168 } else {
169 result[0] = credentials;
170 }
171 } else {
172 return null;
173 }
174
175 return result;
176 }
177
178 /**
179 * Given a proxy URL returns a two element arrays containing the host name and the port
180 *
181 * @param url The proxy host URL.
182 * @param defPort The default proxy port
183 * @return An array containing the host name and the proxy port or null when url is empty
184 */
185 static String[] parseProxy(String url, String defPort) {
186 String[] result = new String[2];
187
188 if (url == null || url.isEmpty())
189 return null;
190
191 int p = url.indexOf("://");
192 if (p != -1)
193 url = url.substring(p + 3);
194
195 if ((p = url.indexOf('@')) != -1)
196 url = url.substring(p + 1);
197
198 if ((p = url.indexOf(':')) != -1) {
199 result[0] = url.substring(0, p);
200 result[1] = url.substring(p + 1);
201 } else {
202 result[0] = url;
203 result[1] = defPort;
204 }
205
206 // remove trailing slash from the host name
207 p = result[0].indexOf("/");
208 if( p != -1 ) {
209 result[0] = result[0].substring(0,p);
210 }
211
212 // remove trailing slash from the port number
213 p = result[1].indexOf("/");
214 if( p != -1 ) {
215 result[1] = result[1].substring(0,p);
216 }
217
218 return result;
219 }
220
221 /**
222 * @return The number of defined proxy
223 */
224 int getCount() {
225 return count;
226 }
227
228 /**
229 * @return {@code true} if the selector defines at least one proxy server
230 */
231 public boolean isValid() {
232 return count > 0;
233 }
234
235 private boolean isUpper(String str) {
236 for (int i = 0; i < str.length(); i++) {
237 if (Character.isLowerCase(str.charAt(i)))
238 return false;
239 }
240 return true;
241 }
242
243 protected final boolean isLogging(int level) {
244 return level <= logLevel;
245 }
246
247 protected final void log(int level, String str) {
248 if (isLogging(level))
249 System.err.println(LOG_PREFIX + str);
250 }
251
252 protected void checkValidProtocol(String protocol) {
253 if ("http".equalsIgnoreCase(protocol))
254 return;
255 if ("https".equalsIgnoreCase(protocol))
256 return;
257 throw new IllegalArgumentException(String.format("Illegal proxy protocol: '%s'", protocol));
258 }
259 }