|
20 | 20 | import com.google.api.client.util.Preconditions;
|
21 | 21 | import com.google.api.client.util.SecurityUtils;
|
22 | 22 | import com.google.api.client.util.SslUtils;
|
23 |
| - |
24 | 23 | import java.io.IOException;
|
25 | 24 | import java.io.InputStream;
|
26 | 25 | import java.net.HttpURLConnection;
|
| 26 | +import java.net.InetSocketAddress; |
27 | 27 | import java.net.Proxy;
|
28 | 28 | import java.net.URL;
|
29 | 29 | import java.security.GeneralSecurityException;
|
30 | 30 | import java.security.KeyStore;
|
31 | 31 | import java.security.cert.CertificateFactory;
|
32 | 32 | import java.util.Arrays;
|
33 |
| - |
34 | 33 | import javax.net.ssl.HostnameVerifier;
|
35 | 34 | import javax.net.ssl.HttpsURLConnection;
|
36 | 35 | import javax.net.ssl.SSLContext;
|
|
60 | 59 | * @author Yaniv Inbar
|
61 | 60 | */
|
62 | 61 | public final class NetHttpTransport extends HttpTransport {
|
| 62 | + private static Proxy defaultProxy() { |
| 63 | + return new Proxy( |
| 64 | + Proxy.Type.HTTP, new InetSocketAddress( |
| 65 | + System.getProperties().getProperty("https.proxyHost"), |
| 66 | + Integer.parseInt(System.getProperties().getProperty("https.proxyPort")))); |
| 67 | + } |
63 | 68 |
|
64 | 69 | /**
|
65 | 70 | * All valid request methods as specified in {@link HttpURLConnection#setRequestMethod}, sorted in
|
@@ -118,11 +123,21 @@ public NetHttpTransport() {
|
118 | 123 | NetHttpTransport(ConnectionFactory connectionFactory,
|
119 | 124 | SSLSocketFactory sslSocketFactory, HostnameVerifier hostnameVerifier) {
|
120 | 125 | this.connectionFactory =
|
121 |
| - connectionFactory == null ? new DefaultConnectionFactory() : connectionFactory; |
| 126 | + getConnectionFactory(connectionFactory); |
122 | 127 | this.sslSocketFactory = sslSocketFactory;
|
123 | 128 | this.hostnameVerifier = hostnameVerifier;
|
124 | 129 | }
|
125 | 130 |
|
| 131 | + private ConnectionFactory getConnectionFactory(ConnectionFactory connectionFactory) { |
| 132 | + if (connectionFactory == null) { |
| 133 | + if (System.getProperties().getProperty("com.api.client.should_use_proxy") != null) { |
| 134 | + return new DefaultConnectionFactory(defaultProxy()); |
| 135 | + } |
| 136 | + return new DefaultConnectionFactory(); |
| 137 | + } |
| 138 | + return connectionFactory; |
| 139 | + } |
| 140 | + |
126 | 141 | @Override
|
127 | 142 | public boolean supportsMethod(String method) {
|
128 | 143 | return Arrays.binarySearch(SUPPORTED_METHODS, method) >= 0;
|
@@ -314,9 +329,12 @@ public Builder setHostnameVerifier(HostnameVerifier hostnameVerifier) {
|
314 | 329 |
|
315 | 330 | /** Returns a new instance of {@link NetHttpTransport} based on the options. */
|
316 | 331 | public NetHttpTransport build() {
|
317 |
| - return proxy == null |
| 332 | + if (System.getProperties().getProperty("com.google.api.client.should_use_proxy") != null) { |
| 333 | + setProxy(defaultProxy()); |
| 334 | + } |
| 335 | + return this.proxy == null |
318 | 336 | ? new NetHttpTransport(connectionFactory, sslSocketFactory, hostnameVerifier)
|
319 |
| - : new NetHttpTransport(proxy, sslSocketFactory, hostnameVerifier); |
| 337 | + : new NetHttpTransport(this.proxy, sslSocketFactory, hostnameVerifier); |
320 | 338 | }
|
321 | 339 | }
|
322 | 340 | }
|
0 commit comments