Search in sources :

Example 6 with MultipartBody

use of org.apache.axiom.mime.MultipartBody 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 7 with MultipartBody

use of org.apache.axiom.mime.MultipartBody in project webservices-axiom by apache.

the class TestBuilderDetach method runTest.

@Override
protected void runTest() throws Throwable {
    MTOMSample sample = MTOMSample.SAMPLE1;
    InstrumentedInputStream in = new InstrumentedInputStream(sample.getInputStream());
    MultipartBody mb = MultipartBody.builder().setInputStream(in).setContentType(sample.getContentType()).build();
    SOAPModelBuilder builder = OMXMLBuilderFactory.createSOAPModelBuilder(metaFactory, mb);
    SOAPEnvelope envelope = builder.getSOAPEnvelope();
    long countBeforeDetach = in.getCount();
    builder.detach();
    assertThat(in.getCount()).isGreaterThan(countBeforeDetach);
    assertThat(in.isClosed()).isFalse();
    int binaryCount = 0;
    for (Iterator<OMNode> it = envelope.getDescendants(false); it.hasNext(); ) {
        OMNode node = it.next();
        if (node instanceof OMText) {
            OMText text = (OMText) node;
            if (text.isBinary()) {
                IOTestUtils.compareStreams(sample.getPart(text.getContentID()), text.getDataHandler().getInputStream());
                binaryCount++;
            }
        }
    }
    assertThat(binaryCount).isGreaterThan(0);
    in.close();
}
Also used : OMNode(org.apache.axiom.om.OMNode) MultipartBody(org.apache.axiom.mime.MultipartBody) InstrumentedInputStream(org.apache.axiom.testutils.io.InstrumentedInputStream) OMText(org.apache.axiom.om.OMText) MTOMSample(org.apache.axiom.ts.soap.MTOMSample) SOAPModelBuilder(org.apache.axiom.soap.SOAPModelBuilder) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope)

Example 8 with MultipartBody

use of org.apache.axiom.mime.MultipartBody in project webservices-axiom by apache.

the class TestGetXMLStreamReaderMTOMEncoded method runTest.

@Override
protected void runTest() throws Throwable {
    InputStream inStream = MTOMSample.SAMPLE2.getInputStream();
    MultipartBody mb = MultipartBody.builder().setInputStream(inStream).setContentType(MTOMSample.SAMPLE2.getContentType()).build();
    OMElement root = OMXMLBuilderFactory.createSOAPModelBuilder(metaFactory, mb).getDocumentElement();
    // Use tree as input to XMLStreamReader
    // Issue XOP:Include events for optimized MTOM text nodes
    XOPEncoded<XMLStreamReader> xopEncodedStream = root.getXOPEncodedStreamReader(cache);
    XMLStreamReader xmlStreamReader = xopEncodedStream.getRootPart();
    DataHandler dh = null;
    while (xmlStreamReader.hasNext() && dh == null) {
        xmlStreamReader.next();
        if (xmlStreamReader.isStartElement()) {
            QName qName = xmlStreamReader.getName();
            if (XOP_INCLUDE.equals(qName)) {
                String hrefValue = xmlStreamReader.getAttributeValue("", "href");
                assertThat(hrefValue).startsWith("cid:");
                if (hrefValue != null) {
                    dh = xopEncodedStream.getAttachmentAccessor().getDataHandler(hrefValue.substring(4));
                }
            }
        }
    }
    assertTrue(dh != null);
    // Make sure next event is an an XOP_Include END element
    xmlStreamReader.next();
    assertTrue(xmlStreamReader.isEndElement());
    assertTrue(XOP_INCLUDE.equals(xmlStreamReader.getName()));
    // Make sure the next event is the end tag of name
    xmlStreamReader.next();
    assertTrue(xmlStreamReader.isEndElement());
    assertTrue("name".equals(xmlStreamReader.getLocalName()));
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) InputStream(java.io.InputStream) MultipartBody(org.apache.axiom.mime.MultipartBody) QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement) DataHandler(javax.activation.DataHandler)

Example 9 with MultipartBody

use of org.apache.axiom.mime.MultipartBody in project webservices-axiom by apache.

the class TestSerialize method runTest.

@Override
protected void runTest() throws Throwable {
    MTOMSample testMessage = MTOMSample.SAMPLE1;
    // Read in message: SOAPPart and 2 image attachments
    InputStream inStream = testMessage.getInputStream();
    MultipartBody mb = MultipartBody.builder().setInputStream(inStream).setContentType(testMessage.getContentType()).build();
    OMOutputFormat oof = new OMOutputFormat();
    oof.setDoOptimize(true);
    oof.setMimeBoundary(testMessage.getBoundary());
    oof.setRootContentId(testMessage.getStart());
    if (base64) {
        oof.setProperty(OMOutputFormat.USE_CTE_BASE64_FOR_NON_TEXTUAL_ATTACHMENTS, Boolean.TRUE);
    }
    // Write out the message
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    OMXMLParserWrapper builder = OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), StAXParserConfiguration.DEFAULT, mb);
    OMElement om = builder.getDocumentElement();
    om.serialize(baos, oof);
    om.close(false);
    String out = baos.toString();
    if (base64) {
        // Do a quick check to see if the data is base64 and is
        // writing base64 compliant code.
        assertTrue(out.indexOf("base64") != -1);
        assertTrue(out.indexOf("GBgcGBQgHBwcJCQgKDBQNDAsL") != -1);
    } else {
        assertTrue(out.indexOf("base64") == -1);
    }
}
Also used : InputStream(java.io.InputStream) MultipartBody(org.apache.axiom.mime.MultipartBody) OMElement(org.apache.axiom.om.OMElement) MTOMSample(org.apache.axiom.ts.soap.MTOMSample) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OMOutputFormat(org.apache.axiom.om.OMOutputFormat) OMXMLParserWrapper(org.apache.axiom.om.OMXMLParserWrapper)

Aggregations

MultipartBody (org.apache.axiom.mime.MultipartBody)9 OMElement (org.apache.axiom.om.OMElement)7 DataHandler (javax.activation.DataHandler)5 OMOutputFormat (org.apache.axiom.om.OMOutputFormat)5 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)5 InputStream (java.io.InputStream)4 OMText (org.apache.axiom.om.OMText)4 OutputStream (java.io.OutputStream)3 QName (javax.xml.namespace.QName)3 SOAPFactory (org.apache.axiom.soap.SOAPFactory)3 MTOMSample (org.apache.axiom.ts.soap.MTOMSample)3 MemoryBlob (org.apache.axiom.blob.MemoryBlob)2 PartDataHandler (org.apache.axiom.mime.PartDataHandler)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PipedInputStream (java.io.PipedInputStream)1 PipedOutputStream (java.io.PipedOutputStream)1 URLConnection (java.net.URLConnection)1 DataSource (javax.activation.DataSource)1 JAXBContext (javax.xml.bind.JAXBContext)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1