|
| 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 | +} |
0 commit comments