Interface Multipart
Multipart
that represents
Multiple part messages.-
Method Summary
Modifier and TypeMethodDescriptionAggregates thisMultipart
.aggregate
(io.netty.util.concurrent.EventExecutor executor) Aggregates thisMultipart
with the specifiedEventExecutor
.aggregateWithPooledObjects
(io.netty.buffer.ByteBufAllocator alloc) (Advanced users only) Aggregates thisMultipart
.aggregateWithPooledObjects
(io.netty.util.concurrent.EventExecutor executor, io.netty.buffer.ByteBufAllocator alloc) (Advanced users only) Aggregates thisMultipart
.Returns all the nested body parts.boundary()
Returns the boundary string.default <T> CompletableFuture<List<T>>
collect
(Function<? super BodyPart, CompletableFuture<? extends T>> function) Collects the elements published by thisMultipart
.default <T> CompletableFuture<List<T>>
collect
(Function<? super BodyPart, CompletableFuture<? extends T>> function, SubscriptionOption... options) Collects the elements published by thisMultipart
.static Multipart
from
(HttpRequest request) Returns a decodedMultipart
from the specifiedHttpRequest
.static Multipart
static Multipart
from
(String boundary, org.reactivestreams.Publisher<? extends HttpData> contents, io.netty.buffer.ByteBufAllocator alloc) static Multipart
static Multipart
static Multipart
static Multipart
static Multipart
static Multipart
toHttpRequest
(RequestHeaders requestHeaders) toHttpRequest
(String path) Converts thisMultipart
into a new completeHttpRequest
.toHttpResponse
(HttpStatus status) Converts thisMultipart
into a new completeHttpResponse
.toHttpResponse
(ResponseHeaders responseHeaders)
-
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:
-
from
Returns a decodedMultipart
from 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(); ByteStreamMessage 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
-
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(); ... } });
Note that a
MimeParsingException
or another exception can be raised while aggregating so handle it properly. -
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(); ... } });
Note that a
MimeParsingException
or another exception can be raised while aggregating so handle it properly. -
aggregateWithPooledObjects
CompletableFuture<AggregatedMultipart> aggregateWithPooledObjects(io.netty.buffer.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()
.Note that a
MimeParsingException
or another exception can be raised while aggregating so handle it properly. -
aggregateWithPooledObjects
CompletableFuture<AggregatedMultipart> aggregateWithPooledObjects(io.netty.util.concurrent.EventExecutor executor, io.netty.buffer.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()
.Note that a
MimeParsingException
or another exception can be raised while aggregating so handle it properly. -
collect
@UnstableApi default <T> CompletableFuture<List<T>> collect(Function<? super BodyPart, CompletableFuture<? extends T>> function) Collects the elements published by thisMultipart
. The returnedCompletableFuture
will be notified when the elements are fully consumed.Note that if this
Multipart
was subscribed by otherSubscriber
already, the returnedCompletableFuture
will be completed with anIllegalStateException
.For example:
Path tempDir = ...; CompletableFuture<List<Object>> collect = Multipart.of(request) .collect(bodyPart -> { if (bodyPart.filename() != null) { final Path path = tempDir.resolve(bodyPart.name()); return bodyPart.writeTo(path).thenApply(ignore -> path); } return bodyPart.aggregate().thenApply(AggregatedHttpObject::contentUtf8); });
Note that a
MimeParsingException
or another exception can be raised while collecting so handle it properly. -
collect
@UnstableApi default <T> CompletableFuture<List<T>> collect(Function<? super BodyPart, CompletableFuture<? extends T>> function, SubscriptionOption... options) Collects the elements published by thisMultipart
. The returnedCompletableFuture
will be notified when the elements are fully consumed.Note that if this
Multipart
was subscribed by otherSubscriber
already, the returnedCompletableFuture
will be completed with anIllegalStateException
.Note that a
MimeParsingException
or another exception can be raised while collecting so handle it properly.
-