Class DefaultHttpClient
- All Implemented Interfaces:
LifeCycle<HttpClient>,HttpClient,ProxyHttpClient,SseClient,StreamingHttpClient,WebSocketClient,Closeable,AutoCloseable
HttpClient interface based on Netty.- Since:
- 1.0
- Author:
- Graeme Rocher
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classKey used for connection pooling and determining host/port. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected ByteBufferFactory<io.netty.buffer.ByteBufAllocator,io.netty.buffer.ByteBuf> protected MediaTypeCodecRegistryFields inherited from interface io.micronaut.http.client.HttpClient
DEFAULT_ERROR_TYPEFields inherited from interface io.micronaut.websocket.WebSocketClient
SCHEME_WS, SCHEME_WSS -
Constructor Summary
ConstructorsConstructorDescriptionDefaultHttpClient(@Nullable LoadBalancer loadBalancer, @NonNull HttpClientConfiguration configuration, @Nullable String contextPath, @Nullable ThreadFactory threadFactory, ClientSslBuilder nettyClientSslBuilder, @NonNull MediaTypeCodecRegistry codecRegistry, @NonNull MessageBodyHandlerRegistry handlerRegistry, @Nullable AnnotationMetadataResolver annotationMetadataResolver, ConversionService conversionService, HttpClientFilter... filters) Construct a client for the given arguments.DefaultHttpClient(@Nullable LoadBalancer loadBalancer, @Nullable HttpVersionSelection explicitHttpVersion, @NonNull HttpClientConfiguration configuration, @Nullable String contextPath, @NonNull HttpClientFilterResolver<ClientFilterResolutionContext> filterResolver, @NonNull List<HttpFilterResolver.FilterEntry> clientFilterEntries, @Nullable ThreadFactory threadFactory, @NonNull ClientSslBuilder nettyClientSslBuilder, @NonNull MediaTypeCodecRegistry codecRegistry, @NonNull MessageBodyHandlerRegistry handlerRegistry, @NonNull WebSocketBeanRegistry webSocketBeanRegistry, @NonNull RequestBinderRegistry requestBinderRegistry, @Nullable io.netty.channel.EventLoopGroup eventLoopGroup, @NonNull io.netty.channel.ChannelFactory<? extends io.netty.channel.socket.SocketChannel> socketChannelFactory, @NonNull io.netty.channel.ChannelFactory<? extends io.netty.channel.socket.DatagramChannel> udpChannelFactory, NettyClientCustomizer clientCustomizer, @Nullable String informationalServiceId, ConversionService conversionService) Construct a client for the given arguments.DefaultHttpClient(@Nullable LoadBalancer loadBalancer, HttpClientConfiguration configuration) DefaultHttpClient(@Nullable URI uri, @NonNull HttpClientConfiguration configuration) DefaultHttpClient(@Nullable URI uri, @NonNull HttpClientConfiguration configuration, @NonNull ClientSslBuilder clientSslBuilder) Constructor used by micronaut-oracle-cloud. -
Method Summary
Modifier and TypeMethodDescriptionprotected io.micronaut.http.client.netty.DefaultHttpClient.NettyRequestWriterbuildNettyRequest(MutableHttpRequest request, URI requestURI, MediaType requestContentType, boolean permitsBody, Consumer<? super Throwable> onError) voidclose()Delegates toLifeCycle.stop().<T extends AutoCloseable>
Publisher<T>connect(Class<T> clientEndpointType, MutableHttpRequest<?> request) Connect the given client endpoint type to the URI over WebSocket.<T extends AutoCloseable>
Publisher<T>Connect the given client endpoint type.Access to the connection manager, for micronaut-oracle-cloud.<I> Publisher<ByteBuffer<?>>dataStream(HttpRequest<I> request) Request a stream of data where each emitted item is aByteBufferinstance.<I> Publisher<ByteBuffer<?>>dataStream(HttpRequest<I> request, @NonNull Argument<?> errorType) Request a stream of data where each emitted item is aByteBufferinstance.<I> Publisher<Event<ByteBuffer<?>>>eventStream(HttpRequest<I> request) Perform an HTTP request and receive data as a stream of SSEEventobjects as they become available without blocking.eventStream(HttpRequest<I> request, @NonNull Argument<B> eventType) Perform an HTTP request and receive data as a stream of SSEEventobjects as they become available without blocking.eventStream(HttpRequest<I> request, @NonNull Argument<B> eventType, @NonNull Argument<?> errorType) Perform an HTTP request and receive data as a stream of SSEEventobjects as they become available without blocking.<I,O, E> Publisher<HttpResponse<O>> Perform an HTTP request for the given request object emitting the full HTTP response from returnedPublisherand 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 aByteBufferwrapped in theHttpResponseobject (which remains the same for each emitted item).<I> Publisher<HttpResponse<ByteBuffer<?>>>exchangeStream(HttpRequest<I> request, @NonNull Argument<?> errorType) Requests a stream data where each emitted item is aByteBufferwrapped in theHttpResponseobject (which remains the same for each emitted item).Get the handler registry for this client.protected Objectorg.slf4j.LoggergetLog()booleanjsonStream(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, @NonNull 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, @NonNull Argument<O> type, @NonNull 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, @NonNull Class<O> type) Perform an HTTP request and receive data as a stream of JSON objects as they become available without blocking.proxy(HttpRequest<?> request) Proxy the given request and emit the response.proxy(HttpRequest<?> request, @NonNull ProxyRequestOptions options) Proxy the given request and emit the response.resolveRedirectURI(HttpRequest<?> parentRequest, HttpRequest<I> request) resolveRequestURI(HttpRequest<I> request) 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 returnedPublisherand converting the response body to the specified type.final voidsetHandlerRegistry(@NonNull MessageBodyHandlerRegistry handlerRegistry) Set the handler registry for this client.voidsetMediaTypeCodecRegistry(MediaTypeCodecRegistry mediaTypeCodecRegistry) Deprecated.start()Starts the lifecyle component.stop()Stops the life cycle component.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.micronaut.http.client.HttpClient
exchange, exchange, exchange, exchange, exchange, refresh, retrieve, retrieve, retrieve, retrieveMethods inherited from interface io.micronaut.http.client.sse.SseClient
eventStream, eventStream, eventStreamMethods inherited from interface io.micronaut.websocket.WebSocketClient
connect, connect
-
Field Details
-
mediaTypeCodecRegistry
-
byteBufferFactory
protected ByteBufferFactory<io.netty.buffer.ByteBufAllocator,io.netty.buffer.ByteBuf> byteBufferFactory
-
-
Constructor Details
-
DefaultHttpClient
public DefaultHttpClient(@Nullable @Nullable LoadBalancer loadBalancer, @NonNull @NonNull HttpClientConfiguration configuration, @Nullable @Nullable String contextPath, @Nullable @Nullable ThreadFactory threadFactory, ClientSslBuilder nettyClientSslBuilder, @NonNull @NonNull MediaTypeCodecRegistry codecRegistry, @NonNull @NonNull MessageBodyHandlerRegistry handlerRegistry, @Nullable @Nullable AnnotationMetadataResolver annotationMetadataResolver, ConversionService conversionService, HttpClientFilter... filters) Construct a client for the given arguments.- Parameters:
loadBalancer- TheLoadBalancerto use for selecting serversconfiguration- TheHttpClientConfigurationobjectcontextPath- The base URI to prepend to request uristhreadFactory- The thread factory to use for client threadsnettyClientSslBuilder- The SSL buildercodecRegistry- TheMediaTypeCodecRegistryto use for encoding and decoding objectshandlerRegistry- The handler registry for encoding and decodingannotationMetadataResolver- The annotation metadata resolverconversionService- The conversion servicefilters- The filters to use
-
DefaultHttpClient
public DefaultHttpClient(@Nullable @Nullable LoadBalancer loadBalancer, @Nullable @Nullable HttpVersionSelection explicitHttpVersion, @NonNull @NonNull HttpClientConfiguration configuration, @Nullable @Nullable String contextPath, @NonNull @NonNull HttpClientFilterResolver<ClientFilterResolutionContext> filterResolver, @NonNull @NonNull List<HttpFilterResolver.FilterEntry> clientFilterEntries, @Nullable @Nullable ThreadFactory threadFactory, @NonNull @NonNull ClientSslBuilder nettyClientSslBuilder, @NonNull @NonNull MediaTypeCodecRegistry codecRegistry, @NonNull @NonNull MessageBodyHandlerRegistry handlerRegistry, @NonNull @NonNull WebSocketBeanRegistry webSocketBeanRegistry, @NonNull @NonNull RequestBinderRegistry requestBinderRegistry, @Nullable @Nullable io.netty.channel.EventLoopGroup eventLoopGroup, @NonNull @NonNull io.netty.channel.ChannelFactory<? extends io.netty.channel.socket.SocketChannel> socketChannelFactory, @NonNull @NonNull io.netty.channel.ChannelFactory<? extends io.netty.channel.socket.DatagramChannel> udpChannelFactory, NettyClientCustomizer clientCustomizer, @Nullable @Nullable String informationalServiceId, ConversionService conversionService) Construct a client for the given arguments.- Parameters:
loadBalancer- TheLoadBalancerto use for selecting serversexplicitHttpVersion- The HTTP version to use. Can be null and defaults toHttpVersion.HTTP_1_1configuration- TheHttpClientConfigurationobjectcontextPath- 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- TheMediaTypeCodecRegistryto use for encoding and decoding objectshandlerRegistry- The handler registry for encoding and decodingwebSocketBeanRegistry- The websocket bean registryrequestBinderRegistry- The request binder registryeventLoopGroup- The event loop group to usesocketChannelFactory- The socket channel factoryudpChannelFactory- The UDP channel factoryclientCustomizer- The pipeline customizerinformationalServiceId- Optional service ID that will be passed to exceptions created by this clientconversionService- The conversion service
-
DefaultHttpClient
- Parameters:
uri- The URL
-
DefaultHttpClient
public DefaultHttpClient() -
DefaultHttpClient
public DefaultHttpClient(@Nullable @Nullable URI uri, @NonNull @NonNull HttpClientConfiguration configuration) - Parameters:
uri- The URIconfiguration- TheHttpClientConfigurationobject
-
DefaultHttpClient
public DefaultHttpClient(@Nullable @Nullable URI uri, @NonNull @NonNull HttpClientConfiguration configuration, @NonNull @NonNull ClientSslBuilder clientSslBuilder) Constructor used by micronaut-oracle-cloud.- Parameters:
uri- The URIconfiguration- TheHttpClientConfigurationobjectclientSslBuilder- The SSL builder
-
DefaultHttpClient
public DefaultHttpClient(@Nullable @Nullable LoadBalancer loadBalancer, HttpClientConfiguration configuration) - Parameters:
loadBalancer- TheLoadBalancerto use for selecting serversconfiguration- TheHttpClientConfigurationobject
-
-
Method Details
-
getConfiguration
- Returns:
- The configuration used by this client
-
getLog
public org.slf4j.Logger getLog()- Returns:
- The client-specific logger name
-
connectionManager
Access to the connection manager, for micronaut-oracle-cloud.- Returns:
- The connection manager of this client
-
start
Description copied from interface:LifeCycleStarts the lifecyle component.- Specified by:
startin interfaceLifeCycle<HttpClient>- Returns:
- This lifecycle component
-
isRunning
public boolean isRunning()- Specified by:
isRunningin interfaceLifeCycle<HttpClient>- Returns:
- Whether the component is running
-
stop
Description copied from interface:LifeCycleStops the life cycle component.- Specified by:
stopin interfaceLifeCycle<HttpClient>- Returns:
- This lifecycle component
-
getMediaTypeCodecRegistry
- Returns:
- The
MediaTypeCodecRegistryused by this client
-
setMediaTypeCodecRegistry
Deprecated.Sets theMediaTypeCodecRegistryused by this client.- Parameters:
mediaTypeCodecRegistry- The registry to use. Should not be null
-
getHandlerRegistry
Get the handler registry for this client.- Returns:
- The handler registry
-
setHandlerRegistry
Set the handler registry for this client.- Parameters:
handlerRegistry- The handler registry
-
toBlocking
- Specified by:
toBlockingin interfaceHttpClient- Returns:
- A blocking HTTP client suitable for testing and non-production scenarios.
-
eventStream
Description copied from interface:SseClientPerform an HTTP request and receive data as a stream of SSE
Eventobjects as they become available without blocking.The downstream
Subscribercan regulate demand via the subscription- Specified by:
eventStreamin interfaceSseClient- Type Parameters:
I- The request body type- Parameters:
request- TheHttpRequestto execute- Returns:
- A
Publisherthat emits anEventwith the data represented as aByteBuffer
-
eventStream
public <I,B> Publisher<Event<B>> eventStream(@NonNull HttpRequest<I> request, @NonNull @NonNull Argument<B> eventType) Description copied from interface:SseClientPerform an HTTP request and receive data as a stream of SSE
Eventobjects as they become available without blocking.The downstream
Subscribercan regulate demand via the subscription- Specified by:
eventStreamin interfaceSseClient- Type Parameters:
I- The request body typeB- The event body type- Parameters:
request- TheHttpRequestto executeeventType- The event data type- Returns:
- A
Publisherthat emits anEventwith the data represented by the eventType argument
-
eventStream
public <I,B> Publisher<Event<B>> eventStream(@NonNull HttpRequest<I> request, @NonNull @NonNull Argument<B> eventType, @NonNull @NonNull Argument<?> errorType) Description copied from interface:SseClientPerform an HTTP request and receive data as a stream of SSE
Eventobjects as they become available without blocking.The downstream
Subscribercan regulate demand via the subscription- Specified by:
eventStreamin interfaceSseClient- Type Parameters:
I- The request body typeB- The event body type- Parameters:
request- TheHttpRequestto executeeventType- The event data typeerrorType- The type that the response body should be coerced into if the server responds with an error- Returns:
- A
Publisherthat emits anEventwith the data represented by the eventType argument
-
dataStream
Description copied from interface:StreamingHttpClientRequest a stream of data where each emitted item is aByteBufferinstance.- Specified by:
dataStreamin interfaceStreamingHttpClient- Type Parameters:
I- The request body type- Parameters:
request- The request- Returns:
- A
Publisherthat emits a stream ofByteBufferinstances
-
dataStream
public <I> Publisher<ByteBuffer<?>> dataStream(@NonNull HttpRequest<I> request, @NonNull @NonNull Argument<?> errorType) Description copied from interface:StreamingHttpClientRequest a stream of data where each emitted item is aByteBufferinstance.- Specified by:
dataStreamin interfaceStreamingHttpClient- Type Parameters:
I- The request body type- Parameters:
request- The requesterrorType- The type that the response body should be coerced into if the server responds with an error- Returns:
- A
Publisherthat emits a stream ofByteBufferinstances
-
exchangeStream
Description copied from interface:StreamingHttpClientRequests a stream data where each emitted item is aByteBufferwrapped in theHttpResponseobject (which remains the same for each emitted item).- Specified by:
exchangeStreamin interfaceStreamingHttpClient- Type Parameters:
I- The request body type- Parameters:
request- TheHttpRequest- Returns:
- A
Publisherthat emits a stream ofByteBufferinstances wrapped by aHttpResponse
-
exchangeStream
public <I> Publisher<HttpResponse<ByteBuffer<?>>> exchangeStream(@NonNull HttpRequest<I> request, @NonNull @NonNull Argument<?> errorType) Description copied from interface:StreamingHttpClientRequests a stream data where each emitted item is aByteBufferwrapped in theHttpResponseobject (which remains the same for each emitted item).- Specified by:
exchangeStreamin interfaceStreamingHttpClient- Type Parameters:
I- The request body type- Parameters:
request- TheHttpRequesterrorType- The type that the response body should be coerced into if the server responds with an error- Returns:
- A
Publisherthat emits a stream ofByteBufferinstances wrapped by aHttpResponse
-
jsonStream
public <I,O> Publisher<O> jsonStream(@NonNull HttpRequest<I> request, @NonNull @NonNull Argument<O> type) Description copied from interface:StreamingHttpClientPerform an HTTP request and receive data as a stream of JSON objects as they become available without blocking.
The downstream
Subscribercan regulate demand via the subscription. Incoming data is buffered.- Specified by:
jsonStreamin interfaceStreamingHttpClient- Type Parameters:
I- The request body typeO- The response type- Parameters:
request- TheHttpRequestto executetype- The type of object to convert the JSON into- Returns:
- A
Publisherthat emits the fullHttpResponseobject
-
jsonStream
public <I,O> Publisher<O> jsonStream(@NonNull HttpRequest<I> request, @NonNull @NonNull Argument<O> type, @NonNull @NonNull Argument<?> errorType) Description copied from interface:StreamingHttpClientPerform an HTTP request and receive data as a stream of JSON objects as they become available without blocking.
The downstream
Subscribercan regulate demand via the subscription. Incoming data is buffered.- Specified by:
jsonStreamin interfaceStreamingHttpClient- Type Parameters:
I- The request body typeO- The response type- Parameters:
request- TheHttpRequestto 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 error- Returns:
- A
Publisherthat emits the fullHttpResponseobject
-
jsonStream
Description copied from interface:StreamingHttpClientPerform an HTTP request and receive data as a stream of JSON objects as they become available without blocking.
The downstream
Subscribercan regulate demand via the subscription- Specified by:
jsonStreamin interfaceStreamingHttpClient- Type Parameters:
I- The request body type- Parameters:
request- TheHttpRequestto execute- Returns:
- A
Publisherthat emits the fullHttpResponseobject
-
jsonStream
public <I,O> Publisher<O> jsonStream(@NonNull HttpRequest<I> request, @NonNull @NonNull Class<O> type) Description copied from interface:StreamingHttpClientPerform an HTTP request and receive data as a stream of JSON objects as they become available without blocking.
The downstream
Subscribercan regulate demand via the subscription- Specified by:
jsonStreamin interfaceStreamingHttpClient- Type Parameters:
I- The request body typeO- The response type- Parameters:
request- TheHttpRequestto executetype- The type of object to convert the JSON into- Returns:
- A
Publisherthat emits the fullHttpResponseobject
-
exchange
public <I,O, Publisher<HttpResponse<O>> exchangeE> (@NonNull HttpRequest<I> request, @NonNull @NonNull Argument<O> bodyType, @NonNull @NonNull Argument<E> errorType) Description copied from interface:HttpClientPerform an HTTP request for the given request object emitting the full HTTP response from returned
Publisherand converting the response body to the specified type.This method will send a
Content-Lengthheader and except a content length header the response and is designed for simple non-streaming exchanges of dataBy default the exchange
Content-Typeis application/json, unless otherwise specified in the passedHttpRequest- Specified by:
exchangein interfaceHttpClient- Type Parameters:
I- The request body typeO- The response body typeE- The error type- Parameters:
request- TheHttpRequestto executebodyType- The body typeerrorType- The error type- Returns:
- A
Publisherthat emits the fullHttpResponseobject
-
retrieve
public <I,O, Publisher<O> retrieveE> (HttpRequest<I> request, Argument<O> bodyType, Argument<E> errorType) Description copied from interface:HttpClientPerform an HTTP request for the given request object emitting the full HTTP response from returnedPublisherand converting the response body to the specified type.- Specified by:
retrievein interfaceHttpClient- Type Parameters:
I- The request body typeO- The response body typeE- The error type- Parameters:
request- TheHttpRequestto executebodyType- The body typeerrorType- The error type- Returns:
- A
Publisherthat emits a result of the given type
-
connect
public <T extends AutoCloseable> Publisher<T> connect(Class<T> clientEndpointType, MutableHttpRequest<?> request) Description copied from interface:WebSocketClientConnect the given client endpoint type to the URI over WebSocket.- Specified by:
connectin interfaceWebSocketClient- Type Parameters:
T- The generic type- Parameters:
clientEndpointType- The endpoint type. Should be a class annotated withClientWebSocketrequest- The original request to establish the connection- Returns:
- A
Publisherthat emits theClientWebSocketinstance
-
connect
public <T extends AutoCloseable> Publisher<T> connect(Class<T> clientEndpointType, Map<String, Object> parameters) Description copied from interface:WebSocketClientConnect the given client endpoint type. UnlikeWebSocketClient.connect(Class, URI)this method will the value declared within theClientWebSocketas the URI and expand the URI with the given parameters.- Specified by:
connectin interfaceWebSocketClient- Type Parameters:
T- The generic type- Parameters:
clientEndpointType- The endpoint type. Should be a class annotated withClientWebSocketparameters- The URI parameters for the endpoint- Returns:
- A
Publisherthat emits theClientWebSocketinstance
-
close
public void close()Description copied from interface:LifeCycleDelegates toLifeCycle.stop().- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceLifeCycle<HttpClient>- Specified by:
closein interfaceWebSocketClient
-
proxy
Description copied from interface:ProxyHttpClientProxy the given request and emit the response. This method expects the full absolute URL to be included in the request. If a relative URL is specified then the method will try to resolve the URI for the current server otherwise an exception will be thrown.- Specified by:
proxyin interfaceProxyHttpClient- Parameters:
request- The request- Returns:
- A publisher that emits the response.
-
proxy
public Publisher<MutableHttpResponse<?>> proxy(@NonNull HttpRequest<?> request, @NonNull @NonNull ProxyRequestOptions options) Description copied from interface:ProxyHttpClientProxy the given request and emit the response. This method expects the full absolute URL to be included in the request. If a relative URL is specified then the method will try to resolve the URI for the current server otherwise an exception will be thrown.- Specified by:
proxyin interfaceProxyHttpClient- Parameters:
request- The requestoptions- Further options for the proxy request- Returns:
- A publisher that emits the response.
-
resolveRequestURI
- Type Parameters:
I- The input type- Parameters:
request- The request- Returns:
- A
Publisherwith the resolved URI
-
resolveRequestURI
- Type Parameters:
I- The input type- Parameters:
request- The requestincludeContextPath- Whether to prepend the client context path- Returns:
- A
Publisherwith the resolved URI
-
resolveRedirectURI
protected <I> Publisher<URI> resolveRedirectURI(HttpRequest<?> parentRequest, HttpRequest<I> request) - Type Parameters:
I- The input type- Parameters:
parentRequest- The parent requestrequest- The redirect location request- Returns:
- A
Publisherwith the resolved URI
-
getLoadBalancerDiscriminator
- Returns:
- The discriminator to use when selecting a server for the purposes of load balancing (defaults to null)
-
buildNettyRequest
protected io.micronaut.http.client.netty.DefaultHttpClient.NettyRequestWriter buildNettyRequest(MutableHttpRequest request, URI requestURI, MediaType requestContentType, boolean permitsBody, Consumer<? super Throwable> onError) throws io.netty.handler.codec.http.multipart.HttpPostRequestEncoder.ErrorDataEncoderException - Parameters:
request- The requestrequestURI- The URI of the requestrequestContentType- The request content typepermitsBody- Whether permits bodyonError- Called when the body publisher encounters an error- Returns:
- A
DefaultHttpClient.NettyRequestWriter - Throws:
io.netty.handler.codec.http.multipart.HttpPostRequestEncoder.ErrorDataEncoderException- if there is an encoder exception
-