Search in sources :

Example 56 with SynapseXPath

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

the class Axis2MessageContextTest method testMessageContextGetStringValueBody.

public void testMessageContextGetStringValueBody() throws Exception {
    SynapseXPath axiomXpath = new SynapseXPath("$body/ns1:a/ns1:c");
    axiomXpath.addNamespace("ns1", nsNamespace1);
    MessageContext synCtx = TestUtils.getTestContext(sampleBody);
    String result = axiomXpath.stringValueOf(synCtx);
    assertEquals("second", result);
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) MessageContext(org.apache.synapse.MessageContext)

Example 57 with SynapseXPath

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

the class XSLTMediatorTest method testTransformXSLTCustomSourceNonMainElement.

public void testTransformXSLTCustomSourceNonMainElement() throws Exception {
    // create a new switch mediator
    XSLTMediator transformMediator = new XSLTMediator();
    // set xpath condition to select source
    SynapseXPath xpath = new SynapseXPath("//m0:CheckPriceRequest");
    xpath.addNamespace("m0", "http://services.samples/xsd");
    transformMediator.setSource(xpath);
    // set XSLT transformation URL
    setXsltTransformationURL(transformMediator, "xslt-key");
    // invoke transformation, with static enveope
    MessageContext synCtx = new TestMessageContextBuilder().addFileEntry("xslt-key", "../../repository/conf/sample/resources/transform/transform_unittest.xslt").setBodyFromString(ENCLOSING_SOURCE).addTextAroundBody().build();
    transformMediator.mediate(synCtx);
    // validate result
    OMContainer body = synCtx.getEnvelope().getBody();
    if (body.getFirstOMChild().getNextOMSibling() instanceof OMElement) {
        OMElement someOtherElem = (OMElement) body.getFirstOMChild().getNextOMSibling();
        assertTrue("someOtherElement".equals(someOtherElem.getLocalName()));
        assertTrue("http://someother".equals(someOtherElem.getNamespace().getNamespaceURI()));
        assertQuoteElement(someOtherElem.getFirstOMChild());
    } else {
        fail("Unexpected element found in SOAP body");
    }
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) TestMessageContextBuilder(org.apache.synapse.TestMessageContextBuilder) MessageContext(org.apache.synapse.MessageContext)

Example 58 with SynapseXPath

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

the class URLRewriteMediatorTest method testConditionalRewriteScenario3.

public void testConditionalRewriteScenario3() throws Exception {
    URLRewriteMediator mediator = new URLRewriteMediator();
    mediator.setOutputProperty("outURL");
    RewriteAction action1 = new RewriteAction();
    action1.setValue("localhost");
    action1.setFragmentIndex(URIFragments.HOST);
    RewriteRule rule1 = new RewriteRule();
    rule1.addRewriteAction(action1);
    EqualEvaluator eval1 = new EqualEvaluator();
    URLTextRetriever txtRtvr1 = new URLTextRetriever();
    txtRtvr1.setSource(EvaluatorConstants.URI_FRAGMENTS.host.name());
    eval1.setTextRetriever(txtRtvr1);
    eval1.setValue("myhost");
    rule1.setCondition(eval1);
    mediator.addRule(rule1);
    RewriteAction action2 = new RewriteAction();
    action2.setValue("/services/SimpleStockQuoteService");
    action2.setFragmentIndex(URIFragments.PATH);
    RewriteAction action3 = new RewriteAction();
    action3.setXpath(new SynapseXPath("get-property('port')"));
    action3.setFragmentIndex(URIFragments.PORT);
    RewriteRule rule2 = new RewriteRule();
    rule2.addRewriteAction(action2);
    rule2.addRewriteAction(action3);
    MatchEvaluator eval2 = new MatchEvaluator();
    URLTextRetriever txtRtvr2 = new URLTextRetriever();
    txtRtvr2.setSource(EvaluatorConstants.URI_FRAGMENTS.path.name());
    eval2.setTextRetriever(txtRtvr2);
    eval2.setRegex(Pattern.compile(".*/MyService"));
    rule2.setCondition(eval2);
    mediator.addRule(rule2);
    MessageContext msgCtx = TestUtils.createLightweightSynapseMessageContext("<empty/>");
    msgCtx.setTo(new EndpointReference("http://myhost:8280/MyService"));
    msgCtx.setProperty("port", 9000);
    mediator.mediate(msgCtx);
    assertEquals(targetURL, msgCtx.getProperty("outURL"));
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) URLTextRetriever(org.apache.synapse.commons.evaluators.source.URLTextRetriever) MessageContext(org.apache.synapse.MessageContext) MatchEvaluator(org.apache.synapse.commons.evaluators.MatchEvaluator) EqualEvaluator(org.apache.synapse.commons.evaluators.EqualEvaluator) EndpointReference(org.apache.axis2.addressing.EndpointReference)

Example 59 with SynapseXPath

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

the class DynamicEndpointTest method testContextProperties.

public void testContextProperties() throws Exception {
    SynapseXPath xpath = new SynapseXPath("$ctx:timeout");
    AbstractEndpoint endpoint = new AddressEndpoint();
    EndpointDefinition definition = new EndpointDefinition();
    endpoint.setDefinition(definition);
    definition.setDynamicTimeoutExpression(xpath);
    MessageContext synCtx = new TestMessageContext();
    synCtx.setProperty("timeout", "90000");
    assertEquals(Long.valueOf((String) xpath.evaluate(synCtx)).longValue(), endpoint.getDefinition().evaluateDynamicEndpointTimeout(synCtx));
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) AbstractEndpoint(org.apache.synapse.endpoints.AbstractEndpoint) AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) TestMessageContext(org.apache.synapse.TestMessageContext) EndpointDefinition(org.apache.synapse.endpoints.EndpointDefinition) TestMessageContext(org.apache.synapse.TestMessageContext) MessageContext(org.apache.synapse.MessageContext)

Example 60 with SynapseXPath

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

the class PropertyMediatorTest method testSetAndReadContextProperty.

public void testSetAndReadContextProperty() throws Exception {
    PropertyMediator propMediator = new PropertyMediator();
    propMediator.setName("name");
    propMediator.setValue("value");
    // set a local property to the synapse context
    PropertyMediator propMediatorTwo = new PropertyMediator();
    propMediatorTwo.setName("nameTwo");
    propMediatorTwo.setValue("valueTwo");
    MessageContext synCtx = TestUtils.getTestContext("<empty/>");
    propMediator.mediate(synCtx);
    propMediatorTwo.mediate(synCtx);
    assertTrue("value".equals((new SynapseXPath("synapse:get-property('name')")).stringValueOf(synCtx)));
    assertTrue("valueTwo".equals((new SynapseXPath("synapse:get-property('nameTwo')")).stringValueOf(synCtx)));
    PropertyMediator propMediatorThree = new PropertyMediator();
    propMediatorThree.setName("name");
    propMediatorThree.setValue("value");
    propMediatorThree.setAction(PropertyMediator.ACTION_REMOVE);
    propMediatorThree.mediate(synCtx);
    assertNull((new SynapseXPath("synapse:get-property('name')")).stringValueOf(synCtx));
    assertTrue("valueTwo".equals((new SynapseXPath("synapse:get-property('nameTwo')")).stringValueOf(synCtx)));
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

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