How to route by call method in proxy with WSO2? - web-services

I want to route process in proxy by call method name. I must get only allowable methods from web services. For example, webService1 has a(), b(), c() methods. webService2 has d(), e() methods. I want to get only a(), b() and d() methods for client. So, I must get request method to do this. I can do it by soap action property like this:
<property name="MY_SOAP_ACTION"
expression="get-property('Action')"
scope="default"
type="STRING"/>
<switch source="get-property('MY_SOAP_ACTION')">
<case regex="add">
<send>
<endpoint>
<address uri="http://localhost:1111/MockService1?wsdl"/>
</endpoint>
</send>
</case>
<case regex="sgrsControlInspection">
<send>
<endpoint>
<address uri="http://localhost:2222/MockService2?wsdl"/>
</endpoint>
</send>
</case>
But web services belong to another foundations and the methods haven't soap action annotation. So how can I do this without action property?

The SOAP action yields the method that you want to call in the end point. It does not need to have annotations specified. The way you have done seems correct. Also please remove the '?wsdl' part from the end point. Your end point configuration should be corrected as follows.
<address uri="http://localhost:1111/MockService1"/>
Other configurations seems fine.

Related

How can i use an JSON or SOAP attribute into a Path URL Param or a Query Param WSO2 Entrerprise Integrator

I'm pretty new on WSO2 IE and I'm trying to use a SOAP request to call a REST API.
This first part is OK but one of my API's needs to receive an attribute into its PATH or QUERY parameters. The attribute is sent by the soap call in it's body.
The question is, how can i get this body attribute and pass it in the PATH/QUERY param of the URL dynamically before i send it?
Picture of my architecture
Your requirement is to construct a dynamic URL. This can be easily done with the ESB Server. Here we have different options to construct this dynamic URL. Following is a sample proxy service that was created to achieve this.
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="sample_proxy"
startOnLoad="true"
statistics="disable"
trace="disable"
transports="http,https">
<target>
<inSequence>
<log level="custom">
<property expression="$body//target" name="Target endpoint"/>
</log>
<property name="uri.var.host" value="http://localhost:8280"/>
<property name="uri.var.context" value="services"/>
<property expression="$body//target" name="uri.var.resourcepath"/>
<call>
<endpoint>
<http method="GET"
uri-template="{uri.var.host}/{uri.var.context}/{uri.var.resourcepath}"/>
</endpoint>
</call>
<respond/>
</inSequence>
</target>
<description/>
</proxy>
We are sending the following request to this proxy service.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<target>sample_path</target>
</soapenv:Body>
</soapenv:Envelope>
In the above proxy service what we have done is construct the dynamic URL with the parameters from the soap message please refer to the blog post [1] to further clarify regarding dynamic URLs.
Here in order to extract the parameters from the body, we have used XPaths. Please refer to the documentation [2] to further clarify regarding XPaths. We have extracted the value inside the target element and added it to the end of the URL. Thus this will invoke the http://localhost:8280/services/sample_path from the ESB server.
If you are interested in constructing the URL with query parameters you can do that by concatenating the required URL and using an address endpoint [1].
[1]-https://dzone.com/articles/constructing-dynamic-endpoint-urls-in-wso2-esb
[2]-https://docs.wso2.com/display/EI6xx/Accessing+Properties+with+XPath#AccessingPropertieswithXPath-$ctx

how to pass query URL in wso2esb to endpoint

I'm having the following API configured in WSO2ESB:
<api xmlns="http://ws.apache.org/ns/synapse" name="service" context="/service">
<resource methods="POST">
<inSequence>
<call>
<endpoint>
<http method="POST" uri-template="https://webapps.localhost/service.php"/>
</endpoint>
</call>
<send/>
</inSequence>
<outSequence>
<send/>
</outSequence>
<faultSequence>
<log level="full"/>
</faultSequence>
</resource>
</api>
The call works fine and POST content goes fine to the endpoint.
curl -X POST -d "a=1&b=2" localhost:8280/service
from the service.php file I can extract POST parameters fine.
Now if I want to have dynamic GET parameters passed as is to the endpoint, what would be the way to do it?
curl -X POST -d "a=1&b=2" localhost:8280/service?c=3&d=4
I know (at least what i understood) property mediator could be used but this is for known parameters in the query url (for example, $url:c) but I don't want to limit it, just pass the query url as is to the destination endpoint.
Any help would be appreciated.
You can access the resource path through REST_URL_POSTFIX
<property name="path" expression="$axis2:REST_URL_POSTFIX"/>
According to your request URL, $ctx:path should contain ?c=3&d=4
Just to update here, use Address EndPoint instead of HTTP EndPoint (which is based on URI-Template)
Usually what I do is defining two variables on the endpoint's URI template, and then use the property mediators to set them.
Something like
<http method="POST" uri-template="https://webapps.localhost/service?c={uri.var.c}&d={uri.var.d}"
Then use the property mediator to set the property uri.var.c and uri.var.d with the intended values.

How to retrieve HTTP REST METHOD of current service at Run time in WSO2 AM Sequence?

How to retrieve HTTP REST METHOD(GET,PUT,POST,DELETE,OPTIONS) in WSO2 Api Manager's Sequence at runtime? I tried to $ctx:REST_METHOD which returns 'null' value.
<sequence name="ec_rest_dynamic_ep" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
<property expression="$ctx:REST_METHOD" name="restmethod"
scope="default" type="STRING"/>
<log>
<property expression="get-property('restmethod')" name="*******************REST_METHOD***********"/>
</log>
</sequence>
Basically, HTTP REST METHOD value of current service & URL context of that service needed to identify the service in order redirect the service to its endpoint dynamically at runtime.
Try the following property.
<property name="Http_Method" expression="get-property('axis2', 'HTTP_METHOD')"/>
You can find more useful properties in [1].
#Pubci's answer is correct. Here is another way.
<property name="Method" scope="transport" expression="$ctx:api.ut.HTTP_METHOD"/>
Some other properties can be found here.

Is it possible to extract and/or pass along a query parameter in ESB REST proxy service

I am building a ReST to ReST proxy service. I need to be able to pass along some query parameters to that service that are incoming with the request. E.g.
myhost.zz/proxyService?foo=1&bar=2
When I define such proxy - and later try to extract the value of 'foo' I get null.
So is it possible to achieve?
You shouls define an API (if you really want a proxy service, look at the end of this answer) :
<api xmlns="http://ws.apache.org/ns/synapse" name="proxyService" context="/proxyService">
<resource methods="POST GET OPTIONS DELETE PUT">
<inSequence>
<property name="FORCE_SC_ACCEPTED" value="true" scope="axis2" type="STRING"></property>
<log level="custom">
<property name="foo" expression="get-property('query.param.foo')"></property>
<property name="bar" expression="get-property('query.param.bar')"></property>
</log>
</inSequence>
</resource>
</api>
call it with this url : http://host:port/proxyService?foo=12&bar=14
look at wso2-esb-service.log : INFO __SynapseService foo = 12, bar = 14
in the "resource", you can define a uri-template (URL Style = uri-template) with, for exemple, "/{scope}/*" and then when you call your api with http://host:port/proxyService/toto?foo=12&bar=14, you can access the "scope" with get-property('uri.var.scope')
to send a REST request, use a http endpoint with a uri-template using the same logic : uri-template="http://other_host:port/Service/{uri.var.scope}/truc?abc={query.param.foo}&dfc={query.param.bar}"
-->
If you want to use a proxy service, you can access to query parameters like this :
request : http://esb:8280/services/MonService?param1=val1&param2=val2
<property name="PARAM1"
expression="tokenize(substring-after(syn:get-property('To'),'param1='),'&')"
scope="default"
type="STRING"/>
<property name="PARAM2"
expression="tokenize(substring-after(syn:get-property('To'),'param2='),'&')"
scope="default"
type="STRING"/>
you can use synapse xpath variable $url to read the query parameter values.
check this[1] for example.
Reading Dynamic query parameter in WSO2 APIM

How to set Payload for Recipient List Group Endpoint?

I have a sequence that is invoking multiple endpoints. My sequence is:
<sequence xmlns="http://ws.apache.org/ns/synapse" name="GetAllData">
<switch xmlns:ns="http://org.apache.synapse/xsd" xmlns:cct="http://www.tempuri.org" xmlns:tns="http://www.tempuri.org" source="//tns:IDFilter/cct:ID">
<case regex=".?">
<log level="custom">
<property name="Property2" value="----------------Inside switch 1 ? ---------------"/>
</log>
<filter xpath="//tns:ChildIDFilter/cct:ID='?'">
<then>
<log level="custom">
<property name="prop1" value="------Inside Then------------------"/>
</log>
**<payloadFactory>
<format>
<select_all_ID_operation xmlns:cir="http://tempuri.org"/>
</format>
</payloadFactory>
<payloadFactory>
<format>
<select_all_ChildID_operation xmlns="http://tempuri.org"/>
</format>
</payloadFactory>
<send>
<endpoint key="ID_Service_EPS"/>
</send>**
</then>
<else>
<log level="custom">
<property name="prop2" value="------Inside Else------------------"/>
</log>
<payloadFactory>
<format>
<select_all_ID_operation xmlns="http://tempuri.org"/>
</format>
</payloadFactory>
<send receive="ValidateAllData">
<endpoint key="ID_Endpoint"/>
</send>
</else>
</filter>
</case>
</switch>
</sequence>
Now in my ID_EPS endpoint, i have two wsdl endpoints and for them i have to pass payload to get data from both wsdl's, but this is not happenning. My question is how to invoke recipient endpoint contaning different wsdl Endpoints and setting payload for it and finally get the concatinated result. Looking forward to your answers.Thanks in advance
In the management console-->endpoint menu, you can find the recipient list endpoint, where provide your both endpoints, and refer that recipient endpoint from your sequence
To send messages to two endpoints (in your case two wsdl endpoints) using recipient list endpoint, the message format accepted by both endpoints should be identical.
If so, you can send messages to both endpoints using a recipient list endpoint and then aggregate the response messages and construct the concatenated result.
Refer to following resource to find a sample configuration of Recipient list endpoint.
http://docs.wso2.org/wiki/display/ESB460/Sample+62%3A+Routing+a+Message+to+a+Dynamic+List+of+Recipients+and+Aggregating+Responses
If two endpoint message formats are not identical, you have to implement a service chaining scenario or you can clone the message and create two different branches for two endpoints and construct the payload required by each service endpoint and send to the service endpoint within that branch. Then you can aggregate the responses received from both branches and construct the concatenated result.