Class AttachmentBuilder
java.lang.Object
com.ibm.websphere.jaxrs20.multipart.AttachmentBuilder
The
Example usage of sending a multipart request using this API might look something like:
AttachmentBuilder
class is used to create instances of IAttachment
for sending
multipart/form-data payloads in a client request or server response. Instances of the
AttachmentBuilder
are not intended to be re-used. Before calling the build()
method,
you must set the inputStream
property using either the inputStream(InputStream)
method
or the inputStream(String, InputStream)
method.
When building a IAttachment
instance, the contentType
property is optional. If left unset,
the default value will be "text/plain"
unless the fileName
property has been set. If the
fileName
property is set, then the default value for contentType
will be
"application/octet-stream"
. This behavior is described in
RFC 7578.
List<IAttachment> parts = new ArrayList<>(); parts.add(AttachmentBuilder.newBuilder("sinpleString") .inputStream(new ByteArrayInputStream("Hello World!".getBytes())) .build()); // content type for this part will be "text/plain" parts.add(AttachmentBuilder.newBuilder("txtFileWithHeader") .inputStream(new FileInputStream("/path/to/myTextFile.txt") .fileName("renamedTextFile.txt") .header("X-MyCustomHeader", someHeaderValue) .build()); // content type for this part will be "application/octet-stream" parts.add(AttachmentBuilder.newBuilder("xmlFile") .inputStream("myXmlFile.xml", new FileInputStream("/path/to/myXmlFile.xml")) .contentType(MediaType.APPLICATION_XML) .build()); Client c = ClientBuilder.newClient(); WebTarget target = c.target("http://somehost:9080/data/multipart/list"); Response r = target.request() .header("Content-Type", "multipart/form-data") .post(Entity.entity(attachments, MediaType.MULTIPART_FORM_DATA_TYPE));
Note that the InputStreams
passed to the builder will be closed by the JAX-RS runtime. Closing
the streams prior to sending may result in unexpected behavior.
-
Method Summary
Modifier and TypeMethodDescriptionbuild()
Build an instance of anIAttachment
using the properties specified on this builder.Sets theContent-ID
header in this attachment part.contentType
(String contentType) Sets theContent-Type
header for this attachment part.contentType
(javax.ws.rs.core.MediaType contentType) Sets theContent-Type
header for this attachment part.Sets the file name of this attachment part.Sets a header for this attachment part.Sets headers for this attachment part.inputStream
(InputStream inputStream) Sets the content of this attachment part as anInputStream
.inputStream
(String fileName, InputStream inputStream) Sets the content of this attachment part as anInputStream
.static AttachmentBuilder
newBuilder
(String fieldName) Create a newAttachmentBuilder
instance for creating a new attachment for a multipart payload.
-
Method Details
-
newBuilder
Create a newAttachmentBuilder
instance for creating a new attachment for a multipart payload. The name will be added as a parameter to the part'sContent-Disposition
header. See RFC 7578, section 4.2 for more details.- Parameters:
fieldName
- the name of the attachment part- Returns:
- this builder
- Throws:
IllegalArgumentException
- if thefieldName
isnull
-
contentId
Sets theContent-ID
header in this attachment part.- Parameters:
contentId
- the ID for this particular attachment- Returns:
- this builder
-
contentType
Sets theContent-Type
header for this attachment part. This method is analagous to callingcontentType(contentType.toString())
- Parameters:
contentType
- theMediaType
for this attachment part.- Returns:
- this builder
-
contentType
Sets theContent-Type
header for this attachment part.- Parameters:
contentType
- the content type string for this attachment part.- Returns:
- this builder
-
header
Sets a header for this attachment part.- Parameters:
headerName
- the name of the headerheaderValues
- the (possibly multi-valued) value of the header- Returns:
- this builder
-
headers
Sets headers for this attachment part.- Parameters:
newHeaders
- the map of header values to set for this attachment- Returns:
- this builder
-
fileName
Sets the file name of this attachment part. If nocontentType
is specified, the default content type will change to"application/octet-stream"
after calling this method. ThefileName
parameter value will be added to theContent-Disposition
header. See RFC 7578, section 4.2 for more details.- Parameters:
fileName
- the file name of this attachment part- Returns:
- this builder
- Throws:
IllegalArgumentException
- iffileName
isnull
-
inputStream
Sets the content of this attachment part as anInputStream
.- Parameters:
inputStream
- content body of this attachment part- Returns:
- this builder
- Throws:
IllegalArgumentException
- ifinputStream
isnull
-
inputStream
Sets the content of this attachment part as anInputStream
. Analogous to callingbuilder.fileName(fileName).inputStream(inputStream)
- Parameters:
fileName
- the file name of this attachment partinputStream
- content body of this attachment part- Returns:
- this builder
- Throws:
IllegalArgumentException
- iffileName
orinputStream
isnull
-
build
Build an instance of anIAttachment
using the properties specified on this builder.- Returns:
- an instance of
IAttachment
- Throws:
IllegalStateException
- if theinputStream
property has not been set
-