Search in sources :

Example 61 with OMNode

use of org.apache.axiom.om.OMNode in project wso2-synapse by wso2.

the class NashornJavaScriptMessageContext method addHeader.

/**
 * Add a new SOAP header to the message.
 *
 * @param mustUnderstand the value for the <code>soapenv:mustUnderstand</code> attribute
 * @param content the XML for the new header
 * @throws ScriptException if an error occurs when converting the XML to OM
 */
public void addHeader(boolean mustUnderstand, Object content) throws ScriptException {
    SOAPEnvelope envelope = mc.getEnvelope();
    SOAPFactory factory = (SOAPFactory) envelope.getOMFactory();
    SOAPHeader header = envelope.getHeader();
    if (header == null) {
        header = factory.createSOAPHeader(envelope);
    }
    OMElement element = xmlHelper.toOMElement(content);
    // We can't add the element directly to the SOAPHeader. Instead, we need to copy the
    // information over to a SOAPHeaderBlock.
    SOAPHeaderBlock headerBlock = header.addHeaderBlock(element.getLocalName(), element.getNamespace());
    for (Iterator it = element.getAllAttributes(); it.hasNext(); ) {
        headerBlock.addAttribute((OMAttribute) it.next());
    }
    headerBlock.setMustUnderstand(mustUnderstand);
    OMNode child = element.getFirstOMChild();
    while (child != null) {
        // Get the next child before addChild will detach the node from its original place.
        OMNode next = child.getNextOMSibling();
        headerBlock.addChild(child);
        child = next;
    }
}
Also used : OMNode(org.apache.axiom.om.OMNode) Iterator(java.util.Iterator) OMElement(org.apache.axiom.om.OMElement) SOAPHeaderBlock(org.apache.axiom.soap.SOAPHeaderBlock) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) SOAPFactory(org.apache.axiom.soap.SOAPFactory) SOAPHeader(org.apache.axiom.soap.SOAPHeader)

Example 62 with OMNode

use of org.apache.axiom.om.OMNode in project wso2-synapse by wso2.

the class SynapseXMLConfigurationSerializerTest method testSerializeConfiguration2.

/**
 * Test serializeConfigurationMethod with registry set for SynapseConfiguration and assert OMElement returned
 */
@Test
public void testSerializeConfiguration2() {
    SynapseXMLConfigurationSerializer serializer = new SynapseXMLConfigurationSerializer();
    SynapseConfiguration synapseConfiguration = new SynapseConfiguration();
    Map<String, OMNode> data = new HashMap<>();
    data.put(KEY_DYNAMIC_ENDPOINT_1, TestUtils.createOMElement(DYNAMIC_ENDPOINT_1));
    data.put(KEY_DYNAMIC_SEQUENCE_1, TestUtils.createOMElement(DYNAMIC_SEQUENCE_1));
    Registry registry = new SimpleInMemoryRegistry(data, 8000L);
    synapseConfiguration.setRegistry(registry);
    OMElement element = serializer.serializeConfiguration(synapseConfiguration);
    Assert.assertNotNull("OMElement is not returned", element);
    Assert.assertEquals("definitions", element.getLocalName());
    Assert.assertTrue("Registry added is not serialized.", element.getChildren().next().toString().contains("registry"));
}
Also used : OMNode(org.apache.axiom.om.OMNode) HashMap(java.util.HashMap) SimpleInMemoryRegistry(org.apache.synapse.registry.SimpleInMemoryRegistry) OMElement(org.apache.axiom.om.OMElement) Registry(org.apache.synapse.registry.Registry) SimpleInMemoryRegistry(org.apache.synapse.registry.SimpleInMemoryRegistry) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) Test(org.junit.Test)

Example 63 with OMNode

use of org.apache.axiom.om.OMNode in project wso2-synapse by wso2.

the class Target method insert.

public void insert(MessageContext synContext, ArrayList<OMNode> sourceNodeList, SynapseLog synLog) throws JaxenException {
    if (targetType == EnrichMediator.CUSTOM) {
        assert xpath != null : "Xpath cannot be null for CUSTOM";
        if (sourceNodeList.isEmpty()) {
            synLog.error("Cannot Enrich message from an empty source.");
            return;
        }
        Object targetObj = xpath.selectSingleNode(synContext);
        // if the type custom is used to enrich a property, It'll be handled in a different method
        if (xpath.getExpression().startsWith(SynapseXPathConstants.GET_PROPERTY_FUNCTION)) {
            this.handleProperty((SynapseXPath) xpath, synContext, sourceNodeList, synLog);
        } else {
            if (targetObj instanceof SOAPHeaderImpl) {
                OMElement targetElem = (OMElement) targetObj;
                ArrayList<OMNode> headerSourceNodeList = new ArrayList<>();
                for (OMNode o : sourceNodeList) {
                    OMElement ins = ((OMElement) o).cloneOMElement();
                    SOAPFactory fac = (SOAPFactory) synContext.getEnvelope().getOMFactory();
                    try {
                        headerSourceNodeList.add(ElementHelper.toSOAPHeaderBlock(ins, fac));
                    } catch (Exception e) {
                        log.error("Error occurred while transforming the OMElement to SOAPHeaderBlock ", e);
                        throw new JaxenException(e);
                    }
                }
                insertElement(headerSourceNodeList, targetElem, synLog);
            } else if (targetObj instanceof OMElement) {
                OMElement targetElem = (OMElement) targetObj;
                insertElement(sourceNodeList, targetElem, synLog);
            } else if (targetObj instanceof OMText) {
                OMText targetText = (OMText) targetObj;
                if (sourceNodeList.get(0) instanceof OMText) {
                    if (targetText.getParent() != null) {
                        Object parent = targetText.getParent();
                        if (parent instanceof OMElement) {
                            ((OMElement) parent).setText(((OMText) sourceNodeList.get(0)).getText());
                        }
                    }
                } else if (sourceNodeList.get(0) instanceof OMElement) {
                    Object targetParent = targetText.getParent();
                    if (targetParent instanceof OMElement) {
                        targetText.detach();
                        synchronized (sourceNodeList.get(0)) {
                            ((OMElement) targetParent).addChild(sourceNodeList.get(0));
                        }
                    }
                }
            } else if (targetObj instanceof OMAttribute) {
                OMAttribute attribute = (OMAttribute) targetObj;
                attribute.setAttributeValue(((OMText) sourceNodeList.get(0)).getText());
            } else {
                synLog.error("Invalid Target object to be enrich.");
                throw new SynapseException("Invalid Target object to be enrich.");
            }
        }
        removeOutdatedJsonStream(((Axis2MessageContext) synContext).getAxis2MessageContext());
    } else if (targetType == EnrichMediator.BODY) {
        SOAPEnvelope env = synContext.getEnvelope();
        SOAPBody body = env.getBody();
        OMElement e = body.getFirstElement();
        if (e != null) {
            insertElementToBody(sourceNodeList, e, synLog, synContext);
        } else {
            // if the body is empty just add as a child
            for (OMNode elem : sourceNodeList) {
                if (elem instanceof OMElement) {
                    synchronized (elem) {
                        body.addChild(elem);
                    }
                } else {
                    synLog.error("Invalid Object type to be inserted into message body");
                }
            }
            removeOutdatedJsonStream(((Axis2MessageContext) synContext).getAxis2MessageContext());
        }
    } else if (targetType == EnrichMediator.ENVELOPE) {
        OMNode node = sourceNodeList.get(0);
        if (node instanceof SOAPEnvelope) {
            try {
                synContext.setEnvelope((SOAPEnvelope) node);
            } catch (AxisFault axisFault) {
                synLog.error("Failed to set the SOAP Envelope");
                throw new SynapseException("Failed to set the SOAP Envelope");
            }
        } else {
            synLog.error("SOAPEnvelope is expected");
            throw new SynapseException("A SOAPEnvelope is expected");
        }
        removeOutdatedJsonStream(((Axis2MessageContext) synContext).getAxis2MessageContext());
    } else if (targetType == EnrichMediator.PROPERTY) {
        assert property != null : "Property cannot be null for PROPERTY type";
        if (action != null && property != null) {
            Object propertyObj = synContext.getProperty(property);
            OMElement documentElement = null;
            try {
                if (isOMElement(propertyObj)) {
                    documentElement = (OMElement) propertyObj;
                } else {
                    documentElement = AXIOMUtil.stringToOM((String) propertyObj);
                }
            } catch (Exception e1) {
            // just ignoring the phaser error
            }
            if (documentElement != null && action.equals(ACTION_ADD_CHILD)) {
                // logic should valid only when adding child elements, and other cases
                // such as sibling and replacement using the else condition
                insertElement(sourceNodeList, documentElement, synLog);
                if (isOMElement(propertyObj)) {
                    synContext.setProperty(property, documentElement);
                } else {
                    synContext.setProperty(property, documentElement.getText());
                }
            } else {
                synContext.setProperty(property, sourceNodeList);
            }
        } else {
            synContext.setProperty(property, sourceNodeList);
        }
    }
}
Also used : AxisFault(org.apache.axis2.AxisFault) SynapseException(org.apache.synapse.SynapseException) ArrayList(java.util.ArrayList) OMElement(org.apache.axiom.om.OMElement) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) SOAPHeaderImpl(org.apache.axiom.soap.impl.llom.SOAPHeaderImpl) SOAPFactory(org.apache.axiom.soap.SOAPFactory) JaxenException(org.jaxen.JaxenException) JsonSyntaxException(com.google.gson.JsonSyntaxException) SynapseException(org.apache.synapse.SynapseException) IOException(java.io.IOException) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) OMNode(org.apache.axiom.om.OMNode) SOAPBody(org.apache.axiom.soap.SOAPBody) JaxenException(org.jaxen.JaxenException) OMText(org.apache.axiom.om.OMText) JsonObject(com.google.gson.JsonObject) OMAttribute(org.apache.axiom.om.OMAttribute) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 64 with OMNode

use of org.apache.axiom.om.OMNode in project wso2-synapse by wso2.

the class EIPUtils method getDetachedMatchingElements.

/**
 * Return the set of detached elements specified by the XPath over the given envelope
 *
 * @param envelope SOAPEnvelope from which the elements will be extracted
 * @param expression SynapseXPath expression describing the elements to be extracted
 * @return List detached OMElements in the envelope matching the expression
 * @throws JaxenException if the XPath expression evaluation fails
 */
public static List<OMNode> getDetachedMatchingElements(SOAPEnvelope envelope, MessageContext synCtxt, SynapseXPath expression) throws JaxenException {
    List<OMNode> elementList = new ArrayList<OMNode>();
    Object o = expression.evaluate(envelope, synCtxt);
    if (o instanceof OMNode) {
        elementList.add(((OMNode) o).detach());
    } else if (o instanceof List) {
        for (Object elem : (List) o) {
            if (elem instanceof OMNode) {
                elementList.add(((OMNode) elem).detach());
            }
        }
    }
    return elementList;
}
Also used : OMNode(org.apache.axiom.om.OMNode) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) ArrayList(java.util.ArrayList) List(java.util.List)

Example 65 with OMNode

use of org.apache.axiom.om.OMNode in project wso2-synapse by wso2.

the class AbstractRegistry method getFormat.

/**
 * @param entry
 * @return the OMElement of the format from the configuration
 */
public OMElement getFormat(Entry entry) {
    OMNode omNode;
    omNode = lookupFormat(entry.getKey());
    return (OMElement) omNode;
}
Also used : OMNode(org.apache.axiom.om.OMNode) OMElement(org.apache.axiom.om.OMElement)

Aggregations

OMNode (org.apache.axiom.om.OMNode)150 OMElement (org.apache.axiom.om.OMElement)87 Iterator (java.util.Iterator)28 OMText (org.apache.axiom.om.OMText)23 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)20 BXMLItem (org.ballerinalang.model.values.BXMLItem)18 StringReader (java.io.StringReader)17 SynapseException (org.apache.synapse.SynapseException)15 OMAttribute (org.apache.axiom.om.OMAttribute)14 OMFactory (org.apache.axiom.om.OMFactory)14 BValue (org.ballerinalang.model.values.BValue)14 BXML (org.ballerinalang.model.values.BXML)14 Test (org.testng.annotations.Test)14 QName (javax.xml.namespace.QName)13 BJSON (org.ballerinalang.model.values.BJSON)13 OMDocument (org.apache.axiom.om.OMDocument)12 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)10 SOAPHeader (org.apache.axiom.soap.SOAPHeader)10 SOAPHeaderBlock (org.apache.axiom.soap.SOAPHeaderBlock)10