Search in sources :

Example 31 with SynapseXPath

use of org.apache.synapse.util.xpath.SynapseXPath in project wso2-synapse by wso2.

the class MessageStoreSerializer method getParameter.

/**
 * Will get the parameter OMElement.
 *
 * @param messageStore the message store definition metadata.
 * @param name         the parameter key.
 * @return the parameter OMElement.
 */
private static OMElement getParameter(MessageStore messageStore, String name) {
    Object paramValue = messageStore.getParameters().get(name);
    OMElement property = null;
    if (paramValue instanceof String) {
        String value = (String) paramValue;
        property = fac.createOMElement("parameter", synNS);
        property.addAttribute(fac.createOMAttribute("name", nullNS, name));
        property.setText(value.trim());
    } else if (paramValue instanceof SynapseXPath) {
        SynapseXPath value = (SynapseXPath) paramValue;
        String expression = value.getExpression();
        Map namespaces = value.getNamespaces();
        property = fac.createOMElement("parameter", synNS);
        property.addAttribute(fac.createOMAttribute("name", nullNS, name));
        property.addAttribute(fac.createOMAttribute("expression", nullNS, expression));
        Set<Map.Entry<String, String>> nameSpaceAttributes = namespaces.entrySet();
        for (Map.Entry<String, String> nameSpaceElement : nameSpaceAttributes) {
            String prefix = nameSpaceElement.getKey();
            String uri = nameSpaceElement.getValue();
            property.declareNamespace(uri, prefix);
        }
    }
    return property;
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) Set(java.util.Set) OMElement(org.apache.axiom.om.OMElement) Map(java.util.Map)

Example 32 with SynapseXPath

use of org.apache.synapse.util.xpath.SynapseXPath in project wso2-synapse by wso2.

the class SynapsePathFactory method getSynapsePath.

public static SynapsePath getSynapsePath(OMElement elem, String expression) throws JaxenException {
    if (expression == null) {
        handleException("XPath expression cannot be null");
    }
    SynapseXPath xpath = new SynapseXPath(expression);
    OMElementUtils.addNameSpaces(xpath, elem, log);
    return xpath;
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath)

Example 33 with SynapseXPath

use of org.apache.synapse.util.xpath.SynapseXPath in project wso2-synapse by wso2.

the class SynapseXPathFactory method getSynapseXPath.

public static SynapseXPath getSynapseXPath(OMElement elem, QName attribName) throws JaxenException {
    SynapseXPath xpath = null;
    OMAttribute xpathAttrib = elem.getAttribute(attribName);
    if (xpathAttrib != null && xpathAttrib.getAttributeValue() != null) {
        try {
            xpath = new SynapseXPath(xpathAttrib.getAttributeValue());
        } catch (org.jaxen.XPathSyntaxException ex) {
            /* Try and see whether the expression can be compiled with XPath 2.0
                     * This will only be done if the failover DOM XPath 2.0 is enabled */
            if (Boolean.parseBoolean(SynapsePropertiesLoader.loadSynapseProperties().getProperty(SynapseConstants.FAIL_OVER_DOM_XPATH_PROCESSING))) {
                if (log.isDebugEnabled()) {
                    log.debug("Trying to compile the expression in XPath 2.0:" + xpathAttrib.getAttributeValue());
                }
                xpath = new SynapseXPath(xpathAttrib.getAttributeValue(), elem);
            } else {
                throw ex;
            }
        }
        OMElementUtils.addNameSpaces(xpath, elem, log);
        xpath.addNamespacesForFallbackProcessing(elem);
    } else {
        handleException("Couldn't find the XPath attribute with the QName : " + attribName.toString() + " in the element : " + elem.toString());
    }
    return xpath;
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 34 with SynapseXPath

use of org.apache.synapse.util.xpath.SynapseXPath in project wso2-synapse by wso2.

the class POJOCommandMediatorFactory method handlePropertyAction.

private void handlePropertyAction(String name, OMElement propElem, POJOCommandMediator m) {
    OMAttribute valueAttr = propElem.getAttribute(ATT_VALUE);
    OMAttribute exprAttr = propElem.getAttribute(ATT_EXPRN);
    OMAttribute ctxNameAttr = propElem.getAttribute(ATT_CTXNAME);
    OMAttribute actionAttr = propElem.getAttribute(ATT_ACTION);
    SynapseXPath xpath = null;
    try {
        if (exprAttr != null) {
            xpath = SynapseXPathFactory.getSynapseXPath(propElem, ATT_EXPRN);
        }
    } catch (JaxenException e) {
        handleException("Error in building the expression as an SynapseXPath" + e);
    }
    // if there is a value attribute there is no action (action is implied as read value)
    if (valueAttr != null) {
        String value = valueAttr.getAttributeValue();
        // all other three attributes can not co-exists
        if (exprAttr != null && ctxNameAttr != null) {
            handleException("Command properties can not contain all three 'value', " + "'expression' and 'context-name' attributes. Only one or " + "combination of two can be there.");
        } else {
            m.addStaticSetterProperty(name, value);
            if (exprAttr != null) {
                // action ==> ReadValueAndUpdateMesssage
                m.addMessageGetterProperty(name, xpath);
            } else if (ctxNameAttr != null) {
                // action ==> ReadValueAndUpdateContext
                m.addContextGetterProperty(name, ctxNameAttr.getAttributeValue());
            }
        // else the action ==> ReadValue
        }
    } else if (propElem.getFirstElement() != null) {
        // all other two attributes can not co-exists
        if (exprAttr != null && ctxNameAttr != null) {
            handleException("Command properties can not contain all the " + "'expression' and 'context-name' attributes with a child. Only one " + "attribute of those can co-exists with a child");
        } else {
            m.addStaticSetterProperty(name, propElem.getFirstElement());
            if (exprAttr != null) {
                // action ==> ReadValueAndUpdateMesssage
                m.addMessageGetterProperty(name, xpath);
            } else if (ctxNameAttr != null) {
                // action ==> ReadValueAndUpdateContext
                m.addContextGetterProperty(name, ctxNameAttr.getAttributeValue());
            }
        // else the action ==> ReadValue
        }
    } else {
        // if both context-name and expression is there
        if (exprAttr != null && ctxNameAttr != null) {
            if (actionAttr != null && actionAttr.getAttributeValue() != null) {
                String action = actionAttr.getAttributeValue();
                if (RM_ACTION.equals(action) || UC_ACTION.equals(action)) {
                    // action ==> ReadMessageAndUpdateContext
                    m.addMessageSetterProperty(name, xpath);
                    m.addContextGetterProperty(name, ctxNameAttr.getAttributeValue());
                } else if (RC_ACTION.equals(action) || UM_ACTION.equals(action)) {
                    // action ==> ReadContextAndUpdateMessage
                    m.addContextSetterProperty(name, ctxNameAttr.getAttributeValue());
                    m.addMessageGetterProperty(name, xpath);
                } else {
                    handleException("Invalid action for " + "the command property with the name " + name);
                }
            } else {
                handleException("Action attribute " + "is required for the command property with name " + name);
            }
        } else {
            // only one of expression or context-name is present
            if (actionAttr != null && actionAttr.getAttributeValue() != null) {
                String action = actionAttr.getAttributeValue();
                if (exprAttr != null) {
                    if (RM_ACTION.equals(action)) {
                        // action ==> ReadMessage
                        m.addMessageSetterProperty(name, xpath);
                    } else if (UM_ACTION.equals(action)) {
                        // action ==> UpdateMessage
                        m.addMessageGetterProperty(name, xpath);
                    } else if (RAUM_ACTION.equals(action)) {
                        // action ==> ReadAndUpdateMessage
                        m.addMessageSetterProperty(name, xpath);
                        m.addMessageGetterProperty(name, xpath);
                    } else {
                        handleException("Invalid action for " + "the command property with the name " + name);
                    }
                } else if (ctxNameAttr != null) {
                    String ctxName = ctxNameAttr.getAttributeValue();
                    if (RC_ACTION.equals(action)) {
                        // action ==> ReadContext
                        m.addContextSetterProperty(name, ctxName);
                    } else if (UC_ACTION.equals(action)) {
                        // action ==> UpdateContext
                        m.addContextGetterProperty(name, ctxName);
                    } else if (RAUC_ACTION.equals(action)) {
                        // action ==> ReadAndUpdateContext
                        m.addContextSetterProperty(name, ctxName);
                        m.addContextGetterProperty(name, ctxName);
                    } else {
                        handleException("Invalid action for " + "the command property with the name " + name);
                    }
                } else {
                    handleException("Unrecognized command property with the name " + name);
                }
            } else {
                // action ==> ReadAndUpdateMessage/Context
                if (exprAttr != null) {
                    m.addMessageSetterProperty(name, xpath);
                    m.addMessageGetterProperty(name, xpath);
                } else if (ctxNameAttr != null) {
                    String ctxName = ctxNameAttr.getAttributeValue();
                    m.addContextSetterProperty(name, ctxName);
                    m.addContextGetterProperty(name, ctxName);
                } else {
                    handleException("Unrecognized command property with the name " + name);
                }
            }
        }
    }
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) JaxenException(org.jaxen.JaxenException) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 35 with SynapseXPath

use of org.apache.synapse.util.xpath.SynapseXPath in project wso2-synapse by wso2.

the class ValueFactory method createSynXpath.

/**
 * Create synapse xpath expression
 * {} type user input is used to create real xpath expression
 *
 * @param elem the element
 * @param key xpath expression with {}
 * @return SynapseXpath
 */
public SynapseXPath createSynXpath(OMElement elem, String key) {
    // derive XPath Expression from key
    String xpathExpr = key.trim().substring(1, key.length() - 1);
    SynapseXPath synapseXPath = null;
    try {
        synapseXPath = SynapseXPathFactory.getSynapseXPath(elem, xpathExpr);
    } catch (JaxenException e) {
        handleException("Can not create Synapse Xpath from given key");
    }
    return synapseXPath;
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) JaxenException(org.jaxen.JaxenException)

Aggregations

SynapseXPath (org.apache.synapse.util.xpath.SynapseXPath)68 MessageContext (org.apache.synapse.MessageContext)24 JaxenException (org.jaxen.JaxenException)20 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)15 OMElement (org.apache.axiom.om.OMElement)14 OMAttribute (org.apache.axiom.om.OMAttribute)11 Value (org.apache.synapse.mediators.Value)9 TestMessageContext (org.apache.synapse.TestMessageContext)8 Pattern (java.util.regex.Pattern)6 Iterator (java.util.Iterator)5 QName (javax.xml.namespace.QName)5 EndpointReference (org.apache.axis2.addressing.EndpointReference)4 SynapseException (org.apache.synapse.SynapseException)4 TestMessageContextBuilder (org.apache.synapse.TestMessageContextBuilder)4 SynapseJsonPath (org.apache.synapse.util.xpath.SynapseJsonPath)4 Method (java.lang.reflect.Method)3 Map (java.util.Map)3 MediatorProperty (org.apache.synapse.mediators.MediatorProperty)3 Field (java.lang.reflect.Field)2 List (java.util.List)2