Search in sources :

Example 6 with SynapseXPath

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

the class XSLTMediatorTest method testMultipleKeys.

/**
 * Test with multiple keys including static and dynamic keys
 *
 * @param num number from 0 to 2
 * @throws Exception Exception in case of an error in tests
 */
private void testMultipleKeys(int num) throws Exception {
    String xsltKeyValue = null;
    String path;
    SynapseXPath xpath;
    Value xsltKey;
    XSLTMediator transformMediator = new XSLTMediator();
    // default source, xsltFile, and state of key (dynamic or static)
    String source = "";
    String xsltFile = "";
    boolean isDynamicKey = true;
    // based on source, different XSLTFiles can be used
    if (num == 0) {
        source = SOURCE_STATIC_KEY;
        xsltFile = "static_key.xslt";
        xsltKeyValue = "StaticXsltKey";
        isDynamicKey = false;
    } else if (num == 1) {
        source = SOURCE_DYNAMIC_KEY1;
        xsltFile = "dynamic_key_1.xslt";
        xsltKeyValue = "DynamicXsltKey1";
        isDynamicKey = true;
    } else if (num == 2) {
        source = SOURCE_DYNAMIC_KEY2;
        xsltFile = "dynamic_key_2.xslt";
        xsltKeyValue = "DynamicXsltKey2";
        isDynamicKey = true;
    }
    if (isDynamicKey) {
        path = "//m0:CheckPriceRequest/m0:" + xsltKeyValue;
        xpath = new SynapseXPath(path);
        xpath.addNamespace("m0", "http://services.samples/xsd");
        // Create key from dynamic key (xpath)
        xsltKey = new Value(xpath);
        // set XSLT transformation URL (Xpath)
        transformMediator.setXsltKey(xsltKey);
    } else {
        // static key
        path = xsltKeyValue;
        // set XSLT transformation URL (static)
        setXsltTransformationURL(transformMediator, path);
    }
    // Mediate twice for synCtx
    MessageContext synCtx = new TestMessageContextBuilder().addEntry(xsltKeyValue, getClass().getResource(xsltFile)).setBodyFromString(source).addTextAroundBody().build();
    transformMediator.mediate(synCtx);
    synCtx = new TestMessageContextBuilder().addEntry(xsltKeyValue, getClass().getResource(xsltFile)).setBodyFromString(source).addTextAroundBody().build();
    transformMediator.mediate(synCtx);
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) TestMessageContextBuilder(org.apache.synapse.TestMessageContextBuilder) Value(org.apache.synapse.mediators.Value) MessageContext(org.apache.synapse.MessageContext)

Example 7 with SynapseXPath

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

the class XSLTMediatorTest method testTransformXSLTCustomSource.

public void testTransformXSLTCustomSource() throws Exception {
    // create a new XSLT 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");
    MessageContext synCtx = new TestMessageContextBuilder().addFileEntry("xslt-key", "../../repository/conf/sample/resources/transform/transform_unittest.xslt").setBodyFromString(SOURCE).addTextAroundBody().build();
    transformMediator.mediate(synCtx);
    // validate result
    assertQuoteElement(synCtx.getEnvelope().getBody().getFirstOMChild().getNextOMSibling());
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) TestMessageContextBuilder(org.apache.synapse.TestMessageContextBuilder) MessageContext(org.apache.synapse.MessageContext)

Example 8 with SynapseXPath

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

the class URLRewriteMediatorTest method testUnconditionalRewriteScenario3.

public void testUnconditionalRewriteScenario3() throws Exception {
    URLRewriteMediator mediator = new URLRewriteMediator();
    mediator.setOutputProperty("outURL");
    RewriteAction action1 = new RewriteAction();
    action1.setValue(targetURL);
    RewriteRule rule1 = new RewriteRule();
    rule1.addRewriteAction(action1);
    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);
    mediator.addRule(rule2);
    MessageContext msgCtx = TestUtils.createLightweightSynapseMessageContext("<empty/>");
    msgCtx.setTo(new EndpointReference("http://localhost:8280"));
    msgCtx.setProperty("port", 9000);
    mediator.mediate(msgCtx);
    assertEquals(targetURL, msgCtx.getProperty("outURL"));
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) MessageContext(org.apache.synapse.MessageContext) EndpointReference(org.apache.axis2.addressing.EndpointReference)

Example 9 with SynapseXPath

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

the class SynapseCommodityServiceTest method testN2N.

public void testN2N() throws Exception {
    // Creating the Simple Commodity Client
    System.getProperties().remove(org.apache.axis2.Constants.AXIS2_CONF);
    ServiceClient businessClient = new ServiceClient(null, null);
    Options options = new Options();
    options.setTo(new EndpointReference("http://127.0.0.1:10100/CommodityQuote"));
    businessClient.setOptions(options);
    OMElement response = null;
    response = businessClient.sendReceive(commodityPayload());
    assertNotNull(response);
    SynapseXPath xPath = new SynapseXPath("//return");
    xPath.addNamespace("ns", "http://services.samples/xsd");
    OMElement returnEle = (OMElement) xPath.selectSingleNode(response);
    assertNotNull(returnEle);
    assertEquals(returnEle.getText().trim(), "100");
}
Also used : Options(org.apache.axis2.client.Options) SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) ServiceClient(org.apache.axis2.client.ServiceClient) OMElement(org.apache.axiom.om.OMElement) EndpointReference(org.apache.axis2.addressing.EndpointReference)

Example 10 with SynapseXPath

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

the class PropertyMediatorTest method testXMLPropertyHandling.

public void testXMLPropertyHandling() throws Exception {
    PropertyMediator propMediatorOne = new PropertyMediator();
    propMediatorOne.setName("nameOne");
    String xml = "<Project><name>Synapse</name></Project>";
    OMElement valueOne = TestUtils.createOMElement(xml);
    propMediatorOne.setValueElement(valueOne);
    // Test setting XML properties
    MessageContext synCtx = TestUtils.getTestContext("<getQuote><symbol>IBM</symbol></getQuote>");
    propMediatorOne.mediate(synCtx);
    Object prop = synCtx.getProperty("nameOne");
    // Objects are not equal, so need to compare the content
    assertEquals(valueOne.toString(), prop.toString());
    // Test XML property retreival
    String exprValue = new SynapseXPath("synapse:get-property('nameOne')").stringValueOf(synCtx);
    assertEquals(xml, exprValue);
    // Test property removal
    propMediatorOne.setAction(PropertyMediator.ACTION_REMOVE);
    propMediatorOne.mediate(synCtx);
    assertNull(synCtx.getProperty("nameOne"));
    // Setting XML properties using expressions
    synCtx.setProperty("nameOne", xml);
    PropertyMediator propertyMediatorTwo = new PropertyMediator();
    propertyMediatorTwo.setName("nameTwo");
    propertyMediatorTwo.setExpression(new SynapseXPath("synapse:get-property('nameOne')"), XMLConfigConstants.DATA_TYPES.OM.name());
    propertyMediatorTwo.mediate(synCtx);
    Object exprProp = synCtx.getProperty("nameTwo");
    assertTrue(exprProp != null && exprProp instanceof OMElement);
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) OMElement(org.apache.axiom.om.OMElement) 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