Search in sources :

Example 41 with OMText

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

the class MTOMStAXSOAPModelBuilderTest method testDeferredLoadingOfAttachments.

/**
 * Test that MIME parts are not loaded before requesting the DataHandlers from the corresponding
 * OMText nodes.
 *
 * @throws Exception
 */
public void testDeferredLoadingOfAttachments() throws Exception {
    Attachments attachments = createAttachmentsForTestMTOMMessage();
    SOAPModelBuilder builder = OMXMLBuilderFactory.createSOAPModelBuilder(attachments);
    OMDocument doc = builder.getDocument();
    // Find all the binary nodes
    List<OMText> binaryNodes = new ArrayList<>();
    for (Iterator<OMSerializable> it = doc.getDescendants(false); it.hasNext(); ) {
        OMSerializable node = it.next();
        if (node instanceof OMText) {
            OMText text = (OMText) node;
            if (text.isBinary()) {
                binaryNodes.add(text);
            }
        }
    }
    assertFalse(binaryNodes.isEmpty());
    // At this moment only the SOAP part should have been loaded
    assertEquals(1, attachments.getContentIDList().size());
    for (OMText node : binaryNodes) {
        // Request the DataHandler and do something with it to make sure
        // the part is loaded
        node.getDataHandler().getInputStream().close();
    }
    assertEquals(binaryNodes.size() + 1, attachments.getContentIDList().size());
}
Also used : OMText(org.apache.axiom.om.OMText) ArrayList(java.util.ArrayList) OMSerializable(org.apache.axiom.om.OMSerializable) SOAPModelBuilder(org.apache.axiom.soap.SOAPModelBuilder) Attachments(org.apache.axiom.attachments.Attachments) OMDocument(org.apache.axiom.om.OMDocument)

Example 42 with OMText

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

the class MTOMStAXSOAPModelBuilderTest method testCreateOMElement.

public void testCreateOMElement() throws Exception {
    OMElement root = createTestMTOMMessage();
    OMElement body = (OMElement) root.getFirstOMChild();
    OMElement data = (OMElement) body.getFirstOMChild();
    Iterator childIt = data.getChildren();
    OMElement child = (OMElement) childIt.next();
    OMText blob = (OMText) child.getFirstOMChild();
    /*
         * Following is the procedure the user has to follow to read objects in
         * OBBlob User has to know the object type & whether it is serializable.
         * If it is not he has to use a Custom Defined DataSource to get the
         * Object.
         */
    byte[] expectedObject = new byte[] { 13, 56, 65, 32, 12, 12, 7, -3, -2, -1, 98 };
    DataHandler actualDH;
    actualDH = blob.getDataHandler();
// ByteArrayInputStream object = (ByteArrayInputStream) actualDH
// .getContent();
// byte[] actualObject= null;
// object.read(actualObject,0,10);
// assertEquals("Object check", expectedObject[5],actualObject[5] );
}
Also used : Iterator(java.util.Iterator) OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement) DataHandler(javax.activation.DataHandler)

Example 43 with OMText

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

the class DigestGenerator method getDigest.

/**
 * This method is an overloaded method for the digest generation for OMElement
 *
 * @param element
 * @param digestAlgorithm
 * @return Returns a byte array representing the calculated digest value
 */
public byte[] getDigest(OMElement element, String digestAlgorithm) throws OMException {
    byte[] digest = new byte[0];
    try {
        MessageDigest md = MessageDigest.getInstance(digestAlgorithm);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        dos.writeInt(1);
        dos.write(getExpandedName(element).getBytes("UnicodeBigUnmarked"));
        dos.write((byte) 0);
        dos.write((byte) 0);
        Collection<OMAttribute> attrs = getAttributesWithoutNS(element);
        dos.writeInt(attrs.size());
        for (Iterator<OMAttribute> itr = attrs.iterator(); itr.hasNext(); ) {
            dos.write(getDigest(itr.next(), digestAlgorithm));
        }
        OMNode node = element.getFirstOMChild();
        // adjoining Texts are merged,
        // there is  no 0-length Text, and
        // comment nodes are removed.
        int length = 0;
        for (Iterator<OMNode> itr = element.getChildren(); itr.hasNext(); ) {
            OMNode child = itr.next();
            if (child instanceof OMElement || child instanceof OMText || child instanceof OMProcessingInstruction) {
                length++;
            }
        }
        dos.writeInt(length);
        while (node != null) {
            dos.write(getDigest(node, digestAlgorithm));
            node = node.getNextOMSibling();
        }
        dos.close();
        md.update(baos.toByteArray());
        digest = md.digest();
    } catch (NoSuchAlgorithmException e) {
        throw new OMException(e);
    } catch (IOException e) {
        throw new OMException(e);
    }
    return digest;
}
Also used : DataOutputStream(java.io.DataOutputStream) OMElement(org.apache.axiom.om.OMElement) ByteArrayOutputStream(java.io.ByteArrayOutputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) OMProcessingInstruction(org.apache.axiom.om.OMProcessingInstruction) OMNode(org.apache.axiom.om.OMNode) OMText(org.apache.axiom.om.OMText) MessageDigest(java.security.MessageDigest) OMAttribute(org.apache.axiom.om.OMAttribute) OMException(org.apache.axiom.om.OMException)

Example 44 with OMText

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

the class TestDiscardPartiallyBuilt method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMElement root = OMXMLBuilderFactory.createOMBuilder(factory, new StringReader("<root><element><a><b>text</b></a><c/></element><sibling/></root>")).getDocumentElement();
    OMElement element = root.getFirstElement();
    // Navigate to the text node so that the element is partially built
    OMElement b = element.getFirstElement().getFirstElement();
    OMText text = (OMText) b.getFirstOMChild();
    assertEquals("text", text.getText());
    element.discard();
    assertAbout(xml()).that(xml(OMElement.class, root)).hasSameContentAs("<root><sibling/></root>");
    // Force the builder to complete the document. If the discard method didn't adjust the
    // element depth correctly, then an exception will occur here
    assertNull(root.getNextOMSibling());
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) StringReader(java.io.StringReader) OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement)

Example 45 with OMText

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

the class TestInsertSiblingBeforeOnSelf method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMElement parent = factory.createOMElement("test", null);
    OMText text = factory.createOMText("test");
    parent.addChild(text);
    try {
        text.insertSiblingBefore(text);
        fail("Expected OMException");
    } catch (OMException ex) {
    // Expected
    }
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement) OMException(org.apache.axiom.om.OMException)

Aggregations

OMText (org.apache.axiom.om.OMText)92 OMElement (org.apache.axiom.om.OMElement)62 OMFactory (org.apache.axiom.om.OMFactory)39 DataHandler (javax.activation.DataHandler)26 OMNode (org.apache.axiom.om.OMNode)21 OMNamespace (org.apache.axiom.om.OMNamespace)10 QName (javax.xml.namespace.QName)8 OMException (org.apache.axiom.om.OMException)8 IOException (java.io.IOException)7 Iterator (java.util.Iterator)7 InputStream (java.io.InputStream)6 OMAttribute (org.apache.axiom.om.OMAttribute)6 Entry (org.apache.synapse.config.Entry)6 ArrayList (java.util.ArrayList)5 DataSource (javax.activation.DataSource)5 ByteArrayDataSource (org.apache.axiom.attachments.ByteArrayDataSource)5 OMOutputFormat (org.apache.axiom.om.OMOutputFormat)5 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)5 OutputStream (java.io.OutputStream)4 StringReader (java.io.StringReader)4