use of org.apache.axiom.attachments.lifecycle.DataHandlerExt in project webservices-axiom by apache.
the class AttachmentsTest method testDataHandlerStreaming.
/**
* Tests that a call to {@link DataHandlerExt#readOnce()} on a {@link DataHandler} returned by
* the {@link Attachments} object streams the content of the MIME part.
*
* @throws Exception
*/
public void testDataHandlerStreaming() throws Exception {
// Note: We are only interested in the MimeMultipart, but we need to create a
// MimeMessage to be able to calculate the correct content type
MimeMessage message = new MimeMessage((Session) null);
final MimeMultipart mp = new MimeMultipart("related");
// Prepare the "SOAP" part
MimeBodyPart bp1 = new MimeBodyPart();
// Obviously this is not SOAP, but this is irrelevant for this test
bp1.setText("<root/>", "utf-8", "xml");
bp1.addHeader("Content-Transfer-Encoding", "binary");
bp1.addHeader("Content-ID", "part1@apache.org");
mp.addBodyPart(bp1);
// Create an attachment that is larger than the maximum heap
DataSource dataSource = new RandomDataSource((int) Math.min(Runtime.getRuntime().maxMemory(), Integer.MAX_VALUE));
MimeBodyPart bp2 = new MimeBodyPart();
bp2.setDataHandler(new DataHandler(dataSource));
bp2.addHeader("Content-Transfer-Encoding", "binary");
bp2.addHeader("Content-ID", "part2@apache.org");
mp.addBodyPart(bp2);
message.setContent(mp);
// Compute the correct content type
message.saveChanges();
// We use a pipe (with a producer running in a separate thread) because obviously we can't
// store the multipart in memory.
final PipedOutputStream pipeOut = new PipedOutputStream();
PipedInputStream pipeIn = new PipedInputStream(pipeOut);
Thread producerThread = new Thread(new Runnable() {
public void run() {
try {
try {
mp.writeTo(pipeOut);
} finally {
pipeOut.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
producerThread.start();
try {
// We configure Attachments to buffer MIME parts in memory. If the part content is not
// streamed, then this will result in an OOM error.
Attachments attachments = new Attachments(pipeIn, message.getContentType());
DataHandlerExt dh = (DataHandlerExt) attachments.getDataHandler("part2@apache.org");
IOTestUtils.compareStreams(dataSource.getInputStream(), dh.readOnce());
} finally {
pipeIn.close();
}
}
use of org.apache.axiom.attachments.lifecycle.DataHandlerExt in project webservices-axiom by apache.
the class AttachmentsTest method testPurgeDataSource.
public void testPurgeDataSource() throws Exception {
InputStream in = getTestResource("mtom/msg-soap-wls81.txt");
MyLifecycleManager manager = new MyLifecycleManager();
Attachments attachments = new Attachments(manager, in, "multipart/related;type=\"text/xml\";boundary=\"----=_Part_0_3437046.1188904239130\";start=__WLS__1188904239161__SOAP__", true, getAttachmentsDir(), "1024");
// Read the attachment once to make sure it is buffered
DataHandler dh = attachments.getDataHandler("__WLS__1188904239162__SOAP__");
assertTrue(dh instanceof DataHandlerExt);
InputStream content = dh.getInputStream();
IOUtils.copy(content, new NullOutputStream());
content.close();
assertEquals(1, manager.getFileCount());
((DataHandlerExt) dh).purgeDataSource();
assertEquals(0, manager.getFileCount());
in.close();
}
Aggregations