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 SummaryModifier and TypeMethodDescriptionbuild()Build an instance of anIAttachmentusing the properties specified on this builder.Sets theContent-IDheader in this attachment part.contentType(String contentType) Sets theContent-Typeheader for this attachment part.contentType(javax.ws.rs.core.MediaType contentType) Sets theContent-Typeheader 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 AttachmentBuildernewBuilder(String fieldName) Create a newAttachmentBuilderinstance for creating a new attachment for a multipart payload.
- 
Method Details- 
newBuilderCreate a newAttachmentBuilderinstance for creating a new attachment for a multipart payload. The name will be added as a parameter to the part'sContent-Dispositionheader. See RFC 7578, section 4.2 for more details.- Parameters:
- fieldName- the name of the attachment part
- Returns:
- this builder
- Throws:
- IllegalArgumentException- if the- fieldNameis- null
 
- 
contentIdSets theContent-IDheader in this attachment part.- Parameters:
- contentId- the ID for this particular attachment
- Returns:
- this builder
 
- 
contentTypeSets theContent-Typeheader for this attachment part. This method is analagous to callingcontentType(contentType.toString())- Parameters:
- contentType- the- MediaTypefor this attachment part.
- Returns:
- this builder
 
- 
contentTypeSets theContent-Typeheader for this attachment part.- Parameters:
- contentType- the content type string for this attachment part.
- Returns:
- this builder
 
- 
headerSets a header for this attachment part.- Parameters:
- headerName- the name of the header
- headerValues- the (possibly multi-valued) value of the header
- Returns:
- this builder
 
- 
headersSets headers for this attachment part.- Parameters:
- newHeaders- the map of header values to set for this attachment
- Returns:
- this builder
 
- 
fileNameSets the file name of this attachment part. If nocontentTypeis specified, the default content type will change to"application/octet-stream"after calling this method. ThefileNameparameter value will be added to theContent-Dispositionheader. See RFC 7578, section 4.2 for more details.- Parameters:
- fileName- the file name of this attachment part
- Returns:
- this builder
- Throws:
- IllegalArgumentException- if- fileNameis- null
 
- 
inputStreamSets the content of this attachment part as anInputStream.- Parameters:
- inputStream- content body of this attachment part
- Returns:
- this builder
- Throws:
- IllegalArgumentException- if- inputStreamis- null
 
- 
inputStreamSets the content of this attachment part as anInputStream. Analogous to callingbuilder.fileName(fileName).inputStream(inputStream)- Parameters:
- fileName- the file name of this attachment part
- inputStream- content body of this attachment part
- Returns:
- this builder
- Throws:
- IllegalArgumentException- if- fileNameor- inputStreamis- null
 
- 
buildBuild an instance of anIAttachmentusing the properties specified on this builder.- Returns:
- an instance of IAttachment
- Throws:
- IllegalStateException- if the- inputStreamproperty has not been set
 
 
-