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"));
}
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");
}
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());
}
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);
}
}
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();
}
}
Aggregations