Search in sources :

Example 66 with DataHandler

use of javax.activation.DataHandler in project webservices-axiom by apache.

the class TestGetTextCharactersFromDataHandler method runTest.

@Override
protected void runTest() throws Throwable {
    DataHandler dh = new DataHandler("test content", "text/plain; charset=utf-8");
    OMText text = metaFactory.getOMFactory().createOMText(dh, true);
    char[] chars = text.getTextCharacters();
    byte[] decoded = Base64.decodeBase64(new String(chars));
    assertEquals("test content", new String(decoded, "utf-8"));
}
Also used : OMText(org.apache.axiom.om.OMText) DataHandler(javax.activation.DataHandler)

Example 67 with DataHandler

use of javax.activation.DataHandler in project webservices-axiom by apache.

the class BufferUtilsTest method testByteArrayDataSourceBackedDataHandlerExceedLimit.

public void testByteArrayDataSourceBackedDataHandlerExceedLimit() throws Exception {
    String str = "This is a test String";
    byte[] b = str.getBytes();
    ByteArrayDataSource bads = new ByteArrayDataSource(b, "text/plain");
    DataHandler dh = new DataHandler(bads);
    int unsupported = BufferUtils.doesDataHandlerExceedLimit(dh, 0);
    assertEquals(-1, unsupported);
    int doesExceed = BufferUtils.doesDataHandlerExceedLimit(dh, 10);
    assertEquals(1, doesExceed);
    int doesNotExceed = BufferUtils.doesDataHandlerExceedLimit(dh, 100);
    assertEquals(0, doesNotExceed);
}
Also used : DataHandler(javax.activation.DataHandler) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource)

Example 68 with DataHandler

use of javax.activation.DataHandler in project webservices-axiom by apache.

the class BufferUtilsTest method testObjectBackedDataHandlerExceedLimit.

public void testObjectBackedDataHandlerExceedLimit() throws Exception {
    String str = "This is a test String";
    DataHandler dh = new DataHandler(str, "text/plain");
    int unsupported = BufferUtils.doesDataHandlerExceedLimit(dh, 0);
    assertEquals(-1, unsupported);
    int doesExceed = BufferUtils.doesDataHandlerExceedLimit(dh, 10);
    assertEquals(1, doesExceed);
    int doesNotExceed = BufferUtils.doesDataHandlerExceedLimit(dh, 100);
    assertEquals(0, doesNotExceed);
}
Also used : DataHandler(javax.activation.DataHandler)

Example 69 with DataHandler

use of javax.activation.DataHandler in project webservices-axiom by apache.

the class TestMTOMForwardStreaming method runTest.

@Override
protected void runTest() throws Throwable {
    DataSource ds1 = new TestDataSource('A', Runtime.getRuntime().maxMemory());
    DataSource ds2 = new TestDataSource('B', Runtime.getRuntime().maxMemory());
    // Programmatically create the original message
    SOAPFactory factory = metaFactory.getSOAP12Factory();
    final SOAPEnvelope orgEnvelope = factory.createSOAPEnvelope();
    SOAPBody orgBody = factory.createSOAPBody(orgEnvelope);
    OMElement orgBodyElement = factory.createOMElement("test", factory.createOMNamespace("urn:test", "p"), orgBody);
    OMElement orgData1 = factory.createOMElement("data", null, orgBodyElement);
    orgData1.addChild(factory.createOMText(new DataHandler(ds1), true));
    OMElement orgData2 = factory.createOMElement("data", null, orgBodyElement);
    orgData2.addChild(factory.createOMText(new DataHandler(ds2), true));
    final OMOutputFormat format = new OMOutputFormat();
    format.setDoOptimize(true);
    format.setSOAP11(false);
    final String contentType = format.getContentType();
    final PipedOutputStream pipe1Out = new PipedOutputStream();
    final PipedInputStream pipe1In = new PipedInputStream(pipe1Out);
    // Create the producer thread (simulating the client sending the MTOM message)
    Thread producerThread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                try {
                    orgEnvelope.serialize(pipe1Out, format);
                } finally {
                    pipe1Out.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    });
    producerThread.start();
    final PipedOutputStream pipe2Out = new PipedOutputStream();
    PipedInputStream pipe2In = new PipedInputStream(pipe2Out);
    // Create the forwarder thread (simulating the mediation engine that forwards the message)
    Thread forwarderThread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                try {
                    MultipartBody mb = MultipartBody.builder().setInputStream(pipe1In).setContentType(contentType).build();
                    SOAPEnvelope envelope = OMXMLBuilderFactory.createSOAPModelBuilder(metaFactory, mb).getSOAPEnvelope();
                    // the element is built. Therefore we need two different test executions.
                    if (buildSOAPPart) {
                        envelope.build();
                    }
                    // Usage of serializeAndConsume should enable streaming
                    envelope.serializeAndConsume(pipe2Out, format);
                } finally {
                    pipe2Out.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    });
    forwarderThread.start();
    try {
        MultipartBody mb = MultipartBody.builder().setInputStream(pipe2In).setContentType(contentType).build();
        SOAPEnvelope envelope = OMXMLBuilderFactory.createSOAPModelBuilder(metaFactory, mb).getSOAPEnvelope();
        OMElement bodyElement = envelope.getBody().getFirstElement();
        Iterator<OMElement> it = bodyElement.getChildElements();
        OMElement data1 = it.next();
        OMElement data2 = it.next();
        IOTestUtils.compareStreams(ds1.getInputStream(), ((PartDataHandler) ((OMText) data1.getFirstOMChild()).getDataHandler()).getPart().getInputStream(false));
        IOTestUtils.compareStreams(ds2.getInputStream(), ((PartDataHandler) ((OMText) data2.getFirstOMChild()).getDataHandler()).getPart().getInputStream(false));
    } finally {
        pipe2In.close();
    }
}
Also used : TestDataSource(org.apache.axiom.testutils.activation.TestDataSource) OMElement(org.apache.axiom.om.OMElement) PipedOutputStream(java.io.PipedOutputStream) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) DataHandler(javax.activation.DataHandler) PartDataHandler(org.apache.axiom.mime.PartDataHandler) PipedInputStream(java.io.PipedInputStream) SOAPFactory(org.apache.axiom.soap.SOAPFactory) DataSource(javax.activation.DataSource) TestDataSource(org.apache.axiom.testutils.activation.TestDataSource) SOAPBody(org.apache.axiom.soap.SOAPBody) PartDataHandler(org.apache.axiom.mime.PartDataHandler) MultipartBody(org.apache.axiom.mime.MultipartBody) OMOutputFormat(org.apache.axiom.om.OMOutputFormat)

Example 70 with DataHandler

use of javax.activation.DataHandler in project webservices-axiom by apache.

the class AttachmentsTest method testTurkishLocale.

private void testTurkishLocale(String contentIDHeaderName) throws Exception {
    Locale locale = Locale.getDefault();
    Locale.setDefault(new Locale("tr", "TR"));
    try {
        MimeMessage message = new MimeMessage((Session) null);
        MimeMultipart mp = new MimeMultipart("related");
        MimeBodyPart bp1 = new MimeBodyPart();
        bp1.setText("<root/>", "utf-8", "xml");
        bp1.addHeader("Content-Transfer-Encoding", "binary");
        mp.addBodyPart(bp1);
        MimeBodyPart bp2 = new MimeBodyPart();
        byte[] content = new byte[8192];
        new Random().nextBytes(content);
        bp2.setDataHandler(new DataHandler("Test", "text/plain"));
        bp2.addHeader("Content-Transfer-Encoding", "binary");
        bp2.addHeader(contentIDHeaderName, "part@apache.org");
        mp.addBodyPart(bp2);
        message.setContent(mp);
        message.saveChanges();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        mp.writeTo(baos);
        String contentType = message.getContentType();
        InputStream in = new ByteArrayInputStream(baos.toByteArray());
        Attachments attachments = new Attachments(in, contentType);
        assertNotNull(attachments.getDataHandler("part@apache.org"));
    } finally {
        Locale.setDefault(locale);
    }
}
Also used : Locale(java.util.Locale) Random(java.util.Random) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) PipedInputStream(java.io.PipedInputStream) ExceptionInputStream(org.apache.axiom.testutils.io.ExceptionInputStream) InputStream(java.io.InputStream) DataHandler(javax.activation.DataHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Aggregations

DataHandler (javax.activation.DataHandler)270 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)61 Test (org.junit.Test)59 InputStream (java.io.InputStream)57 DataSource (javax.activation.DataSource)53 MimeBodyPart (javax.mail.internet.MimeBodyPart)50 FileDataSource (javax.activation.FileDataSource)47 IOException (java.io.IOException)43 Exchange (org.apache.camel.Exchange)39 ByteArrayOutputStream (java.io.ByteArrayOutputStream)34 MimeMultipart (javax.mail.internet.MimeMultipart)33 ByteArrayInputStream (java.io.ByteArrayInputStream)32 MimeMessage (javax.mail.internet.MimeMessage)30 MessagingException (javax.mail.MessagingException)27 File (java.io.File)24 Message (org.apache.camel.Message)21 OMElement (org.apache.axiom.om.OMElement)20 QName (javax.xml.namespace.QName)19 URL (java.net.URL)17 ArrayList (java.util.ArrayList)17