Search in sources :

Example 1 with BlobOMDataSource

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);
}
Also used : OMNode(org.apache.axiom.om.OMNode) OMNamespace(org.apache.axiom.om.OMNamespace) XMLStreamReader(javax.xml.stream.XMLStreamReader) BlobOMDataSource(org.apache.axiom.om.ds.BlobOMDataSource) SOAPHeaderBlock(org.apache.axiom.soap.SOAPHeaderBlock) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) OMSourcedElement(org.apache.axiom.om.OMSourcedElement) SOAPHeader(org.apache.axiom.soap.SOAPHeader)

Example 2 with BlobOMDataSource

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;
}
Also used : OMNamespace(org.apache.axiom.om.OMNamespace) BlobOMDataSource(org.apache.axiom.om.ds.BlobOMDataSource) MemoryBlob(org.apache.axiom.blob.MemoryBlob) QName(javax.xml.namespace.QName) OutputStream(java.io.OutputStream) SOAPHeaderBlock(org.apache.axiom.soap.SOAPHeaderBlock)

Example 3 with BlobOMDataSource

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);
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMNode(org.apache.axiom.om.OMNode) OMNamespace(org.apache.axiom.om.OMNamespace) XMLStreamReader(javax.xml.stream.XMLStreamReader) BlobOMDataSource(org.apache.axiom.om.ds.BlobOMDataSource) OMElement(org.apache.axiom.om.OMElement) OMSourcedElement(org.apache.axiom.om.OMSourcedElement)

Example 4 with BlobOMDataSource

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);
    }
}
Also used : BlobOMDataSource(org.apache.axiom.om.ds.BlobOMDataSource) XMLStreamException(javax.xml.stream.XMLStreamException) OutputStream(java.io.OutputStream) WritableBlob(org.apache.axiom.blob.WritableBlob) IOException(java.io.IOException) OMException(org.apache.axiom.om.OMException)

Aggregations

BlobOMDataSource (org.apache.axiom.om.ds.BlobOMDataSource)4 OMNamespace (org.apache.axiom.om.OMNamespace)3 OutputStream (java.io.OutputStream)2 XMLStreamReader (javax.xml.stream.XMLStreamReader)2 OMNode (org.apache.axiom.om.OMNode)2 OMSourcedElement (org.apache.axiom.om.OMSourcedElement)2 SOAPHeaderBlock (org.apache.axiom.soap.SOAPHeaderBlock)2 IOException (java.io.IOException)1 QName (javax.xml.namespace.QName)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 MemoryBlob (org.apache.axiom.blob.MemoryBlob)1 WritableBlob (org.apache.axiom.blob.WritableBlob)1 OMElement (org.apache.axiom.om.OMElement)1 OMException (org.apache.axiom.om.OMException)1 OMFactory (org.apache.axiom.om.OMFactory)1 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)1 SOAPHeader (org.apache.axiom.soap.SOAPHeader)1