@Internal public class DefaultHttpClient extends Object implements WebSocketClient, HttpClient, StreamingHttpClient, SseClient, ProxyHttpClient, Closeable, AutoCloseable
HttpClient
interface based on Netty.Modifier and Type | Field and Description |
---|---|
protected ByteBufferFactory<io.netty.buffer.ByteBufAllocator,io.netty.buffer.ByteBuf> |
byteBufferFactory |
protected MediaTypeCodecRegistry |
mediaTypeCodecRegistry |
SCHEME_WS, SCHEME_WSS
DEFAULT_ERROR_TYPE
Constructor and Description |
---|
DefaultHttpClient() |
DefaultHttpClient(LoadBalancer loadBalancer,
HttpClientConfiguration configuration,
List<InvocationInstrumenterFactory> invocationInstrumenterFactories) |
DefaultHttpClient(LoadBalancer loadBalancer,
HttpClientConfiguration configuration,
String contextPath,
ThreadFactory threadFactory,
NettyClientSslBuilder nettyClientSslBuilder,
MediaTypeCodecRegistry codecRegistry,
AnnotationMetadataResolver annotationMetadataResolver,
List<InvocationInstrumenterFactory> invocationInstrumenterFactories,
HttpClientFilter... filters)
Construct a client for the given arguments.
|
DefaultHttpClient(LoadBalancer loadBalancer,
HttpVersion explicitHttpVersion,
HttpClientConfiguration configuration,
String contextPath,
HttpClientFilterResolver<ClientFilterResolutionContext> filterResolver,
List<HttpFilterResolver.FilterEntry<HttpClientFilter>> clientFilterEntries,
ThreadFactory threadFactory,
NettyClientSslBuilder nettyClientSslBuilder,
MediaTypeCodecRegistry codecRegistry,
WebSocketBeanRegistry webSocketBeanRegistry,
RequestBinderRegistry requestBinderRegistry,
io.netty.channel.EventLoopGroup eventLoopGroup,
io.netty.channel.ChannelFactory socketChannelFactory,
Collection<ChannelPipelineListener> pipelineListeners,
NettyClientCustomizer clientCustomizer,
List<InvocationInstrumenterFactory> invocationInstrumenterFactories,
String informationalServiceId)
Construct a client for the given arguments.
|
DefaultHttpClient(URI uri) |
DefaultHttpClient(URI uri,
HttpClientConfiguration configuration) |
Modifier and Type | Method and Description |
---|---|
protected io.micronaut.http.client.netty.DefaultHttpClient.NettyRequestWriter |
buildNettyRequest(MutableHttpRequest request,
URI requestURI,
MediaType requestContentType,
boolean permitsBody,
Argument<?> bodyType,
Consumer<? super Throwable> onError) |
void |
close()
Delegates to
LifeCycle.stop() . |
<T extends AutoCloseable> |
connect(Class<T> clientEndpointType,
Map<String,Object> parameters)
Connect the given client endpoint type.
|
<T extends AutoCloseable> |
connect(Class<T> clientEndpointType,
MutableHttpRequest<?> request)
Connect the given client endpoint type to the URI over WebSocket.
|
<I> Publisher<ByteBuffer<?>> |
dataStream(HttpRequest<I> request)
Request a stream of data where each emitted item is a
ByteBuffer instance. |
<I> Publisher<ByteBuffer<?>> |
dataStream(HttpRequest<I> request,
Argument<?> errorType)
Request a stream of data where each emitted item is a
ByteBuffer instance. |
<I> Publisher<Event<ByteBuffer<?>>> |
eventStream(HttpRequest<I> request)
Perform an HTTP request and receive data as a stream of SSE
Event objects as they become available without blocking. |
<I,B> Publisher<Event<B>> |
eventStream(HttpRequest<I> request,
Argument<B> eventType)
Perform an HTTP request and receive data as a stream of SSE
Event objects as they become available without blocking. |
<I,B> Publisher<Event<B>> |
eventStream(HttpRequest<I> request,
Argument<B> eventType,
Argument<?> errorType)
Perform an HTTP request and receive data as a stream of SSE
Event objects as they become available without blocking. |
<I,O,E> Publisher<HttpResponse<O>> |
exchange(HttpRequest<I> request,
Argument<O> bodyType,
Argument<E> errorType)
Perform an HTTP request for the given request object emitting the full HTTP response from returned
Publisher and converting the response body to the specified type. |
<I> Publisher<HttpResponse<ByteBuffer<?>>> |
exchangeStream(HttpRequest<I> request)
Requests a stream data where each emitted item is a
ByteBuffer wrapped in the HttpResponse object
(which remains the same for each emitted item). |
<I> Publisher<HttpResponse<ByteBuffer<?>>> |
exchangeStream(HttpRequest<I> request,
Argument<?> errorType)
Requests a stream data where each emitted item is a
ByteBuffer wrapped in the HttpResponse object
(which remains the same for each emitted item). |
HttpClientConfiguration |
getConfiguration() |
protected Object |
getLoadBalancerDiscriminator() |
org.slf4j.Logger |
getLog() |
MediaTypeCodecRegistry |
getMediaTypeCodecRegistry() |
boolean |
isRunning() |
<I> Publisher<Map<String,Object>> |
jsonStream(HttpRequest<I> request)
Perform an HTTP request and receive data as a stream of JSON objects as they become available without blocking.
|
<I,O> Publisher<O> |
jsonStream(HttpRequest<I> request,
Argument<O> type)
Perform an HTTP request and receive data as a stream of JSON objects as they become available without blocking.
|
<I,O> Publisher<O> |
jsonStream(HttpRequest<I> request,
Argument<O> type,
Argument<?> errorType)
Perform an HTTP request and receive data as a stream of JSON objects as they become available without blocking.
|
<I,O> Publisher<O> |
jsonStream(HttpRequest<I> request,
Class<O> type)
Perform an HTTP request and receive data as a stream of JSON objects as they become available without blocking.
|
protected URI |
prependContextPath(URI requestURI) |
Publisher<MutableHttpResponse<?>> |
proxy(HttpRequest<?> request)
Proxy the given request and emit the response.
|
Publisher<MutableHttpResponse<?>> |
proxy(HttpRequest<?> request,
ProxyRequestOptions options)
Proxy the given request and emit the response.
|
protected <I> Publisher<URI> |
resolveRedirectURI(HttpRequest<?> parentRequest,
HttpRequest<I> request) |
protected <I> Publisher<URI> |
resolveRequestURI(HttpRequest<I> request) |
protected <I> Publisher<URI> |
resolveRequestURI(HttpRequest<I> request,
boolean includeContextPath) |
<I,O,E> Publisher<O> |
retrieve(HttpRequest<I> request,
Argument<O> bodyType,
Argument<E> errorType)
Perform an HTTP request for the given request object emitting the full HTTP response from returned
Publisher and converting the response body to the specified type. |
void |
setMediaTypeCodecRegistry(MediaTypeCodecRegistry mediaTypeCodecRegistry)
Sets the
MediaTypeCodecRegistry used by this client. |
HttpClient |
start()
Starts the lifecyle component.
|
HttpClient |
stop()
Stops the life cycle component.
|
BlockingHttpClient |
toBlocking() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
connect, connect, create, create, create, create
create, create
exchange, exchange, exchange, exchange, exchange, refresh, retrieve, retrieve, retrieve, retrieve
create, create, eventStream, eventStream, eventStream
create, create
protected MediaTypeCodecRegistry mediaTypeCodecRegistry
protected ByteBufferFactory<io.netty.buffer.ByteBufAllocator,io.netty.buffer.ByteBuf> byteBufferFactory
public DefaultHttpClient(@Nullable LoadBalancer loadBalancer, @NonNull HttpClientConfiguration configuration, @Nullable String contextPath, @Nullable ThreadFactory threadFactory, NettyClientSslBuilder nettyClientSslBuilder, MediaTypeCodecRegistry codecRegistry, @Nullable AnnotationMetadataResolver annotationMetadataResolver, List<InvocationInstrumenterFactory> invocationInstrumenterFactories, HttpClientFilter... filters)
loadBalancer
- The LoadBalancer
to use for selecting serversconfiguration
- The HttpClientConfiguration
objectcontextPath
- The base URI to prepend to request uristhreadFactory
- The thread factory to use for client threadsnettyClientSslBuilder
- The SSL buildercodecRegistry
- The MediaTypeCodecRegistry
to use for encoding and decoding objectsannotationMetadataResolver
- The annotation metadata resolverinvocationInstrumenterFactories
- The invocation instrumeter factories to instrument netty handlers execution withfilters
- The filters to usepublic DefaultHttpClient(@Nullable LoadBalancer loadBalancer, @Nullable HttpVersion explicitHttpVersion, @NonNull HttpClientConfiguration configuration, @Nullable String contextPath, @NonNull HttpClientFilterResolver<ClientFilterResolutionContext> filterResolver, List<HttpFilterResolver.FilterEntry<HttpClientFilter>> clientFilterEntries, @Nullable ThreadFactory threadFactory, @NonNull NettyClientSslBuilder nettyClientSslBuilder, @NonNull MediaTypeCodecRegistry codecRegistry, @NonNull WebSocketBeanRegistry webSocketBeanRegistry, @NonNull RequestBinderRegistry requestBinderRegistry, @Nullable io.netty.channel.EventLoopGroup eventLoopGroup, @NonNull io.netty.channel.ChannelFactory socketChannelFactory, Collection<ChannelPipelineListener> pipelineListeners, NettyClientCustomizer clientCustomizer, List<InvocationInstrumenterFactory> invocationInstrumenterFactories, @Nullable String informationalServiceId)
loadBalancer
- The LoadBalancer
to use for selecting serversexplicitHttpVersion
- The HTTP version to use. Can be null and defaults to HttpVersion.HTTP_1_1
configuration
- The HttpClientConfiguration
objectcontextPath
- The base URI to prepend to request urisfilterResolver
- The http client filter resolverclientFilterEntries
- The client filter entriesthreadFactory
- The thread factory to use for client threadsnettyClientSslBuilder
- The SSL buildercodecRegistry
- The MediaTypeCodecRegistry
to use for encoding and decoding objectswebSocketBeanRegistry
- The websocket bean registryrequestBinderRegistry
- The request binder registryeventLoopGroup
- The event loop group to usesocketChannelFactory
- The socket channel factorypipelineListeners
- The listeners to call for pipeline customizationclientCustomizer
- The pipeline customizerinvocationInstrumenterFactories
- The invocation instrumeter factories to instrument netty handlers execution withinformationalServiceId
- Optional service ID that will be passed to exceptions created by this clientpublic DefaultHttpClient()
public DefaultHttpClient(@Nullable URI uri, @NonNull HttpClientConfiguration configuration)
uri
- The URIconfiguration
- The HttpClientConfiguration
objectpublic DefaultHttpClient(@Nullable LoadBalancer loadBalancer, HttpClientConfiguration configuration, List<InvocationInstrumenterFactory> invocationInstrumenterFactories)
loadBalancer
- The LoadBalancer
to use for selecting serversconfiguration
- The HttpClientConfiguration
objectinvocationInstrumenterFactories
- The invocation instrumeter factories to instrument netty handlers execution withpublic HttpClientConfiguration getConfiguration()
public org.slf4j.Logger getLog()
public HttpClient start()
LifeCycle
start
in interface LifeCycle<HttpClient>
public boolean isRunning()
isRunning
in interface LifeCycle<HttpClient>
public HttpClient stop()
LifeCycle
stop
in interface LifeCycle<HttpClient>
public MediaTypeCodecRegistry getMediaTypeCodecRegistry()
MediaTypeCodecRegistry
used by this clientpublic void setMediaTypeCodecRegistry(MediaTypeCodecRegistry mediaTypeCodecRegistry)
MediaTypeCodecRegistry
used by this client.mediaTypeCodecRegistry
- The registry to use. Should not be nullpublic BlockingHttpClient toBlocking()
toBlocking
in interface HttpClient
public <I> Publisher<Event<ByteBuffer<?>>> eventStream(@NonNull HttpRequest<I> request)
SseClient
Perform an HTTP request and receive data as a stream of SSE Event
objects as they become available without blocking.
The downstream Subscriber
can regulate demand via the subscription
eventStream
in interface SseClient
I
- The request body typerequest
- The HttpRequest
to executePublisher
that emits an Event
with the data represented as a ByteBuffer
public <I,B> Publisher<Event<B>> eventStream(@NonNull HttpRequest<I> request, @NonNull Argument<B> eventType)
SseClient
Perform an HTTP request and receive data as a stream of SSE Event
objects as they become available without blocking.
The downstream Subscriber
can regulate demand via the subscription
eventStream
in interface SseClient
I
- The request body typeB
- The event body typerequest
- The HttpRequest
to executeeventType
- The event data typePublisher
that emits an Event
with the data represented by the eventType argumentpublic <I,B> Publisher<Event<B>> eventStream(@NonNull HttpRequest<I> request, @NonNull Argument<B> eventType, @NonNull Argument<?> errorType)
SseClient
Perform an HTTP request and receive data as a stream of SSE Event
objects as they become available without blocking.
The downstream Subscriber
can regulate demand via the subscription
eventStream
in interface SseClient
I
- The request body typeB
- The event body typerequest
- The HttpRequest
to executeeventType
- The event data typeerrorType
- The type that the response body should be coerced into if the server responds with an errorPublisher
that emits an Event
with the data represented by the eventType argumentpublic <I> Publisher<ByteBuffer<?>> dataStream(@NonNull HttpRequest<I> request)
StreamingHttpClient
ByteBuffer
instance.dataStream
in interface StreamingHttpClient
I
- The request body typerequest
- The requestPublisher
that emits a stream of ByteBuffer
instancespublic <I> Publisher<ByteBuffer<?>> dataStream(@NonNull HttpRequest<I> request, @NonNull Argument<?> errorType)
StreamingHttpClient
ByteBuffer
instance.dataStream
in interface StreamingHttpClient
I
- The request body typerequest
- The requesterrorType
- The type that the response body should be coerced into if the server responds with an errorPublisher
that emits a stream of ByteBuffer
instancespublic <I> Publisher<HttpResponse<ByteBuffer<?>>> exchangeStream(@NonNull HttpRequest<I> request)
StreamingHttpClient
ByteBuffer
wrapped in the HttpResponse
object
(which remains the same for each emitted item).exchangeStream
in interface StreamingHttpClient
I
- The request body typerequest
- The HttpRequest
Publisher
that emits a stream of ByteBuffer
instances wrapped by a HttpResponse
public <I> Publisher<HttpResponse<ByteBuffer<?>>> exchangeStream(@NonNull HttpRequest<I> request, @NonNull Argument<?> errorType)
StreamingHttpClient
ByteBuffer
wrapped in the HttpResponse
object
(which remains the same for each emitted item).exchangeStream
in interface StreamingHttpClient
I
- The request body typerequest
- The HttpRequest
errorType
- The type that the response body should be coerced into if the server responds with an errorPublisher
that emits a stream of ByteBuffer
instances wrapped by a HttpResponse
public <I,O> Publisher<O> jsonStream(@NonNull HttpRequest<I> request, @NonNull Argument<O> type)
StreamingHttpClient
Perform an HTTP request and receive data as a stream of JSON objects as they become available without blocking.
The downstream Subscriber
can regulate demand via the subscription. Incoming data
is buffered.
jsonStream
in interface StreamingHttpClient
I
- The request body typeO
- The response typerequest
- The HttpRequest
to executetype
- The type of object to convert the JSON intoPublisher
that emits the full HttpResponse
objectpublic <I,O> Publisher<O> jsonStream(@NonNull HttpRequest<I> request, @NonNull Argument<O> type, @NonNull Argument<?> errorType)
StreamingHttpClient
Perform an HTTP request and receive data as a stream of JSON objects as they become available without blocking.
The downstream Subscriber
can regulate demand via the subscription. Incoming data
is buffered.
jsonStream
in interface StreamingHttpClient
I
- The request body typeO
- The response typerequest
- The HttpRequest
to executetype
- The type of object to convert the JSON intoerrorType
- The type that the response body should be coerced into if the server responds with an errorPublisher
that emits the full HttpResponse
objectpublic <I> Publisher<Map<String,Object>> jsonStream(@NonNull HttpRequest<I> request)
StreamingHttpClient
Perform an HTTP request and receive data as a stream of JSON objects as they become available without blocking.
The downstream Subscriber
can regulate demand via the subscription
jsonStream
in interface StreamingHttpClient
I
- The request body typerequest
- The HttpRequest
to executePublisher
that emits the full HttpResponse
objectpublic <I,O> Publisher<O> jsonStream(@NonNull HttpRequest<I> request, @NonNull Class<O> type)
StreamingHttpClient
Perform an HTTP request and receive data as a stream of JSON objects as they become available without blocking.
The downstream Subscriber
can regulate demand via the subscription
jsonStream
in interface StreamingHttpClient
I
- The request body typeO
- The response typerequest
- The HttpRequest
to executetype
- The type of object to convert the JSON intoPublisher
that emits the full HttpResponse
objectpublic <I,O,E> Publisher<HttpResponse<O>> exchange(@NonNull HttpRequest<I> request, @NonNull Argument<O> bodyType, @NonNull Argument<E> errorType)
HttpClient
Perform an HTTP request for the given request object emitting the full HTTP response from returned
Publisher
and converting the response body to the specified type.
This method will send a Content-Length
header and except a content length header the response and is
designed for simple non-streaming exchanges of data
By default the exchange Content-Type
is application/json, unless otherwise specified in the passed
HttpRequest
exchange
in interface HttpClient
I
- The request body typeO
- The response body typeE
- The error typerequest
- The HttpRequest
to executebodyType
- The body typeerrorType
- The error typePublisher
that emits the full HttpResponse
objectpublic <I,O,E> Publisher<O> retrieve(HttpRequest<I> request, Argument<O> bodyType, Argument<E> errorType)
HttpClient
Publisher
and converting the response body to the specified type.retrieve
in interface HttpClient
I
- The request body typeO
- The response body typeE
- The error typerequest
- The HttpRequest
to executebodyType
- The body typeerrorType
- The error typePublisher
that emits a result of the given typepublic <T extends AutoCloseable> Publisher<T> connect(Class<T> clientEndpointType, MutableHttpRequest<?> request)
WebSocketClient
connect
in interface WebSocketClient
T
- The generic typeclientEndpointType
- The endpoint type. Should be a class annotated with ClientWebSocket
request
- The original request to establish the connectionPublisher
that emits the ClientWebSocket
instancepublic <T extends AutoCloseable> Publisher<T> connect(Class<T> clientEndpointType, Map<String,Object> parameters)
WebSocketClient
WebSocketClient.connect(Class, URI)
this method will the value declared within the ClientWebSocket
as the URI
and expand the URI with the given parameters.connect
in interface WebSocketClient
T
- The generic typeclientEndpointType
- The endpoint type. Should be a class annotated with ClientWebSocket
parameters
- The URI parameters for the endpointPublisher
that emits the ClientWebSocket
instancepublic void close()
LifeCycle
LifeCycle.stop()
.close
in interface LifeCycle<HttpClient>
close
in interface WebSocketClient
close
in interface Closeable
close
in interface AutoCloseable
public Publisher<MutableHttpResponse<?>> proxy(@NonNull HttpRequest<?> request)
ProxyHttpClient
proxy
in interface ProxyHttpClient
request
- The requestpublic Publisher<MutableHttpResponse<?>> proxy(@NonNull HttpRequest<?> request, @NonNull ProxyRequestOptions options)
ProxyHttpClient
proxy
in interface ProxyHttpClient
request
- The requestoptions
- Further options for the proxy requestprotected <I> Publisher<URI> resolveRequestURI(HttpRequest<I> request)
I
- The input typerequest
- The requestPublisher
with the resolved URIprotected <I> Publisher<URI> resolveRequestURI(HttpRequest<I> request, boolean includeContextPath)
I
- The input typerequest
- The requestincludeContextPath
- Whether to prepend the client context pathPublisher
with the resolved URIprotected <I> Publisher<URI> resolveRedirectURI(HttpRequest<?> parentRequest, HttpRequest<I> request)
I
- The input typeparentRequest
- The parent requestrequest
- The redirect location requestPublisher
with the resolved URIprotected URI prependContextPath(URI requestURI)
requestURI
- The request URIprotected Object getLoadBalancerDiscriminator()
protected io.micronaut.http.client.netty.DefaultHttpClient.NettyRequestWriter buildNettyRequest(MutableHttpRequest request, URI requestURI, MediaType requestContentType, boolean permitsBody, @Nullable Argument<?> bodyType, Consumer<? super Throwable> onError) throws io.netty.handler.codec.http.multipart.HttpPostRequestEncoder.ErrorDataEncoderException
request
- The requestrequestURI
- The URI of the requestrequestContentType
- The request content typepermitsBody
- Whether permits bodybodyType
- The body typeonError
- Called when the body publisher encounters an errorNettyRequestWriter
HttpPostRequestEncoder.ErrorDataEncoderException
- if there is an encoder exception