All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.api.client.http.apache.v2.ApacheHttpTransport Maven / Gradle / Ivy

There is a newer version: 1.45.3
Show newest version
/*
 * Copyright 2019 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.http.apache.v2;

import com.google.api.client.http.HttpMethods;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.util.Preconditions;
import com.google.api.client.util.SecurityUtils;
import com.google.api.client.util.SslUtils;
import java.io.IOException;
import java.io.InputStream;
import java.net.ProxySelector;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.cert.CertificateFactory;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLContext;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpOptions;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.methods.HttpTrace;
import org.apache.http.config.SocketConfig;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.impl.conn.SystemDefaultRoutePlanner;

/**
 * Thread-safe HTTP transport based on the Apache HTTP Client library.
 *
 * 

* Implementation is thread-safe, as long as any parameter modification to the * {@link #getHttpClient() Apache HTTP Client} is only done at initialization time. For maximum * efficiency, applications should use a single globally-shared instance of the HTTP transport. *

* *

* Default settings are specified in {@link #newDefaultHttpClient()}. Use the * {@link #ApacheHttpTransport(HttpClient)} constructor to override the Apache HTTP Client used. * Please read the Apache HTTP * Client connection management tutorial for more complex configuration options. *

* * @since 1.30 * @author Yaniv Inbar */ public final class ApacheHttpTransport extends HttpTransport { /** Apache HTTP client. */ private final HttpClient httpClient; /** * Constructor that uses {@link #newDefaultHttpClient()} for the Apache HTTP client. * * @since 1.30 */ public ApacheHttpTransport() { this(newDefaultHttpClient()); } /** * Constructor that allows an alternative Apache HTTP client to be used. * *

* Note that in the previous version, we overrode several settings, however, we are no longer able * to do so. *

* *

If you choose to provide your own Apache HttpClient implementation, be sure that

*
    *
  • HTTP version is set to 1.1.
  • *
  • Redirects are disabled (google-http-client handles redirects).
  • *
  • Retries are disabled (google-http-client handles retries).
  • *
* * @param httpClient Apache HTTP client to use * * @since 1.30 */ public ApacheHttpTransport(HttpClient httpClient) { this.httpClient = httpClient; } /** * Creates a new instance of the Apache HTTP client that is used by the * {@link #ApacheHttpTransport()} constructor. * *

* Settings: *

*
    *
  • The client connection manager is set to {@link PoolingHttpClientConnectionManager}.
  • *
  • The socket buffer size is set to 8192 using {@link SocketConfig}.
  • *
  • *
  • The route planner uses {@link SystemDefaultRoutePlanner} with * {@link ProxySelector#getDefault()}, which uses the proxy settings from system * properties.
  • *
* * @return new instance of the Apache HTTP client * @since 1.30 */ public static HttpClient newDefaultHttpClient() { // Set socket buffer sizes to 8192 SocketConfig socketConfig = SocketConfig.custom() .setRcvBufSize(8192) .setSndBufSize(8192) .build(); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(-1, TimeUnit.MILLISECONDS); // Disable the stale connection check (previously configured in the HttpConnectionParams connectionManager.setValidateAfterInactivity(-1); return HttpClientBuilder.create() .useSystemProperties() .setSSLSocketFactory(SSLConnectionSocketFactory.getSocketFactory()) .setDefaultSocketConfig(socketConfig) .setMaxConnTotal(200) .setMaxConnPerRoute(20) .setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault())) .setConnectionManager(connectionManager) .disableRedirectHandling() .disableAutomaticRetries() .build(); } @Override public boolean supportsMethod(String method) { return true; } @Override protected ApacheHttpRequest buildRequest(String method, String url) { HttpRequestBase requestBase; if (method.equals(HttpMethods.DELETE)) { requestBase = new HttpDelete(url); } else if (method.equals(HttpMethods.GET)) { requestBase = new HttpGet(url); } else if (method.equals(HttpMethods.HEAD)) { requestBase = new HttpHead(url); } else if (method.equals(HttpMethods.PATCH)) { requestBase = new HttpPatch(url); } else if (method.equals(HttpMethods.POST)) { requestBase = new HttpPost(url); } else if (method.equals(HttpMethods.PUT)) { requestBase = new HttpPut(url); } else if (method.equals(HttpMethods.TRACE)) { requestBase = new HttpTrace(url); } else if (method.equals(HttpMethods.OPTIONS)) { requestBase = new HttpOptions(url); } else { requestBase = new HttpExtensionMethod(method, url); } return new ApacheHttpRequest(httpClient, requestBase); } /** * Shuts down the connection manager and releases allocated resources. This closes all * connections, whether they are currently used or not. * * @since 1.30 */ @Override public void shutdown() throws IOException { if (httpClient instanceof CloseableHttpClient) { ((CloseableHttpClient) httpClient).close(); } } /** * Returns the Apache HTTP client. * * @since 1.30 */ public HttpClient getHttpClient() { return httpClient; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy