Search in sources :

Example 51 with SynapseXPath

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

the class PayloadFactoryMediatorSerializerTest method testSerializeSpecificMediator5.

/**
 * Test SerializeSpecificMediator method with PathArgument with expression added for payloadFactory mediator
 * and assert that expression is added.
 */
@Test
public void testSerializeSpecificMediator5() throws JaxenException {
    PayloadFactoryMediatorSerializer serializer = new PayloadFactoryMediatorSerializer();
    PayloadFactoryMediator payloadFactoryMediator = new PayloadFactoryMediator();
    Argument argument = new Argument();
    argument.setExpression(new SynapseXPath("//name"));
    payloadFactoryMediator.addPathArgument(argument);
    payloadFactoryMediator.setFormat(format);
    OMElement element = serializer.serializeSpecificMediator(payloadFactoryMediator);
    MediatorFactory mediatorFactory = new PayloadFactoryMediatorFactory();
    Mediator mediator = mediatorFactory.createMediator(element, null);
    Assert.assertNotNull(element);
    Assert.assertEquals("Expression added for path argument is not serialized", "//name", ((PayloadFactoryMediator) mediator).getPathArgumentList().get(0).getExpression().toString());
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) Argument(org.apache.synapse.mediators.transform.Argument) PayloadFactoryMediator(org.apache.synapse.mediators.transform.PayloadFactoryMediator) OMElement(org.apache.axiom.om.OMElement) PayloadFactoryMediator(org.apache.synapse.mediators.transform.PayloadFactoryMediator) Mediator(org.apache.synapse.Mediator) Test(org.junit.Test)

Example 52 with SynapseXPath

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

the class AbstractDBMediatorFactory method buildDataSource.

/**
 * Reads the data source configuration for all mediators based on the <code>AbstractDBMediator</code>
 * and stores the configuration in the mediator for datasource initialization and de-serialization.
 *
 * @param elem the configuration element of the mediator
 * @param mediator the mediator on which the configuration shall be stored
 */
protected void buildDataSource(OMElement elem, AbstractDBMediator mediator) {
    OMElement pool;
    try {
        // get the 'pool' element and determine if we need to create a DataSource or
        // lookup using JNDI
        SynapseXPath xpath = new SynapseXPath("self::node()/syn:connection/syn:pool");
        xpath.addNamespace("syn", XMLConfigConstants.SYNAPSE_NAMESPACE);
        pool = (OMElement) xpath.selectSingleNode(elem);
        if (pool.getFirstChildWithName(DSNAME_Q) != null) {
            readLookupConfig(mediator, pool);
        } else if (pool.getFirstChildWithName(DRIVER_Q) != null) {
            readCustomDataSourceConfig(pool, mediator);
        } else {
            handleException("The DataSource connection information must be specified for " + "using a custom DataSource connection pool or for a JNDI lookup");
        }
    } catch (JaxenException e) {
        handleException("Error looking up DataSource connection information", e);
    }
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) JaxenException(org.jaxen.JaxenException) OMElement(org.apache.axiom.om.OMElement)

Example 53 with SynapseXPath

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

the class Target method insert.

/**
 * Inserts the given object into the target specified by the current Target object.
 * @param synCtx Message Context to be enriched with the object.
 * @param object Object to be inserted.
 */
public void insert(MessageContext synCtx, Object object) {
    if (value.getExpression() != null && value.getExpression() instanceof SynapseXPath) {
        SynapseXPath expression = (SynapseXPath) value.getExpression();
        Object targetObj = null;
        try {
            targetObj = expression.selectSingleNode(synCtx);
        } catch (JaxenException e) {
            handleException("Failed to select the target.", e);
        }
        if (targetObj instanceof OMText) {
            Object targetParent = ((OMText) targetObj).getParent();
            if (targetParent != null && targetParent instanceof OMElement) {
                ((OMElement) targetParent).setText(object == null ? "" : object.toString());
            } else {
                handleException("Invalid target is specified by the expression: " + expression);
            }
        } else {
            handleException("Invalid target is specified by the expression: " + expression);
        }
    } else if (value.getKeyValue() != null) {
        synCtx.setProperty(value.getKeyValue(), object);
    } else {
        handleException("Invalid target description. " + value);
    }
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) JaxenException(org.jaxen.JaxenException) OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement)

Example 54 with SynapseXPath

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

the class DefaultInMemorySubscriptionManager method init.

public void init() {
    try {
        // TODO: pick values from the constants
        topicHeaderName = getPropertyValue("topicHeaderName");
        if (topicHeaderName == null) {
            handleException("Unable to create topic header topic header name is null");
        }
        topicHeaderNS = getPropertyValue("topicHeaderNS");
        if (topicHeaderNS == null) {
            handleException("Unable to create topic header topic header namespace is null");
        }
        topicXPath = new SynapseXPath("s11:Header/ns:" + topicHeaderName + " | s12:Header/ns:" + topicHeaderName);
        topicXPath.addNamespace("s11", "http://schemas.xmlsoap.org/soap/envelope/");
        topicXPath.addNamespace("s12", "http://www.w3.org/2003/05/soap-envelope");
        topicXPath.addNamespace("ns", topicHeaderNS);
    } catch (JaxenException e) {
        handleException("Unable to create the topic header XPath", e);
    }
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) JaxenException(org.jaxen.JaxenException)

Example 55 with SynapseXPath

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

the class InboundEndpoint method resolveSecureVaultExpressions.

private void resolveSecureVaultExpressions(Properties props) {
    Pattern vaultLookupPattern = Pattern.compile(secureVaultRegex);
    for (Map.Entry<Object, Object> entry : props.entrySet()) {
        String value = (String) entry.getValue();
        Matcher lookupMatcher = vaultLookupPattern.matcher(value);
        // setting value initially
        String newParamValue = value;
        while (lookupMatcher.find()) {
            Value expression = null;
            // getting the expression with out curly brackets
            String expressionStr = lookupMatcher.group(0).substring(1, lookupMatcher.group(0).length() - 1);
            try {
                expression = new Value(new SynapseXPath(expressionStr));
            } catch (JaxenException e) {
                log.error("Error while building the expression : " + expressionStr);
            }
            if (expression != null) {
                String resolvedValue = expression.evaluateValue(synapseEnvironment.createMessageContext());
                if (resolvedValue == null || resolvedValue.isEmpty()) {
                    log.warn("Found Empty value for expression : " + expression.getExpression());
                    resolvedValue = "";
                }
                // replacing the expression with resolved value
                newParamValue = newParamValue.replaceFirst(secureVaultRegex, resolvedValue);
                props.put(entry.getKey(), newParamValue);
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) Matcher(java.util.regex.Matcher) JaxenException(org.jaxen.JaxenException) Value(org.apache.synapse.mediators.Value) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

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