Class AttachmentBuilder
- java.lang.Object
-
- com.ibm.websphere.jaxrs20.multipart.AttachmentBuilder
-
public class AttachmentBuilder extends java.lang.ObjectTheAttachmentBuilderclass is used to create instances ofIAttachmentfor sending multipart/form-data payloads in a client request or server response. Instances of theAttachmentBuilderare not intended to be re-used. Before calling thebuild()method, you must set theinputStreamproperty using either theinputStream(InputStream)method or theinputStream(String, InputStream)method.When building a
IAttachmentinstance, thecontentTypeproperty is optional. If left unset, the default value will be"text/plain"unless thefileNameproperty has been set. If thefileNameproperty is set, then the default value forcontentTypewill be"application/octet-stream". This behavior is described in RFC 7578.Example usage of sending a multipart request using this API might look something like: 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
InputStreamspassed to the builder will be closed by the JAX-RS runtime. Closing the streams prior to sending may result in unexpected behavior.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description IAttachmentbuild()Build an instance of anIAttachmentusing the properties specified on this builder.AttachmentBuildercontentId(java.lang.String contentId)Sets theContent-IDheader in this attachment part.AttachmentBuildercontentType(java.lang.String contentType)Sets theContent-Typeheader for this attachment part.AttachmentBuildercontentType(javax.ws.rs.core.MediaType contentType)Sets theContent-Typeheader for this attachment part.AttachmentBuilderfileName(java.lang.String fileName)Sets the file name of this attachment part.AttachmentBuilderheader(java.lang.String headerName, java.lang.String... headerValues)Sets a header for this attachment part.AttachmentBuilderheaders(javax.ws.rs.core.MultivaluedMap<java.lang.String,java.lang.String> newHeaders)Sets headers for this attachment part.AttachmentBuilderinputStream(java.io.InputStream inputStream)Sets the content of this attachment part as anInputStream.AttachmentBuilderinputStream(java.lang.String fileName, java.io.InputStream inputStream)Sets the content of this attachment part as anInputStream.static AttachmentBuildernewBuilder(java.lang.String fieldName)Create a newAttachmentBuilderinstance for creating a new attachment for a multipart payload.
-
-
-
Method Detail
-
newBuilder
public static AttachmentBuilder newBuilder(java.lang.String fieldName)
Create 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:
java.lang.IllegalArgumentException- if thefieldNameisnull
-
contentId
public AttachmentBuilder contentId(java.lang.String contentId)
Sets theContent-IDheader in this attachment part.- Parameters:
contentId- the ID for this particular attachment- Returns:
- this builder
-
contentType
public AttachmentBuilder contentType(javax.ws.rs.core.MediaType contentType)
Sets theContent-Typeheader for this attachment part. This method is analagous to callingcontentType(contentType.toString())- Parameters:
contentType- theMediaTypefor this attachment part.- Returns:
- this builder
-
contentType
public AttachmentBuilder contentType(java.lang.String contentType)
Sets theContent-Typeheader for this attachment part.- Parameters:
contentType- the content type string for this attachment part.- Returns:
- this builder
-
header
public AttachmentBuilder header(java.lang.String headerName, java.lang.String... headerValues)
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
public AttachmentBuilder headers(javax.ws.rs.core.MultivaluedMap<java.lang.String,java.lang.String> newHeaders)
Sets headers for this attachment part.- Parameters:
newHeaders- the map of header values to set for this attachment- Returns:
- this builder
-
fileName
public AttachmentBuilder fileName(java.lang.String fileName)
Sets 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:
java.lang.IllegalArgumentException- iffileNameisnull
-
inputStream
public AttachmentBuilder inputStream(java.io.InputStream inputStream)
Sets the content of this attachment part as anInputStream.- Parameters:
inputStream- content body of this attachment part- Returns:
- this builder
- Throws:
java.lang.IllegalArgumentException- ifinputStreamisnull
-
inputStream
public AttachmentBuilder inputStream(java.lang.String fileName, java.io.InputStream 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:
java.lang.IllegalArgumentException- iffileNameorinputStreamisnull
-
build
public IAttachment build()
Build an instance of anIAttachmentusing the properties specified on this builder.- Returns:
- an instance of
IAttachment - Throws:
java.lang.IllegalStateException- if theinputStreamproperty has not been set
-
-