Interface HttpRequest
- All Superinterfaces:
HttpMessage,org.reactivestreams.Publisher<HttpObject>,Request,StreamMessage<HttpObject>
- All Known Subinterfaces:
HttpRequestWriter
- All Known Implementing Classes:
FilteredHttpRequest
Request.
Note: The initial RequestHeaders is not signaled to Subscribers. It is readily available
via headers().
-
Method Summary
Modifier and TypeMethodDescriptiondefault @Nullable List<Locale.LanguageRange>Returns a list ofLocale.LanguageRanges that are specified inHttpHeaderNames.ACCEPT_LANGUAGEin the order of client-side preferences.default CompletableFuture<AggregatedHttpRequest>Aggregates this request.aggregate(AggregationOptions options) Aggregates this request with the specifiedAggregationOptions.default CompletableFuture<AggregatedHttpRequest>aggregate(io.netty.util.concurrent.EventExecutor executor) Aggregates this request.default CompletableFuture<AggregatedHttpRequest>aggregateWithPooledObjects(io.netty.buffer.ByteBufAllocator alloc) Deprecated.default CompletableFuture<AggregatedHttpRequest>aggregateWithPooledObjects(io.netty.util.concurrent.EventExecutor executor, io.netty.buffer.ByteBufAllocator alloc) Deprecated.Returns the authority of this request.static HttpRequestBuilderbuilder()Returns a newHttpRequestBuilder.Returns the value of the'content-type'header.headers()Returns the initial HTTP/2 headers of this request.default HttpRequestdefault HttpRequestTransforms an error emitted by thisHttpRequestby applying the specifiedFunction.default HttpRequestmapHeaders(Function<? super RequestHeaders, ? extends RequestHeaders> function) default HttpRequestmapTrailers(Function<? super HttpHeaders, ? extends HttpHeaders> function) default HttpMethodmethod()Returns the method of this request.static HttpRequestof(HttpMethod method, String path) Creates a new HTTP request with empty content and closes the stream.static HttpRequestof(HttpMethod method, String path, MediaType mediaType, byte[] content) Creates a new HTTP request and closes the stream.static HttpRequestof(HttpMethod method, String path, MediaType mediaType, HttpData content) Creates a new HTTP request and closes the stream.static HttpRequestof(HttpMethod method, String path, MediaType mediaType, HttpData content, HttpHeaders trailers) Creates a new HTTP request and closes the stream.static HttpRequestof(HttpMethod method, String path, MediaType mediaType, CharSequence content) Creates a new HTTP request and closes the stream.static HttpRequestof(HttpMethod method, String path, MediaType mediaType, String content) Creates a new HTTP request and closes the stream.static HttpRequestCreates a new HTTP request and closes the stream.static HttpRequestof(RequestHeaders headers) Creates a newHttpRequestwith empty content and closes the stream.static HttpRequestof(RequestHeaders headers, HttpData content) Creates a newHttpRequestand closes the stream.static HttpRequestof(RequestHeaders headers, HttpData... contents) Creates a newHttpRequestthat publishes the givenHttpObjects and closes the stream.static HttpRequestof(RequestHeaders headers, HttpData content, HttpHeaders trailers) Creates a newHttpRequestand closes the stream.static HttpRequestof(RequestHeaders headers, CompletionStage<? extends StreamMessage<? extends HttpObject>> stage) Creates a new HTTP request whosePublisheris produced by the specifiedCompletionStage.static HttpRequestof(RequestHeaders headers, CompletionStage<? extends StreamMessage<? extends HttpObject>> stage, io.netty.util.concurrent.EventExecutor subscriberExecutor) Creates a new HTTP request whosePublisheris produced by the specifiedCompletionStage.static HttpRequestof(RequestHeaders headers, org.reactivestreams.Publisher<? extends HttpData> publisher, HttpHeaders trailers) Creates a new instance from an existingRequestHeaders,Publisherand trailers.static HttpRequestof(RequestHeaders headers, org.reactivestreams.Publisher<? extends HttpObject> publisher) Creates a new instance from an existingRequestHeadersandPublisher.default Stringpath()Returns the path of this request.default HttpRequestdefault HttpRequestApplies the specifiedConsumerto an error emitted by thisHttpRequest.default HttpRequestpeekTrailers(Consumer<? super HttpHeaders> action) scheme()Returns the scheme of this request.selectLocale(Iterable<Locale> supportedLocales) Matches theLocales supported by the server to theHttpHeaderNames.ACCEPT_LANGUAGEand returning the best match according to client preference.selectLocale(Locale... supportedLocales) Matches theLocales supported by the server to theHttpHeaderNames.ACCEPT_LANGUAGEand returning the best match according to client preference.default SplitHttpRequestsplit()Returns a newSplitHttpRequestwhich splits a stream ofHttpObjects intoHttpDatas and an HTTP trailers.default SplitHttpRequestsplit(io.netty.util.concurrent.EventExecutor executor) Returns a newSplitHttpRequestwhich splits a stream ofHttpObjects intoHttpDatas and an HTTP trailers.static HttpRequestWriterstreaming(HttpMethod method, String path) Creates a new HTTP request that can be used to stream an arbitrary number ofHttpObjectwith the initialRequestHeadersof the specifiedHttpMethodandpath.static HttpRequestWriterstreaming(RequestHeaders headers) Creates a new HTTP request that can be used to stream an arbitrary number ofHttpObjectwith the specified initialRequestHeaders.default HttpRequestDuplicatorReturns a newStreamMessageDuplicatorthat duplicates thisStreamMessageinto one or moreStreamMessages, which publish the same elements.default HttpRequestDuplicatortoDuplicator(long maxRequestLength) Returns a newStreamMessageDuplicatorthat duplicates thisHttpMessageinto one or moreHttpMessages, which publish the same elements.default HttpRequestDuplicatortoDuplicator(io.netty.util.concurrent.EventExecutor executor) Returns a newStreamMessageDuplicatorthat duplicates thisStreamMessageinto one or moreStreamMessages, which publish the same elements.default HttpRequestDuplicatortoDuplicator(io.netty.util.concurrent.EventExecutor executor, long maxRequestLength) Returns a newStreamMessageDuplicatorthat duplicates thisHttpMessageinto one or moreHttpMessages, which publish the same elements.default URIuri()Returns the URI of this request.default HttpRequestwithHeaders(RequestHeaders newHeaders) Returns a newHttpRequestderived from thisHttpRequestby replacing itsRequestHeaderswith the specifiednewHeaders.default HttpRequestwithHeaders(RequestHeadersBuilder newHeadersBuilder) Returns a newHttpRequestderived from thisHttpRequestby replacing itsRequestHeaderswith what's built from the specifiednewHeadersBuilder.Methods inherited from interface com.linecorp.armeria.common.HttpMessage
decode, decodeMethods inherited from interface com.linecorp.armeria.common.stream.StreamMessage
abort, abort, collect, collect, collect, decode, decode, defaultSubscriberExecutor, demand, endWith, filter, isComplete, isEmpty, isOpen, map, mapAsync, mapParallel, mapParallel, peek, peek, recoverAndResume, recoverAndResume, subscribe, subscribe, subscribe, subscribe, subscribe, subscribe, toInputStream, toInputStream, whenComplete, writeTo
-
Method Details
-
streaming
Creates a new HTTP request that can be used to stream an arbitrary number ofHttpObjectwith the initialRequestHeadersof the specifiedHttpMethodandpath. -
streaming
Creates a new HTTP request that can be used to stream an arbitrary number ofHttpObjectwith the specified initialRequestHeaders. -
of
Creates a new HTTP request with empty content and closes the stream.- Parameters:
method- the HTTP method of the requestpath- the path of the request
-
of
Creates a new HTTP request and closes the stream.- Parameters:
method- the HTTP method of the requestpath- the path of the requestmediaType- theMediaTypeof the request contentcontent- the content of the request
-
of
Creates a new HTTP request and closes the stream.- Parameters:
method- the HTTP method of the requestpath- the path of the requestmediaType- theMediaTypeof the request contentcontent- the content of the request
-
of
@FormatMethod static HttpRequest of(HttpMethod method, String path, MediaType mediaType, @FormatString String format, Object... args) Creates a new HTTP request and closes the stream. The content of the request is formatted byString.format(Locale, String, Object...)with English locale.- Parameters:
method- the HTTP method of the requestpath- the path of the requestmediaType- theMediaTypeof the request contentformat- the format string of the request contentargs- the arguments referenced by the format specifiers in the format string
-
of
Creates a new HTTP request and closes the stream. Thecontentwill be wrapped usingHttpData.wrap(byte[]), so any changes made tocontentwill be reflected in the request.- Parameters:
method- the HTTP method of the requestpath- the path of the requestmediaType- theMediaTypeof the request contentcontent- the content of the request
-
of
Creates a new HTTP request and closes the stream.- Parameters:
method- the HTTP method of the requestpath- the path of the requestmediaType- theMediaTypeof the request contentcontent- the content of the request
-
of
static HttpRequest of(HttpMethod method, String path, MediaType mediaType, HttpData content, HttpHeaders trailers) Creates a new HTTP request and closes the stream.- Parameters:
method- the HTTP method of the requestpath- the path of the requestmediaType- theMediaTypeof the request contentcontent- the content of the requesttrailers- the HTTP trailers
-
of
Creates a newHttpRequestwith empty content and closes the stream. -
of
Creates a newHttpRequestand closes the stream. -
of
Creates a newHttpRequestand closes the stream.- Throws:
IllegalStateException- if the headers are malformed.
-
of
Creates a newHttpRequestthat publishes the givenHttpObjects and closes the stream. -
of
static HttpRequest of(RequestHeaders headers, org.reactivestreams.Publisher<? extends HttpObject> publisher) Creates a new instance from an existingRequestHeadersandPublisher.Note that the
HttpObjects in thePublisherare not released whenSubscription.cancel()orStreamMessage.abort()is called. You should add a hook in order to release the elements. SeePublisherBasedStreamMessagefor more information. -
of
@UnstableApi static HttpRequest of(RequestHeaders headers, org.reactivestreams.Publisher<? extends HttpData> publisher, HttpHeaders trailers) Creates a new instance from an existingRequestHeaders,Publisherand trailers.Note that the
HttpDatas in thePublisherare not released whenSubscription.cancel()orStreamMessage.abort()is called. You should add a hook in order to release the elements. SeePublisherBasedStreamMessagefor more information. -
of
@UnstableApi static HttpRequest of(RequestHeaders headers, CompletionStage<? extends StreamMessage<? extends HttpObject>> stage) Creates a new HTTP request whosePublisheris produced by the specifiedCompletionStage. If the specifiedCompletionStagefails, the returned request will be closed with the same cause as well.- Parameters:
stage- theCompletionStagewhich will produce the actual data and trailers
-
of
@UnstableApi static HttpRequest of(RequestHeaders headers, CompletionStage<? extends StreamMessage<? extends HttpObject>> stage, io.netty.util.concurrent.EventExecutor subscriberExecutor) Creates a new HTTP request whosePublisheris produced by the specifiedCompletionStage. If the specifiedCompletionStagefails, the returned request will be closed with the same cause as well.- Parameters:
stage- theCompletionStagewhich will produce the actual data and trailerssubscriberExecutor- theEventExecutorwhich will be used when a user subscribes the returnedHttpRequestusingStreamMessage.subscribe(Subscriber)orStreamMessage.subscribe(Subscriber, SubscriptionOption...).
-
builder
Returns a newHttpRequestBuilder. -
headers
RequestHeaders headers()Returns the initial HTTP/2 headers of this request. -
uri
Returns the URI of this request. This method is a shortcut forheaders().uri(). -
scheme
Returns the scheme of this request. This method is a shortcut forheaders().scheme(). -
method
Returns the method of this request. This method is a shortcut forheaders().method(). -
path
Returns the path of this request. This method is a shortcut forheaders().path(). -
authority
Returns the authority of this request. This method is a shortcut forheaders().authority(). -
contentType
Returns the value of the'content-type'header.- Returns:
- the valid header value if present, or
nullotherwise.
-
acceptLanguages
Returns a list ofLocale.LanguageRanges that are specified inHttpHeaderNames.ACCEPT_LANGUAGEin the order of client-side preferences. If the client does not send the header, this will contain only a wild cardLocale.LanguageRange. -
selectLocale
Matches theLocales supported by the server to theHttpHeaderNames.ACCEPT_LANGUAGEand returning the best match according to client preference. It does this viaBasic Filtering eachLocale.LanguageRangeand picking the first match. This is the "classic" algorithm described in RFC2616 Accept-Language (obsoleted) and also referenced in RFC7231 Accept-Language. -
selectLocale
Matches theLocales supported by the server to theHttpHeaderNames.ACCEPT_LANGUAGEand returning the best match according to client preference. It does this viaBasic Filtering eachLocale.LanguageRangeand picking the first match. This is the "classic" algorithm described in RFC2616 Accept-Language (obsoleted) and also referenced in RFC7231 Accept-Language. -
withHeaders
Returns a newHttpRequestderived from thisHttpRequestby replacing itsRequestHeaderswith the specifiednewHeaders. Note that the content stream and trailers of thisHttpRequestis not duplicated, which means you can subscribe to only one of the twoHttpRequests.If you are using this method for intercepting an
HttpRequestin a decorator, make sure to updateRequestContext.request()withRequestContext.updateRequest(HttpRequest), e.g.> public class MyService extends SimpleDecoratingHttpService { > @Override > public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) { > // Create a new request with an additional header. > final HttpRequest newReq = > req.withHeaders(req.headers().toBuilder() > .set("x-custom-header", "value") > .build()); > > // Update the ctx.request. > ctx.updateRequest(newReq); > > // Delegate the new request with the updated context. > return delegate().serve(ctx, newReq); > } > } -
withHeaders
Returns a newHttpRequestderived from thisHttpRequestby replacing itsRequestHeaderswith what's built from the specifiednewHeadersBuilder. Note that the content stream and trailers of thisHttpRequestis not duplicated, which means you can subscribe to only one of the twoHttpRequests.If you are using this method for intercepting an
HttpRequestin a decorator, make sure to updateRequestContext.request()withRequestContext.updateRequest(HttpRequest), e.g.> public class MyService extends SimpleDecoratingHttpService { > @Override > public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) { > // Create a new request with an additional header. > final HttpRequest newReq = > req.withHeaders(req.headers().toBuilder() > .set("x-custom-header", "value")); > > // Update the ctx.request. > ctx.updateRequest(newReq); > > // Delegate the new request with the updated context. > return delegate().serve(ctx, newReq); > } > } -
aggregate
Aggregates this request with the specifiedAggregationOptions. The returnedCompletableFuturewill be notified when the content and the trailers of the request are fully received.AggregationOptions options = AggregationOptions.builder() .cacheResult(false) .executor(...) .build(); HttpRequest request = ...; AggregatedHttpRequest aggregated = request.aggregate(options).join(); -
aggregate
Aggregates this request. The returnedCompletableFuturewill be notified when the content and the trailers of the request is received fully.The
AggregatedHttpRequestis cached by default. So you can repeatedly call this method and get the cached value after the first aggregation.HttpRequest request = ...; AggregatedHttpRequest aggregated0 = request.aggregate().join(); AggregatedHttpRequest aggregated1 = request.aggregate().join(); assert aggregated0 == aggregated1; -
aggregate
default CompletableFuture<AggregatedHttpRequest> aggregate(io.netty.util.concurrent.EventExecutor executor) Aggregates this request. The returnedCompletableFuturewill be notified when the content and the trailers of the request is received fully.The
AggregatedHttpRequestis cached by default. So you can repeatedly call this method and get the cached value after the first aggregation.HttpRequest request = ...; AggregatedHttpRequest aggregated0 = request.aggregate(executor).join(); AggregatedHttpRequest aggregated1 = request.aggregate(executor).join(); assert aggregated0 == aggregated1; -
aggregateWithPooledObjects
@Deprecated @UnstableApi default CompletableFuture<AggregatedHttpRequest> aggregateWithPooledObjects(io.netty.buffer.ByteBufAllocator alloc) Deprecated.(Advanced users only) Aggregates this request. The returnedCompletableFuturewill be notified when the content and the trailers of the request 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
AggregatedHttpRequestis not cached. So it is NOT allowed to access theAggregatedHttpRequestfrom this method after the first aggregation.HttpRequest request = ...; AggregatedHttpRequest aggregated = request.aggregateWithPooledObjects(alloc).join(); // An `IllegalStateException` will be raised. request.aggregateWithPooledObjects(alloc).join();- See Also:
-
aggregateWithPooledObjects
@Deprecated default CompletableFuture<AggregatedHttpRequest> aggregateWithPooledObjects(io.netty.util.concurrent.EventExecutor executor, io.netty.buffer.ByteBufAllocator alloc) Deprecated.(Advanced users only) Aggregates this request. The returnedCompletableFuturewill be notified when the content and the trailers of the request 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
AggregatedHttpRequestis not cached. So it is NOT allowed to access theAggregatedHttpRequestfrom this method after the first aggregation.HttpRequest request = ...; AggregatedHttpRequest aggregated = request.aggregateWithPooledObjects(executor, alloc).join(); // An `IllegalStateException` will be raised. request.aggregateWithPooledObjects(executor, alloc).join(); -
toDuplicator
Description copied from interface:StreamMessageReturns a newStreamMessageDuplicatorthat duplicates thisStreamMessageinto one or moreStreamMessages, which publish the same elements. Note that you cannot subscribe to thisStreamMessageanymore after you call this method. To subscribe, callStreamMessageDuplicator.duplicate()from the returnedStreamMessageDuplicator.- Specified by:
toDuplicatorin interfaceStreamMessage<HttpObject>
-
toDuplicator
Description copied from interface:StreamMessageReturns a newStreamMessageDuplicatorthat duplicates thisStreamMessageinto one or moreStreamMessages, which publish the same elements. Note that you cannot subscribe to thisStreamMessageanymore after you call this method. To subscribe, callStreamMessageDuplicator.duplicate()from the returnedStreamMessageDuplicator.- Specified by:
toDuplicatorin interfaceStreamMessage<HttpObject>- Parameters:
executor- the executor to duplicate
-
toDuplicator
Description copied from interface:HttpMessageReturns a newStreamMessageDuplicatorthat duplicates thisHttpMessageinto one or moreHttpMessages, which publish the same elements. Note that you cannot subscribe to thisHttpMessageanymore after you call this method. To subscribe, callStreamMessageDuplicator.duplicate()from the returnedStreamMessageDuplicator.- Specified by:
toDuplicatorin interfaceHttpMessage- Parameters:
maxRequestLength- the maximum content length that the duplicator can hold in its buffer.ContentTooLargeExceptionis raised if the length of the bufferedHttpDatais greater than this value.
-
toDuplicator
default HttpRequestDuplicator toDuplicator(io.netty.util.concurrent.EventExecutor executor, long maxRequestLength) Description copied from interface:HttpMessageReturns a newStreamMessageDuplicatorthat duplicates thisHttpMessageinto one or moreHttpMessages, which publish the same elements. Note that you cannot subscribe to thisHttpMessageanymore after you call this method. To subscribe, callStreamMessageDuplicator.duplicate()from the returnedStreamMessageDuplicator.- Specified by:
toDuplicatorin interfaceHttpMessage- Parameters:
executor- the executor to duplicatemaxRequestLength- the maximum content length that the duplicator can hold in its buffer.ContentTooLargeExceptionis raised if the length of the bufferedHttpDatais greater than this value.
-
split
Returns a newSplitHttpRequestwhich splits a stream ofHttpObjects intoHttpDatas and an HTTP trailers.SplitHttpMessage.trailers()might not complete until the entire request body is consumed completely. -
split
Returns a newSplitHttpRequestwhich splits a stream ofHttpObjects intoHttpDatas and an HTTP trailers.SplitHttpMessage.trailers()might not complete until the entire request body is consumed completely. -
mapHeaders
Transforms theRequestHeadersof thisHttpRequestby applying the specifiedFunction.For example:
HttpRequest request = HttpRequest.of(HttpMethod.GET, "/items"); HttpRequest transformed = request.mapHeaders(headers -> headers.toBuilder() .add("TraceId", "1") .build()); assert transformed.aggregate().join().headers().get("TraceId").equals("1"); -
mapData
Transforms theHttpDatas emitted by thisHttpRequestby applying the specifiedFunction.For example:
HttpRequest request = HttpRequest.of(RequestHeaders.of(HttpMethod.POST, "/items"), HttpData.ofUtf8("data1,data2")); HttpRequest transformed = request.mapData(data -> { return HttpData.ofUtf8(data.toStringUtf8().replaceAll(",", "\n")); }); assert transformed.aggregate().join().contentUtf8().equals("data1\ndata2");- Specified by:
mapDatain interfaceHttpMessage
-
mapTrailers
Transforms the trailers emitted by thisHttpRequestby applying the specifiedFunction.For example:
HttpRequest request = HttpRequest.of(RequestHeaders.of(HttpMethod.POST, "/items"), HttpData.ofUtf8("...")); HttpRequest transformed = request.mapTrailers(trailers -> { return trailers.withMutations(builder -> builder.set("trailer1", "foo")); }); assert transformed.aggregate().join().trailers().get("trailer1").equals("foo");- Specified by:
mapTrailersin interfaceHttpMessage
-
mapError
Transforms an error emitted by thisHttpRequestby applying the specifiedFunction.For example:
HttpRequest request = HttpRequest.ofFailure(new IllegalStateException("Something went wrong.")); HttpRequest transformed = request.mapError(cause -> { if (cause instanceof IllegalStateException) { return new MyDomainException(cause); } else { return cause; } });- Specified by:
mapErrorin interfaceStreamMessage<HttpObject>
-
peekData
Applies the specifiedConsumerto theHttpDatas emitted by thisHttpRequest.For example:
HttpRequest request = HttpRequest.of(RequestHeaders.of(HttpMethod.POST, "/items"), HttpData.ofUtf8("data1,data2")); HttpRequest peeked = request.peekData(data -> { assert data.toStringUtf8().equals("data1,data2"); });- Specified by:
peekDatain interfaceHttpMessage
-
peekTrailers
Applies the specifiedConsumerto the trailers emitted by thisHttpRequest.For example:
HttpRequest request = HttpRequest.of(RequestHeaders.of(HttpMethod.POST, "/items"), HttpData.ofUtf8("..."), HttpHeaders.of("trailer", "foo")); HttpRequest peeked = request.peekTrailers(trailers -> { assert trailers.get("trailer").equals("foo"); });- Specified by:
peekTrailersin interfaceHttpMessage
-
peekError
Applies the specifiedConsumerto an error emitted by thisHttpRequest.For example:
HttpRequest request = HttpRequest.ofFailure(new IllegalStateException("Something went wrong.")); HttpRequest peeked = request.peekError(cause -> { assert cause instanceof IllegalStateException; });- Specified by:
peekErrorin interfaceStreamMessage<HttpObject>
-
aggregate(AggregationOptions)withAggregationOptions.usePooledObjects(ByteBufAllocator).