From d0dbca720479f1ca8627c58eda195857ea0b2423 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Tue, 27 Oct 2020 09:05:36 -0700 Subject: [PATCH] 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 --- google-api-client/pom.xml | 4 + .../apache/GoogleApacheHttpTransport.java | 3 + .../googleapis/apache/package-info.java | 2 +- .../apache/v2/GoogleApacheHttpTransport.java | 74 +++++++++++++++++++ .../googleapis/apache/v2/package-info.java | 22 ++++++ 5 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 google-api-client/src/main/java/com/google/api/client/googleapis/apache/v2/GoogleApacheHttpTransport.java create mode 100644 google-api-client/src/main/java/com/google/api/client/googleapis/apache/v2/package-info.java diff --git a/google-api-client/pom.xml b/google-api-client/pom.xml index 0e3bdd0c4..0f3255184 100644 --- a/google-api-client/pom.xml +++ b/google-api-client/pom.xml @@ -150,5 +150,9 @@ com.google.guava guava + + com.google.http-client + google-http-client-apache-v2 + diff --git a/google-api-client/src/main/java/com/google/api/client/googleapis/apache/GoogleApacheHttpTransport.java b/google-api-client/src/main/java/com/google/api/client/googleapis/apache/GoogleApacheHttpTransport.java index 78a520511..20b50dbc1 100644 --- a/google-api-client/src/main/java/com/google/api/client/googleapis/apache/GoogleApacheHttpTransport.java +++ b/google-api-client/src/main/java/com/google/api/client/googleapis/apache/GoogleApacheHttpTransport.java @@ -36,12 +36,15 @@ * * @since 1.14 * @author Yaniv Inbar + * @deprecated Use com.google.api.client.googleapis.apache.v2.GoogleApacheHttpTransport */ +@Deprecated public final class GoogleApacheHttpTransport { /** * Returns a new instance of {@link ApacheHttpTransport} that uses * {@link GoogleUtils#getCertificateTrustStore()} for the trusted certificates. + * @deprecated Use com.google.api.client.googleapis.apache.v2.GoogleApacheHttpTransport.newTrustedTransport() */ public static ApacheHttpTransport newTrustedTransport() throws GeneralSecurityException, IOException { diff --git a/google-api-client/src/main/java/com/google/api/client/googleapis/apache/package-info.java b/google-api-client/src/main/java/com/google/api/client/googleapis/apache/package-info.java index 7c03ab082..0012639d5 100644 --- a/google-api-client/src/main/java/com/google/api/client/googleapis/apache/package-info.java +++ b/google-api-client/src/main/java/com/google/api/client/googleapis/apache/package-info.java @@ -13,7 +13,7 @@ */ /** - * Google API's support based on the Apache HTTP Client. + * Google APIs support based on the Apache HTTP Client. * * @since 1.14 * @author Yaniv Inbar diff --git a/google-api-client/src/main/java/com/google/api/client/googleapis/apache/v2/GoogleApacheHttpTransport.java b/google-api-client/src/main/java/com/google/api/client/googleapis/apache/v2/GoogleApacheHttpTransport.java new file mode 100644 index 000000000..b5f91f6ed --- /dev/null +++ b/google-api-client/src/main/java/com/google/api/client/googleapis/apache/v2/GoogleApacheHttpTransport.java @@ -0,0 +1,74 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed 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 com.google.api.client.googleapis.apache.v2; + +import com.google.api.client.googleapis.GoogleUtils; +import com.google.api.client.http.apache.v2.ApacheHttpTransport; +import com.google.api.client.util.SslUtils; +import java.io.IOException; +import java.net.ProxySelector; +import java.security.GeneralSecurityException; +import java.security.KeyStore; +import java.util.concurrent.TimeUnit; +import javax.net.ssl.SSLContext; +import org.apache.http.client.HttpClient; +import org.apache.http.config.SocketConfig; +import org.apache.http.conn.socket.LayeredConnectionSocketFactory; +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; +import org.apache.http.impl.conn.SystemDefaultRoutePlanner; + +/** + * Utilities for Google APIs based on {@link ApacheHttpTransport}. + * + * @since 1.31 + */ +public final class GoogleApacheHttpTransport { + + /** + * Returns a new instance of {@link ApacheHttpTransport} that uses + * {@link GoogleUtils#getCertificateTrustStore()} for the trusted certificates. + */ + public static ApacheHttpTransport newTrustedTransport() throws GeneralSecurityException, + IOException { + PoolingHttpClientConnectionManager connectionManager = + new PoolingHttpClientConnectionManager(-1, TimeUnit.MILLISECONDS); + + // Disable the stale connection check (previously configured in the HttpConnectionParams + connectionManager.setValidateAfterInactivity(-1); + + // Use the included trust store + KeyStore trustStore = GoogleUtils.getCertificateTrustStore(); + SSLContext sslContext = SslUtils.getTlsSslContext(); + SslUtils.initSslContext(sslContext, trustStore, SslUtils.getPkixTrustManagerFactory()); + LayeredConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext); + + HttpClient client = HttpClientBuilder.create() + .useSystemProperties() + .setSSLSocketFactory(socketFactory) + .setMaxConnTotal(200) + .setMaxConnPerRoute(20) + .setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault())) + .setConnectionManager(connectionManager) + .disableRedirectHandling() + .disableAutomaticRetries() + .build(); + return new ApacheHttpTransport(client); + } + + private GoogleApacheHttpTransport() { + } +} diff --git a/google-api-client/src/main/java/com/google/api/client/googleapis/apache/v2/package-info.java b/google-api-client/src/main/java/com/google/api/client/googleapis/apache/v2/package-info.java new file mode 100644 index 000000000..4560a4bec --- /dev/null +++ b/google-api-client/src/main/java/com/google/api/client/googleapis/apache/v2/package-info.java @@ -0,0 +1,22 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed 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. + */ + +/** + * Google APIs support based on the Apache HTTP Client. + * + * @since 1.31 + */ + +package com.google.api.client.googleapis.apache.v2; +