How to add additional information in the response? - wso2

Scenario
I've integrated an external service in my API. In addition to the response from the external service, I want to add a few more JSON keys and values.
API
<?xml version="1.0" encoding="UTF-8"?>
<api context="/PhoneVerify" name="PhoneVerifi" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST">
<inSequence>
<payloadFactory media-type="xml">
<format>
<soapenv:Envelope xmlns:cmpa="http://XXX.XXX.com" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://object.pmd.com/xsd">
<soapenv:Header/>
<soapenv:Body>
<cmpa:verify>
<!--Optional:-->
<cmpa:userName>XXXXX</cmpa:userName>
<!--Optional:-->
<cmpa:passwd>XXXXX</cmpa:passwd>
<!--Optional:-->
<cmpa:request>
<!--Optional:-->
<xsd:cnic>$1</xsd:cnic>
<!--Optional:-->
<xsd:msisdn>$2</xsd:msisdn>
<!--Optional:-->
<xsd:transactionID>$3</xsd:transactionID>
</cmpa:request>
</cmpa:verify>
</soapenv:Body>
</soapenv:Envelope>
</format>
<args>
<arg evaluator="json" expression="$.cnic"/>
<arg evaluator="json" expression="$.msisdn"/>
<arg evaluator="json" expression="$.transactionID"/>
</args>
</payloadFactory>
<log category="DEBUG" level="full"/>
<header name="Action" scope="default" value="verify"/>
<send>
<endpoint>
<address format="soap11" uri="https://XXXXX.com/CMPA/services/CnicMsisdnPairing.CnicMsisdnPairingHttpsSoap11Endpoint/">
<suspendOnFailure>
<initialDuration>-1</initialDuration>
<progressionFactor>1</progressionFactor>
</suspendOnFailure>
<markForSuspension>
<retriesBeforeSuspension>0</retriesBeforeSuspension>
</markForSuspension>
</address>
</endpoint>
</send>
</inSequence>
<outSequence>
<property name="messageType" scope="axis2" type="STRING" value="application/json"/>
<payloadFactory media-type="json">
<format>
{
"status" : "success"
"response": "$1"
}</format>
<args>
<arg evaluator="json" expression="$.verifyResponse.return"/>
</args>
</payloadFactory>
<property name="messageType" scope="axis2" type="STRING" value="application/json"/>
<respond/>
</outSequence>
<faultSequence>
<payloadFactory media-type="json">
<format>
{
"status" : "failure"
}</format>
<args/>
</payloadFactory>
</faultSequence>
</resource>
</api>
Response from External Service
"verifyResponse":{
"return" : {
"#type":"ax21:Response",
"message":"Duplicate Transaction ID",
"responseCode":"08",
"status":"00"
}
}
Desired Response
{
"status": "success"
"response": {
"#type":"ax21:Response",
"message":"Duplicate Transaction ID",
"responseCode":"08",
"status":"00"}
}
Question
My exact question is that when I try to achieve the above problem, I'm getting the response as shown below (basically its wrapped in " "). How can I make achieved the desired response?
{
"status": "success"
"response": "{"#type":"ax21:Response","message":"Duplicate Transaction ID","responseCode":"08","status":"00"}"
}

The Payload factory is adding " " since you have defined the argument $1 within " ". If you modify the Payload factory as below you should be able to get the desired output.
<payloadFactory media-type="json">
<format>
{
"status" : "success",
"response": $1
}
</format>
<args>
<arg evaluator="json" expression="$.verifyResponse.return"/>
</args>
</payloadFactory>
I have tried the above in the latest Integration Studio(8.1.0), if the above solution doesn't work please specify the MI version you are using.

Related

how to convert xml to json in wso2

Scenario
I've implemented an API which makes a soap call to external service. I need to convert the response from xml to json (which is actually happening). But somehow I'm unable to convert the nested xml nodes to json.
API
<?xml version="1.0" encoding="UTF-8"?>
<api context="/nadra" name="NadraVerification" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST">
<inSequence>
<payloadFactory media-type="xml">
<format>
<soapenv:Envelope xmlns:nad="http://XXXX.Verification" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<nad:GetCitizenDemographics>
<nad:franchizeID>XXX</nad:franchizeID>
<nad:xml_request_data><![CDATA[
<CITIZEN_VERIFICATION><USER_VERIFICATION><USERNAME>XXXX</USERNAME><PASSWORD>XXXX</PASSWORD></USER_VERIFICATION><REQUEST_DATA><TRANSACTION_ID>$4</TRANSACTION_ID><SESSION_ID></SESSION_ID><CITIZEN_NUMBER>$5</CITIZEN_NUMBER><CONTACT_NUMBER>$6</CONTACT_NUMBER><AREA_NAME>$7</AREA_NAME></REQUEST_DATA></CITIZEN_VERIFICATION>
]]></nad:xml_request_data>
</nad:GetCitizenDemographics>
</soapenv:Body>
</soapenv:Envelope>
</format>
<args>
<arg evaluator="json" expression="$.transactionId"/>
<arg evaluator="json" expression="$.citizenNo"/>
<arg evaluator="json" expression="$.contactNo"/>
<arg evaluator="json" expression="$.areaName"/>
</args>
</payloadFactory>
<header name="Action" scope="default" value="http://XXXX.XXXX.XXXX/ICitizenVerification/GetCitizenDemographics"/>
<send>
<endpoint>
<address format="soap11" uri="https://verification.nadra.gov.pk/bioverisys/xml">
<suspendOnFailure>
<initialDuration>-1</initialDuration>
<progressionFactor>1</progressionFactor>
</suspendOnFailure>
<markForSuspension>
<retriesBeforeSuspension>0</retriesBeforeSuspension>
</markForSuspension>
</address>
</endpoint>
</send>
</inSequence>
<outSequence>
<property name="messageType" scope="axis2" type="STRING" value="application/json"/>
<payloadFactory media-type="json">
<format>
{
"status" : "success"
"response": $1
}</format>
<args>
<arg evaluator="json" expression="$.GetCitizenDemographicsResponse.GetCitizenDemographicsResult"/>
</args>
</payloadFactory>
<respond/>
</outSequence>
<faultSequence>
<payloadFactory media-type="json">
<format>
{
"status" : "failure"
}</format>
<args/>
</payloadFactory>
</faultSequence>
</resource>
</api>
Response from External Service after converting XML to JSON
{
"GetCitizenDemographicsResponse": {
"GetCitizenDemographicsResult": "<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n<CITIZEN_VERIFICATION xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\r\n <RESPONSE_DATA>\r\n <RESPONSE_STATUS>\r\n <CODE>100</CODE>\r\n
<MESSAGE>successful</MESSAGE>\r\n </RESPONSE_STATUS>\r\n <SESSION_ID>00001343681312</SESSION_ID>\r\n
<CITIZEN_NUMBER>XXXX</CITIZEN_NUMBER>\r\n <PERSON_DATA>\r\n <NAME>ایوب جمال</NAME>\r\n
<FATHER_HUSBAND_NAME>XXX</FATHER_HUSBAND_NAME>\r\n <PRESENT_ADDRESS>XXXX</PRESENT_ADDRESS>\r\n
<PERMANANT_ADDRESS>XXXX</PERMANANT_ADDRESS>\r\n <DATE_OF_BIRTH>1996-01-10</DATE_OF_BIRTH>\r\n
<GENDER>male</GENDER>\r\n <EXPIRY_DATE>2030-07-11</EXPIRY_DATE>\r\n </PERSON_DATA>\r\n
</RESPONSE_DATA>\r\n</CITIZEN_VERIFICATION>"
}
}
Actual Response
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><GetCitizenDemographicsResponse xmlns="http://NADRA.Citizen.Verification"><GetCitizenDemographicsResult><?xml version="1.0" encoding="utf-16"?>
<CITIZEN_VERIFICATION xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<RESPONSE_DATA>
<RESPONSE_STATUS>
<CODE>107</CODE>
<MESSAGE>invalid citizen nummber</MESSAGE>
</RESPONSE_STATUS>
<SESSION_ID />
<CITIZEN_NUMBER>$5</CITIZEN_NUMBER>
</RESPONSE_DATA>
</CITIZEN_VERIFICATION></GetCitizenDemographicsResult></GetCitizenDemographicsResponse></s:Body></s:Envelope>
Question
My exact question is how can I convert the nested xml to json as well?. Since it has converted the first two xml nodes but the third xml node hasn't been converted.
Since you are using the Payload Factory to modify the response, you can use an XPath expression to extract the values from the soap endpoint. You don't need to explicitly convert the SOAP response to JSON using the messageType property before the Payload Factory.
Remove the messageType property
Use an XPath expression in the Payload Factory
Update Answer
According to the response from the backend, in the SOAP body, it has declared the <?xml version="1.0" encoding="UTF-8"?> tag before the CITIZEN_VERIFICATION element. Because of this, we need to first assign it to a property with OM type and then use it in the payload factory. Since we have a namespace defined for GetCitizenDemographicsResponse we need to add the namespace tag as well to the property mediator.
Option 1: With namespace
<property type="OM" xmlns:ns="http://NADRA.Citizen.Verification" expression="//ns:GetCitizenDemographicsResponse/*" name="Payload"/>
Or you can use local-name() function to get the value without defining the namespace in property mediator.
Option 2: Without defining the namespace
<property type="OM" expression="//*[local-name()='GetCitizenDemographicsResult']" name="Payload"/>
<outSequence>
<property type="OM" xmlns:ns="http://NADRA.Citizen.Verification" expression="//ns:GetCitizenDemographicsResponse/*" name="Payload"/>
<payloadFactory media-type="json">
<format>
{
"status" : "success",
"response": $1
}
</format>
<args>
<arg expression="$ctx:Payload" />
</args>
</payloadFactory>
<respond/>
</outSequence>

Wso2 EI 7.1.0 response processing slow

I am using Wso2 EI 7.1.0 for one of my ESB use case. I implemented flow like API mediator-->Iterate mediator-->Send mediator--->Aggregate--->Payload Factory--->Respond, here I am facing more time taking at Payload Factory while responding, I am using default configurations and I tested this at Linux 16 Core + 32 GB RAM server. I am adding template XML, please assist.
<?xml version="1.0" encoding="UTF-8"?>
<api context="/cbr" name="CBRAPI" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST">
<inSequence>
<script language="groovy"><![CDATA[def start_time = System.currentTimeMillis();
mc.setProperty('start_time',start_time);]]></script>
<class description="" name="com.custom.wso2.plugin.ot.CacheBuildMediator">
<property name="DB_CONFIG" value="jdbc:mysql://<IP>:3306/WSO2,user,password,com.mysql.cj.jdbc.Driver"/>
</class>
<propertyGroup>
<property name="TRIGGER_NAME" scope="default" type="STRING" value="TIG1"/>
<property expression="//soapenv:Envelope" name="payload" scope="default" type="STRING" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wos="http://www.huawei.com/bme/cbsinterface/woservices"/>
<property expression="//soapenv:Envelope/soapenv:Body/wos:WorkOrderRequestMsg/WorkOrderRequest/wos:OrderParam[wos:Code='SubscriberNo']/wos:Value" name="SubscriberNo" scope="axis2" type="STRING" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wos="http://www.huawei.com/bme/cbsinterface/woservices"/>
<property expression="//soapenv:Envelope/soapenv:Body/wos:WorkOrderRequestMsg/WorkOrderRequest/wos:OrderParam[wos:Code='RechargeAmount']/wos:Value" name="RechargeAmount" scope="axis2" type="STRING" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wos="http://www.huawei.com/bme/cbsinterface/woservices"/>
<property expression="//soapenv:Envelope/soapenv:Body/wos:WorkOrderRequestMsg/WorkOrderRequest/wos:OrderParam[wos:Code='RechargeChannelID']/wos:Value" name="RechargeChannelID" scope="axis2" type="STRING" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wos="http://www.huawei.com/bme/cbsinterface/woservices"/>
<property expression="//soapenv:Envelope/soapenv:Body/wos:WorkOrderRequestMsg/WorkOrderRequest/wos:OrderParam[wos:Code='RechargeType']/wos:Value" name="RechargeType" scope="axis2" type="STRING" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wos="http://www.huawei.com/bme/cbsinterface/woservices"/>
<property expression="//soapenv:Envelope/soapenv:Body/wos:WorkOrderRequestMsg/WorkOrderRequest/wos:OrderParam[wos:Code='CurrencyID']/wos:Value" name="CurrencyID" scope="axis2" type="STRING" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wos="http://www.huawei.com/bme/cbsinterface/woservices"/>
<property expression="//soapenv:Envelope/soapenv:Body/wos:WorkOrderRequestMsg/WorkOrderRequest/wos:OrderParam[wos:Code='RechargeTime']/wos:Value" name="RechargeTime" scope="axis2" type="STRING" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wos="http://www.huawei.com/bme/cbsinterface/woservices"/>
</propertyGroup>
<class name="com.custom.wso2.plugin.ot.MvelExecuteMediator"/>
<script language="groovy"><![CDATA[def payload=mc.getProperty('RULE_PAYLOAD');
def curr_time=System.currentTimeMillis();
def time= (curr_time- mc.getProperty('start_time'));
mc.setProperty('mvel_exe_time',time);
mc.setProperty('db_report_time',curr_time);
mc.setPayloadXML(payload);]]></script>
<dbreport>
<connection>
<pool>
<driver>com.mysql.cj.jdbc.Driver</driver>
<url>jdbc:mysql://IP:3306/WSO2</url>
<user>user</user>
<password>password</password>
<property name="maxidle" value="10"/>
<property name="initialsize" value="10"/>
<property name="maxactive" value="10"/>
</pool>
</connection>
<statement>
<sql><![CDATA[INSERT INTO `payload_info`(payload,subscriberno,rechargeamount,rechargechannel,rechargetype,currency,rechargetime)VALUES(?,?,?,?,?,?,?);]]></sql>
<parameter expression="get-property('payload')" type="VARCHAR"/>
<parameter expression="get-property('axis2','SubscriberNo')" type="VARCHAR"/>
<parameter expression="get-property('axis2','RechargeAmount')" type="INTEGER"/>
<parameter expression="get-property('axis2','RechargeChannelID')" type="INTEGER"/>
<parameter expression="get-property('axis2','RechargeType')" type="INTEGER"/>
<parameter expression="get-property('axis2','CurrencyID')" type="INTEGER"/>
<parameter expression="get-property('axis2','RechargeTime')" type="VARCHAR"/>
</statement>
</dbreport>
<script language="groovy"><![CDATA[def start_time=mc.getProperty('db_report_time');
def curr_time=System.currentTimeMillis();
def time= (curr_time- start_time);
mc.setProperty('db_report_time',time);
mc.setProperty('end_point_time',curr_time);]]></script>
<filter description="Validate rule" xpath="boolean(get-property('FLOW_CONTINUE'))">
<then>
<class name="com.custom.wso2.plugin.ot.REParamAliasMediator">
<property name="SYNO_NAME_TAGS" value="RechargeAmount,RechargeChannelID,SubscriberNo,RechargeType"/>
</class>
<iterate expression="//rule" id="RULE_LIST_ITERATE">
<target>
<sequence>
<propertyGroup>
<property expression="$body/rule/id" name="rule_id" scope="default" type="STRING"/>
<property expression="$body/rule/url" name="uri.var.url" scope="default" type="STRING"/>
</propertyGroup>
<payloadFactory media-type="json">
<format>{
"requestId":"189898909090",
"timeStamp":"2021/06/28 19:45:52",
"msisdn":"889399977",
"keyWord":"RECHARGE_NOTIFICATION",
"campaignId":"658",
"scheduleId":"$5",
"dataSet":{
"parameters":[
{
"name":"$6",
"value":"$1"
},
{
"name":"$8",
"value":"$2"
},
{
"name":"$7",
"value":"$3"
},
{
"name":"$9",
"value":"0"
},
{
"name":"RECHARGE_DATE",
"value":"$4"
},
{
"name":"RULE_ID",
"value":"$5"
}
]
}
}</format>
<args>
<arg evaluator="xml" expression="get-property('axis2','RechargeAmount')" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wos="http://www.huawei.com/bme/cbsinterface/woservices"/>
<arg evaluator="xml" expression="get-property('axis2','SubscriberNo')" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wos="http://www.huawei.com/bme/cbsinterface/woservices"/>
<arg evaluator="xml" expression="get-property('axis2','RechargeChannelID')" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wos="http://www.huawei.com/bme/cbsinterface/woservices"/>
<arg evaluator="xml" expression="get-property('axis2','RechargeTime')" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wos="http://www.huawei.com/bme/cbsinterface/woservices"/>
<arg evaluator="xml" expression="get-property('rule_id') "/>
<arg evaluator="xml" expression="get-property('axis2','RechargeAmount_alias') "/>
<arg evaluator="xml" expression="get-property('axis2','RechargeChannelID_alias') "/>
<arg evaluator="xml" expression="get-property('axis2','SubscriberNo_alias') "/>
<arg evaluator="xml" expression="get-property('axis2','RechargeType_alias') "/>
</args>
</payloadFactory>
<send>
<endpoint>
<http method="post" uri-template="{uri.var.url}">
<suspendOnFailure>
<initialDuration>-1</initialDuration>
<progressionFactor>1</progressionFactor>
</suspendOnFailure>
<markForSuspension>
<retriesBeforeSuspension>0</retriesBeforeSuspension>
</markForSuspension>
</http>
</endpoint>
</send>
</sequence>
</target>
</iterate>
</then>
<else>
<property name="messageType" scope="axis2" type="STRING" value="application/soap+xml"/>
<payloadFactory media-type="xml">
<format>
<soapenv:Envelope xmlns:cbs="http://www.huawei.com/bme/cbsinterface/cbscommon" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wos="http://www.huawei.com/bme/cbsinterface/woservices">
<soapenv:Body>
<wos:WorkOrderResultMsg>
<ResultHeader>
<cbs:Version>1</cbs:Version>
<cbs:ResultCode>0</cbs:ResultCode>
<cbs:ResultDesc>Operation success.</cbs:ResultDesc>
</ResultHeader>
</wos:WorkOrderResultMsg>
</soapenv:Body>
</soapenv:Envelope>
</format>
<args/>
</payloadFactory>
<respond/>
</else>
</filter>
</inSequence>
<outSequence>
<propertyGroup>
<property name="Aggregated_Responses" scope="default">
<ResponseDetail xmlns=""/>
</property>
<property name="messageType" scope="axis2" type="STRING" value="application/soap+xml"/>
</propertyGroup>
<aggregate id="RULE_LIST_ITERATE">
<completeCondition>
<messageCount max="-1" min="-1"/>
</completeCondition>
<onComplete aggregateElementType="root" enclosingElementProperty="Aggregated_Responses" expression="$body/*[1]">
<script language="groovy"><![CDATA[def start_time=mc.getProperty('end_point_time');
def curr_time=System.currentTimeMillis();
def time= (curr_time- start_time);
mc.setProperty('end_point_time',time);
mc.setProperty('resp_switch_time', curr_time);]]></script>
<filter xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xpath="count( $body//ResponseDetail/jsonObject[responseCode!='SC0000']) >=1">
<then>
<payloadFactory media-type="xml">
<format>
<soapenv:Envelope xmlns:cbs="http://www.huawei.com/bme/cbsinterface/cbscommon" xmlns:wos="http://www.huawei.com/bme/cbsinterface/woservices">
<soapenv:Body>
<wos:WorkOrderResultMsg>
<ResultHeader>
<cbs:Version>1</cbs:Version>
<cbs:ResultCode>1</cbs:ResultCode>
<cbs:ResultDesc>Operation failed.</cbs:ResultDesc>
</ResultHeader>
</wos:WorkOrderResultMsg>
</soapenv:Body>
</soapenv:Envelope>
</format>
<args/>
</payloadFactory>
</then>
<else>
<class name="com.custom.wso2.plugin.ot.REParamAliasMediator">
<property name="ATTRIBUTE_NAME_TAGS" value="Subscriber,Recharge_Amount,Recharge_Channel"/>
</class>
<payloadFactory media-type="xml">
<format>
<soapenv:Envelope xmlns:cbs="http://www.huawei.com/bme/cbsinterface/cbscommon" xmlns:wos="http://www.huawei.com/bme/cbsinterface/woservices">
<soapenv:Body>
<wos:WorkOrderResultMsg>
<ResultHeader>
<cbs:Version>1</cbs:Version>
<cbs:ResultCode>0</cbs:ResultCode>
<cbs:ResultDesc>Operation success.</cbs:ResultDesc>
</ResultHeader>
<WorkOrderResult>
<wos:OrderParam>
<wos:Code>$1</wos:Code>
<wos:Value>$4</wos:Value>
</wos:OrderParam>
<wos:OrderParam>
<wos:Code>$2</wos:Code>
<wos:Value>$5</wos:Value>
</wos:OrderParam>
<wos:OrderParam>
<wos:Code>$3</wos:Code>
<wos:Value>$6</wos:Value>
</wos:OrderParam>
</WorkOrderResult>
</wos:WorkOrderResultMsg>
</soapenv:Body>
</soapenv:Envelope>
</format>
<args>
<arg evaluator="xml" expression="get-property('axis2','Subscriber_alias') "/>
<arg evaluator="xml" expression="get-property('axis2','Recharge_Amount_alias') "/>
<arg evaluator="xml" expression="get-property('axis2','Recharge_Channel_alias') "/>
<arg evaluator="xml" expression="$body//ResponseDetail/jsonObject/dataSets/parameters[name='Subscriber']/value"/>
<arg evaluator="xml" expression="$body//ResponseDetail/jsonObject/dataSets/parameters[name='Recharge_Amount']/value"/>
<arg evaluator="xml" expression="$body//ResponseDetail/jsonObject/dataSets/parameters[name='Recharge_Channel']/value"/>
</args>
</payloadFactory>
</else>
</filter>
<script language="groovy"><![CDATA[def cuur_time=System.currentTimeMillis();
mc.setProperty('total_time',(cuur_time-mc.getProperty('start_time')));
mc.setProperty('resp_switch_time',(cuur_time-mc.getProperty('resp_switch_time')));]]></script>
<log level="custom" separator="|">
<property expression="get-property('total_time')" name="Total time taken"/>
<property expression="get-property('mvel_exe_time')" name="Total Mvel exe time taken"/>
<property expression="get-property('db_report_time')" name="Total DB report time taken"/>
<property expression="get-property('end_point_time')" name="Total End point call time taken"/>
<property expression="get-property('resp_switch_time')" name="Total Resp switch time taken"/>
</log>
<respond/>
</onComplete>
</aggregate>
</outSequence>
<faultSequence>
<property name="messageType" scope="axis2" type="STRING" value="application/soap+xml"/>
<payloadFactory media-type="xml">
<format>
<soapenv:Envelope xmlns:cbs="http://www.huawei.com/bme/cbsinterface/cbscommon" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wos="http://www.huawei.com/bme/cbsinterface/woservices">
<soapenv:Body>
<wos:WorkOrderResultMsg>
<ResultHeader>
<cbs:Version>1</cbs:Version>
<cbs:ResultCode>1</cbs:ResultCode>
<cbs:ResultDesc>Operation failed.</cbs:ResultDesc>
</ResultHeader>
</wos:WorkOrderResultMsg>
</soapenv:Body>
</soapenv:Envelope>
</format>
<args/>
</payloadFactory>
<respond/>
</faultSequence>
</resource>
</api>

WSO2 EI 6.6.0 Class Mediator not being able to use SOAP call return content

I have the following API in EI 6.6.0:
<?xml version='1.0' encoding='UTF-8'?>
<api xmlns="http://ws.apache.org/ns/synapse" name="sample" context="/sample">
<resource methods="POST">
<inSequence>
<payloadFactory media-type="xml">
<format>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<P xmlns="http://tempuri.org/">
<P1>$1</P1>
<P2>$2</P2>
<P3>$3</P3>
</P>
</soap:Body>
</soap:Envelope>
</format>
<args>
<arg evaluator="json" expression="$.p1" />
<arg evaluator="json" expression="$.p2" />
<arg evaluator="json" expression="$.p3" />
</args>
</payloadFactory>
<log level="full" />
<property name="Content-Type" value="text/xml;charset=UTF-8" scope="axis2"/>
<header name="Accept" scope="transport" value="text/xml"/>
<call>
<endpoint>
<wsdl Action="name_of_the_action" service="name_of_the_service" port="name_of_soap_port" uri="http://<ip>/path?WSDL" />
</endpoint>
</call>
<class name="my_mediator_package"></class>
<log level="full" />
<payloadFactory media-type="xml">
<format>
<retorno xmlns="">
<msg>$1</msg>
</retorno>
</format>
<args>
<arg evaluator="xml" expression="get-property('property_set_on_mediator')" />
</args>
</payloadFactory>
<property name="messageType" value="application/xml" scope="axis2" type="STRING" />
<respond />
</inSequence>
<outSequence>
</outSequence>
<faultSequence>
<property name="text" value="An unexpected error occured"/>
<property name="message" expression="get-property('ERROR_MESSAGE')"/>
<payloadFactory media-type="xml">
<format>
<error xmlns="">
<msg>$1</msg>
</error>
</format>
<args>
<arg evaluator="xml" expression="get-property('ERROR_MESSAGE')"/>
</args>
</payloadFactory>
<property name="messageType" value="application/json" scope="axis2" type="STRING"/>
<respond/>
</faultSequence>
</resource>
</api>
my mediate method content:
public boolean mediate(MessageContext synCtx) {
org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) synCtx)
.getAxis2MessageContext();
try {
// Getting the json payload to string
String jsonPayloadToString = JsonUtil.jsonPayloadToString(((Axis2MessageContext) synCtx)
.getAxis2MessageContext());
System.out.println("original payload : \n" + jsonPayloadToString + "\n");
I'm not being able to use the return from my SOAP call in my mediator so I can work on it.
When I run the API I get the following from my mediator code:
original payload:
{}
Is there a way so I can obtain the SOAP call returned envelope and use it in my mediator?
The JSON payload is coming as empty because you are calling a SOAP backend and getting a SOAP payload. You can use synCtx.getEnvelope() in your mediator to get the SOAPEnvelope from the response.
1- make sure the json payload is there. So, log the json properties inside your inSequence.
2- I'm not being able to use the return from my SOAP call in my mediator so I can work on it. you can see the response payload in your outSequence which currently is doing nothing.
3- According to your scenario which is simply calling a SOAP webservice, you do not need a class mediator. In other words, when you do not need manipulating the initial payload and then pass it to the destination service, logically implementing your own class mediator benefits you nothing.
Also, there are quite number of samples in https://docs.wso2.com/display/EI611 which will help you.
Please let me know if your problem is solved.

getting cognito acces_token with wso2 esb

I'm trying to get access token through AWS Cognito with client credentials but getting something else.
I'm doing this in wso2 Enterprise integrator 6.1.0
<payloadFactory media-type="xml">
<format>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/1999/XMLSchema/instance">
<soapenv:Header>
<Content-Type xmlns="">$1</Content-Type>
<Authorization xmlns="">$2</Authorization>
</soapenv:Header>
<soapenv:Body/>
</soapenv:Envelope>
</format>
<args>
<arg evaluator="xml" value="application/x-www-form-urlencoded"/>
<arg value="Basic 2354sdfmdtrerkdfdgkeryryrtwdasr345345twsdfwsedtr34"/>
</args>
</payloadFactory>
<log level="full"/>
<property name="DISABLE_CHUNKING" scope="axis2" type="STRING" value="true"/>
<endpoint key="validateUser-ext-ep"/>
</call>
<log level="full"/>
after this the response i'm getting is like this :
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Body>
<ns:binary xmlns:ns="http://ws.apache.org/commons/ns/payload">PCFET0NUWVBFIGh0bWw+CjxodG1sIGxhbiYWNrZ3JvdW5kLWN1c3RvbWl6YWJsZSBtb2RhbC1jb250ZW50LW1vYmlsZSI+CiAgICAgICAgICAgICAgICA8ZGl2PgogICAgICAgICAgICAgICAgICAgIDxkaXYgY2xhc3M9ImJhbm5lci1jdXN0b21pemFibGUiPgogICAgICAgICAgICAgICAgICAgICAgICA8Y2VudGVyPgogICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgIDwvY2VudGVyPgogICAgICAgICAgICAgICAgICAgIDwvZGl2PgogICAgICAgICAgICAgICAgPC9kaXY+CiAgICAgICAgICAgICAgICA8ZGl2IGNsYXNzPSJtb2RhbC1ib2R5Ij4KICAgICAgICAgICAgICAgICAgICA8cCBhbGlnbj0iY2VudGVyIj5BbiBlcnJvciB3YXMgZW5jb3VudGVyZWQgd2l0aCB0aGUgcmVxdWVzdGVkIHBhZ2UuPC9wPgogICAgICAgICAgICAgICAgICAgIDxicj4KICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgIDwvZGl2PgogICAgICAgICAgICA8L2Rpdj4KICAgICAgICA8L2Rpdj4KICAgIDwvZGl2Pgo8L2JvZHk+Cgo8L2h0bWw+Cg==</ns:binary>l
</soapenv:Body>
</soapenv:Envelope>
I don't know if I'm doing something wrong as in postman I sent the data same way I was getting the token there I passed the Authrization as Basic dfudne4r49859dfnw34598sdfs base64 endcoded(client:client_secret) and Content-Type : application/x-www-form-urlencoded in header and in params I passed grant_type: client_credential for this I was able to get the token but when I tried in wso2 esb I got the above error
the endpoint looks like :https://xxxxxxxxxx.us-east-1.amazoncognito.com/oauth2/token?grant_type=client_credentials
this is the API to get the Access token and it returns the access token
<?xml version="1.0" encoding="UTF-8"?>
<api context="/api/myService" name="my-service-api" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST" uri-template="/getToken">
<inSequence>
<script language="js"><![CDATA[var payload = mc.getPayloadXML();
var log = mc.getServiceLog();
var client_id = payload..*::client_id.toString();
var client_secret = payload..*::client_secret.toString();
mc.setProperty("client_id", client_id);
mc.setProperty("client_secret", client_secret);]]>
</script>
<payloadFactory media-type="json">
<format/>
<args/>
</payloadFactory>
<property name="ContentType" scope="axis2" type="STRING" value="application/x-www-form-urlencoded"/>
<property expression="fn:concat($ctx:client_id,':',$ctx:client_secret)" name="credentials" scope="default" type="STRING"/>
<property expression="fn:concat('Basic ', base64Encode($ctx:credentials))" name="Authorization" scope="transport" type="STRING" xmlns:ns="http://org.apache.synapse/xsd"/>
<property name="FORCE_POST_PUT_NOBODY" scope="axis2" type="BOOLEAN" value="true"/>
<property name="DISABLE_CHUNKING" scope="axis2" type="STRING" value="true"/>
<call>
<endpoint>
<http method="post" uri-template="https://xxxxxxxxxxx.amazoncognito.com/oauth2/token?grant_type=client_credentials"/>
</endpoint>
</call>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>
Call the above API like this :
<payloadFactory media-type="xml">
<format>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<root>
<client_id>$1</client_id>
<client_secret>$2</client_secret>
</root>
</soapenv:Body>
</soapenv:Envelope>
</format>
<args>
<arg evaluator="xml" expression="get-property('client_id')"/>
<arg evaluator="xml" expression="get-property('client_secret')"/>
</args>
</payloadFactory>
<property name="ContentType" scope="default" type="STRING" value="application/xml"/>
<property action="remove" name="REST_URL_POSTFIX" scope="axis2"/>
<call>
<endpoint>
<http method="post" uri-template="http://localhost:8280/api/myService/getToken"/>
</endpoint>
</call>
<script language="js">
<![CDATA[ var tokenContainer = mc.getPayloadJSON();
var log = mc.getServiceLog();
mc.setProperty("Authorization_header", tokenContainer.access_token);]]>
</script>
The Response will be
{"access_token":"LzSq94JoaUT2LwJlkEl35CXX0MdwqtUKIL8Wvi7dm4SqcSofR4xF5xBZre83MZXpHOr-Hg","expires_in":360,"token_type":"Bearer"}
In last script mediator you can access the token Access token throught getting it by mc.getPayloadJSON() this will give the same response as shwon above

Set Custom Payload in wso2 ESB or AM?

I have a rest API in ESB server.I need to set payload as follows
<fields>Name</fields>
<query>
<term>
<NUM>100</NUM>
</term>
</query>
any suggestion ?
you could use the payloadfactory mediator in ESB 4.8.1, take this xml as an example. you need to do some changes :-) but it´s an start:
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="JsonToXMLProxy"
transports="https http local"
startOnLoad="true"
trace="disable">
<description/>
<target>
<endpoint>
<address uri="http://www.w3schools.com/webservices/tempconvert.asmx" format="soap11"/>
</endpoint>
<inSequence>
<log>
<property name="TEMPERATURA_ENTRADA" expression="json-eval($.celsius)"/>
</log>
<payloadFactory media-type="xml">
<format>
<web:CelsiusToFahrenheit xmlns:web="http://www.w3schools.com/webservices/">
<web:Celsius>$1</web:Celsius>
</web:CelsiusToFahrenheit>
</format>
<args>
<arg evaluator="json" expression="$.celsius"/>
</args>
</payloadFactory>
<header name="Action"
value="http://www.w3schools.com/webservices/CelsiusToFahrenheit"/>
</inSequence>
<outSequence>
<log>
<property xmlns:p="http://www.w3schools.com/webservices/"
name="TEMPERATURA_SALIDA"
expression="//p:CelsiusToFahrenheitResponse/p:CelsiusToFahrenheitResult"/>
</log>
<payloadFactory media-type="json">
<format>
"Temperatura" : {
"EnFahrenheit" : $1
}
</format>
<args>
<arg xmlns:p="http://www.w3schools.com/webservices/"
evaluator="xml"
expression="//p:CelsiusToFahrenheitResponse/p:CelsiusToFahrenheitResult"/>
</args>
</payloadFactory>
<property name="messageType" value="application/json" scope="axis2"/>
<send/>
</outSequence>
</target>
</proxy>
Try payload/enrich mediators ..