Search in sources :

Example 1 with SynapseJsonPath

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

the class Value method getExpression.

/**
 * Retrieving dynamic key
 *
 * @return SynapsePath
 */
public SynapsePath getExpression() {
    if (expression == null && keyValue != null && hasExprTypeKey()) {
        try {
            String expressionString = keyValue.substring(1, keyValue.length() - 1);
            if (expressionString.startsWith("json-eval(")) {
                // Remove "json-eval" and extract the json expression
                SynapseJsonPath expressionTypeKey = new SynapseJsonPath(expressionString.substring(10, expressionString.length() - 1));
                expression = expressionTypeKey;
            } else {
                SynapseXPath expressionTypeKey = new SynapseXPath(expressionString);
                for (OMNamespace aNamespaceList : namespaceList) {
                    expressionTypeKey.addNamespace(aNamespaceList);
                }
                expression = expressionTypeKey;
            }
        } catch (JaxenException e) {
            expression = null;
            handleException("Can not evaluate escaped expression..");
        }
    }
    return expression;
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) SynapseJsonPath(org.apache.synapse.util.xpath.SynapseJsonPath) JaxenException(org.jaxen.JaxenException)

Example 2 with SynapseJsonPath

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

the class ValueFactory method createTextValue.

/**
 * Create a key instance
 *
 * @param elem OMElement
 * @return Key
 */
public Value createTextValue(OMElement elem) {
    Value key = null;
    // OMAttribute attKey = elem.getAttribute(new QName(name));
    String textValue = elem.getText();
    if (textValue != null) {
        boolean hasEscape = isEscapedExpression(textValue);
        if (!hasEscape && isDynamicKey(textValue)) {
            // Filter json-eval expressions
            if (textValue.startsWith("{json-eval(")) {
                // Get the JSON expression in-between "{}"
                textValue = textValue.substring(1, textValue.length() - 1);
                SynapseJsonPath synJsonPath = createSynJsonPath(textValue);
                key = new Value(synJsonPath);
            } else {
                SynapseXPath synXpath = createSynXpath(elem, textValue);
                key = new Value(synXpath);
            }
        } else if (hasEscape) {
            /**
             * escaped expr
             */
            key = new Value(getEscapedExpression(textValue));
            key.setNamespaces(elem);
        } else {
            /**
             * static key
             */
            key = new Value(textValue);
        }
    } else {
        handleException("Text value is required for the element '" + elem.getLocalName() + "'");
    }
    return key;
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) SynapseJsonPath(org.apache.synapse.util.xpath.SynapseJsonPath) Value(org.apache.synapse.mediators.Value)

Example 3 with SynapseJsonPath

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

the class SynapsePathFactory method getSynapsePath.

public static SynapsePath getSynapsePath(OMElement elem, QName attribName) throws JaxenException {
    SynapsePath path = null;
    OMAttribute pathAttrib = elem.getAttribute(attribName);
    if (pathAttrib != null && pathAttrib.getAttributeValue() != null) {
        if (pathAttrib.getAttributeValue().startsWith("json-eval(")) {
            path = new SynapseJsonPath(pathAttrib.getAttributeValue().substring(10, pathAttrib.getAttributeValue().length() - 1));
        } else {
            try {
                path = new SynapseXPath(pathAttrib.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: " + pathAttrib.getAttributeValue());
                    }
                    path = new SynapseXPath(pathAttrib.getAttributeValue(), elem);
                } else {
                    throw ex;
                }
            }
        }
        OMElementUtils.addNameSpaces(path, elem, log);
        path.addNamespacesForFallbackProcessing(elem);
    } else {
        handleException("Couldn't find the XPath attribute with the QName : " + attribName.toString() + " in the element : " + elem.toString());
    }
    return path;
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) SynapseJsonPath(org.apache.synapse.util.xpath.SynapseJsonPath) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 4 with SynapseJsonPath

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

the class ValueFactory method createSynJsonPath.

/**
 * Create synapse jsonpath expression
 *
 * @param key jsonpath expression eg: json-eval($.info)
 * @return SynapseJsonPath
 */
public SynapseJsonPath createSynJsonPath(String key) {
    // Derive JsonPath Expression from key removing "json-eval(" & ")"
    String jsonPathExpr = key.trim().substring(10, key.length() - 1);
    SynapseJsonPath synapseJsonPath = null;
    try {
        synapseJsonPath = SynapseJsonPathFactory.getSynapseJsonPath(jsonPathExpr);
    } catch (JaxenException e) {
        handleException("Can not create SynapseJsonPath from given: " + key);
    }
    return synapseJsonPath;
}
Also used : SynapseJsonPath(org.apache.synapse.util.xpath.SynapseJsonPath) JaxenException(org.jaxen.JaxenException)

Example 5 with SynapseJsonPath

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

the class ValueFactory method createValue.

/**
 * Create a key instance
 *
 * @param elem OMElement
 * @return Key
 */
public Value createValue(String name, OMElement elem) {
    Value key = null;
    OMAttribute attKey = elem.getAttribute(new QName(name));
    if (attKey != null) {
        String attributeValue = attKey.getAttributeValue();
        boolean hasEscape = isEscapedExpression(attributeValue);
        if (!hasEscape && isDynamicKey(attributeValue)) {
            // Filter json-eval expressions
            if (attributeValue.startsWith("{json-eval(")) {
                // Get the expression in-between "{}"
                attributeValue = attributeValue.substring(1, attributeValue.length() - 1);
                SynapseJsonPath synJsonPath = createSynJsonPath(attributeValue);
                key = new Value(synJsonPath);
            } else {
                SynapseXPath synXpath = createSynXpath(elem, attributeValue);
                key = new Value(synXpath);
            }
        } else if (hasEscape) {
            /**
             * escaped expr
             */
            key = new Value(getEscapedExpression(attributeValue));
            key.setNamespaces(elem);
        } else {
            /**
             * static key
             */
            key = new Value(attributeValue);
        }
    } else {
        handleException("The '" + name + "' attribute is required for the element '" + elem.getLocalName() + "'");
    }
    return key;
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) QName(javax.xml.namespace.QName) SynapseJsonPath(org.apache.synapse.util.xpath.SynapseJsonPath) Value(org.apache.synapse.mediators.Value) OMAttribute(org.apache.axiom.om.OMAttribute)

Aggregations

SynapseJsonPath (org.apache.synapse.util.xpath.SynapseJsonPath)6 SynapseXPath (org.apache.synapse.util.xpath.SynapseXPath)4 Value (org.apache.synapse.mediators.Value)3 OMAttribute (org.apache.axiom.om.OMAttribute)2 JaxenException (org.jaxen.JaxenException)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ProcessingException (com.github.fge.jsonschema.core.exceptions.ProcessingException)1 ProcessingMessage (com.github.fge.jsonschema.core.report.ProcessingMessage)1 ProcessingReport (com.github.fge.jsonschema.core.report.ProcessingReport)1 JsonSchema (com.github.fge.jsonschema.main.JsonSchema)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 QName (javax.xml.namespace.QName)1 Source (javax.xml.transform.Source)1 StreamSource (javax.xml.transform.stream.StreamSource)1 Schema (javax.xml.validation.Schema)1 Validator (javax.xml.validation.Validator)1 OMTextImpl (org.apache.axiom.om.impl.llom.OMTextImpl)1 SynapseException (org.apache.synapse.SynapseException)1 SynapseLog (org.apache.synapse.SynapseLog)1