Top Description Fields Constructors Methods
org.eclipse.jetty.client.transport

public Class HttpClientTransportDynamic

extends AbstractConnectorHttpClientTransport
Class Inheritance
Imports
java.io.IOException, java.net.SocketAddress, java.util.ArrayList, .Arrays, .List, .Locale, .Map, .Optional, java.util.stream.Collectors, .Stream, org.eclipse.jetty.alpn.client.ALPNClientConnection, .ALPNClientConnectionFactory, org.eclipse.jetty.client.AbstractConnectorHttpClientTransport, .Destination, .HttpClientTransport, .MultiplexConnectionPool, .Origin, .Request, org.eclipse.jetty.http.HttpHeader, .HttpScheme, .HttpVersion, org.eclipse.jetty.io.ClientConnectionFactory, .ClientConnector, .Connection, .EndPoint, org.slf4j.Logger, .LoggerFactory

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 org.eclipse.jetty.client.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(Request).

Field Summary

Modifier and TypeField and Description
private final List<ClientConnectionFactory.Info>
private static final Logger
private final List<String>

Constructor Summary

AccessConstructor and Description
public
HttpClientTransportDynamic()

Creates a transport that speaks only HTTP/1.1.

public
public
HttpClientTransportDynamic(ClientConnector
the ClientConnector used by this transport
connector
,
ClientConnectionFactory.Info...
the application protocols that this transport can speak
factoryInfos
)

Creates a transport with the given ClientConnector and the given application protocols.

Method Summary

Modifier and TypeMethod and Description
private Optional<ClientConnectionFactory.Info>
findClientConnectionFactoryInfo(List<String> protocols, boolean secure)

private static ClientConnector
public Connection
newConnection(EndPoint
the org.eclipse.jetty.io.EndPoint to link the newly created connection to
endPoint
,
Map<String, Object>
the context data to create the connection
context
)

Implements org.eclipse.jetty.io.ClientConnectionFactory.newConnection.

public Destination
newDestination(Origin
the destination origin
origin
)

Implements org.eclipse.jetty.client.HttpClientTransport.newDestination.

Creates a new, transport-specific, HttpDestination object.

protected Connection
public Origin
newOrigin(Request
the request that triggers the creation of the Origin
request
)

Implements org.eclipse.jetty.client.HttpClientTransport.newOrigin.

Creates a new Origin with the given request.

public void
upgrade(EndPoint endPoint, Map<String, Object> context)

Inherited from org.eclipse.jetty.client.AbstractConnectorHttpClientTransport:
connectconnectdoStartgetClientConnectorgetSelectors

Field Detail

factoryInfosback to summary
private final List<ClientConnectionFactory.Info> factoryInfos
LOGback to summary
private static final Logger LOG

Hides org.eclipse.jetty.client.AbstractHttpClientTransport.LOG.

protocolsback to summary
private final List<String> protocols

Constructor Detail

HttpClientTransportDynamicback to summary
public HttpClientTransportDynamic()

Creates a transport that speaks only HTTP/1.1.

HttpClientTransportDynamicback to summary
public HttpClientTransportDynamic(ClientConnectionFactory.Info... factoryInfos)
HttpClientTransportDynamicback to summary
public HttpClientTransportDynamic(ClientConnector connector, ClientConnectionFactory.Info... factoryInfos)

Creates a transport with the given ClientConnector and the given application protocols.

Parameters
connector:ClientConnector

the ClientConnector used by this transport

factoryInfos:ClientConnectionFactory.Info[]

the application protocols that this transport can speak

Method Detail

findClientConnectionFactoryInfoback to summary
private Optional<ClientConnectionFactory.Info> findClientConnectionFactoryInfo(List<String> protocols, boolean secure)
findClientConnectorback to summary
private static ClientConnector findClientConnector(ClientConnectionFactory.Info[] infos)
newConnectionback to summary
public Connection newConnection(EndPoint endPoint, Map<String, Object> context) throws IOException

Implements org.eclipse.jetty.io.ClientConnectionFactory.newConnection.

Parameters
endPoint:EndPoint

Doc from org.eclipse.jetty.io.ClientConnectionFactory.newConnection.

the org.eclipse.jetty.io.EndPoint to link the newly created connection to

context:Map<String, Object>

Doc from org.eclipse.jetty.io.ClientConnectionFactory.newConnection.

the context data to create the connection

Returns:Connection

Doc from org.eclipse.jetty.io.ClientConnectionFactory.newConnection.

a new Connection

Annotations
@Override
Exceptions
IOException:

Doc from org.eclipse.jetty.io.ClientConnectionFactory.newConnection.

if the connection cannot be created

newDestinationback to summary
public Destination newDestination(Origin origin)

Implements org.eclipse.jetty.client.HttpClientTransport.newDestination.

Doc from org.eclipse.jetty.client.HttpClientTransport.newDestination.

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.

Parameters
origin:Origin

the destination origin

Returns:Destination

a new, transport-specific, HttpDestination object

Annotations
@Override
newNegotiatedConnectionback to summary
protected Connection newNegotiatedConnection(EndPoint endPoint, Map<String, Object> context) throws IOException
newOriginback to summary
public Origin newOrigin(Request request)

Implements org.eclipse.jetty.client.HttpClientTransport.newOrigin.

Doc from org.eclipse.jetty.client.HttpClientTransport.newOrigin.

Creates a new Origin with the given request.

Parameters
request:Request

the request that triggers the creation of the Origin

Returns:Origin

an Origin that identifies a destination

Annotations
@Override
upgradeback to summary
public void upgrade(EndPoint endPoint, Map<String, Object> context)