WSO2 ESB 4.6.0 Get PayloadFactory format from configuration - 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.

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.

Dynamically add new element to json payload in WSO2 EI by using enrich mediator

I need to enrich existing json payload with new elements which is dynamically passed to existing payload instead of static value. Can anyone please help me?
Existing payload:
{
"Type":"CAR",
"Identifier":"2db23c39-9d3f-4e61-b3c5-e8725a2f1b90",
"ListingType":"New",
"SaleStatus":"For Sale"
}
Expected :
{
"Type":"CAR",
"Identifier":"2db23c39-9d3f-4e61-b3c5-e8725a2f1b90",
"ListingType":"New",
"SaleStatus":"For Sale",
"messageId":"urn:uuid:ccdafb72-c4"
}
Here messageId is ESB generated MessageID automatically.
<!-- ************API Request set to incomingRequest property************ -->
<property description="incomingRequest" expression="json-eval($.)" name="incomingRequest"
scope="default" type="STRING"/>
<payloadFactory media-type="json">
<format>$1</format>
<args>
<arg evaluator="xml" expression="get-property('incomingRequest')"/>
</args>
</payloadFactory>
<enrich description="">
<source type="inline" clone="true">
<messageId xmlns="">evaluate(get-property('operation','messageId'))
</messageId>
</source>
<target action="child" xpath="//jsonObject" />
</enrich>
<enrich>
<source clone="true" xpath="//jsonObject" />
<target type="body" />
</enrich>
<log level="full"/>
**Getting Wrong output**
{
"Type":"CAR",
"Identifier":"2db23c39-9d3f-4e61-b3c5-e8725a2f1b90",
"ListingType":"New",
"SaleStatus":"For Sale",
"messageId": "evaluate(get-property('operation','messageId'))"
}
Note:
I have followed this
Inline content on the enrich mediator is not evaluated. (considered as string)
So, we can use the property mediator to evaluate the expression.
<property name="prop1" expression="get-property('operation','messageId')"/>
Then we can use the above property in enrich mediator.
<enrich description="">
<source type="property" property="prop1"></source>
<target action="child" xpath="//jsonObject" />
</enrich>
BTW, In latest product versions, the Enrich mediator has the native JSON support.
More details in Here
I had similar scenario where i wanted to append some elements to a received json response:
First i got the response received into a property:
property expression="json-eval($)" name="RESPONSE" scope="default"
type="STRING"
Then i created a payload and added the new properties:
<payloadFactory media-type="json">
<format>
{"details": $1,
"timestamp":"$2",
"value":"$3"}
</format>
<args>
<arg evaluator="xml"
expression="get-property('RESPONSE')" />
<arg evaluator="xml"
expression="get-property('REQUEST_TIMESTAMP')" />
<arg evaluator="xml"
expression="get-property('VALUE')" />
</args>
</payloadFactory>
Then I returned the response using loopback. Note that REQUEST_TIMESTAMP and VALUE are defined as properties.

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

How to use the same payload factory JSON multiple times

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

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