Class HttpClientTransportDynamic

  • All Implemented Interfaces:
    HttpClientTransport, ClientConnectionFactory, Container, Destroyable, Dumpable, Dumpable.DumpableContainer, LifeCycle

    public class HttpClientTransportDynamic
    extends AbstractConnectorHttpClientTransport

    A HttpClientTransport that can dynamically switch among different application protocols.

    Applications create HttpClientTransportDynamic instances specifying all the application protocols it supports, in order of preference. The typical case is when the server supports both HTTP/1.1 and HTTP/2, but the client does not know that. In this case, the application will create a HttpClientTransportDynamic in this way:

     ClientConnector clientConnector = new ClientConnector();
     // Configure the clientConnector.
     // Prepare the application protocols.
     ClientConnectionFactory.Info h1 = HttpClientConnectionFactory.HTTP11;
     HTTP2Client http2Client = new HTTP2Client(clientConnector);
     ClientConnectionFactory.Info h2 = new ClientConnectionFactoryOverHTTP2.HTTP2(http2Client);
     // Create the HttpClientTransportDynamic, preferring h2 over h1.
     HttpClientTransport transport = new HttpClientTransportDynamic(clientConnector, h2, h1);
     // Create the HttpClient.
     client = new HttpClient(transport);

    Note how in the code above the HttpClientTransportDynamic has been created with the application protocols h2 and h1, without the need to specify TLS (which is implied by the request scheme) or ALPN (which is implied by HTTP/2 over TLS).

    When a request is first sent, (scheme, host, port) are not enough to identify the destination because the same origin may speak different protocols. For example, the Jetty server supports speaking clear-text http/1.1 and h2c on the same port. Imagine a client sending a h2c request to that port; this will create a destination and connections that speak h2c; it won't be possible to use the connections from that destination to send http/1.1 requests. Therefore a destination is identified by a Origin and applications can customize the creation of the origin (for example depending on request protocol version, or request headers, or request attributes, or even request path) by overriding HttpClientTransport.newOrigin(HttpRequest).

    • Constructor Detail

      • HttpClientTransportDynamic

        public HttpClientTransportDynamic()
        Creates a transport that speaks only HTTP/1.1.
      • HttpClientTransportDynamic

        public HttpClientTransportDynamic​(ClientConnector connector,
                                          ClientConnectionFactory.Info... factoryInfos)
        Creates a transport with the given ClientConnector and the given application protocols.
        connector - the ClientConnector used by this transport
        factoryInfos - the application protocols that this transport can speak
    • Method Detail

      • newOrigin

        public Origin newOrigin​(HttpRequest request)
        Description copied from interface: HttpClientTransport
        Creates a new Origin with the given request.
        request - the request that triggers the creation of the Origin
        an Origin that identifies a destination
      • newHttpDestination

        public HttpDestination newHttpDestination​(Origin origin)
        Description copied from interface: HttpClientTransport
        Creates a new, transport-specific, HttpDestination object.

        HttpDestination controls the destination-connection cardinality: protocols like HTTP have 1-N cardinality, while multiplexed protocols like HTTP/2 have a 1-1 cardinality.

        origin - the destination origin
        a new, transport-specific, HttpDestination object
      • newConnection

        public Connection newConnection​(EndPoint endPoint,
                                        java.util.Map<java.lang.String,​java.lang.Object> context)
        endPoint - the EndPoint to link the newly created connection to
        context - the context data to create the connection
        a new Connection
        Throws: - if the connection cannot be created
      • upgrade

        public void upgrade​(EndPoint endPoint,
                            java.util.Map<java.lang.String,​java.lang.Object> context)
      • newNegotiatedConnection

        protected Connection newNegotiatedConnection​(EndPoint endPoint,
                                                     java.util.Map<java.lang.String,​java.lang.Object> context)