Class HttpTransport

  • Direct Known Subclasses:
    Apache5HttpTransport, ApacheHttpTransport, ApacheHttpTransport, MockHttpTransport, NetHttpTransport

    public abstract class HttpTransport
    extends java.lang.Object
    Thread-safe abstract HTTP transport.

    Implementation is thread-safe, and subclasses must be thread-safe. For maximum efficiency, applications should use a single globally-shared instance of the HTTP transport.

    The recommended concrete implementation HTTP transport library to use depends on what environment you are running in:

    • Google App Engine: use com.google.api.client.extensions.appengine.http.UrlFetchTransport.
      • com.google.api.client.apache.ApacheHttpTransport doesn't work on App Engine because the Apache HTTP Client opens its own sockets (though in theory there are ways to hack it to work on App Engine that might work).
      • com.google.api.client.javanet.NetHttpTransport is discouraged due to a bug in the App Engine SDK itself in how it parses HTTP headers in the response.
    • Android:
      • For maximum backwards compatibility with older SDK's use newCompatibleTransport from com.google.api.client.extensions.android.http.AndroidHttp (read its JavaDoc for details).
      • If your application is targeting Gingerbread (SDK 2.3) or higher, simply use com.google.api.client.javanet.NetHttpTransport.
    • Other Java environments
      • com.google.api.client.googleapis.javanet.GoogleNetHttpTransport is included in google-api-cient 1.22.0, so easy to include.
      • com.google.api.client.javanet.NetHttpTransport is based on the HttpURLConnection built into the Java SDK, so it used to be the preferred choice.
      • com.google.api.client.apache.ApacheHttpTransport is a good choice for users of the Apache HTTP Client, especially if you need some of the configuration options available in that library.

    Some HTTP transports do not support all HTTP methods. Use supportsMethod(String) to check if a certain HTTP method is supported. Calling buildRequest() on a method that is not supported will result in an IllegalArgumentException.

    Subclasses should override supportsMethod(String) and buildRequest(String, String) to build requests and specify which HTTP methods are supported.

    Since:
    1.0
    • Field Summary

      Fields 
      Modifier and Type Field Description
      (package private) static java.util.logging.Logger LOGGER  
      private static java.lang.String[] SUPPORTED_METHODS
      All valid request methods as specified in supportsMethod(String), sorted in ascending alphabetical order.
    • Constructor Summary

      Constructors 
      Constructor Description
      HttpTransport()  
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) HttpRequest buildRequest()
      Builds a request without specifying the HTTP method.
      protected abstract LowLevelHttpRequest buildRequest​(java.lang.String method, java.lang.String url)
      Builds a low level HTTP request for the given HTTP method.
      HttpRequestFactory createRequestFactory()
      Returns a new instance of an HTTP request factory based on this HTTP transport.
      HttpRequestFactory createRequestFactory​(HttpRequestInitializer initializer)
      Returns a new instance of an HTTP request factory based on this HTTP transport with the given HTTP request initializer.
      boolean isMtls()
      Returns whether the transport is mTLS.
      boolean isShutdown()
      Returns whether the transport is shutdown or not.
      void shutdown()
      Default implementation does nothing, but subclasses may override to possibly release allocated system resources or close connections.
      boolean supportsMethod​(java.lang.String method)
      Returns whether a specified HTTP method is supported by this transport.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • LOGGER

        static final java.util.logging.Logger LOGGER
      • SUPPORTED_METHODS

        private static final java.lang.String[] SUPPORTED_METHODS
        All valid request methods as specified in supportsMethod(String), sorted in ascending alphabetical order.
    • Constructor Detail

      • HttpTransport

        public HttpTransport()
    • Method Detail

      • createRequestFactory

        public final HttpRequestFactory createRequestFactory()
        Returns a new instance of an HTTP request factory based on this HTTP transport.
        Returns:
        new instance of an HTTP request factory
        Since:
        1.4
      • createRequestFactory

        public final HttpRequestFactory createRequestFactory​(HttpRequestInitializer initializer)
        Returns a new instance of an HTTP request factory based on this HTTP transport with the given HTTP request initializer.
        Parameters:
        initializer - HTTP request initializer or null for none
        Returns:
        new instance of an HTTP request factory
        Since:
        1.4
      • buildRequest

        HttpRequest buildRequest()
        Builds a request without specifying the HTTP method.
        Returns:
        new HTTP request
      • supportsMethod

        public boolean supportsMethod​(java.lang.String method)
                               throws java.io.IOException
        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.

        Parameters:
        method - HTTP method
        Throws:
        java.io.IOException - I/O exception
        Since:
        1.12
      • isMtls

        public boolean isMtls()
        Returns whether the transport is mTLS.
        Returns:
        boolean indicating if the transport is mTLS.
        Since:
        1.38
      • buildRequest

        protected abstract LowLevelHttpRequest buildRequest​(java.lang.String method,
                                                            java.lang.String url)
                                                     throws java.io.IOException
        Builds a low level HTTP request for the given HTTP method.
        Parameters:
        method - HTTP method
        url - URL
        Returns:
        new low level HTTP request
        Throws:
        java.lang.IllegalArgumentException - if HTTP method is not supported
        java.io.IOException
        Since:
        1.12
      • shutdown

        public void shutdown()
                      throws java.io.IOException
        Default implementation does nothing, but subclasses may override to possibly release allocated system resources or close connections.
        Throws:
        java.io.IOException - I/O exception
        Since:
        1.4
      • isShutdown

        public boolean isShutdown()
        Returns whether the transport is shutdown or not.
        Returns:
        true if the transport is shutdown.
        Since:
        1.44.0