Search in sources :

Example 41 with OMOutputFormat

use of org.apache.axiom.om.OMOutputFormat in project webservices-axiom by apache.

the class AttachmentsTest method testSWAWriteWithIncomingOrder.

public void testSWAWriteWithIncomingOrder() throws Exception {
    // Read the stream that has soap xml followed by BAttachment then AAttachment
    InputStream inStream = SwASample.SAMPLE1.getInputStream();
    Attachments attachments = new Attachments(inStream, SwASample.SAMPLE1.getContentType());
    // Get the contentIDs to force the reading
    attachments.getAllContentIDs();
    // Get the root
    SOAPModelBuilder builder = OMXMLBuilderFactory.createSOAPModelBuilder(attachments.getRootPartInputStream(), "UTF-8");
    OMElement root = builder.getDocumentElement();
    StringWriter xmlWriter = new StringWriter();
    root.serialize(xmlWriter);
    // Serialize the message using the legacy behavior (order by content id)
    OMOutputFormat format = new OMOutputFormat();
    format.setCharSetEncoding("utf-8");
    format.setDoingSWA(true);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    MIMEOutputUtils.writeSOAPWithAttachmentsMessage(xmlWriter, baos, attachments, format);
    String text = baos.toString();
    // Assert that AAttachment occurs before BAttachment since
    // that is the natural ordering of the content ids.
    assertTrue(text.indexOf("BAttachment") < text.indexOf("AAttachment"));
}
Also used : StringWriter(java.io.StringWriter) InputStream(java.io.InputStream) OMElement(org.apache.axiom.om.OMElement) SOAPModelBuilder(org.apache.axiom.soap.SOAPModelBuilder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OMOutputFormat(org.apache.axiom.om.OMOutputFormat)

Example 42 with OMOutputFormat

use of org.apache.axiom.om.OMOutputFormat in project webservices-axiom by apache.

the class TestSerializeWithIgnoreXMLDeclaration method runTest.

@Override
protected void runTest(OMDocument document) throws Throwable {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    OMOutputFormat format = new OMOutputFormat();
    format.setIgnoreXMLDeclaration(true);
    document.serializeAndConsume(baos, format);
    String xmlDocument = new String(baos.toByteArray(), "utf-8");
    assertThat(xmlDocument).doesNotContain("<?xml");
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) OMOutputFormat(org.apache.axiom.om.OMOutputFormat)

Example 43 with OMOutputFormat

use of org.apache.axiom.om.OMOutputFormat in project webservices-axiom by apache.

the class TestReadAttachmentBeforeRootPartComplete method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    // Programmatically create the message
    OMElement orgRoot = factory.createOMElement("root", null);
    OMElement orgChild1 = factory.createOMElement("child1", null, orgRoot);
    DataSource ds = new RandomDataSource(54321, 4096);
    orgChild1.addChild(factory.createOMText(new DataHandler(ds), true));
    // Create a child with a large text content and insert it after the binary node.
    // If we don't do this, then the root part may be buffered entirely by the parser,
    // and the test would not be effective.
    OMElement orgChild2 = factory.createOMElement("child2", null, orgRoot);
    String s = RandomUtils.randomString(128 * 1024);
    orgChild2.setText(s);
    // Serialize the message
    OMOutputFormat format = new OMOutputFormat();
    format.setDoOptimize(true);
    MemoryBlob blob = Blobs.createMemoryBlob();
    OutputStream out = blob.getOutputStream();
    orgRoot.serialize(out, format);
    out.close();
    // Parse the message
    OMXMLParserWrapper builder = OMXMLBuilderFactory.createOMBuilder(factory, StAXParserConfiguration.NON_COALESCING, MultipartBody.builder().setInputStream(blob.getInputStream()).setContentType(format.getContentType()).build());
    OMElement root = builder.getDocumentElement();
    OMElement child1 = (OMElement) root.getFirstOMChild();
    OMText text = (OMText) child1.getFirstOMChild();
    assertTrue(text.isBinary());
    // Access the DataHandler
    DataHandler dh = text.getDataHandler();
    IOTestUtils.compareStreams(ds.getInputStream(), dh.getInputStream());
    OMElement child2 = (OMElement) child1.getNextOMSibling();
    assertFalse(child2.isComplete());
    assertEquals(s, child2.getText());
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource) MemoryBlob(org.apache.axiom.blob.MemoryBlob) OutputStream(java.io.OutputStream) OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement) DataHandler(javax.activation.DataHandler) OMOutputFormat(org.apache.axiom.om.OMOutputFormat) OMXMLParserWrapper(org.apache.axiom.om.OMXMLParserWrapper) RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource) DataSource(javax.activation.DataSource)

Example 44 with OMOutputFormat

use of org.apache.axiom.om.OMOutputFormat 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)

Example 45 with OMOutputFormat

use of org.apache.axiom.om.OMOutputFormat in project webservices-axiom by apache.

the class TestSetOptimize method runTest.

@Override
protected void runTest() throws Throwable {
    InputStream in = XOP_SPEC_SAMPLE.getInputStream();
    try {
        OMDocument document = OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), StAXParserConfiguration.DEFAULT, MultipartBody.builder().setInputStream(in).setContentType(XOP_SPEC_SAMPLE.getContentType()).build()).getDocument();
        for (Iterator<OMSerializable> it = document.getDescendants(false); it.hasNext(); ) {
            OMSerializable node = it.next();
            if (node instanceof OMText) {
                OMText text = (OMText) node;
                if (text.isBinary()) {
                    text.setOptimize(optimize);
                }
            }
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        OMOutputFormat format = new OMOutputFormat();
        format.setDoOptimize(true);
        document.serialize(out, format);
        Multipart mp = new MimeMultipart(new ByteArrayDataSource(out.toByteArray(), format.getContentType()));
        assertThat(mp.getCount()).isEqualTo(optimize ? 3 : 1);
    } finally {
        in.close();
    }
}
Also used : MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) MimeMultipart(javax.mail.internet.MimeMultipart) InputStream(java.io.InputStream) OMText(org.apache.axiom.om.OMText) OMSerializable(org.apache.axiom.om.OMSerializable) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OMOutputFormat(org.apache.axiom.om.OMOutputFormat) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) OMDocument(org.apache.axiom.om.OMDocument)

Aggregations

OMOutputFormat (org.apache.axiom.om.OMOutputFormat)64 MessageFormatter (org.apache.axis2.transport.MessageFormatter)24 ByteArrayOutputStream (java.io.ByteArrayOutputStream)21 OutputStream (java.io.OutputStream)18 IOException (java.io.IOException)16 AxisFault (org.apache.axis2.AxisFault)13 MessageContext (org.apache.axis2.context.MessageContext)12 OMElement (org.apache.axiom.om.OMElement)11 DataHandler (javax.activation.DataHandler)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InputStream (java.io.InputStream)6 StringWriter (java.io.StringWriter)6 XMLStreamException (javax.xml.stream.XMLStreamException)6 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)6 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)5 OMText (org.apache.axiom.om.OMText)5 Map (java.util.Map)4 MultipartBody (org.apache.axiom.mime.MultipartBody)4 SOAPFactory (org.apache.axiom.soap.SOAPFactory)4 Collection (java.util.Collection)3