Search in sources :

Example 46 with SynapseXPath

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

the class ReplaceMediatorFactory method createSpecificMediator.

public ReplaceMediator createSpecificMediator(OMElement elem, Properties properties) {
    ReplaceMediator mediator = new ReplaceMediator();
    processAuditStatus(mediator, elem);
    OMAttribute attTarget = elem.getAttribute(ATT_TARGET);
    OMAttribute attProperty = elem.getAttribute(ATT_PROPERTY);
    if (attTarget != null) {
        try {
            mediator.setTarget(new SynapseXPath(attTarget));
        } catch (JaxenException e) {
            handleException("Invalid XPath specified for the target attribute : " + attTarget.getAttributeValue());
        }
    }
    if (attProperty != null) {
        mediator.setProperty(attProperty.getAttributeValue());
    } else {
        handleException("The 'property' attribute is required for the replace mediator");
    }
    return mediator;
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) JaxenException(org.jaxen.JaxenException) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 47 with SynapseXPath

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

the class EndpointDefinitionFactory method createDefinition.

/**
 * Extracts the QoS information from the XML which represents a WSDL/Address/Default endpoints
 *
 * @param elem XML which represents the endpoint with QoS information
 * @return the created endpoint definition
 */
public EndpointDefinition createDefinition(OMElement elem) {
    EndpointDefinition definition = new EndpointDefinition();
    OMAttribute optimize = elem.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "optimize"));
    OMAttribute encoding = elem.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "encoding"));
    if (optimize != null && optimize.getAttributeValue().length() > 0) {
        String method = optimize.getAttributeValue().trim();
        if ("mtom".equalsIgnoreCase(method)) {
            definition.setUseMTOM(true);
        } else if ("swa".equalsIgnoreCase(method)) {
            definition.setUseSwa(true);
        }
    }
    if (encoding != null && encoding.getAttributeValue() != null) {
        definition.setCharSetEncoding(encoding.getAttributeValue());
    }
    OMElement wsAddr = elem.getFirstChildWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "enableAddressing"));
    if (wsAddr != null) {
        definition.setAddressingOn(true);
        OMAttribute version = wsAddr.getAttribute(new QName("version"));
        if (version != null && version.getAttributeValue() != null) {
            String versionValue = version.getAttributeValue().trim().toLowerCase();
            if (SynapseConstants.ADDRESSING_VERSION_FINAL.equals(versionValue) || SynapseConstants.ADDRESSING_VERSION_SUBMISSION.equals(versionValue)) {
                definition.setAddressingVersion(version.getAttributeValue());
            } else {
                handleException("Unknown value for the addressing version. Possible values " + "for the addressing version are 'final' and 'submission' only.");
            }
        }
        String useSepList = wsAddr.getAttributeValue(new QName("separateListener"));
        if (useSepList != null) {
            if ("true".equals(useSepList.trim().toLowerCase())) {
                definition.setUseSeparateListener(true);
            }
        }
    }
    OMElement wsSec = elem.getFirstChildWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "enableSec"));
    if (wsSec != null) {
        definition.setSecurityOn(true);
        OMAttribute policyKey = wsSec.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "policy"));
        OMAttribute inboundPolicyKey = wsSec.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "inboundPolicy"));
        OMAttribute outboundPolicyKey = wsSec.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "outboundPolicy"));
        // Checks whether the specified policy is dynamic or static and modify the endpoint definition accordingly
        if (policyKey != null && policyKey.getAttributeValue() != null) {
            String p = policyKey.getAttributeValue();
            Pattern pattern = Pattern.compile("\\{.*\\}");
            if (pattern.matcher(p).matches()) {
                try {
                    p = p.trim().substring(1, p.length() - 1);
                    SynapseXPath xpath = null;
                    xpath = new SynapseXPath(p);
                    definition.setDynamicPolicy(xpath);
                    definition.setWsSecPolicyKey(p);
                } catch (JaxenException e) {
                    handleException("Couldn't assign dynamic endpoint policy as Synapse expression");
                }
            } else {
                String wsSecPolicy = p.trim();
                definition.setWsSecPolicyKey(wsSecPolicy);
            }
        } else {
            if (inboundPolicyKey != null && inboundPolicyKey.getAttributeValue() != null) {
                definition.setInboundWsSecPolicyKey(inboundPolicyKey.getAttributeValue());
            }
            if (outboundPolicyKey != null && outboundPolicyKey.getAttributeValue() != null) {
                definition.setOutboundWsSecPolicyKey(outboundPolicyKey.getAttributeValue());
            }
        }
    }
    // set the timeout configuration
    OMElement timeout = elem.getFirstChildWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "timeout"));
    if (timeout != null) {
        OMElement duration = timeout.getFirstChildWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "duration"));
        if (duration != null) {
            String d = duration.getText();
            if (d != null) {
                try {
                    Pattern pattern = Pattern.compile("\\{.*\\}");
                    if (pattern.matcher(d).matches()) {
                        d = d.trim().substring(1, d.length() - 1);
                        SynapseXPath xpath = new SynapseXPath(d);
                        definition.setDynamicTimeoutExpression(xpath);
                    } else {
                        long timeoutMilliSeconds = Long.parseLong(d.trim());
                        definition.setTimeoutDuration(timeoutMilliSeconds);
                    }
                } catch (NumberFormatException e) {
                    handleException("Endpoint timeout duration expected as a " + "number but was not a number");
                } catch (JaxenException e) {
                    handleException("Couldn't assign dynamic endpoint timeout as Synapse expression");
                }
            }
        }
        OMElement action = timeout.getFirstChildWithName(new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "responseAction"));
        if (action != null && action.getText() != null) {
            String actionString = action.getText();
            if ("discard".equalsIgnoreCase(actionString.trim())) {
                definition.setTimeoutAction(SynapseConstants.DISCARD);
            } else if ("fault".equalsIgnoreCase(actionString.trim())) {
                definition.setTimeoutAction(SynapseConstants.DISCARD_AND_FAULT);
            } else {
                handleException("Invalid timeout action, action : " + actionString + " is not supported");
            }
        }
    }
    OMElement markAsTimedOut = elem.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, XMLConfigConstants.MARK_FOR_SUSPENSION));
    if (markAsTimedOut != null) {
        OMElement timeoutCodes = markAsTimedOut.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, XMLConfigConstants.ERROR_CODES));
        if (timeoutCodes != null && timeoutCodes.getText() != null) {
            StringTokenizer st = new StringTokenizer(timeoutCodes.getText().trim(), ", ");
            while (st.hasMoreTokens()) {
                String s = st.nextToken();
                try {
                    definition.addTimeoutErrorCode(Integer.parseInt(s));
                } catch (NumberFormatException e) {
                    handleException("The timeout error codes should be specified " + "as valid numbers separated by commas : " + timeoutCodes.getText(), e);
                }
            }
        }
        OMElement retriesBeforeSuspend = markAsTimedOut.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, XMLConfigConstants.RETRIES_BEFORE_SUSPENSION));
        if (retriesBeforeSuspend != null && retriesBeforeSuspend.getText() != null) {
            try {
                definition.setRetriesOnTimeoutBeforeSuspend(Integer.parseInt(retriesBeforeSuspend.getText().trim()));
            } catch (NumberFormatException e) {
                handleException("The retries before suspend [for timeouts] should be " + "specified as a valid number : " + retriesBeforeSuspend.getText(), e);
            }
        }
        OMElement retryDelay = markAsTimedOut.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, XMLConfigConstants.RETRY_DELAY));
        if (retryDelay != null && retryDelay.getText() != null) {
            try {
                definition.setRetryDurationOnTimeout(Integer.parseInt(retryDelay.getText().trim()));
            } catch (NumberFormatException e) {
                handleException("The retry delay for timeouts should be specified " + "as a valid number : " + retryDelay.getText(), e);
            }
        }
    }
    // support backwards compatibility with Synapse 1.2 - for suspendDurationOnFailure
    OMElement suspendDurationOnFailure = elem.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, "suspendDurationOnFailure"));
    if (suspendDurationOnFailure != null && suspendDurationOnFailure.getText() != null) {
        log.warn("Configuration uses deprecated style for endpoint 'suspendDurationOnFailure'");
        try {
            definition.setInitialSuspendDuration(1000 * Long.parseLong(suspendDurationOnFailure.getText().trim()));
            definition.setSuspendProgressionFactor((float) 1.0);
        } catch (NumberFormatException e) {
            handleException("The initial suspend duration should be specified " + "as a valid number : " + suspendDurationOnFailure.getText(), e);
        }
    }
    OMElement suspendOnFailure = elem.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, XMLConfigConstants.SUSPEND_ON_FAILURE));
    if (suspendOnFailure != null) {
        OMElement suspendCodes = suspendOnFailure.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, XMLConfigConstants.ERROR_CODES));
        if (suspendCodes != null && suspendCodes.getText() != null) {
            StringTokenizer st = new StringTokenizer(suspendCodes.getText().trim(), ", ");
            while (st.hasMoreTokens()) {
                String s = st.nextToken();
                try {
                    definition.addSuspendErrorCode(Integer.parseInt(s));
                } catch (NumberFormatException e) {
                    handleException("The suspend error codes should be specified " + "as valid numbers separated by commas : " + suspendCodes.getText(), e);
                }
            }
        }
        OMElement initialDuration = suspendOnFailure.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, XMLConfigConstants.SUSPEND_INITIAL_DURATION));
        if (initialDuration != null && initialDuration.getText() != null) {
            try {
                definition.setInitialSuspendDuration(Integer.parseInt(initialDuration.getText().trim()));
            } catch (NumberFormatException e) {
                handleException("The initial suspend duration should be specified " + "as a valid number : " + initialDuration.getText(), e);
            }
        }
        OMElement progressionFactor = suspendOnFailure.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, XMLConfigConstants.SUSPEND_PROGRESSION_FACTOR));
        if (progressionFactor != null && progressionFactor.getText() != null) {
            try {
                definition.setSuspendProgressionFactor(Float.parseFloat(progressionFactor.getText().trim()));
            } catch (NumberFormatException e) {
                handleException("The suspend duration progression factor should be specified " + "as a valid float : " + progressionFactor.getText(), e);
            }
        }
        OMElement maximumDuration = suspendOnFailure.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, XMLConfigConstants.SUSPEND_MAXIMUM_DURATION));
        if (maximumDuration != null && maximumDuration.getText() != null) {
            try {
                definition.setSuspendMaximumDuration(Long.parseLong(maximumDuration.getText().trim()));
            } catch (NumberFormatException e) {
                handleException("The maximum suspend duration should be specified " + "as a valid number : " + maximumDuration.getText(), e);
            }
        }
    }
    OMElement retryConfig = elem.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, XMLConfigConstants.RETRY_CONFIG));
    if (retryConfig != null) {
        OMElement retryDisabledErrorCodes = retryConfig.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, "disabledErrorCodes"));
        if (retryDisabledErrorCodes != null && retryDisabledErrorCodes.getText() != null) {
            StringTokenizer st = new StringTokenizer(retryDisabledErrorCodes.getText().trim(), ", ");
            while (st.hasMoreTokens()) {
                String s = st.nextToken();
                try {
                    definition.addRetryDisabledErrorCode(Integer.parseInt(s));
                } catch (NumberFormatException e) {
                    handleException("The suspend error codes should be specified as valid " + "numbers separated by commas : " + retryDisabledErrorCodes.getText(), e);
                }
            }
        }
        OMElement retryEnabledErrorCodes = retryConfig.getFirstChildWithName(new QName(SynapseConstants.SYNAPSE_NAMESPACE, "enabledErrorCodes"));
        if (retryEnabledErrorCodes != null && retryEnabledErrorCodes.getText() != null) {
            StringTokenizer st = new StringTokenizer(retryEnabledErrorCodes.getText().trim(), ", ");
            while (st.hasMoreTokens()) {
                String s = st.nextToken();
                try {
                    definition.addRetryEnabledErrorCode(Integer.parseInt(s));
                } catch (NumberFormatException e) {
                    handleException("The suspend error codes should be specified as valid " + "numbers separated by commas : " + retryEnabledErrorCodes.getText(), e);
                }
            }
        }
    }
    return definition;
}
Also used : Pattern(java.util.regex.Pattern) SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) StringTokenizer(java.util.StringTokenizer) QName(javax.xml.namespace.QName) JaxenException(org.jaxen.JaxenException) EndpointDefinition(org.apache.synapse.endpoints.EndpointDefinition) OMElement(org.apache.axiom.om.OMElement) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 48 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, 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 49 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, 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 50 with SynapseXPath

use of org.apache.synapse.util.xpath.SynapseXPath 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

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