Class ApacheHttpTransport

public final class ApacheHttpTransport extends HttpTransport

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

Implementation is thread-safe, as long as any parameter modification to the 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 #newDefaultHttpClient(). Use the #ApacheHttpTransport(HttpClient) constructor to override the Apache HTTP Client used. Please read the Apache HTTP Client connection management tutorial for more complex configuration options.

Inheritance

java.lang.Object > HttpTransport > ApacheHttpTransport

Constructors

ApacheHttpTransport()

public ApacheHttpTransport()

Constructor that uses #newDefaultHttpClient() for the Apache HTTP client.

ApacheHttpTransport(HttpClient httpClient)

public ApacheHttpTransport(HttpClient httpClient)

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).
Parameter
NameDescription
httpClientorg.apache.http.client.HttpClient

Apache HTTP client to use

ApacheHttpTransport(HttpClient httpClient, boolean isMtls)

public ApacheHttpTransport(HttpClient httpClient, boolean isMtls)

Beta
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).
Parameters
NameDescription
httpClientorg.apache.http.client.HttpClient

Apache HTTP client to use

isMtlsboolean

If the HTTP client is mutual TLS

Methods

buildRequest(String method, String url)

protected ApacheHttpRequest buildRequest(String method, String url)

Builds a low level HTTP request for the given HTTP method.

Parameters
NameDescription
methodString
urlString
Returns
TypeDescription
com.google.api.client.http.apache.v2.ApacheHttpRequest
Overrides

getHttpClient()

public HttpClient getHttpClient()

Returns the Apache HTTP client.

Returns
TypeDescription
org.apache.http.client.HttpClient

isMtls()

public boolean isMtls()

Returns if the underlying HTTP client is mTLS.

Returns
TypeDescription
boolean
Overrides

newDefaultHttpClient()

public static HttpClient newDefaultHttpClient()

Creates a new instance of the Apache HTTP client that is used by the #ApacheHttpTransport() constructor.

Settings:

  • The client connection manager is set to PoolingHttpClientConnectionManager.
  • <The retry mechanism is turned off using HttpClientBuilder#disableRedirectHandling.
  • The route planner uses SystemDefaultRoutePlanner with ProxySelector#getDefault(), which uses the proxy settings from system properties.
Returns
TypeDescription
org.apache.http.client.HttpClient

new instance of the Apache HTTP client

newDefaultHttpClientBuilder()

public static HttpClientBuilder newDefaultHttpClientBuilder()

Creates a new Apache HTTP client builder that is used by the #ApacheHttpTransport() constructor.

Settings:

  • The client connection manager is set to PoolingHttpClientConnectionManager.
  • <The retry mechanism is turned off using HttpClientBuilder#disableRedirectHandling.
  • The route planner uses SystemDefaultRoutePlanner with ProxySelector#getDefault(), which uses the proxy settings from system properties.
Returns
TypeDescription
org.apache.http.impl.client.HttpClientBuilder

new instance of the Apache HTTP client

shutdown()

public void shutdown()

Shuts down the connection manager and releases allocated resources. This closes all connections, whether they are currently used or not.

Overrides Exceptions
TypeDescription
IOException

supportsMethod(String method)

public boolean supportsMethod(String method)

Returns whether a specified HTTP method is supported by this transport.

Default implementation returns true if and only if the request method is "DELETE", "GET", "POST", or "PUT". Subclasses should override.

Parameter
NameDescription
methodString
Returns
TypeDescription
boolean
Overrides