How to use the same payload factory JSON multiple times - json

I have a payload factory in the requestpayloadsequence file in the WSO2 project. I have to use the same payload factory multiple times using some filter. Currently i have to repeat the same piece of code (shown below) for each filter. Is there any way that i can use the same payload factory without using the same code multiple times.
Is there any way i can set this payload factory to some parameter value. Could any body help how to achieve this.Below is the payloadfacotry code I am currently using.
<payloadFactory media-type="json">
<format> {
"1st": "$1",
"2nd": "$2",
"3rd": "$3",
"4th": "$4",
}
</format>
<args>
<arg evaluator="xml" expression="get-property('1')"/>
<arg evaluator="xml" expression="get-property('2')"/>
<arg evaluator="xml" expression="get-property('3')"/>
<arg evaluator="xml" expression="get-property('4')"/>
</args>
</payloadFactory>

You can use a sequence template :
<template xmlns="http://ws.apache.org/ns/synapse" name="PayloadTemplate">
<parameter name="P1"/>
<parameter name="P2"/>
<parameter name="P3"/>
<parameter name="P4"/>
<sequence>
<payloadFactory media-type="json">
<format> {
"1st": "$1",
"2nd": "$2",
"3rd": "$3",
"4th": "$4",
}
</format>
<args>
<arg evaluator="xml" expression="$func:P1"/>
<arg evaluator="xml" expression="$func:P2"/>
<arg evaluator="xml" expression="$func:P3"/>
<arg evaluator="xml" expression="$func:P4"/>
</args>
</payloadFactory>
</sequence>
</template>
Call this template in your mediation :
<call-template target="PayloadTemplate">
<with-param name="P1" value="{get-property('WWW')}"/>
<with-param name="P2" value="{get-property('XXX')}"/>
<with-param name="P3" value="{get-property('YYY')}"/>
<with-param name="P4" value="{get-property('ZZZ')}"/>
</call-template>

Try using Clone Mediator
URL :
https://docs.wso2.com/display/ESB403/Clone+Mediator

Related

Apache ant parsing json response

I am trying to run a command on a build script. The following code returns a json as response but how can I access the values inside the json response?
<exec executable="curl" failonerror="${env.curlfailonerror}" outputproperty="curlout" errorproperty="curlerror">
<arg value="https://api.appcenter.ms/v0.1/apps/${ownerName}/${app_Name}/uploads/releases"/>
<arg value="-X" />
<arg value="POST"/>
<arg value="--header"/>
<arg value="accept: application/json"/>
<arg value="--header"/>
<arg value="Content-Type: application/json"/>
<arg value="--header"/>
<arg value="X-API-Token: ${appCenterApiToken}"/>
</exec>
<echo>Curl output ${curlout} ${curlerror}</echo>
<property name="otherCrap" value="${curlout.upload_domain}" />
<echo>Curl output ${curlout.upload_domain}</echo>
This is the response I get back but not sure how I can read each of the values.
{
"id": "{ID}",
"package_asset_id": "{PACKAGE_ASSET_ID}",
"upload_domain": "https://file.appcenter.ms",
"token": "{TOKEN}",
"url_encoded_token": "{URL_ENCODED_TOKEN}"
}
Any help would be greatly appreciated.

WSO2 API Manager Tool: Unable to convert JSONtoSOAP and then SOAPtoJSON to communicate between mock backend and API

Following the guidelines from WSO2 Documentation at: https://docs.wso2.com/display/AM260/Convert+a+JSON+Message+to+SOAP+and+SOAP+to+JSON
The intended response was
I revised it a couple times but keep getting "400: bad request error"
my curl and the error
EDIT¹: After running tests I found out that the issue is just with the SOAPtoJSON conversion. When I POST without the OUT (SOAPtoJSON) sequence, I get the XML answer exactly as intended as shown here
Thats my JSONtoSOAP.xml:
<?xml version="1.0" encoding="UTF-8"?>
<sequence name="JSONtoSOAP" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
<payloadFactory media-type="xml">
<format>
<soap12:Envelope xmlns:soap12="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap12:Body>
<CheckPhoneNumber xmlns="http://ws.cdyne.com/PhoneVerify/query">
<PhoneNumber>$1</PhoneNumber>
<LicenseKey>$2</LicenseKey>
</CheckPhoneNumber>
</soap12:Body>
</soap12:Envelope>
</format>
<args>
<arg evaluator="xml" expression="//request/PhoneNumber" literal="true"/>
<arg evaluator="xml" expression="//request/LicenseKey" literal="true"/>
</args>
</payloadFactory>
<property name="messageType" scope="axis2" type="STRING" value="application/soap+xml"/>
</sequence>
Thats my SOAPtoJSON.xml:
<?xml version="1.0" encoding="UTF-8"?>
<sequence name="SOAPtoJSON" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
<log level="custom" separator=",">
<property name="TRACE" value="Global Mediation Extension"/>
</log>
<payloadFactory media-type="xml">
<format>
<CheckPhoneNumber xmlns="http://ws.cdyne.com/PhoneVerify/query">
<PhoneNumber>$1</PhoneNumber>
<LicenseKey>$2</LicenseKey>
</CheckPhoneNumber>
</format>
<args>
<arg evaluator="xml" expression="//request/PhoneNumber"/>
<arg evaluator="xml" expression="//request/LicenseKey"/>
</args>
</payloadFactory>
<property name="messageType" scope="axis2" type="STRING" value="application/json"/>
</sequence>
I'm really new to the technology. And I just followed the steps from the documentation as mentioned. Maybe is was just a silly mistake (even though I revised it several times)
Maybe someone got the same problem and can help me out.
Thanks in advance.
Here is a new feature which generates a REST interface for your SOAP service. Try that. It supports SOAP-REST conversion automatically.
https://docs.wso2.com/display/AM260/Generate+REST+APIs+from+SOAP+Backends

wso2 API Manager - How to publish an API using a template?

I have been experimenting with wso2 API Manager 1.8.0 to expose a back end(http/post) system as a REST API. I am able to get it working, but I had to edit the API configuration through the Service Bus source view as pasted below. I would like to have the same template used for all of the new APIs that get published using API Publisher to ensure that we don't have to edit the service bus source every time an API get created.
Please suggest any options that you may have used..
<inSequence>
<script language="js" key="transform_script" function="buildQueryString"/>
<property name="uri.query" expression="get-property('queryString')"/>
<filter source="$ctx:AM_KEY_TYPE" regex="PRODUCTION">
<then>
<property name="Authorization"
expression="fn:concat('Basic ', base64Encode('testuser:testuser#1'))"
scope="transport"/>
<send>
<endpoint name="testuser--AddNumbers_APIsandboxEndpoint_0">
<http method="POST"
uri-template="https://testhost:8443/test/http/6731cc67-3850-4b9b-b486-62cf2a664b46?${uri.query}"/>
</endpoint>
</send>
<bam>
<serverProfile name="bam-profile">
<streamConfig name="org_wso2_apimgt_statistics_destination" version="1.0.0"/>
</serverProfile>
</bam>
</then>
<else>
<property name="Authorization"
expression="fn:concat('Basic ', base64Encode('testuser:testuser#1'))"
scope="transport"/>
<send>
<endpoint name="testuser--AddNumbers_APIsandboxEndpoint_0">
<http method="POST"
uri-template="https://testhost:8443/test/http/6731cc67-3850-4b9b-b486-62cf2a664b46?${uri.query}"/>
</endpoint>
</send>
<bam>
<serverProfile name="bam-profile">
<streamConfig name="org_wso2_apimgt_statistics_destination" version="1.0.0"/>
</serverProfile>
</bam>
</else>
</filter>
</inSequence>
<outSequence>
<payloadFactory media-type="json">
<format>
{
"apiName": "$1",
"apiVersion": "$2",
"runResponse":
{
"runId": "$3",
"runStart": "$4",
"runEnd": "$5",
"flowResponse": "$6",
"flowResult": "$7"
}
}
</format>
<args>
<arg evaluator="xml" expression="get-property('apiName')"/>
<arg evaluator="xml" expression="get-property('apiVersion')"/>
<arg evaluator="json" expression="$.runResponse.runReturn.item[0].value"/>
<arg evaluator="json" expression="$.runResponse.runReturn.item[3].value"/>
<arg evaluator="json" expression="$.runResponse.runReturn.item[4].value"/>
<arg evaluator="json" expression="$.runResponse.runReturn.item[5].value"/>
<arg evaluator="json" expression="$.runResponse.runReturn.item[6].value"/>
</args>
</payloadFactory>
<property name="messageType" value="application/json" scope="axis2"/>
<send/>
</outSequence>
There appears to be some documentation for the older versions of API Manager about the API template under <APIM_HOME>/repository/resources/api-templates/default_api_template.xml file.
With API Manager 1.8.0, I can not find the same documentation page but editing the velocity_template.xml works for non-prototype API definitions.
I am still trying to incorporate my requirements into it.
Actually you want to do some do some transformation for your message. APIM 1.8 supports mediation extension. where you can add your own in,out and fault sequence and have your transformation there. this mediation extension support for globally and per api basis. you can find the details here[1]
so if you create a global level sequence it will be applied to all apis.In your case you have to create one global insequnce to add basic oauth related stuff and one global out sequence to add payload factory related stuff
1.https://docs.wso2.com/display/AM180/Adding+Mediation+Extensions

Iterate Mediator over json arrays

I'd like to iterate the results array of a json message
{
"results":[
{
"category":"mdl",
"subcategory":"ip",
"ip":"103.14.120.121",
"subtype":"IP-MDL",
"date":"1405074556",
"longitude":"75.3333",
"latitude":"19.8833",
"country":"India",
"city":"Aurangabad",
"organization":"Good Domain Registry Private Limited",
"isp":"Good Domain Registry Private Limited"
},
{
"category":"mdl",
"subcategory":"ip",
"ip":"108.162.198.96",
"subtype":"IP-MDL",
"date":"1405074556",
"longitude":"-122.3933",
"latitude":"37.7697",
"country":"United States",
"city":"San Francisco",
"organization":"CloudFlare",
"isp":"CloudFlare"
}
]
}
unfortunately iteration mediator doesn't seem to accept non xpath expression
anyone knows how to iterate over the previous message json results the same way i do it in xml ?
I'd like to use ESB json-native support but doesn't seem to be able to break down json messages as with XML (i've tried iterate mediator, json payload factory and script mediator without any success)
<iterate expression="json-eval(results)">
<target>
<sequence>
<property name="messageType" value="application/json" scope="axis2"/>
<payloadFactory media-type="json">
<format>
{
"event":{
"providerName":"$1",
"providerSource":"$2",
"providerClassification":"$3",
"providerVersion":"$4",
"body":{
"results":{$5}
}
}
}
</format>
<args>
<arg value="bdigital"/>
<arg evaluator="json" expression="$.results.category"/>
<arg evaluator="json" expression="$.results.subcategory"/>
<arg value="1.0"/>
<arg evaluator="json" expression="$.results"/>
</args>
</payloadFactory>
<call>
<endpoint>
<http method="post"
uri-template="http://BD-VM-PP-CIC01:8280/services/CICQueue"/>
</endpoint>
</call>
</sequence>
</target>
</iterate>
thanks in advance,
You could do the following?
<property name="messageType" value="application/xml" scope="axis2" type="STRING"></property>
<iterate xmlns:ns="http://org.apache.synapse/xsd" continueParent="true" expression="//jsonObject/results" id="MyIterator">
<target>
<sequence>
<property name="cat" expression="json-eval($.results.category)" type="STRING"></property>
etc.
There is a new version in WSO2 EI in which you can iterate over JSON using JSON Path. You can do something like below.
<iterate id="iterate-over-users" preservePayload="true" attachPath="json-eval($.results)" expression="json-eval($.results)">
<target>
<sequence>
<send>
<endpoint>
<http method="POST" uri-template="http://backend.com"/>
</endpoint>
</send>
</sequence>
</target>
</iterate>
Refer to the following blog for more info. https://medium.com/#arunans23/iterate-over-json-payload-with-wso2-enterprise-integrator-8ccb9cdd2c70

WSO2 ESB 4.6.0 Get PayloadFactory format from configuration

I am trying to put together a sequence that pulls out properties from an initial request and stores those properties until the response comes back, replace certain regions and send that to another service. I have successfully accomplished this but I want to refine my approach by hosting the payloadFactory format in the registry and reference it using the key attribute within the format tag.
Here is an example of what I want to accomplish: Sajini's Blog
Here is the code I am currently working with:
<sequence xmlns="http://ws.apache.org/ns/synapse">
<payloadFactory>
<format>
<AddEditEmailAddress xmlns="http://tempuri.org/">
<eResponse xmlns:a="http://schemas.datacontract.org/2004/07/NoelGroup.Users.Core.Task.BusinessLayer" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> $1
<Success xmlns="http://schemas.datacontract.org/2004/07/NoelGroup.Users.Task.BusinessLayer">true</Success> $2
</eResponse>
<personId>$3</personId>
</AddEditEmailAddress>
</format>
<args>
<arg xmlns:ns="http://org.apache.synapse/xsd" expression="get-property('default','BROKEN_OBJECT')"/>
<arg xmlns:ns="http://org.apache.synapse/xsd" xmlns:a="http://schemas.datacontract.org/2004/07/NoelGroup.Users.Core.Task.BusinessLayer" expression="get-property('default','EMAILS')"/>
<arg xmlns:ns="http://org.apache.synapse/xsd" expression="get-property('default','PERSON_ID')"/>
</args>
</payloadFactory>
And here is what I desire:
<sequence xmlns="http://ws.apache.org/ns/synapse">
<payloadFactory>
<format key="conf:/User_Sync_Assets/Email_Req.xml"/>
<args>
<arg xmlns:ns="http://org.apache.synapse/xsd" expression="get-property('default','BROKEN_OBJECT')"/>
<arg xmlns:ns="http://org.apache.synapse/xsd" xmlns:a="http://schemas.datacontract.org/2004/07/NoelGroup.Users.Core.Task.BusinessLayer" expression="get-property('default','EMAILS')"/>
<arg xmlns:ns="http://org.apache.synapse/xsd" expression="get-property('default','PERSON_ID')"/>
</args>
</payloadFactory>
This feature will be available in ESB 4.7.0 release.