log.debug("The XML writing is completed. Now the attachments are written");
isComplete = true;
try {
rootPartOutputStream.close();
// First write the attachments added properly through the DataHandlerWriter extension
XOPEncodingStreamWriter encoder = (XOPEncodingStreamWriter)xmlWriter;
for (Iterator it = encoder.getContentIDs().iterator(); it.hasNext(); ) {
String contentID = (String)it.next();
DataHandler dataHandler = encoder.getDataHandler(contentID);
if (preserveAttachments || !(dataHandler instanceof DataHandlerExt)) {
multipartWriter.writePart(dataHandler, contentID);
} else {
OutputStream out = multipartWriter.writePart(dataHandler.getContentType(), contentID);
BufferUtils.inputStream2OutputStream(((DataHandlerExt)dataHandler).readOnce(), out);