Class MultiPartFormData
A container class for multipart/form-data
parsing and generation.
Use MultiPartFormData.Parser
to parse multipart/form-data
, and use
MultiPartFormData.ContentSource
to generate multipart/form-data
from parts.
Once the parsing of the multipart/form-data
content completes successfully,
a MultiPartFormData.Parts
object is provided.
MultiPartFormData.Parser
may be configured to save multipart files in a configurable
directory, and configure the max size of such files, etc. via MultiPartConfig
.
Typical usage:
// Some headers that include Content-Type.
HttpFields headers = ...;
String boundary = MultiPart.extractBoundary(headers.get(HttpHeader.CONTENT_TYPE));
// Some multipart/form-data content.
Content.Source content = ...;
// Create and configure MultiPartFormData.Parser.
MultiPartFormData.Parser parser = new MultiPartFormData.Parser(boundary);
// Where to store the files.
parser.setFilesDirectory(Path.of("/tmp"));
// Max 1 MiB files.
parser.setMaxFileSize(1024 * 1024);
// Parse the content.
parser.parse(content, new Promise.Invocable<>()
{
@Override
public void succeeded(MultiPartFormData.Parts parts)
{
// Use the parts.
}
@Override
public void failed(Throwable x)
{
// Handle failure.
}
}
For usage within a server environment, use:
Request request = ...;
String contentType = request.getHeaders().get("Content-Type");
MultiPartConfig config = new MultiPartConfig.Builder()
.location(Path.of("/tmp"))
.maxPartSize(1024 * 1024)
.build();
MultiPartFormData.onParts(request, request, contentType, config, new Promise.Invocable<>()
{
@Override
public void succeeded(MultiPartFormData.Parts parts)
{
// Use the parts.
}
@Override
public void failed(Throwable x)
{
// Handle failure.
}
});
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
The multipart/form-data specific content source.static class
static class
An ordered list ofMultiPart.Part
s that can be accessed by index or by name, or iterated over. -
Method Summary
Modifier and TypeMethodDescriptionfrom
(Content.Source content, Attributes attributes, String contentType, MultiPartConfig config) Deprecated, for removal: This API element is subject to removal in a future version.from
(Attributes attributes, String boundary, Function<MultiPartFormData.Parser, CompletableFuture<MultiPartFormData.Parts>> parse) Deprecated, for removal: This API element is subject to removal in a future version.from
(Attributes attributes, MultiPartCompliance compliance, ComplianceViolation.Listener listener, String boundary, Function<MultiPartFormData.Parser, CompletableFuture<MultiPartFormData.Parts>> parse) Deprecated, for removal: This API element is subject to removal in a future version.get
(Attributes attributes) Deprecated, for removal: This API element is subject to removal in a future version.usegetParts(Attributes)
insteadstatic MultiPartFormData.Parts
getParts
(Content.Source content, Attributes attributes, String contentType, MultiPartConfig config) Getmultipart/form-data
MultiPartFormData.Parts
from aContent.Source
, caching the results in anAttributes
.static MultiPartFormData.Parts
getParts
(Attributes attributes) Getmultipart/form-data
MultiPartFormData.Parts
from anAttributes
, typically cached there by calls togetParts(Content.Source, Attributes, String, MultiPartConfig)
oronParts(Content.Source, Attributes, String, MultiPartConfig, Promise.Invocable)
static void
onParts
(Content.Source content, Attributes attributes, String contentType, MultiPartConfig config, Promise.Invocable<MultiPartFormData.Parts> promise) Asynchronously getmultipart/form-data
MultiPartFormData.Parts
from aContent.Source
, caching the results in anAttributes
.
-
Method Details
-
getParts
Getmultipart/form-data
MultiPartFormData.Parts
from anAttributes
, typically cached there by calls togetParts(Content.Source, Attributes, String, MultiPartConfig)
oronParts(Content.Source, Attributes, String, MultiPartConfig, Promise.Invocable)
- Parameters:
attributes
- the attributes where the futureParts are cahced- Returns:
- the parts or null
-
getParts
public static MultiPartFormData.Parts getParts(Content.Source content, Attributes attributes, String contentType, MultiPartConfig config) Getmultipart/form-data
MultiPartFormData.Parts
from aContent.Source
, caching the results in anAttributes
. If not already available, theParts
are read and parsed, blocking if necessary.Calls to
onParts
andgetParts
methods are idempotent, and can be called multiple times, with subsequent calls returning the results of the first call.- Parameters:
content
- the source of the multipart content.attributes
- the attributes where the Parts are cached.contentType
- the value of theHttpHeader.CONTENT_TYPE
header.config
- the multipart configuration.- Returns:
- the parts
-
onParts
public static void onParts(Content.Source content, Attributes attributes, String contentType, MultiPartConfig config, Promise.Invocable<MultiPartFormData.Parts> promise) Asynchronously getmultipart/form-data
MultiPartFormData.Parts
from aContent.Source
, caching the results in anAttributes
. If not already available, theParts
are read and parsed.Calls to
onParts
andgetParts
methods are idempotent, and can be called multiple times, with subsequent calls returning the results of the first call.- Parameters:
content
- the source of the multipart content.attributes
- the attributes where the futureParts are tracked.contentType
- the value of theHttpHeader.CONTENT_TYPE
header.config
- the multipart configuration.promise
- The action to take when theMultiPartFormData.Parts
are available.
-
from
@Deprecated(forRemoval=true, since="12.0.15") public static CompletableFuture<MultiPartFormData.Parts> from(Content.Source content, Attributes attributes, String contentType, MultiPartConfig config) Deprecated, for removal: This API element is subject to removal in a future version.- Parameters:
content
- the source of the multipart content.attributes
- the attributes where the futureParts are tracked.contentType
- the value of theHttpHeader.CONTENT_TYPE
header.config
- the multipart configuration.- Returns:
- the future parts
-
from
@Deprecated(forRemoval=true, since="12.0.15") public static CompletableFuture<MultiPartFormData.Parts> from(Attributes attributes, String boundary, Function<MultiPartFormData.Parser, CompletableFuture<MultiPartFormData.Parts>> parse) Deprecated, for removal: This API element is subject to removal in a future version.Returnsmultipart/form-data
parts usingMultiPartCompliance.RFC7578
. -
from
@Deprecated(forRemoval=true, since="12.0.15") public static CompletableFuture<MultiPartFormData.Parts> from(Attributes attributes, MultiPartCompliance compliance, ComplianceViolation.Listener listener, String boundary, Function<MultiPartFormData.Parser, CompletableFuture<MultiPartFormData.Parts>> parse) Deprecated, for removal: This API element is subject to removal in a future version.Returnsmultipart/form-data
parts using the givenMultiPartCompliance
and listener.- Parameters:
attributes
- the attributes where the futureParts are trackedcompliance
- the compliance modelistener
- the compliance violation listenerboundary
- the boundary for themultipart/form-data
partsparse
- the parser completable future- Returns:
- the future parts
-
get
@Deprecated(forRemoval=true, since="12.0.15") public static CompletableFuture<MultiPartFormData.Parts> get(Attributes attributes) Deprecated, for removal: This API element is subject to removal in a future version.usegetParts(Attributes)
insteadReturnsmultipart/form-data
parts if they have already been created.- Parameters:
attributes
- the attributes where the futureParts are tracked- Returns:
- the future parts
-
getParts(Content.Source, Attributes, String, MultiPartConfig)
and/oronParts(Content.Source, Attributes, String, MultiPartConfig, Promise.Invocable)