8000 enabling https proxy properties · Geisirc/google-http-java-client@ace3289 · GitHub
[go: up one dir, main page]

Skip to content

Commit ace3289

Browse files
Daniel Ribeiromattwhisenhunt
authored andcommitted
enabling https proxy properties
1 parent 395bb17 commit ace3289

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

google-http-client/src/main/java/com/google/api/client/http/javanet/NetHttpTransport.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,16 @@
2020
import com.google.api.client.util.Preconditions;
2121
import com.google.api.client.util.SecurityUtils;
2222
import com.google.api.client.util.SslUtils;
23-
2423
import java.io.IOException;
2524
import java.io.InputStream;
2625
import java.net.HttpURLConnection;
26+
import java.net.InetSocketAddress;
2727
import java.net.Proxy;
2828
import java.net.URL;
2929
import java.security.GeneralSecurityException;
3030
import java.security.KeyStore;
3131
import java.security.cert.CertificateFactory;
3232
import java.util.Arrays;
33-
3433
import javax.net.ssl.HostnameVerifier;
3534
import javax.net.ssl.HttpsURLConnection;
3635
import javax.net.ssl.SSLContext;
@@ -60,6 +59,12 @@
6059
* @author Yaniv Inbar
6160
*/
6261
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+
}
6368

6469
/**
6570
* All valid request methods as specified in {@link HttpURLConnection#setRequestMethod}, sorted in
@@ -118,11 +123,21 @@ public NetHttpTransport() {
118123
NetHttpTransport(ConnectionFactory connectionFactory,
119124
SSLSocketFactory sslSocketFactory, HostnameVerifier hostnameVerifier) {
120125
this.connectionFactory =
121-
connectionFactory == null ? new DefaultConnectionFactory() : connectionFactory;
126+
getConnectionFactory(connectionFactory);
122127
this.sslSocketFactory = sslSocketFactory;
123128
this.hostnameVerifier = hostnameVerifier;
124129
}
125130

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+
126141
@Override
127142
public boolean supportsMethod(String method) {
128143
return Arrays.binarySearch(SUPPORTED_METHODS, method) >= 0;
@@ -314,9 +329,12 @@ public Builder setHostnameVerifier(HostnameVerifier hostnameVerifier) {
314329

315330
/** Returns a new instance of {@link NetHttpTransport} based on the options. */
316331
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
318336
? new NetHttpTransport(connectionFactory, sslSocketFactory, hostnameVerifier)
319-
: new NetHttpTransport(proxy, sslSocketFactory, hostnameVerifier);
337+
: new NetHttpTransport(this.proxy, sslSocketFactory, hostnameVerifier);
320338
}
321339
}
322340
}

0 commit comments

Comments
 (0)
0