Search in sources :

Example 1 with DataHandlerExt

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();
    }
}
Also used : RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource) PipedOutputStream(java.io.PipedOutputStream) DataHandlerExt(org.apache.axiom.attachments.lifecycle.DataHandlerExt) DataHandler(javax.activation.DataHandler) PipedInputStream(java.io.PipedInputStream) MIMEException(org.apache.axiom.mime.MIMEException) IOException(java.io.IOException) OMException(org.apache.axiom.om.OMException) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) SizeAwareDataSource(org.apache.axiom.ext.activation.SizeAwareDataSource) RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource) DataSource(javax.activation.DataSource) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 2 with DataHandlerExt

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();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PipedInputStream(java.io.PipedInputStream) ExceptionInputStream(org.apache.axiom.testutils.io.ExceptionInputStream) InputStream(java.io.InputStream) DataHandlerExt(org.apache.axiom.attachments.lifecycle.DataHandlerExt) DataHandler(javax.activation.DataHandler) NullOutputStream(org.apache.commons.io.output.NullOutputStream)

Aggregations

PipedInputStream (java.io.PipedInputStream)2 DataHandler (javax.activation.DataHandler)2 DataHandlerExt (org.apache.axiom.attachments.lifecycle.DataHandlerExt)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 PipedOutputStream (java.io.PipedOutputStream)1 DataSource (javax.activation.DataSource)1 MimeBodyPart (javax.mail.internet.MimeBodyPart)1 MimeMessage (javax.mail.internet.MimeMessage)1 MimeMultipart (javax.mail.internet.MimeMultipart)1 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)1 SizeAwareDataSource (org.apache.axiom.ext.activation.SizeAwareDataSource)1 MIMEException (org.apache.axiom.mime.MIMEException)1 OMException (org.apache.axiom.om.OMException)1 RandomDataSource (org.apache.axiom.testutils.activation.RandomDataSource)1 ExceptionInputStream (org.apache.axiom.testutils.io.ExceptionInputStream)1 NullOutputStream (org.apache.commons.io.output.NullOutputStream)1