Class MultipartEntityBuilder
- java.lang.Object
-
- org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder
-
public class MultipartEntityBuilder extends java.lang.ObjectBuilder for multipartHttpEntitys.This class constructs multipart entities with a boundary determined by either a random UUID or an explicit boundary set via
setBoundary(String).IMPORTANT: it is responsibility of the caller to validate / sanitize content of body parts. For instance, when using an explicit boundary, it's the caller's responsibility to ensure the body parts do not contain the boundary value, which can prevent the consumer of the entity from correctly parsing / processing the body parts.
- Since:
- 5.0
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.Stringboundaryprivate static java.lang.StringBOUNDARY_PREFIXprivate java.nio.charset.Charsetcharsetprivate org.apache.hc.core5.http.ContentTypecontentTypeprivate static org.apache.hc.core5.http.NameValuePair[]EMPTY_NAME_VALUE_ARRAYAn empty immutableNameValuePairarray.private java.lang.StringepilogueThe epilogue of the multipart message.private static org.slf4j.LoggerLOGThe logger for this class.private HttpMultipartModemodeprivate java.util.List<MultipartPart>multipartPartsprivate java.lang.StringpreambleThe preamble of the multipart message.
-
Constructor Summary
Constructors Constructor Description MultipartEntityBuilder()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description MultipartEntityBuilderaddBinaryBody(java.lang.String name, byte[] b)MultipartEntityBuilderaddBinaryBody(java.lang.String name, byte[] b, org.apache.hc.core5.http.ContentType contentType, java.lang.String filename)MultipartEntityBuilderaddBinaryBody(java.lang.String name, java.io.File file)MultipartEntityBuilderaddBinaryBody(java.lang.String name, java.io.File file, org.apache.hc.core5.http.ContentType contentType, java.lang.String filename)MultipartEntityBuilderaddBinaryBody(java.lang.String name, java.io.InputStream stream)MultipartEntityBuilderaddBinaryBody(java.lang.String name, java.io.InputStream stream, org.apache.hc.core5.http.ContentType contentType, java.lang.String filename)MultipartEntityBuilderaddBinaryBody(java.lang.String name, java.nio.file.Path path)Adds body with contents from the given source Path.MultipartEntityBuilderaddBinaryBody(java.lang.String name, java.nio.file.Path path, org.apache.hc.core5.http.ContentType contentType, java.lang.String fileName)Adds body with contents from the given source Path.MultipartEntityBuilderaddEpilogue(java.lang.String epilogue)Adds an epilogue to the multipart entity being constructed.MultipartEntityBuilderaddParameter(org.apache.hc.core5.http.message.BasicNameValuePair parameter)Add parameter to the currentContentType.MultipartEntityBuilderaddPart(java.lang.String name, ContentBody contentBody)MultipartEntityBuilderaddPart(MultipartPart multipartPart)MultipartEntityBuilderaddPreamble(java.lang.String preamble)Adds a preamble to the multipart entity being constructed.MultipartEntityBuilderaddTextBody(java.lang.String name, java.lang.String text)MultipartEntityBuilderaddTextBody(java.lang.String name, java.lang.String text, org.apache.hc.core5.http.ContentType contentType)org.apache.hc.core5.http.HttpEntitybuild()(package private) MultipartFormEntitybuildEntity()static MultipartEntityBuildercreate()private java.lang.StringgetRandomBoundary()Generates a random boundary using UUID.MultipartEntityBuildersetBoundary(java.lang.String boundary)Sets a custom boundary string for the multipart entity.MultipartEntityBuildersetCharset(java.nio.charset.Charset charset)MultipartEntityBuildersetContentType(org.apache.hc.core5.http.ContentType contentType)MultipartEntityBuildersetLaxMode()MultipartEntityBuildersetMimeSubtype(java.lang.String subType)MultipartEntityBuildersetMode(HttpMultipartMode mode)MultipartEntityBuildersetStrictMode()
-
-
-
Field Detail
-
contentType
private org.apache.hc.core5.http.ContentType contentType
-
mode
private HttpMultipartMode mode
-
boundary
private java.lang.String boundary
-
charset
private java.nio.charset.Charset charset
-
multipartParts
private java.util.List<MultipartPart> multipartParts
-
BOUNDARY_PREFIX
private static final java.lang.String BOUNDARY_PREFIX
- See Also:
- Constant Field Values
-
LOG
private static final org.slf4j.Logger LOG
The logger for this class.
-
preamble
private java.lang.String preamble
The preamble of the multipart message. This field stores the optional preamble that should be added at the beginning of the multipart message. It can benullif no preamble is needed.
-
epilogue
private java.lang.String epilogue
The epilogue of the multipart message. This field stores the optional epilogue that should be added at the end of the multipart message. It can benullif no epilogue is needed.
-
EMPTY_NAME_VALUE_ARRAY
private static final org.apache.hc.core5.http.NameValuePair[] EMPTY_NAME_VALUE_ARRAY
An empty immutableNameValuePairarray.
-
-
Method Detail
-
create
public static MultipartEntityBuilder create()
-
setMode
public MultipartEntityBuilder setMode(HttpMultipartMode mode)
-
setLaxMode
public MultipartEntityBuilder setLaxMode()
-
setStrictMode
public MultipartEntityBuilder setStrictMode()
-
setBoundary
public MultipartEntityBuilder setBoundary(java.lang.String boundary)
Sets a custom boundary string for the multipart entity.If
nullis provided, the builder reverts to its default logic of using a random UUID.IMPORTANT: when setting an explicit boundary, it is responsibility of the caller to validate / sanitize content of body parts to ensure they do not contain the boundary value.
- Parameters:
boundary- the boundary string, ornullto use a random UUID.- Returns:
- this builder instance
-
setMimeSubtype
public MultipartEntityBuilder setMimeSubtype(java.lang.String subType)
- Since:
- 4.4
-
setContentType
public MultipartEntityBuilder setContentType(org.apache.hc.core5.http.ContentType contentType)
- Returns:
- this instance.
- Since:
- 4.5
-
addParameter
public MultipartEntityBuilder addParameter(org.apache.hc.core5.http.message.BasicNameValuePair parameter)
Add parameter to the currentContentType.- Parameters:
parameter- The name-value pair parameter to add to theContentType.- Returns:
- this instance.
- Since:
- 5.2
-
setCharset
public MultipartEntityBuilder setCharset(java.nio.charset.Charset charset)
-
addPart
public MultipartEntityBuilder addPart(MultipartPart multipartPart)
- Since:
- 4.4
-
addPart
public MultipartEntityBuilder addPart(java.lang.String name, ContentBody contentBody)
-
addTextBody
public MultipartEntityBuilder addTextBody(java.lang.String name, java.lang.String text, org.apache.hc.core5.http.ContentType contentType)
-
addTextBody
public MultipartEntityBuilder addTextBody(java.lang.String name, java.lang.String text)
-
addBinaryBody
public MultipartEntityBuilder addBinaryBody(java.lang.String name, byte[] b, org.apache.hc.core5.http.ContentType contentType, java.lang.String filename)
-
addBinaryBody
public MultipartEntityBuilder addBinaryBody(java.lang.String name, byte[] b)
-
addBinaryBody
public MultipartEntityBuilder addBinaryBody(java.lang.String name, java.io.File file, org.apache.hc.core5.http.ContentType contentType, java.lang.String filename)
-
addBinaryBody
public MultipartEntityBuilder addBinaryBody(java.lang.String name, java.nio.file.Path path)
Adds body with contents from the given source Path.- Parameters:
name- The part name.path- The source path.- Returns:
thisinstance.- Since:
- 5.6
-
addBinaryBody
public MultipartEntityBuilder addBinaryBody(java.lang.String name, java.nio.file.Path path, org.apache.hc.core5.http.ContentType contentType, java.lang.String fileName)
Adds body with contents from the given source Path.- Parameters:
name- The part name.path- The source path.contentType- The content type.fileName- The file name to override the Path's file name.- Returns:
thisinstance.- Since:
- 5.6
-
addBinaryBody
public MultipartEntityBuilder addBinaryBody(java.lang.String name, java.io.File file)
-
addBinaryBody
public MultipartEntityBuilder addBinaryBody(java.lang.String name, java.io.InputStream stream, org.apache.hc.core5.http.ContentType contentType, java.lang.String filename)
-
addBinaryBody
public MultipartEntityBuilder addBinaryBody(java.lang.String name, java.io.InputStream stream)
-
getRandomBoundary
private java.lang.String getRandomBoundary()
Generates a random boundary using UUID. The UUID is a v4 random UUID generated from a cryptographically-secure random source.A cryptographically-secure random number source is used to generate the UUID, to avoid a malicious actor crafting a body part that contains the boundary value to tamper with the entity structure.
-
addPreamble
public MultipartEntityBuilder addPreamble(java.lang.String preamble)
Adds a preamble to the multipart entity being constructed. The preamble is the text that appears before the first boundary delimiter. The preamble is optional and may be null.- Parameters:
preamble- The preamble text to add to the multipart entity- Returns:
- this instance.
- Since:
- 5.3
-
addEpilogue
public MultipartEntityBuilder addEpilogue(java.lang.String epilogue)
Adds an epilogue to the multipart entity being constructed. The epilogue is the text that appears after the last boundary delimiter. The epilogue is optional and may be null.- Parameters:
epilogue- The epilogue text to add to the multipart entity- Returns:
- this instance.
- Since:
- 5.3
-
buildEntity
MultipartFormEntity buildEntity()
-
build
public org.apache.hc.core5.http.HttpEntity build()
-
-