Interface HttpResponse
- All Superinterfaces:
HttpMessage
,org.reactivestreams.Publisher<HttpObject>
,Response
,StreamMessage<HttpObject>
- All Known Subinterfaces:
HttpResponseWriter
- All Known Implementing Classes:
FilteredHttpResponse
Response
.-
Method Summary
Modifier and TypeMethodDescriptiondefault CompletableFuture<AggregatedHttpResponse>
Aggregates this response.aggregate
(AggregationOptions options) Aggregates this response with the specifiedAggregationOptions
.default CompletableFuture<AggregatedHttpResponse>
aggregate
(EventExecutor executor) Aggregates this response.default CompletableFuture<AggregatedHttpResponse>
Deprecated.default CompletableFuture<AggregatedHttpResponse>
aggregateWithPooledObjects
(EventExecutor executor, ByteBufAllocator alloc) Deprecated.static HttpResponseBuilder
builder()
Returns a newHttpResponseBuilder
.static HttpResponse
delayed
(AggregatedHttpResponse response, Duration delay) Creates a new HTTP response that delegates to the providedAggregatedHttpResponse
, beginning publishing afterdelay
has passed from a randomScheduledExecutorService
.static HttpResponse
delayed
(AggregatedHttpResponse response, Duration delay, ScheduledExecutorService executor) Creates a new HTTP response that delegates to the providedAggregatedHttpResponse
, beginning publishing afterdelay
has passed from the providedScheduledExecutorService
.static HttpResponse
delayed
(HttpResponse response, Duration delay) Creates a new HTTP response that delegates to the providedHttpResponse
, beginning publishing afterdelay
has passed from a randomScheduledExecutorService
.static HttpResponse
delayed
(HttpResponse response, Duration delay, ScheduledExecutorService executor) Creates a new HTTP response that delegates to the providedHttpResponse
, beginning publishing afterdelay
has passed from the providedScheduledExecutorService
.static HttpResponse
delayed
(Supplier<? extends HttpResponse> responseSupplier, Duration delay) Invokes the specifiedSupplier
and creates a new HTTP response that delegates to the providedHttpResponse
bySupplier
.static HttpResponse
delayed
(Supplier<? extends HttpResponse> responseSupplier, Duration delay, ScheduledExecutorService executor) Invokes the specifiedSupplier
and creates a new HTTP response that delegates to the providedHttpResponse
Supplier
, beginning publishing afterdelay
has passed from the providedScheduledExecutorService
.static HttpResponse
from
(CompletableFuture<? extends HttpResponse> future) Creates a new HTTP response that delegates to theHttpResponse
produced by the specifiedCompletableFuture
.static HttpResponse
from
(CompletionStage<? extends HttpResponse> stage) Creates a new HTTP response that delegates to theHttpResponse
produced by the specifiedCompletionStage
.static HttpResponse
from
(CompletionStage<? extends HttpResponse> stage, EventExecutor subscriberExecutor) Creates a new HTTP response that delegates to theHttpResponse
produced by the specifiedCompletionStage
.static HttpResponse
from
(Supplier<? extends HttpResponse> responseSupplier, Executor executor) Creates a new HTTP response that delegates to theHttpResponse
provided by theSupplier
.default HttpResponse
default HttpResponse
Transforms an error emitted by thisHttpResponse
by applying the specifiedFunction
.default HttpResponse
mapHeaders
(Function<? super ResponseHeaders, ? extends ResponseHeaders> function) Transforms the non-informationalResponseHeaders
emitted byHttpResponse
by applying the specifiedFunction
.default HttpResponse
mapInformational
(Function<? super ResponseHeaders, ? extends ResponseHeaders> function) default HttpResponse
mapTrailers
(Function<? super HttpHeaders, ? extends HttpHeaders> function) static HttpResponse
of
(int statusCode) Creates a new HTTP response of the specifiedstatusCode
.static HttpResponse
of
(HttpObject... objs) Creates a new HTTP response of the specified objects.static HttpResponse
of
(HttpStatus status) Creates a new HTTP response of the specifiedHttpStatus
.static HttpResponse
of
(HttpStatus status, MediaType mediaType, byte[] content) Creates a new HTTP response of the specifiedHttpStatus
.static HttpResponse
of
(HttpStatus status, MediaType mediaType, HttpData content) Creates a new HTTP response of the specifiedHttpStatus
.static HttpResponse
of
(HttpStatus status, MediaType mediaType, HttpData content, HttpHeaders trailers) Creates a new HTTP response of the specifiedHttpStatus
.static HttpResponse
of
(HttpStatus status, MediaType mediaType, CharSequence content) Creates a new HTTP response of the specifiedHttpStatus
.static HttpResponse
of
(HttpStatus status, MediaType mediaType, String content) Creates a new HTTP response of the specifiedHttpStatus
.static HttpResponse
of
(HttpStatus status, MediaType mediaType, String format, Object... args) Creates a new HTTP response of the specifiedHttpStatus
.static HttpResponse
Creates a new HTTP response of OK status with the content.static HttpResponse
Creates a new HTTP response of OK status with the content.static HttpResponse
of
(ResponseHeaders headers) Creates a new HTTP response of the specified headers.static HttpResponse
of
(ResponseHeaders headers, HttpData content) Creates a new HTTP response of the specified headers and content.static HttpResponse
of
(ResponseHeaders headers, HttpData content, HttpHeaders trailers) Creates a new HTTP response of the specified objects.static HttpResponse
of
(ResponseHeaders headers, org.reactivestreams.Publisher<? extends HttpObject> publisher) Creates a new HTTP response with the specified headers whose stream is produced from an existingPublisher
.static HttpResponse
Creates a new HTTP response of OK status with the content as UTF_8.static HttpResponse
Creates a new HTTP response of OK status with the content as UTF_8.static HttpResponse
of
(org.reactivestreams.Publisher<? extends HttpObject> publisher) Creates a new HTTP response whose stream is produced from an existingPublisher
.static HttpResponse
Creates a new failed HTTP response.static HttpResponse
ofJson
(HttpStatus status, MediaType contentType, Object content) Creates a new HTTP response with the specifiedHttpStatus
,MediaType
andcontent
that is converted into JSON using the defaultObjectMapper
.static HttpResponse
ofJson
(HttpStatus status, Object content) Creates a new HTTP response with the specifiedHttpStatus
andcontent
that is converted into JSON using the defaultObjectMapper
.static HttpResponse
Creates a new HTTP response with the specifiedMediaType
andcontent
that is converted into JSON using the defaultObjectMapper
.static HttpResponse
ofJson
(ResponseHeaders headers, Object content) Creates a new HTTP response with the specifiedResponseHeaders
andcontent
that is converted into JSON using the defaultObjectMapper
.static HttpResponse
Creates a new HTTP response with the specifiedcontent
that is converted into JSON using the defaultObjectMapper
.static HttpResponse
ofRedirect
(HttpStatus redirectStatus, String location) Creates a new HTTP response of the redirect to specific location.static HttpResponse
ofRedirect
(HttpStatus redirectStatus, String format, Object... args) Creates a new HTTP response of the redirect to specific location using string format.static HttpResponse
ofRedirect
(String location) Creates a new HTTP response of the temporary redirect to specific location.static HttpResponse
ofRedirect
(String format, Object... args) Creates a new HTTP response of the temporary redirect to specific location using string format.default HttpResponse
default HttpResponse
Applies the specifiedConsumer
to an error emitted by thisHttpResponse
.default HttpResponse
peekHeaders
(Consumer<? super ResponseHeaders> action) Applies the specifiedConsumer
to the non-informationalResponseHeaders
emitted by thisHttpResponse
.default HttpResponse
peekTrailers
(Consumer<? super HttpHeaders> action) default <T extends Throwable>
HttpResponserecover
(Class<T> causeClass, Function<? super T, ? extends HttpResponse> function) Recovers a failedHttpResponse
by switching to a returned fallbackHttpResponse
when the thrownThrowable
is the same type or a subtype of the specifiedcauseClass
.default HttpResponse
recover
(Function<? super Throwable, ? extends HttpResponse> function) Recovers a failedHttpResponse
by switching to a returned fallbackHttpResponse
when any error occurs before aResponseHeaders
is written.default SplitHttpResponse
split()
default SplitHttpResponse
split
(EventExecutor executor) static HttpResponseWriter
Creates a new HTTP response that can stream an arbitrary number ofHttpObject
to the client.default HttpResponseDuplicator
Returns a newStreamMessageDuplicator
that duplicates thisStreamMessage
into one or moreStreamMessage
s, which publish the same elements.default HttpResponseDuplicator
toDuplicator
(long maxResponseLength) Returns a newStreamMessageDuplicator
that duplicates thisHttpMessage
into one or moreHttpMessage
s, which publish the same elements.default HttpResponseDuplicator
toDuplicator
(EventExecutor executor) Returns a newStreamMessageDuplicator
that duplicates thisStreamMessage
into one or moreStreamMessage
s, which publish the same elements.default HttpResponseDuplicator
toDuplicator
(EventExecutor executor, long maxResponseLength) Returns a newStreamMessageDuplicator
that duplicates thisHttpMessage
into one or moreHttpMessage
s, which publish the same elements.Returns aCompletableFuture
which completes when 1) the response stream has been closed (theStreamMessage
has been completed) or 2) the result value is set (theCompletionStage
has completed.)Methods inherited from interface com.linecorp.armeria.common.HttpMessage
decode, decode
Methods inherited from interface com.linecorp.armeria.common.stream.StreamMessage
abort, abort, collect, collect, collect, decode, decode, defaultSubscriberExecutor, demand, filter, isComplete, isEmpty, isOpen, map, mapAsync, mapParallel, mapParallel, peek, peek, recoverAndResume, recoverAndResume, subscribe, subscribe, subscribe, subscribe, subscribe, toInputStream, toInputStream, writeTo
-
Method Details
-
streaming
Creates a new HTTP response that can stream an arbitrary number ofHttpObject
to the client. The first object written must be of typeResponseHeaders
. -
from
Creates a new HTTP response that delegates to theHttpResponse
produced by the specifiedCompletionStage
. If the specifiedCompletionStage
fails, the returned response will be closed with the same cause as well.- Parameters:
stage
- theCompletionStage
which will produce the actualHttpResponse
-
from
Creates a new HTTP response that delegates to theHttpResponse
produced by the specifiedCompletableFuture
. If the specifiedCompletableFuture
fails, the returned response will be closed with the same cause as well.- Parameters:
future
- theCompletableFuture
which will produce the actualHttpResponse
-
from
static HttpResponse from(CompletionStage<? extends HttpResponse> stage, EventExecutor subscriberExecutor) Creates a new HTTP response that delegates to theHttpResponse
produced by the specifiedCompletionStage
. If the specifiedCompletionStage
fails, the returned response will be closed with the same cause as well.- Parameters:
stage
- theCompletionStage
which will produce the actualHttpResponse
subscriberExecutor
- theEventExecutor
which will be used when a user subscribes the returnedHttpResponse
usingStreamMessage.subscribe(Subscriber)
orStreamMessage.subscribe(Subscriber, SubscriptionOption...)
.
-
from
Creates a new HTTP response that delegates to theHttpResponse
provided by theSupplier
.- Parameters:
responseSupplier
- theSupplier
invokes returning the providedHttpResponse
executor
- theExecutor
that executes theSupplier
.
-
delayed
Creates a new HTTP response that delegates to the providedAggregatedHttpResponse
, beginning publishing afterdelay
has passed from a randomScheduledExecutorService
. -
delayed
static HttpResponse delayed(AggregatedHttpResponse response, Duration delay, ScheduledExecutorService executor) Creates a new HTTP response that delegates to the providedAggregatedHttpResponse
, beginning publishing afterdelay
has passed from the providedScheduledExecutorService
. -
delayed
Creates a new HTTP response that delegates to the providedHttpResponse
, beginning publishing afterdelay
has passed from a randomScheduledExecutorService
. -
delayed
static HttpResponse delayed(HttpResponse response, Duration delay, ScheduledExecutorService executor) Creates a new HTTP response that delegates to the providedHttpResponse
, beginning publishing afterdelay
has passed from the providedScheduledExecutorService
. -
delayed
Invokes the specifiedSupplier
and creates a new HTTP response that delegates to the providedHttpResponse
bySupplier
.The
Supplier
is invoked from the current thread-localRequestContext
's event loop. If there's no thread localRequestContext
is set, one of the threads fromCommonPools.workerGroup().next()
will be used. -
delayed
static HttpResponse delayed(Supplier<? extends HttpResponse> responseSupplier, Duration delay, ScheduledExecutorService executor) Invokes the specifiedSupplier
and creates a new HTTP response that delegates to the providedHttpResponse
Supplier
, beginning publishing afterdelay
has passed from the providedScheduledExecutorService
. -
of
Creates a new HTTP response of the specifiedstatusCode
.- Throws:
IllegalArgumentException
- if the specifiedstatusCode
is informational.
-
of
Creates a new HTTP response of the specifiedHttpStatus
.- Throws:
IllegalArgumentException
- if the specifiedHttpStatus
is informational.
-
of
Creates a new HTTP response of the specifiedHttpStatus
.- Parameters:
mediaType
- theMediaType
of the response contentcontent
- the content of the response- Throws:
IllegalArgumentException
- if the specifiedHttpStatus
is informational.
-
of
Creates a new HTTP response of the specifiedHttpStatus
.- Parameters:
mediaType
- theMediaType
of the response contentcontent
- the content of the response- Throws:
IllegalArgumentException
- if the specifiedHttpStatus
is informational.
-
of
Creates a new HTTP response of OK status with the content as UTF_8.- Parameters:
content
- the content of the response
-
of
Creates a new HTTP response of OK status with the content as UTF_8. The content of the response is formatted byString.format(Locale, String, Object...)
with English locale.- Parameters:
format
- the format string of the response contentargs
- the arguments referenced by the format specifiers in the format string
-
of
Creates a new HTTP response of OK status with the content.- Parameters:
mediaType
- theMediaType
of the response contentcontent
- the content of the response
-
of
@FormatMethod static HttpResponse of(MediaType mediaType, @FormatString String format, Object... args) Creates a new HTTP response of OK status with the content. The content of the response is formatted byString.format(Locale, String, Object...)
with English locale.- Parameters:
mediaType
- theMediaType
of the response contentformat
- the format string of the response contentargs
- the arguments referenced by the format specifiers in the format string
-
of
@FormatMethod static HttpResponse of(HttpStatus status, MediaType mediaType, @FormatString String format, Object... args) Creates a new HTTP response of the specifiedHttpStatus
. The content of the response is formatted byString.format(Locale, String, Object...)
with English locale.- Parameters:
mediaType
- theMediaType
of the response contentformat
- the format string of the response contentargs
- the arguments referenced by the format specifiers in the format string- Throws:
IllegalArgumentException
- if the specifiedHttpStatus
is informational.
-
of
Creates a new HTTP response of the specifiedHttpStatus
. Thecontent
will be wrapped usingHttpData.wrap(byte[])
, so any changes made tocontent
will be reflected in the response.- Parameters:
mediaType
- theMediaType
of the response contentcontent
- the content of the response- Throws:
IllegalArgumentException
- if the specifiedHttpStatus
is informational.
-
of
Creates a new HTTP response of the specifiedHttpStatus
.- Parameters:
mediaType
- theMediaType
of the response contentcontent
- the content of the response- Throws:
IllegalArgumentException
- if the specifiedHttpStatus
is informational.
-
of
static HttpResponse of(HttpStatus status, MediaType mediaType, HttpData content, HttpHeaders trailers) Creates a new HTTP response of the specifiedHttpStatus
.- Parameters:
mediaType
- theMediaType
of the response contentcontent
- the content of the responsetrailers
- the HTTP trailers- Throws:
IllegalArgumentException
- if the specifiedHttpStatus
is informational.
-
of
Creates a new HTTP response of the specified headers.- Throws:
IllegalArgumentException
- if the status of the specifiedResponseHeaders
is informational.
-
of
Creates a new HTTP response of the specified headers and content.- Throws:
IllegalArgumentException
- if the status of the specifiedResponseHeaders
is informational.
-
of
Creates a new HTTP response of the specified objects.- Throws:
IllegalArgumentException
- if the status of the specifiedResponseHeaders
is informational.
-
of
Creates a new HTTP response of the specified objects. -
of
Creates a new HTTP response whose stream is produced from an existingPublisher
.Note that the
HttpObject
s in thePublisher
are not released whenSubscription.cancel()
orStreamMessage.abort()
is called. You should add a hook in order to release the elements. SeePublisherBasedStreamMessage
for more information. -
of
static HttpResponse of(ResponseHeaders headers, org.reactivestreams.Publisher<? extends HttpObject> publisher) Creates a new HTTP response with the specified headers whose stream is produced from an existingPublisher
.Note that the
HttpObject
s in thePublisher
are not released whenSubscription.cancel()
orStreamMessage.abort()
is called. You should add a hook in order to release the elements. SeePublisherBasedStreamMessage
for more information. -
ofJson
Creates a new HTTP response with the specifiedcontent
that is converted into JSON using the defaultObjectMapper
.- Throws:
IllegalArgumentException
- if failed to encode thecontent
into JSON.- See Also:
-
ofJson
Creates a new HTTP response with the specifiedHttpStatus
andcontent
that is converted into JSON using the defaultObjectMapper
.- Throws:
IllegalArgumentException
- if failed to encode thecontent
into JSON.- See Also:
-
ofJson
Creates a new HTTP response with the specifiedMediaType
andcontent
that is converted into JSON using the defaultObjectMapper
.- Throws:
IllegalArgumentException
- if the specifiedMediaType
is not a JSON compatible type; or if failed to encode thecontent
into JSON.- See Also:
-
ofJson
Creates a new HTTP response with the specifiedHttpStatus
,MediaType
andcontent
that is converted into JSON using the defaultObjectMapper
.- Throws:
IllegalArgumentException
- if the specifiedMediaType
is not a JSON compatible type; or if failed to encode thecontent
into JSON.- See Also:
-
ofJson
Creates a new HTTP response with the specifiedResponseHeaders
andcontent
that is converted into JSON using the defaultObjectMapper
.- Throws:
IllegalArgumentException
- if failed to encode thecontent
into JSON.- See Also:
-
ofRedirect
Creates a new HTTP response of the redirect to specific location. -
ofRedirect
Creates a new HTTP response of the redirect to specific location using string format. -
ofRedirect
Creates a new HTTP response of the temporary redirect to specific location. -
ofRedirect
Creates a new HTTP response of the temporary redirect to specific location using string format. -
ofFailure
Creates a new failed HTTP response. -
builder
Returns a newHttpResponseBuilder
. -
whenComplete
CompletableFuture<Void> whenComplete()Description copied from interface:Response
Returns aCompletableFuture
which completes when 1) the response stream has been closed (theStreamMessage
has been completed) or 2) the result value is set (theCompletionStage
has completed.)- Specified by:
whenComplete
in interfaceResponse
- Specified by:
whenComplete
in interfaceStreamMessage<HttpObject>
-
aggregate
Aggregates this response with the specifiedAggregationOptions
. The returnedCompletableFuture
will be notified when the content and the trailers of the response are fully received.AggregationOptions options = AggregationOptions.builder() .cacheResult(false) .executor(...) .build(); HttpResponse request = ...; AggregatedHttpResponse aggregated = response.aggregate(options).join();
-
aggregate
Aggregates this response. The returnedCompletableFuture
will be notified when the content and the trailers of the response are received fully.The
AggregatedHttpResponse
is cached by default. So it is allowed to repeatedly call this method and get the cached value after the first aggregation.HttpResponse response = ...; AggregatedHttpResponse aggregated0 = response.aggregate().join(); AggregatedHttpResponse aggregated1 = response.aggregate().join(); assert aggregated0 == aggregated1;
-
aggregate
Aggregates this response. The returnedCompletableFuture
will be notified when the content and the trailers of the response are received fully.The
AggregatedHttpResponse
is cached by default. So it is allowed to repeatedly call this method and get the cached value after the first aggregation.HttpResponse response = ...; AggregatedHttpResponse aggregated0 = response.aggregate(executor).join(); AggregatedHttpResponse aggregated1 = response.aggregate(executor).join(); assert aggregated0 == aggregated1;
-
aggregateWithPooledObjects
@Deprecated @UnstableApi default CompletableFuture<AggregatedHttpResponse> aggregateWithPooledObjects(ByteBufAllocator alloc) Deprecated.(Advanced users only) Aggregates this response. The returnedCompletableFuture
will be notified when the content and the trailers of the response are received fully.AggregatedHttpObject.content()
will return a pooled object, and the caller must ensure to release it. If you don't know what this means, useaggregate()
.The pooled
AggregatedHttpResponse
is not cached. So it is NOT allowed to access theAggregatedHttpResponse
from this method after the first aggregation.HttpResponse response = ...; AggregatedHttpResponse aggregated = response.aggregateWithPooledObjects(alloc).join(); // An `IllegalStateException` will be raised. response.aggregateWithPooledObjects(alloc).join();
- See Also:
-
aggregateWithPooledObjects
@Deprecated default CompletableFuture<AggregatedHttpResponse> aggregateWithPooledObjects(EventExecutor executor, ByteBufAllocator alloc) Deprecated.Aggregates this response. The returnedCompletableFuture
will be notified when the content and the trailers of the response is received fully.AggregatedHttpObject.content()
will return a pooled object, and the caller must ensure to release it. If you don't know what this means, useaggregate()
.The pooled
AggregatedHttpResponse
is not cached. So it is NOT allowed to access theAggregatedHttpResponse
from this method after the first aggregation.HttpResponse response = ...; AggregatedHttpResponse aggregated = response.aggregateWithPooledObjects(executor, alloc).join(); // An `IllegalStateException` will be raised. response.aggregateWithPooledObjects(executor, alloc).join();
-
toDuplicator
Description copied from interface:StreamMessage
Returns a newStreamMessageDuplicator
that duplicates thisStreamMessage
into one or moreStreamMessage
s, which publish the same elements. Note that you cannot subscribe to thisStreamMessage
anymore after you call this method. To subscribe, callStreamMessageDuplicator.duplicate()
from the returnedStreamMessageDuplicator
.- Specified by:
toDuplicator
in interfaceStreamMessage<HttpObject>
-
toDuplicator
Description copied from interface:StreamMessage
Returns a newStreamMessageDuplicator
that duplicates thisStreamMessage
into one or moreStreamMessage
s, which publish the same elements. Note that you cannot subscribe to thisStreamMessage
anymore after you call this method. To subscribe, callStreamMessageDuplicator.duplicate()
from the returnedStreamMessageDuplicator
.- Specified by:
toDuplicator
in interfaceStreamMessage<HttpObject>
- Parameters:
executor
- the executor to duplicate
-
toDuplicator
Description copied from interface:HttpMessage
Returns a newStreamMessageDuplicator
that duplicates thisHttpMessage
into one or moreHttpMessage
s, which publish the same elements. Note that you cannot subscribe to thisHttpMessage
anymore after you call this method. To subscribe, callStreamMessageDuplicator.duplicate()
from the returnedStreamMessageDuplicator
.- Specified by:
toDuplicator
in interfaceHttpMessage
- Parameters:
maxResponseLength
- the maximum content length that the duplicator can hold in its buffer.ContentTooLargeException
is raised if the length of the bufferedHttpData
is greater than this value.
-
toDuplicator
Description copied from interface:HttpMessage
Returns a newStreamMessageDuplicator
that duplicates thisHttpMessage
into one or moreHttpMessage
s, which publish the same elements. Note that you cannot subscribe to thisHttpMessage
anymore after you call this method. To subscribe, callStreamMessageDuplicator.duplicate()
from the returnedStreamMessageDuplicator
.- Specified by:
toDuplicator
in interfaceHttpMessage
- Parameters:
executor
- the executor to duplicatemaxResponseLength
- the maximum content length that the duplicator can hold in its buffer.ContentTooLargeException
is raised if the length of the bufferedHttpData
is greater than this value.
-
split
Returns a newSplitHttpResponse
which splits a stream ofHttpObject
s intoHttpHeaders
andHttpData
.SplitHttpResponse.headers()
will be completed before publishing the firstHttpData
.SplitHttpMessage.trailers()
might not complete until the entire response body is consumed completely. -
split
Returns a newSplitHttpResponse
which splits a stream ofHttpObject
s intoHttpHeaders
andHttpData
.SplitHttpResponse.headers()
will be completed before publishing the firstHttpData
.SplitHttpMessage.trailers()
might not complete until the entire response body is consumed completely. -
mapInformational
default HttpResponse mapInformational(Function<? super ResponseHeaders, ? extends ResponseHeaders> function) -
mapHeaders
default HttpResponse mapHeaders(Function<? super ResponseHeaders, ? extends ResponseHeaders> function) Transforms the non-informationalResponseHeaders
emitted byHttpResponse
by applying the specifiedFunction
.For example:
HttpResponse response = HttpResponse.of("OK"); HttpResponse transformed = response.mapHeaders(headers -> { return headers.withMutations(builder -> { builder.set(HttpHeaderNames.USER_AGENT, "my-server"); }); }); assert transformed.aggregate().join().headers() .get(HttpHeaderNames.USER_AGENT).equals("my-server");
-
mapData
Transforms theHttpData
s emitted by thisHttpResponse
by applying the specifiedFunction
.For example:
HttpResponse response = HttpResponse.of("data1,data2"); HttpResponse transformed = response.mapData(data -> { return HttpData.ofUtf8(data.toStringUtf8().replaceAll(",", "\n")); }); assert transformed.aggregate().join().contentUtf8().equals("data1\ndata2");
- Specified by:
mapData
in interfaceHttpMessage
-
mapTrailers
Transforms the trailers emitted by thisHttpResponse
by applying the specifiedFunction
.For example:
HttpResponse response = HttpResponse.of("data"); HttpResponse transformed = response.mapTrailers(trailers -> { return trailers.withMutations(builder -> builder.add("trailer1", "foo")); }); assert transformed.aggregate().join().trailers().get("trailer1").equals("foo");
- Specified by:
mapTrailers
in interfaceHttpMessage
-
mapError
Transforms an error emitted by thisHttpResponse
by applying the specifiedFunction
.For example:
HttpResponse response = HttpResponse.ofFailure(new IllegalStateException("Something went wrong.")); HttpResponse transformed = response.mapError(cause -> { if (cause instanceof IllegalStateException) { return new MyDomainException(cause); } else { return cause; } });
- Specified by:
mapError
in interfaceStreamMessage<HttpObject>
-
peekHeaders
Applies the specifiedConsumer
to the non-informationalResponseHeaders
emitted by thisHttpResponse
.For example:
HttpResponse response = HttpResponse.of(ResponseHeaders.of(HttpStatus.OK)); HttpResponse peeked = response.peekHeaders(headers -> { assert headers.status() == HttpStatus.OK; });
-
peekData
Applies the specifiedConsumer
to theHttpData
s emitted by thisHttpResponse
.For example:
HttpResponse response = HttpResponse.of("data1,data2"); HttpResponse peeked = response.peekData(data -> { assert data.toStringUtf8().equals("data1,data2"); });
- Specified by:
peekData
in interfaceHttpMessage
-
peekTrailers
Applies the specifiedConsumer
to the trailers emitted by thisHttpResponse
.For example:
HttpResponse response = HttpResponse.of(ResponseHeaders.of(HttpStatus.OK), HttpData.ofUtf8("..."), HttpHeaders.of("trailer", "foo")); HttpResponse peeked = response.peekTrailers(trailers -> { assert trailers.get("trailer").equals("foo"); });
- Specified by:
peekTrailers
in interfaceHttpMessage
-
peekError
Applies the specifiedConsumer
to an error emitted by thisHttpResponse
.For example:
HttpResponse response = HttpResponse.ofFailure(new IllegalStateException("Something went wrong.")); HttpResponse peeked = response.peekError(cause -> { assert cause instanceof IllegalStateException; });
- Specified by:
peekError
in interfaceStreamMessage<HttpObject>
-
recover
Recovers a failedHttpResponse
by switching to a returned fallbackHttpResponse
when any error occurs before aResponseHeaders
is written. Note that the failedHttpResponse
cannot be recovered from an error if aResponseHeaders
was written already.Example:
HttpResponse response = HttpResponse.ofFailure(new IllegalStateException("Oops...")); // The failed HttpResponse will be recovered by the fallback function. HttpResponse recovered = response.recover(cause -> HttpResponse.of("Fallback")); assert recovered.aggregate().join().contentUtf8().equals("Fallback"); // As HTTP headers and body were written already before an error occurred, // the fallback function could not be applied for the failed HttpResponse. HttpResponseWriter response = HttpResponse.streaming(); response.write(ResponseHeaders.of(HttpStatus.OK)); response.write(HttpData.ofUtf8("Hello")); response.close(new IllegalStateException("Oops...")); HttpResponse notRecovered = response.recover(cause -> HttpResponse.of("Fallback")); // The IllegalStateException will be raised even though a fallback function was added. notRecovered.aggregate().join();
-
recover
@UnstableApi default <T extends Throwable> HttpResponse recover(Class<T> causeClass, Function<? super T, ? extends HttpResponse> function) Recovers a failedHttpResponse
by switching to a returned fallbackHttpResponse
when the thrownThrowable
is the same type or a subtype of the specifiedcauseClass
.Example:
HttpResponse response = HttpResponse.ofFailure(new IllegalStateException("Oops...")); // The failed HttpResponse will be recovered by the fallback function. HttpResponse recovered = response.recover(IllegalStateException.class, cause -> HttpResponse.of("Fallback")); assert recovered.aggregate().join().contentUtf8().equals("Fallback"); // As HTTP headers and body were written already before an error occurred, // the fallback function could not be applied for the failed HttpResponse. HttpResponseWriter response = HttpResponse.streaming(); response.write(ResponseHeaders.of(HttpStatus.OK)); response.write(HttpData.ofUtf8("Hello")); response.close(new IllegalStateException("Oops...")); HttpResponse notRecovered = response.recover(IllegalStateException.class, cause -> HttpResponse.of("Fallback")); // The IllegalStateException will be raised even though a fallback function was added. notRecovered.aggregate().join(); HttpResponse response = HttpResponse.ofFailure(new IllegalStateException("Oops...")); // Use the shortcut recover method as a chain. HttpResponse recoverChain = response.recover(RuntimeException.class, cause -> { final IllegalArgumentException ex = new IllegalArgumentException("Oops2..."); // If a failed response is returned from the first chain return HttpResponse.ofFailure(ex); }) // If the shortcut exception type is correct, catch and recover in the second chain. .recover(IllegalArgumentException.class, cause -> HttpResponse.of("fallback")); recoverChain.aggregate().join(); HttpResponse response = HttpResponse.ofFailure(new IllegalStateException("Oops...")); // If the exception type does not match HttpResponse mismatchRecovered = response.recover(IllegalArgumentException.class, cause -> HttpResponse.of("Fallback")); // In this case, CompletionException is thrown. (can't recover exception) mismatchRecovered.aggregate().join();
-
aggregate(AggregationOptions)
withAggregationOptions.usePooledObjects(ByteBufAllocator)
.