use of org.apache.axiom.om.ds.BlobOMDataSource in project webservices-axiom by apache.
the class TestBlobOMDataSource method runTest.
@Override
protected void runTest() throws Throwable {
SOAPEnvelope soapEnvelope = soapFactory.createSOAPEnvelope();
SOAPHeader soapHeader = soapFactory.createSOAPHeader(soapEnvelope);
String localName = "myPayload";
String encoding = "utf-8";
String payload = "<tns:myPayload xmlns:tns=\"urn://test\">Payload One</tns:myPayload>";
OMNamespace ns = soapFactory.createOMNamespace("urn://test", "tns");
BlobOMDataSource ds = new BlobOMDataSource(Blobs.createBlob(payload.getBytes(encoding)), encoding);
// Set an empty MustUnderstand property on the data source
ds.setProperty(SOAPHeaderBlock.MUST_UNDERSTAND_PROPERTY, null);
OMSourcedElement omse = soapFactory.createSOAPHeaderBlock(localName, ns, ds);
soapHeader.addChild(omse);
OMNode firstChild = soapHeader.getFirstOMChild();
assertTrue("Expected OMSourcedElement child", firstChild instanceof SOAPHeaderBlock);
SOAPHeaderBlock child = (SOAPHeaderBlock) firstChild;
assertTrue("OMSourcedElement is expanded. This is unexpected", !child.isExpanded());
assertThat(child.getDataSource()).isSameAs(ds);
// Make sure that getting the MustUnderstand property does not cause expansion.
assertTrue(!child.getMustUnderstand());
assertTrue("OMSourcedElement is expanded. This is unexpected", !child.isExpanded());
assertThat(child.getDataSource()).isSameAs(ds);
// A BlobOMDataSource does not consume the backing object when read.
// Thus getting the XMLStreamReader of the BlobOMDataSource should not
// cause expansion of the OMSourcedElement.
XMLStreamReader reader = child.getXMLStreamReader();
reader.next();
assertTrue("OMSourcedElement is expanded. This is unexpected", !child.isExpanded());
// Likewise, a BlobOMDataSource does not consume the backing object when
// written. Thus serializing the OMSourcedElement should not cause the expansion
// of the OMSourcedElement.
assertTrue("The payload was not present in the output", soapHeader.toString().indexOf(payload) > 0);
assertTrue("OMSourcedElement is expanded. This is unexpected", !child.isExpanded());
assertThat(child.getDataSource()).isSameAs(ds);
}
use of org.apache.axiom.om.ds.BlobOMDataSource in project webservices-axiom by apache.
the class ElementHelper method toSOAPHeaderBlock.
/**
* This is a method to convert regular OMElements to SOAPHeaderBlocks.
*
* @param omElement
* @param factory
* @return TODO
* @throws Exception
*/
public static SOAPHeaderBlock toSOAPHeaderBlock(OMElement omElement, SOAPFactory factory) throws Exception {
if (omElement instanceof SOAPHeaderBlock)
return (SOAPHeaderBlock) omElement;
QName name = omElement.getQName();
String localName = name.getLocalPart();
OMNamespace namespace = factory.createOMNamespace(name.getNamespaceURI(), name.getPrefix());
MemoryBlob blob = Blobs.createMemoryBlob();
OutputStream out = blob.getOutputStream();
omElement.serialize(out);
out.close();
BlobOMDataSource ds = new BlobOMDataSource(blob, "utf-8");
SOAPHeaderBlock block = factory.createSOAPHeaderBlock(localName, namespace, ds);
return block;
}
use of org.apache.axiom.om.ds.BlobOMDataSource in project webservices-axiom by apache.
the class TestBlobOMDataSource method runTest.
@Override
protected void runTest() throws Throwable {
OMFactory factory = metaFactory.getOMFactory();
String localName = "myPayload";
String encoding = "utf-8";
String payload = "<tns:myPayload xmlns:tns=\"urn://test\">Payload One</tns:myPayload>";
OMNamespace ns = factory.createOMNamespace("urn://test", "tns");
BlobOMDataSource ds = new BlobOMDataSource(Blobs.createBlob(payload.getBytes(encoding)), encoding);
OMElement parent = factory.createOMElement("root", null);
OMSourcedElement omse = factory.createOMElement(ds, localName, ns);
parent.addChild(omse);
OMNode firstChild = parent.getFirstOMChild();
assertTrue("Expected OMSourcedElement child", firstChild instanceof OMSourcedElement);
OMSourcedElement child = (OMSourcedElement) firstChild;
assertTrue("OMSourcedElement is expanded. This is unexpected", !child.isExpanded());
assertThat(child.getDataSource()).isSameAs(ds);
// A BlobOMDataSource does not consume the backing object when read.
// Thus getting the XMLStreamReader of the BlobOMDataSource should not
// cause expansion of the OMSourcedElement.
XMLStreamReader reader = child.getXMLStreamReader();
reader.next();
assertTrue("OMSourcedElement is expanded. This is unexpected", !child.isExpanded());
// Likewise, a BlobOMDataSource does not consume the backing object when
// written. Thus serializing the OMSourcedElement should not cause the expansion
// of the OMSourcedElement.
assertTrue("The payload was not present in the output", parent.toString().indexOf(payload) > 0);
assertTrue("OMSourcedElement is expanded. This is unexpected", !child.isExpanded());
// If a consumer calls build or buildWithAttachments on the tree, the
// tree should not be expanded.
parent.build();
assertTrue("OMSourcedElement is expanded after build(). This is unexpected", !child.isExpanded());
parent.buildWithAttachments();
assertTrue("OMSourcedElement is expanded after buildWithAttachments(). This is unexpected", !child.isExpanded());
// Test getting the raw bytes from the BlobOMDataSource.
assertThat(child.getDataSource()).isSameAs(ds);
}
use of org.apache.axiom.om.ds.BlobOMDataSource in project webservices-axiom by apache.
the class BlobOMDataSourceCustomBuilder method create.
@Override
public OMDataSource create(OMElement element) throws OMException {
try {
WritableBlob blob = blobFactory.createBlob();
OutputStream out = blob.getOutputStream();
try {
element.serializeAndConsume(out);
} finally {
out.close();
}
return new BlobOMDataSource(blob, encoding);
} catch (XMLStreamException ex) {
throw new OMException(ex);
} catch (IOException ex) {
throw new OMException(ex);
}
}
Aggregations