Skip to content

Commit d0dbca7

Browse files
authored
feat: add GoogleApacheHttpTransport that uses the v2 ApacheHttpTransport implementation (#1568)
* feat: add GoogleApacheHttpTransport that uses the v2 ApacheHttpTransport implementation * address review feedback * address review comments * address review feedback
1 parent 771cce9 commit d0dbca7

File tree

5 files changed

+104
-1
lines changed

5 files changed

+104
-1
lines changed

google-api-client/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,5 +150,9 @@
150150
<groupId>com.google.guava</groupId>
151151
<artifactId>guava</artifactId>
152152
</dependency>
153+
<dependency>
154+
<groupId>com.google.http-client</groupId>
155+
<artifactId>google-http-client-apache-v2</artifactId>
156+
</dependency>
153157
</dependencies>
154158
</project>

google-api-client/src/main/java/com/google/api/client/googleapis/apache/GoogleApacheHttpTransport.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,15 @@
3636
*
3737
* @since 1.14
3838
* @author Yaniv Inbar
39+
* @deprecated Use com.google.api.client.googleapis.apache.v2.GoogleApacheHttpTransport
3940
*/
41+
@Deprecated
4042
public final class GoogleApacheHttpTransport {
4143

4244
/**
4345
* Returns a new instance of {@link ApacheHttpTransport} that uses
4446
* {@link GoogleUtils#getCertificateTrustStore()} for the trusted certificates.
47+
* @deprecated Use com.google.api.client.googleapis.apache.v2.GoogleApacheHttpTransport.newTrustedTransport()
4548
*/
4649
public static ApacheHttpTransport newTrustedTransport() throws GeneralSecurityException,
4750
IOException {

google-api-client/src/main/java/com/google/api/client/googleapis/apache/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414

1515
/**
16-
* Google API's support based on the Apache HTTP Client.
16+
* Google APIs support based on the Apache HTTP Client.
1717
*
1818
* @since 1.14
1919
* @author Yaniv Inbar
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.api.client.googleapis.apache.v2;
16+
17+
import com.google.api.client.googleapis.GoogleUtils;
18+
import com.google.api.client.http.apache.v2.ApacheHttpTransport;
19+
import com.google.api.client.util.SslUtils;
20+
import java.io.IOException;
21+
import java.net.ProxySelector;
22+
import java.security.GeneralSecurityException;
23+
import java.security.KeyStore;
24+
import java.util.concurrent.TimeUnit;
25+
import javax.net.ssl.SSLContext;
26+
import org.apache.http.client.HttpClient;
27+
import org.apache.http.config.SocketConfig;
28+
import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
29+
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
30+
import org.apache.http.impl.client.HttpClientBuilder;
31+
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
32+
import org.apache.http.impl.conn.SystemDefaultRoutePlanner;
33+
34+
/**
35+
* Utilities for Google APIs based on {@link ApacheHttpTransport}.
36+
*
37+
* @since 1.31
38+
*/
39+
public final class GoogleApacheHttpTransport {
40+
41+
/**
42+
* Returns a new instance of {@link ApacheHttpTransport} that uses
43+
* {@link GoogleUtils#getCertificateTrustStore()} for the trusted certificates.
44+
*/
45+
public static ApacheHttpTransport newTrustedTransport() throws GeneralSecurityException,
46+
IOException {
47+
PoolingHttpClientConnectionManager connectionManager =
48+
new PoolingHttpClientConnectionManager(-1, TimeUnit.MILLISECONDS);
49+
50+
// Disable the stale connection check (previously configured in the HttpConnectionParams
51+
connectionManager.setValidateAfterInactivity(-1);
52+
53+
// Use the included trust store
54+
KeyStore trustStore = GoogleUtils.getCertificateTrustStore();
55+
SSLContext sslContext = SslUtils.getTlsSslContext();
56+
SslUtils.initSslContext(sslContext, trustStore, SslUtils.getPkixTrustManagerFactory());
57+
LayeredConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext);
58+
59+
HttpClient client = HttpClientBuilder.create()
60+
.useSystemProperties()
61+
.setSSLSocketFactory(socketFactory)
62+
.setMaxConnTotal(200)
63+
.setMaxConnPerRoute(20)
64+
.setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault()))
65+
.setConnectionManager(connectionManager)
66+
.disableRedirectHandling()
67+
.disableAutomaticRetries()
68+
.build();
69+
return new ApacheHttpTransport(client);
70+
}
71+
72+
private GoogleApacheHttpTransport() {
73+
}
74+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
/**
16+
* Google APIs support based on the Apache HTTP Client.
17+
*
18+
* @since 1.31
19+
*/
20+
21+
package com.google.api.client.googleapis.apache.v2;
22+

0 commit comments

Comments
 (0)