Repository navigation
Expand file tree
/
Copy pathProxy.java
More file actions
347 lines (314 loc) · 10.3 KB
/
Copy pathProxy.java
File metadata and controls
347 lines (314 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.openqa.selenium;
import java.util.Map;
/**
* Configuration parameters for using proxies in WebDriver. Generally you should pass an object of
* this type to a WebDriver constructor, or in some cases to the profile object used in the
* WebDriver construction. For simplicity, setting values here commits the proxy to a certain
* configuration. That is, it is an error to set an <code>httpProxy</code> manually and then turn
* on proxy autodetect.
*/
public class Proxy {
public enum ProxyType {
// Keep these in sync with the Firefox preferences numbers:
// http://kb.mozillazine.org/Network.proxy.type
DIRECT, // Direct connection, no proxy (default on Windows)
MANUAL, // Manual proxy settings (e.g. for httpProxy)
PAC, // Proxy auto-configuration from URL
RESERVED_1, // Never used (but reserved in Firefox)
AUTODETECT, // Proxy auto-detection (presumably with WPAD)
SYSTEM, // Use system settings (default on Linux)
UNSPECIFIED
}
private ProxyType proxyType = ProxyType.UNSPECIFIED;
private boolean autodetect = false;
private String ftpProxy;
private String httpProxy;
private String noProxy;
private String sslProxy;
private String socksProxy;
private String socksUsername;
private String socksPassword;
private String proxyAutoconfigUrl;
public Proxy() {
// Empty default constructor
}
public Proxy(Map<String, ?> raw) {
if (raw.containsKey("proxyType") && raw.get("proxyType") != null) {
setProxyType(ProxyType.valueOf(((String) raw.get("proxyType")).toUpperCase()));
}
if (raw.containsKey("ftpProxy") && raw.get("ftpProxy") != null) {
setFtpProxy((String) raw.get("ftpProxy"));
}
if (raw.containsKey("httpProxy") && raw.get("httpProxy") != null) {
setHttpProxy((String) raw.get("httpProxy"));
}
if (raw.containsKey("noProxy") && raw.get("noProxy") != null) {
setNoProxy((String) raw.get("noProxy"));
}
if (raw.containsKey("sslProxy") && raw.get("sslProxy") != null) {
setSslProxy((String) raw.get("sslProxy"));
}
if (raw.containsKey("socksProxy") && raw.get("socksProxy") != null) {
setSocksProxy((String) raw.get("socksProxy"));
}
if (raw.containsKey("socksUsername") && raw.get("socksUsername") != null) {
setSocksUsername((String) raw.get("socksUsername"));
}
if (raw.containsKey("socksPassword") && raw.get("socksPassword") != null) {
setSocksPassword((String) raw.get("socksPassword"));
}
if (raw.containsKey("proxyAutoconfigUrl") && raw.get("proxyAutoconfigUrl") != null) {
setProxyAutoconfigUrl((String) raw.get("proxyAutoconfigUrl"));
}
if (raw.containsKey("autodetect") && raw.get("autodetect") != null) {
setAutodetect((Boolean) raw.get("autodetect"));
}
}
/**
* Gets the {@link ProxyType}. This can signal if set to use a direct connection (without proxy),
* manually set proxy settings, auto-configured proxy settings, or whether to use the default
* system proxy settings. It defaults to {@link ProxyType#UNSPECIFIED}.
*
* @return the proxy type employed
*/
public ProxyType getProxyType() {
return this.proxyType;
}
/**
* Explicitly sets the proxy type, useful for forcing direct connection on Linux.
*
* @param proxyType type of proxy being used
* @return reference to self
*/
public Proxy setProxyType(ProxyType proxyType) {
verifyProxyTypeCompatibility(proxyType);
this.proxyType = proxyType;
return this;
}
/**
* Whether to autodetect proxy settings.
*
* @return true if set to autodetect proxy settings, false otherwise
*/
public boolean isAutodetect() {
return autodetect;
}
/**
* Specifies whether to autodetect proxy settings.
*
* @param autodetect set to true to use proxy auto detection, false to leave proxy settings
* unspecified
* @return reference to self
*/
public Proxy setAutodetect(boolean autodetect) {
if (this.autodetect == autodetect) {
return this;
}
if (autodetect) {
verifyProxyTypeCompatibility(ProxyType.AUTODETECT);
this.proxyType = ProxyType.AUTODETECT;
} else {
this.proxyType = ProxyType.UNSPECIFIED;
}
this.autodetect = autodetect;
return this;
}
/**
* Gets the FTP proxy.
*
* @return the FTP proxy hostname if present, or null if not set
*/
public String getFtpProxy() {
return ftpProxy;
}
/**
* Specify which proxy to use for FTP connections.
*
* @param ftpProxy the proxy host, expected format is <code>hostname.com:1234</code>
* @return reference to self
*/
public Proxy setFtpProxy(String ftpProxy) {
verifyProxyTypeCompatibility(ProxyType.MANUAL);
this.proxyType = ProxyType.MANUAL;
this.ftpProxy = ftpProxy;
return this;
}
/**
* Gets the HTTP proxy.
*
* @return the HTTP proxy hostname if present, or null if not set
*/
public String getHttpProxy() {
return httpProxy;
}
/**
* Specify which proxy to use for HTTP connections.
*
* @param httpProxy the proxy host, expected format is <code>hostname:1234</code>
* @return reference to self
*/
public Proxy setHttpProxy(String httpProxy) {
verifyProxyTypeCompatibility(ProxyType.MANUAL);
this.proxyType = ProxyType.MANUAL;
this.httpProxy = httpProxy;
return this;
}
/**
* Gets proxy bypass (noproxy) addresses.
*
* @return The proxy bypass (noproxy) addresses
*/
public String getNoProxy() {
return noProxy;
}
/**
* Sets proxy bypass (noproxy) addresses
*
* @param noProxy The proxy bypass (noproxy) addresses
* @return reference to self
*/
public Proxy setNoProxy(String noProxy) {
verifyProxyTypeCompatibility(ProxyType.MANUAL);
this.proxyType = ProxyType.MANUAL;
this.noProxy = noProxy;
return this;
}
/**
* Gets the SSL tunnel proxy.
*
* @return the SSL tunnel proxy hostname if present, null otherwise
*/
public String getSslProxy() {
return sslProxy;
}
/**
* Specify which proxy to use for SSL connections.
*
* @param sslProxy the proxy host, expected format is <code>hostname.com:1234</code>
* @return reference to self
*/
public Proxy setSslProxy(String sslProxy) {
verifyProxyTypeCompatibility(ProxyType.MANUAL);
this.proxyType = ProxyType.MANUAL;
this.sslProxy = sslProxy;
return this;
}
/**
* Gets the SOCKS proxy.
*
* @return the SOCKS proxy if present, null otherwise
*/
public String getSocksProxy() {
return socksProxy;
}
/**
* Specifies which proxy to use for SOCKS.
*
* @param socksProxy the proxy host, expected format is <code>hostname.com:1234</code>
* @return reference to self
*/
public Proxy setSocksProxy(String socksProxy) {
verifyProxyTypeCompatibility(ProxyType.MANUAL);
this.proxyType = ProxyType.MANUAL;
this.socksProxy = socksProxy;
return this;
}
/**
* Gets the SOCKS proxy's username. Supported by SOCKS v5 and above.
*
* @return the SOCKS proxy's username
*/
public String getSocksUsername() {
return socksUsername;
}
/**
* Specifies a username for the SOCKS proxy. Supported by SOCKS v5 and above.
*
* @param username username for the SOCKS proxy
* @return reference to self
*/
public Proxy setSocksUsername(String username) {
verifyProxyTypeCompatibility(ProxyType.MANUAL);
this.proxyType = ProxyType.MANUAL;
this.socksUsername = username;
return this;
}
/**
* Gets the SOCKS proxy's password. Supported by SOCKS v5 and above.
*
* @return the SOCKS proxy's password
*/
public String getSocksPassword() {
return socksPassword;
}
/**
* Specifies a password for the SOCKS proxy. Supported by SOCKS v5 and above.
*
* @param password password for the SOCKS proxy
* @return reference to self
*/
public Proxy setSocksPassword(String password) {
verifyProxyTypeCompatibility(ProxyType.MANUAL);
this.proxyType = ProxyType.MANUAL;
this.socksPassword = password;
return this;
}
/**
* Gets the proxy auto-configuration URL.
*
* @return the proxy auto-configuration URL
*/
public String getProxyAutoconfigUrl() {
return proxyAutoconfigUrl;
}
/**
* Specifies the URL to be used for proxy auto-configuration. Expected format is
* <code>http://hostname.com:1234/pacfile</code>. This is required if {@link #getProxyType()} is
* set to {@link ProxyType#PAC}, ignored otherwise.
*
* @param proxyAutoconfigUrl the URL for proxy auto-configuration
* @return reference to self
*/
public Proxy setProxyAutoconfigUrl(String proxyAutoconfigUrl) {
verifyProxyTypeCompatibility(ProxyType.PAC);
this.proxyType = ProxyType.PAC;
this.proxyAutoconfigUrl = proxyAutoconfigUrl;
return this;
}
private void verifyProxyTypeCompatibility(ProxyType compatibleProxy) {
if (proxyType != ProxyType.UNSPECIFIED && proxyType != compatibleProxy) {
throw new IllegalStateException(String.format(
"Specified proxy type (%s) not compatible with current setting (%s)",
compatibleProxy, proxyType));
}
}
@SuppressWarnings({"unchecked"})
public static Proxy extractFrom(Capabilities capabilities) {
Object rawProxy = capabilities.getCapability("proxy");
Proxy proxy = null;
if (rawProxy != null) {
if (rawProxy instanceof Proxy) {
proxy = (Proxy) rawProxy;
} else if (rawProxy instanceof Map) {
proxy = new Proxy((Map<String, ?>) rawProxy);
}
}
return proxy;
}
}