How to make a dynamic xPath and execute it? - properties

I collect string to xpath
<property xmlns:t="https://services" name="xPathElemet" expression="fn:concat(//t:SNILS/, $func:element)"/>
and I want to run this xpath and write the value of a Property
<property name="KEY" expression="get-property('xPathElemet')"/>
but receive only collected a string
how to xpath of the Property?
example code sequence :
<iterate continueParent="true" expression="//t:Employee">
<target>
<sequence>
<call-template target="save_element">
<with-param name="key_element" value="Name"/>
</call-template>
</sequence>
</target>
</iterate>
example code template :
<template xmlns="http://ws.apache.org/ns/synapse" name="save_element">
<parameter name="key_element"/> <!--example: "Name"-->
<sequence>
<property name="KEY" expression="fn:concat(//t:Employee/t:SNILS, ':' ,$func:key_element)" scope="default"
type="STRING"/> <!--example: "111-111-111-1:Name"-->
<property name="xPathElemet" expression="fn:concat('//t:Employee/t:', $func:element)"/> <!--example: "//t:Employee/t:Name"-->
<property name="VALUE" expression="get-property('xPathElemet')" scope="default" type="STRING"/> <!--example: Den (Now it does not work)-->
<dbreport>
<connection>
<pool>
<seetings/>
</pool>
</connection>
<statement>
<sql>
<![CDATA[insert into cache( key , value ) values (?, ?);]]></sql> <!--insert new line where key = "111-111-111-1:Name" and value = "Den"-->
<parameter expression="get-property('KEY')" type="VARCHAR"/>
<parameter expression="get-property('VALUE')" type="VARCHAR"/>
</statement>
</dbreport>
</sequence>
</template>
example xml:
<Employees xmlns="https://services">
<Employee>
<SNILS>111-111-111-1</SNILS>
<Name>Den</Name>
</Employee>
<Employee>
<SNILS>111-111-111-2</SNILS>
<Name>Elena</Name>
</Employee>
</Employees>

Use evaluate
In your case:
<property xmlns:t="https://services" name="xPathElemet" expression="fn:concat(//t:SNILS/, $func:element)"/>
<property name="KEY" expression="evaluate(get-property('xPathElemet'))"/>
You can find more infomation in this blog.

Please try the following and see if it works
<property xmlns:t="https://services" name="xPathElemet" expression="fn:concat(//t:SNILS/, $func:element)"/>
<property xmlns:t="https://services" name="xPathfull" expression="fn:concat(get-property('xPathElemet'),'/text()')"/>
<property name="KEY" expression="get-property('xPathfull')"/>
What was missing from your code was the /text() which refer to the parameters of the given xpath. Please try this

Related

WSO2 EI: Can I use mediator to request another API and pass its response to the body request?

In my case, I want to add a dynamic value ("Bearer" + {access-token}) to the header mediator .
So before the header mediator, I want to invoke a get-token API and extract {access-token} element from its response. How can I get that ? Thank you so much.
You can achieve such requirements with mediation sequences. You can refer to this blog for more detailed instructions on how to develop a sequence for your requirement. The blog is written for the API Manager product, but nevertheless, you can follow the same to get it done in the EI.
A sample mediation sequence will be as following
<?xml version="1.0" encoding="UTF-8"?>
<sequence name="oauth2-sequence" xmlns="http://ws.apache.org/ns/synapse">
<!-- token generation to the oauth server's token endpoint -->
<!-- add the base64 encoded credentials -->
<property name="client-authorization-header" scope="default" type="STRING" value="MDZsZ3BTMnh0enRhOXBsaXZGUzliMnk4aEZFYTpmdE4yWTdLcnE2SWRsenBmZ1RuTVU1bkxjUFFh" />
<property name="request-body" expression="json-eval($)" scope="default" type="STRING" />
<property name="resource" expression="get-property('axis2', 'REST_URL_POSTFIX')" scope="default" type="STRING" />
<!-- creating a request payload for client_credentials -->
<payloadFactory media-type="xml">
<format>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<root xmlns="">
<grant_type>client_credentials</grant_type>
</root>
</soapenv:Body>
</soapenv:Envelope>
</format>
<args></args>
</payloadFactory>
<!-- set related headers to call the token endpoint -->
<header name="Authorization" expression="fn:concat('Basic ', get-property('client-authorization-header'))" scope="transport" />
<header name="Content-Type" value="application/x-www-form-urlencoded" scope="transport" />
<property name="messageType" value="application/x-www-form-urlencoded" scope="axis2" type="STRING" />
<property name="REST_URL_POSTFIX" value="" scope="axis2" type="STRING" />
<!-- change the token endpoint -->
<call blocking="true">
<endpoint>
<http method="POST" uri-template="https://localhost:9443/oauth2/token" />
</endpoint>
</call>
<!-- append the acquired access token and make the call to the backend service -->
<property name="bearer-token" expression="json-eval($.access_token)" scope="default" type="STRING" />
<property name="REST_URL_POSTFIX" expression="get-property('resource')" scope="axis2" type="STRING" />
<header name="Authorization" expression="fn:concat('Bearer ', get-property('bearer-token'))" scope="transport" />
<payloadFactory media-type="json">
<format>$1</format>
<args>
<arg evaluator="xml" expression="get-property('request-body')" />
</args>
</payloadFactory>
<!-- perform a send or call to complete the execution of the backend service call in EI -->
</sequence>
Hope this helps you to start with implementation.

OnComplete in Aggregate sequence not triggered after the Iteration sequence

I'm building a microservice where I have a main sequence that first reads a json file and then goes into iteration sequence that is connected to an aggregator sequence with an ID. Here is the order from the main sequence:
<property expression="json-eval($)" name="ORIGINAL_PAYLOAD" scope="default" type="STRING"/>
<sequence key="data-iterate-seq"/>
<property name="aggregatedResponses" scope="default">
<batch_response/>
</property>
<sequence key="data-aggregate-seq"/>
The problem I'm facing is that once it goes into iterate it first triggers the code in aggregate that is outside of OnComplete, then it goes back and does the iterations and never goes to the OnComplete neither it goes back to the main sequence. I managed to make it continue with the main sequence using continueParent=true but this way it never goes into OnComplete of the aggregation sequence. I have also tried switching the onComplete expression to $body/*[1] and //jsonObject but it didn't work Edit: neither did the <send/> and <loopback/> operators.
Here is my iterator
<?xml version="1.0" encoding="UTF-8"?>
<sequence name="data-iterate-seq" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
<iterate attachPath="json-eval($)" expression="json-eval($.records)" id="requestIterator" preservePayload="true">
<target>
<sequence>
<payloadFactory media-type="json">
<format>$1</format>
<args>
<arg evaluator="json" expression="$"/>
</args>
</payloadFactory>
<property name="messageType" scope="axis2" type="STRING" value="application/json"/>
<log level="custom" separator="
">
<property expression="json-eval($)" name="BODY_INSIDE_ITERATE"/>
</log>
<!-- Prepare Payload -->
<property expression="json-eval($.recordId)" name="recordId" scope="default" type="STRING"/>
<property name="entitySetName" scope="default" type="STRING" value="accounts"/>
<property expression="concat(get-property('msdynamics365.resource'),'/api/data/',get-property('msdynamics365.api.version'),'/')" name="patch-url" scope="axis2" type="STRING"/>
<payloadFactory media-type="json">
<format>{"batch_response": "{ "relativeUrlTemplate": "{{apiUrl}}{{entitySetName}}{{entityRecordId}}",
"method": "PATCH",
"entitySetName": "$1",
"entityRecordId": "$2",
"data": {
"name":"$3",
"email":"$4"
}
}}
</format>
<args>
<arg evaluator="xml" expression="$ctx:entitySetName"/>
<arg evaluator="xml" expression="$ctx:recordId"/>
<arg evaluator="json" expression="$.data.name"/>
<arg evaluator="json" expression="$.data.emailaddress1"/>
</args>
</payloadFactory>
<property name="messageType" scope="axis2" type="STRING" value="application/json"/>
<log level="full"/>
<log level="custom" separator="
">
<property name="Results" value="Number of iterations"/>
</log>
</sequence>
</target>
</iterate>
</sequence>
and my aggregator:
<?xml version="1.0" encoding="UTF-8"?>
<sequence name="data-aggregate-seq" onError="data-fault-seq" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
<aggregate id="requestIterator">
<completeCondition>
<messageCount max="-1" min="-1"/>
</completeCondition>
<onComplete aggregateElementType="root" enclosingElementProperty="aggregatedResponses" expression="json-eval($)" xmlns:ns="http://org.apache.synapse/xsd">
<property expression="json-eval($)" name="aggregatedResponses" scope="default" type="STRING"/>
<log level="full">
<property expression="json-eval($)" name="OUT_SEQUENCE_AGGREGATED_RESPONSE"/>
</log>
<log level="full">
<property expression="get-property('aggregatedResponses')" name="Responses"/>
</log>
</onComplete>
</aggregate>
<log level="custom">
<property expression="json-eval($)" name="FINAL_PAYLOAD"/>
<property expression="get-property('aggregatedResponses')" name="aggregated_Response"/>
</log>
</sequence>
Any suggestions on what I'm doing wrong or anything that might help would be really appreciated!
Thank you in advance!!
According to the documentation, you need to use a call or send mediator inside the Iterate mediator to continue the message flow.
In Iterate you need to send the split messages to an endpoint to
continue the message flow.
For me, it seems you are only transforming the message payload. Hence you can use Foreach mediator to do that without Iterate mediator.

WSO2 ESB: Display dataService response

How do you display the response returned by calling a webservice endpoint on a sequence?
Below is the sequence that I use. I would like to display the return value from the dataservice called "CDServiceEndpoint" on the wso2carbon.log. Is that possible? If not, how can I get the data displayed.
<sequence xmlns="http://ws.apache.org/ns/synapse" name="ConcurGetADPExtractFlow" onError="GeneralErrorHandler">
<property xmlns:ns="http://org.apache.synapse/xsd" xmlns:ns3="http://org.apache.synapse/xsd" name="current-context-details" expression="concat(get-property('current-context-details'), ', ConcurGetADPExtractFlow')" />
<property name="scenario" value="ConcurGetADPExtractFlow" />
<log level="custom">
<property name="DEBUGGING" value="ConcurGetADPExtractFlow" />
<property name="start-date" value="2015-02-23" />
<property xmlns:ns="http://org.apache.synapse/xsd" xmlns:ns3="http://org.apache.synapse/xsd" name="End Date" expression="get-property('current-date')" />
</log>
<xslt key="Concur_Get_ADP_Extract_Transformation">
<property name="start-date" value="2015-03-02" />
<property xmlns:ns="http://org.apache.synapse/xsd" xmlns:ns3="http://org.apache.synapse/xsd" name="end-date" expression="get-property('current-date')" />
</xslt>
<property name="post-data-service-sequence" value="ConcurTransformADPExtractFlow" />
<property name="OUT_ONLY" value="false" />
<send receive="DataServiceInvocationErrorFlow">
<endpoint key="ConcurDataServiceEndpoint" />
</send>
<description>Sends a request to the data service for the ADP extract data.</description>
</sequence>
Below is how my DataServiceInvocationErrorFlow looks like.
<sequence xmlns="http://ws.apache.org/ns/synapse" name="DataServiceInvocationErrorFlow">
<filter xmlns:ns="http://org.apache.synapse/xsd" xmlns:m="http://ws.wso2.org/dataservice" xmlns:ns3="http://org.apache.synapse/xsd" xpath="//m:DataServiceFault">
<then>
<log level="custom">
<property name="status" value="data-service-fault" />
</log>
<property name="error_message" expression="//m:DataServiceFault" />
<sequence key="GeneralErrorHandler" />
<drop />
</then>
<else>
<filter source="string-length(get-property('ERROR_MESSAGE'))" regex="0.0">
<then>
<filter xpath="//soapenv:Fault">
<then>
<log level="custom">
<property name="status" value="ERROR" />
</log>
<property name="error_message" expression="//soapenv:Fault" />
<sequence key="GeneralErrorHandler" />
<drop />
</then>
<else>
<log level="custom">
<property name="status" value="success response from DSS" />
</log>
<filter source="string-length(normalize-space(get-property('post-data-service-sequence')))" regex="0.0">
<then />
<else>
<property name="temp-post-data-service-sequence" expression="get-property('post-data-service-sequence')" />
<property name="post-data-service-sequence" value="" />
<sequence key="{get-property('temp-post-data-service-sequence')}" />
</else>
</filter>
</else>
</filter>
</then>
<else>
<property name="error_message" expression="get-property('ERROR_MESSAGE')" />
<sequence key="GeneralErrorHandler" />
<drop />
</else>
</filter>
</else>
</filter>
</sequence>
If by CDServiceEndpoint you meant ConcurDataServiceEndpoint, then you are already handling the response from that endpoint in your DataServiceInvocationErrorFlow sequence that you defined on the Send mediator, which by the way is confusingly named, since your receive sequence will run no matter if you get an error response or a good one. All you need to do is log it inside DataServiceInvocationErrorFlow using Log mediator.
Had you not defined a receive sequence on your Send mediator, the response would have gotten to your outSequence, where you can log it with Log mediator too.

Issue in getting response back while connecting to Oracle DB in WSO2 esb

I am working on sample application in WSO2 esb
which connects to Oracle Database and return the response of a query. Below mentioned is my service. Issue I am facing is I am not getting the response back after execution of service.However I am able to see the value returned as a query response, but not able to render it in response document.
Can anybody suggest, what I am missing?
DB credentials fields have been hashed.
Proxy :
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="getEmployeeDetails"
transports="https,http"
statistics="disable"
trace="disable"
startOnLoad="true">
<target>
<inSequence>
<property name="OUT_ONLY" value="true" scope="default" type="BOOLEAN"/>
<log level="full" category="DEBUG"/>
<dblookup>
<connection>
<pool>
<password>****</password>
<user>****</user>
<url>****</url>
<driver>oracle.jdbc.xa.client.OracleXADataSource</driver>
</pool>
</connection>
<statement>
<sql>select firstname from employee where lastname = 'pawar'</sql>
<result name="firstname" column="firstname"/>
</statement>
</dblookup>
<log level="custom">
<property name="firstname" expression="get-property('firstname')"/>
</log>
<payloadFactory media-type="xml">
<format>
<GetEmployeeDetailsResponse xmlns="">
<out>$1</out>
</GetEmployeeDetailsResponse>
</format>
<args>
<arg evaluator="xml" expression="get-property('firstname')"/>
</args>
</payloadFactory>
</inSequence>
<outSequence>
<log level="full" category="DEBUG"/>
</outSequence>
</target>
<publishWSDL uri="file:/development/data/wso2/wsdl/Employee.wsdl"/>
<description/>
</proxy>
You have to send back the message generated by your payloadFactory in your inSequence :
<header name="To" action="remove"/>
<property name="RESPONSE" value="true" scope="default" type="STRING"/>
<property name="NO_ENTITY_BODY" scope="axis2" action="remove"/>
<send/>
Your mediation is "OUT_ONLY", so, your outSequence will never been executed

How to call stored proc using nhibernate?

I have research about it and all solution are pointing out to one solution, to use the libe of codes below and put it int .hbm.xml file. but I do not have one. What I have is hibernate.cfg.xml and nhvalidator.cfg.xml.
I have read from here : http://forums.asp.net/t/1407518.aspx/1
but where can I type the query tags? I typed it in the hibernate.cfg.xml (see below) but it is not working.
<?xml version="1.0" encoding="utf-16"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Server=localhost\MSSQLSERVERR2;Database=SupplierSearch;Trusted_Connection=True</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<property name="cache.use_minimal_puts">false</property>
<property name="use_outer_join">false</property>
</session-factory>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="Quarry.SupplierSearch"
assembly="SupplierSearch">
<class name="SupplierSearch" table="Client" lazy="true">
<id name="ClientId" column="ClientId">
<generator class="native" />
</id>
<property name="FirstName" column="FirstName" />
<property name="ClientId" column="ClientId" />
<loader query-ref="GetAllClient"/>
</class>
<sql-query name="GetAllClient" >
<return alias="GetAllClient" class="SupplierSearch">
<return-property name="ClientId" column="ClientId"/>
<return-property name="FirstName" column="FirstName"/>
</return>
exec GetAllClient
</sql-query>
</hibernate-mapping>
</hibernate-configuration>
since it is not working, I tried typing it in my Client.nhv.xml (see below) where client is mapped)
<?xml version="1.0" encoding="utf-8"?>
<nhv-mapping assembly="Quarry.SupplierSearch" namespace="Quarry.SupplierSearch.Model" xmlns="urn:nhibernate-validator-1.0">
<class name="Client">
<property name="Address1">
<not-null />
</property>
<property name="Address2">
<not-null />
</property>
<property name="BusinessName">
<not-null />
</property>
<property name="ClientId">
<not-null />
<digits integerDigits="10" />
</property>
<property name="Country">
<not-null />
</property>
<property name="FirstName">
<not-null />
</property>
<property name="LastName">
<not-null />
</property>
<property name="ListingType">
<not-null />
<digits integerDigits="10" />
</property>
<property name="PhoneNumber">
<not-null />
</property>
<property name="PostCode">
<not-null />
</property>
<property name="State">
<not-null />
</property>
</class>
<loader query-ref="GetAllClient"/>
<sql-query name="GetAllClient">
<load-collection alias="Clients" role ="Client"/>
exec [GetAllClient]
</sql-query>
</nhv-mapping>
any suggestion to get this working? thanks
It needs to be called Client.hbm.xml not Client.hbv.xmland an embedded resource.
edit I not familiar with any tools that generate hbv nor have I ever seen a mapping that begins with <nhv-mapping .. >. There must be a custom plugin/dll that you must use to get this working. What tool are you using?
However have you seen this blog post to get SP's to work without any custom tools.
using stored procedures is not supported with load-collection (scalar only).
use the format:
<sql-query name="GetAllClient">
<load-collection alias="Clients" role ="Client"/>
SELECT {c.*}
FROM client c
</sql-query>
(with the stuff in your stored proc innards in place of the "SELECT...FROM..." part of course.)