Package org.apache.axiom.mime

# Class MultipartBodyWriter

java.lang.Object
org.apache.axiom.mime.MultipartBodyWriter

public final class MultipartBodyWriter extends Object
Writes a MIME multipart body as used by XOP/MTOM and SOAP with Attachments. MIME parts are written using `writePart(ContentType, ContentTransferEncoding, String, List)` or `writePart(Blob, ContentType, ContentTransferEncoding, String, List)`. Calls to both methods can be mixed, i.e. it is not required to use the same method for all MIME parts. Instead, the caller should choose the most convenient method for each part (depending on the form in which the content is available). After all parts have been written, `complete()` must be called to write the final MIME boundary.

The following semantics are defined for the `contentTransferEncoding` and `contentID` arguments of the two write methods:

- The content transfer encoding specified by the `contentTransferEncoding` argument is applied by the write method; the caller only provides the unencoded data. The implementation ensures that the MIME part has a `Content-Transfer-Encoding` header appropriate for the applied encoding.
- The content ID passed as argument is always the raw ID (without the angle brackets). The implementation translates this into a properly formatted `Content-ID` header.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `MultipartBodyWriter(OutputStream out, String boundary)`
  Constructor.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `complete()`
  Complete writing of the MIME multipart package.
  `void`
  `writePart(Blob blob, ContentType contentType, ContentTransferEncoding contentTransferEncoding, String contentID, List<Header> extraHeaders)`
  Write a MIME part.
  `OutputStream`
  `writePart(ContentType contentType, ContentTransferEncoding contentTransferEncoding, String contentID, List<Header> extraHeaders)`
  Start writing a MIME part.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### MultipartBodyWriter

    public MultipartBodyWriter(OutputStream out, String boundary)
    Constructor.
    Parameters:
      `out` - the output stream to write the multipart body to
      `boundary` - the MIME boundary
    See Also:
      - `UIDGenerator.generateMimeBoundary()`

- ## Method Details

  - ### writePart

    public OutputStream writePart(ContentType contentType, ContentTransferEncoding contentTransferEncoding, String contentID, List<Header> extraHeaders) throws IOException
    Start writing a MIME part. The methods returns an `OutputStream` that the caller can use to write the content of the MIME part. After writing the content, `OutputStream.close()` must be called to complete the writing of the MIME part.
    Parameters:
      `contentType` - the content type of the MIME part; may be `null`
      `contentTransferEncoding` - the content transfer encoding to be used (see above); must not be `null`
      `contentID` - the content ID of the MIME part (see above); may be `null`
      `extraHeaders` - a list of `Header` objects with additional headers to write to the MIME part; may be `null`
    Returns:
      an output stream to write the content of the MIME part
    Throws:
      `IOException` - if an I/O error occurs when writing to the underlying stream

  - ### writePart

    public void writePart(Blob blob, ContentType contentType, ContentTransferEncoding contentTransferEncoding, String contentID, List<Header> extraHeaders) throws IOException
    Write a MIME part.
    Parameters:
      `blob` - the content of the MIME part to write
      `contentType` - the content type; may be `null`
      `contentTransferEncoding` - the content transfer encoding to be used (see above); must not be `null`
      `contentID` - the content ID of the MIME part (see above)
      `extraHeaders` - a list of `Header` objects with additional headers to write to the MIME part
    Throws:
      `IOException` - if an I/O error occurs when writing the part to the underlying stream

  - ### complete

    public void complete() throws IOException
    Complete writing of the MIME multipart package. This method does not close the underlying stream.
    Throws:
      `IOException` - if an I/O error occurs when writing to the underlying stream