Generate/Create New Query Parameter in WSO2-AM Sequence - wso2

I have a use case where create new set of query parameters(Say 4 new query parms) based on a query parameter come from request and send all query parameters(both newly created and old) into target server. How to achieve ? I have created a logic to split that query param come from request using Script Mediator and set into query param function like mc.setProperty("query.param.IndA", IndA);
<script function="restGET"
key="conf:repository/resources/scripts/rest.js" language="js"/>
<header name="To" scope="default" value="http://localhost:8443/res/c/r/cust/0.0.1/e"/>
But this doesn't set in request and reached to end server. Is that anyway better way than this approach ?

You can use REST_URL_POSTFIX property to append values to the target endpoint. Please refer the example which uses that property.

Answer given by #pubci is correct. Here I am adding some additional info on reading query parameter from request- To read the query parameter from the request no need to have script mediator , you can just use synapse xpath variable $url [1]. You can find a sample here[2]
[1] https://docs.wso2.com/display/ESB481/Synapse+XPath+Variables#SynapseXPathVariables-$trp
[2] https://jenananthan.wordpress.com/2015/09/22/how-to-read-dynamic-query-parameter-in-esbapim-synapse/

Related

WSO2 EI, getting HTTP request's message contents inside Sequence

I have been reading this documentation more than two weeks and it is still hard to understand some concepts.
Lets consider simple case. User makes request to our API then EI must call specific endpoint depending on requests body and show response.
Now there is the main problem on getting data of HTTP request.
I know that it could be accessed via Property Mediator, but this documentation does not help to understand anything.
if user makes GET request like this:
https://my-ei-domain.com:8280/myapi/mymethod?query1=1&query2=2
How it is possible to get values of query1 and query2
If user makes POST request with urlencoded form data how to get them?
If user makes POST request with JSON body like this:
{
"var1": "one",
"var2": 2,
"var3": {
"var3_var1": "two"
}
}
How to get values of these variables?
User makes POST request with XML body:
<body>
<var1>1</var1>
<var2>2</var2>
<var3>
<var1>3</var1>
</var3>
<body>
How to get these values?
Next problem. Sometimes users will send some variables inside Headers of requests. How to get them?
These cases are not described well in documentation. Even if they are, they just scattered across multiple pages.
if user makes GET request like this... . Using as described in
documentation URL Template or URI mapping
https://docs.wso2.com/display/EI611/Working+with+APIs
If user makes POST request with urlencoded form data how to get them... There is no another way to get data as described above. Actually Content-Type is used to identify how data transfered and I think (since ESB server use apache http core lib, widely used) will able to handle param values. Anyway that is easy to reproduce. Create rest service, send urlencode request and see how it works.
If user makes POST request with JSON body like this... Rules are described in documentation how to work with json in synapse engine https://docs.wso2.com/display/ESB480/JSON+Support (paragraph Accessing content from JSON payloads)
User makes POST request with XML body: to rest service ? to webservice? Anyway, xpath is commonly used to access certain parts of xml documents. But, synapse engine has to identify that xml document is received and parse it. Engine relies on Content-Type, details in here https://docs.wso2.com/display/ESB480/Working+with+Message+Builders+and+Formatters
Next problem. Sometimes users will send some variables inside Headers of requests. How to get them? In here how to read http headers in esb
Most of answers in documentation or easy to find by in web.
Your question is quite long and I can see you are requesting tutorials on how to achieve these. I hope the following Wso2 related tutorials will help you:
Enterprise Service Integration
Service Orchestration
Restful Integration
As an example let me explain the answers to a few of your questions:
To get information from a query like the following:
http://127.0.0.1:8280/pizzashop/api/menu/pizza?val=thin&type=crust
you may use property mediators like this:
<property name="Type" expression="$ctx:query.param.type"></property>
<property name="Val" expression="$ctx:query.param.val"></property>
WSO2 uses mediators to achieve most of the tasks. For example the payloadFactory mediator can be used to process the json and xml body and transform them. To process the following json
{ "payment":
{
"amount_lkr": "175.00",
"card_no": "1234-5678-9876-5432"
}
}
we can use payloadFactory mediator as follows:
<payloadFactory media-type="json">
<format>{"purchaseInformation": {"amount": "$1","cc": "$2"}}</format>
<args>
<arg evaluator="json" expression="$.payment.amount_lkr"></arg>
<arg evaluator="json" expression="$.payment.card_no"></arg>
</args>
</payloadFactory>
Similarly this mediator and others can be used to process xml as well in a POST request.
Please learn from the tutorials. The details are too broad to be explained in a single answer

How to get All element url in wso2ESB

I am working on wso2ESB version 4.9.
I have these URL:
http//192.168.0.1/getinfo/id=10&name=alice&from=AU$code=ddddd
http//192.168.0.1/adduser/id=11&name=bob&from=USA&tel=25644&city=newyork$code=ddddd
So i want get all of element of URL and separate code and call web-service without code variable . My URL element is changing but always has code variable.
Thanks
You can read query param string inside a script mediator (javascript). You can read it like this and manipulate as per your need. Then you can append it to the outgoing request.
mc.getProperty('QUERY_PARAMS')

WSO2 api manager not showing destination adress in API Usage by Destination

I'm using WSO2 AM 2.0.
I'm using dynamic endpoint throught custom sequence.
The problem is that my endpoint destination adress is not showing now
I have verifyed that it is insert empty in my analytics DB.
Is there any way to add this information?
In your custom sequence, please set another property named "ENDPOINT_ADDRESS" with the same value as the one you assign to "To" header. It will solve your problem.
For consequent invokes, the destination address will be set correctly.
ex:
<header name="To" value="https://localhost:9448/am/sample/pizzashack/v1/api/menu"/>
<property name="ENDPOINT_ADDRESS" value="https://localhost:9448/am/sample/pizzashack/v1/api/menu"/>

MD5 of javascript content in WSO2 ESB API

I have to deal with javascript content in API resource in WSO2 ESB. Specifically, I want to get the md5 hash from message which it is a javascript.
When I use messageType = application/javascript I get a binary, no JS message.
Can I do this? If I use binary to get md5, Will it work?
Finally, I solved this question as given below:
At first, I changed in axis2.xml the messageBuldir and messageFormater property to "text/javascript" pointing to "org.apache.axis2.format.PlainTextBuilder" and "org.apache.axis2.format.PlainTextFormatter".
At second I got the message in API with next expression: "s11:Body/child::[fn:position()=1] | s12:Body/child::[fn:position()=1]". With that, I can save the js message in a property.
Finally, I wrote a class mediator to calculate MD5 of property with JS.

How To Know The Client Url in Wso2ESB sing property Mediator

i have two proxys . i wish to filter the Client url in second proxy means
my first proxy implemented and send some data to 2nd proxy.Its working fine But my wish i dont want allow any other service to use my proxy .In that case i need to filter based On the URL. We have option in ESb get-property('To') for know to url of target service but How we can know that Which client is calling me
`<property name="client-host" expression="get-property('From')"/>`
this property not working
even i defined in Client and target service also but its returning "null"
<property name="client-host" expression="get-property('To')"/>
this property returning me present service URl
But i need Client Url Who is calling me...
how could i define it and where can i define either in Client Or Target
http://docs.wso2.org/wiki/display/ESB460/Property+Mediator#PropertyMediator-PropertiesToFromActionFaultToReplyToMessageID
can you enable the ws-addressing module to your proxies and see that "from" property is working or not?
Generally from,to action properties are captured from transport receiver..
WS-addressing will capture same properties and if they are already available, ws-addressing will overwrite them...