Interface Multipart
public interface Multipart
A reactive
Multipart
that represents
Multiple part messages.-
Method Summary
Modifier and Type Method Description CompletableFuture<AggregatedMultipart>
aggregate()
Aggregates thisMultipart
.CompletableFuture<AggregatedMultipart>
aggregate(EventExecutor executor)
Aggregates thisMultipart
with the specifiedEventExecutor
.CompletableFuture<AggregatedMultipart>
aggregateWithPooledObjects(ByteBufAllocator alloc)
(Advanced users only) Aggregates thisMultipart
.CompletableFuture<AggregatedMultipart>
aggregateWithPooledObjects(EventExecutor executor, ByteBufAllocator alloc)
(Advanced users only) Aggregates thisMultipart
.StreamMessage<BodyPart>
bodyParts()
Returns all the nested body parts.String
boundary()
Returns the boundary string.static Multipart
from(HttpRequest request)
Returns a decodedMultipart
from the specifiedHttpRequest
.static Multipart
from(String boundary, org.reactivestreams.Publisher<? extends HttpData> contents)
static Multipart
from(String boundary, org.reactivestreams.Publisher<? extends HttpData> contents, ByteBufAllocator alloc)
Returns a decodedMultipart
from the the specifiedboundary
,Publisher
ofHttpData
andByteBufAllocator
.static Multipart
of(BodyPart... parts)
static Multipart
of(Iterable<? extends BodyPart> parts)
static Multipart
of(String boundary, BodyPart... parts)
static Multipart
of(String boundary, Iterable<? extends BodyPart> parts)
static Multipart
of(String boundary, org.reactivestreams.Publisher<? extends BodyPart> parts)
static Multipart
of(org.reactivestreams.Publisher<? extends BodyPart> parts)
HttpRequest
toHttpRequest(RequestHeaders requestHeaders)
HttpRequest
toHttpRequest(String path)
Converts thisMultipart
into a new completeHttpRequest
.HttpResponse
toHttpResponse(HttpStatus status)
Converts thisMultipart
into a new completeHttpResponse
.HttpResponse
toHttpResponse(ResponseHeaders responseHeaders)
StreamMessage<HttpData>
toStreamMessage()
-
Method Details
-
of
-
of
-
of
-
of
-
of
-
of
-
from
Returns a decodedMultipart
from the specifiedHttpRequest
. You can reactively subscribe to body parts using thePublisher
ofbodyParts()
:
, or aggregate thisimport reactor.core.publisher.Flux; HttpRequest req = ...; Multipart multiPart = Multipart.from(req); Flux.from(multiPart.bodyParts()) .subscribe(bodyPart -> { Flux.from(bodyPart.content()) .map(HttpData::toStringUtf8) .collectList() .subscribe(contents -> { ... }); });
Multipart
usingaggregate()
:Multipart.from(req).aggregate() .thenAccept(multipart -> { for (AggregatedBodyPart bodyPart : multipart.bodyParts()) { String content = bodyPart.contentUtf8(); ... } });
- See Also:
bodyParts()
,aggregate()
-
from
Returns a decodedMultipart
from the the specifiedboundary
andPublisher
ofHttpData
. For instance,Multipart
could be decoded from the specifiedHttpResponse
in the following way:HttpResponse response = ...; SplitHttpResponse splitResponse = response.split(); ResponseHeaders responseHeaders = splitResponse.headers().join(); StreamMessage<HttpData> responseContents = splitResponse.body(); MediaType contentType = responseHeaders.contentType(); if (contentType != null && contentType.isMultipart()) { String boundary = Multiparts.getBoundary(contentType); Multipart multipart = Multipart.from(boundary, responseContents); ... } else { handleNonMultipartResponse(responseHeaders, responseContents); }
-
from
static Multipart from(String boundary, org.reactivestreams.Publisher<? extends HttpData> contents, ByteBufAllocator alloc)Returns a decodedMultipart
from the the specifiedboundary
,Publisher
ofHttpData
andByteBufAllocator
. -
toHttpRequest
Converts thisMultipart
into a new completeHttpRequest
. This method is commonly used to send a multipart request to the specifiedpath
of an endpoint.For example:
HttpHeaders headers = HttpHeaders.of(HttpHeaderNames.CONTENT_DISPOSITION, ContentDisposition.of("form-data", "file", "test.txt")); byte[] fileData = ...; BodyPart filePart = BodyPart.builder() .headers(headers) .content(fileData) .build(); HttpRequest request = Multipart.of(filePart).toHttpRequest("/upload"); CompletableFuture<AggregatedHttpResponse> response = client.execute(request).aggregate();
-
toHttpRequest
Converts thisMultipart
into a new completeHttpRequest
with the specifiedRequestHeaders
. This method is commonly used to send a multipart request using the specifiedRequestHeaders
.For example:
HttpHeaders headers = HttpHeaders.of(HttpHeaderNames.CONTENT_DISPOSITION, ContentDisposition.of("form-data", "file", "test.txt")); byte[] fileData = ...; BodyPart filePart = BodyPart.builder() .headers(headers) .content(fileData) .build(); RequestHeaders requestHeaders = RequestHeaders.builder(HttpMethod.POST, "/upload") .contentType(MediaType.MULTIPART_RELATED) .build(); HttpRequest request = Multipart.of(filePart).toHttpRequest(requestHeaders); CompletableFuture<AggregatedHttpResponse> response = client.execute(request).aggregate();
-
toHttpResponse
Converts thisMultipart
into a new completeHttpResponse
. This method is commonly used to send a multipart response with the specifiedHttpStatus
.For example:
HttpHeaders headers = HttpHeaders.of(HttpHeaderNames.CONTENT_DISPOSITION, ContentDisposition.of("form-data", "file", "test.txt")); byte[] fileData = ...; BodyPart filePart = BodyPart.builder() .headers(headers) .content(fileData) .build(); HttpResponse response = Multipart.of(filePart).toHttpResponse(HttpStatus.OK);
-
toHttpResponse
Converts thisMultipart
into a new completeHttpResponse
with the specifiedResponseHeaders
. This method is commonly used to send a multipart response using the specifiedResponseHeaders
.For example:
HttpHeaders headers = HttpHeaders.of(HttpHeaderNames.CONTENT_DISPOSITION, ContentDisposition.of("form-data", "file", "test.txt")); byte[] fileData = ...; BodyPart filePart = BodyPart.builder() .headers(headers) .content(fileData) .build(); ResponseHeaders responseHeaders = ResponseHeaders.builder(HttpStatus.OK) .contentType(MediaType.MULTIPART_RELATED) .build(); HttpResponse response = Multipart.of(filePart).toHttpResponse(responseHeaders);
-
toStreamMessage
-
boundary
String boundary()Returns the boundary string. -
bodyParts
Returns all the nested body parts.Note: Once a
BodyPart
is subscribed, you should subscribe toBodyPart.content()
before subscribing to the nextBodyPart
.
If you don't know what this means, useimport reactor.core.publisher.Flux; HttpRequest req = ...; Multipart multiPart = Multipart.from(req); // Good: Flux.from(multiPart.bodyParts()) .subscribe(bodyPart -> { Flux.from(bodyPart.content()) // Safely subscribe to BodyPart.content() .map(HttpData::toStringUtf8) .collectList() .subscribe(contents -> { ... }); }); // Bad: Flux.from(multiPart.bodyParts()) .collectList() // This will subscribe BodyPart.content() first before you subscribe to it. .subscribe(bodyParts -> { bodyParts.forEach(part -> { Flux.from(part.content()) .collectList() // Throws IllegalStateException("Only single subscriber is allowed") .subscribe(contents -> { ... }); }); });
aggregate()
. -
aggregate
CompletableFuture<AggregatedMultipart> aggregate()Aggregates thisMultipart
. The returnedCompletableFuture
will be notified when theBodyPart
s of theMultipart
is received fully.For example:
HttpRequest req = ...; Multipart.from(req).aggregate() .thenAccept(multipart -> { for (AggregatedBodyPart bodyPart : multipart.bodyParts()) { String content = bodyPart.contentUtf8(); ... } });
-
aggregate
Aggregates thisMultipart
with the specifiedEventExecutor
. The returnedCompletableFuture
will be notified when theBodyPart
s of theMultipart
is received fully.For example:
HttpRequest req = ...; EventExecutor executor = ...; Multipart.from(req).aggregate(executor) .thenAccept(multipart -> { for (AggregatedBodyPart bodyPart : multipart.bodyParts()) { String content = bodyPart.contentUtf8(); ... } });
-
aggregateWithPooledObjects
(Advanced users only) Aggregates thisMultipart
. The returnedCompletableFuture
will be notified when theBodyPart
s of theMultipart
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()
. -
aggregateWithPooledObjects
CompletableFuture<AggregatedMultipart> aggregateWithPooledObjects(EventExecutor executor, ByteBufAllocator alloc)(Advanced users only) Aggregates thisMultipart
. The returnedCompletableFuture
will be notified when theBodyPart
s of theMultipart
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()
.
-