Sending custom response from ESB - wso2

I have a string in a custom mediator in the inSequence . I want this string to be the final response from the esb . I want to sent this string back to the client as response.How can i do this?

After adding what ever the string you have inside the custom mediator you can send the current payload back to the client using the following config.
<header name="To" action="remove"/>
<property name="RESPONSE" value="true"/>
<send/>

Related

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.

Eclipse removes drop mediator after send

I tried to implement a validation with FAULT in it something like this
<on-fail>
<makefault version="soap11">
<code xmlns:tns="http://www.w3.org/2003/05/soap-envelope" value="tns:Receiver"/>
<reason value="Invalid Request!!!"/>
<role/>
</makefault>
<log level="full"/>
<property name="RESPONSE" value="true"/>
<header name="To" action="remove"/>
<send/>
<drop/>
</on-fail>
therefore I added DROP after SEND what function just as I wanted (without DROP I just recived FAULT message but the process did not stop what resulted into invoking some End points with incorrect inputs)
Then I needed to check on something and I open the same sequence in Eclipse and discovered that DROP got removed.
I tried to drag and drop DROP but got an error message that the SEND mediator cannot be followed by another mediator.
why?
do you happen to know a better way how to implement SEND + DROP so there is not a risk that I lose this when I open it in Eclipse?
thx a lot!
You do not need to add a Drop Mediator after a Send. It is invalid to add any mediators after the Send mediator, since the message context will be dropped after the send. May be instead of a SEND mediator you can try adding a Respond Mediator. Your use case is not so clear.
In Eclipse Developer Studio, we cannot specify any mediator after SEND mediator/ RESPOND mediator/ DROP mediator. This is because, ideally mediation flow should not continue after those mediators.
For your case, can you try CALL mediator[1] instead of SEND mediator followed by DROP mediator as follows
<on-fail>
<makefault version="soap11">
<code xmlns:tns="http://www.w3.org/2003/05/soap-envelope" value="tns:Receiver"/>
<reason value="Invalid Request!!!"/>
<role/>
</makefault>
<log level="full"/>
<property name="RESPONSE" value="true"/>
<header name="To" action="remove"/>
<call/>
<drop/>
</on-fail>
https://docs.wso2.com/display/ESB490/Call+Mediator

Get URI VAR from SynapseXPath Variable in WSO2 ESB API

I want to get a URI VAR value from request to call correct case in switch in WSO2 ESB API resource, like this:
<api name="apk" context="/apk"><resource methods="GET" uri-
template="/apk/{appName}"><inSequence><header name="App"
scope="transport" action="remove"/><switch source="get-
property('uri.var.appName')"><case regex="BEBE"><send><endpoint><http
method="GET" uri-template="http://localhost/apk/Bebe.apk></http>
</endpoint></send></case><case regex="CITAS"><send><endpoint><http
method="GET" uri-template="http://localhost/apk/Citas.apk></http>
</endpoint></send></case></switch></inSequence></resource></api>
In switch, source="get-property('uri.var.appName')" it's not correct.
Can I get this value using $url SynapseXpath valiables like $url?
What you have done is correct. Please try the same with a log mediator and see whether you get the value properly.
<log level="custom">
<property name="AppName" expression="get-property('uri.var.appName')"/>
</log>
Alternatively you can assign this to a property and use that inside your switch mediator.

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 can we receive the url into wso2 esb and give sucess response back to them

I am going to receive the url(Ex:- www.google.com) into my WS02 ESB and send response back to them.
How can we receive the url's(Ex:- www.google.com) into wso2 esb and give sucess response back to them.
If you want to response back to calling party with success HTTP response. Please set below property on your receiving path.
<property action="set" name="OUT_ONLY" value="true"/>
<property action="set" name="FORCE_SC_ACCEPTED" scope="axis2" value="true"/>
Does the ESB receive the URL as a property in the message context or are you referring to the URL where the request message is originated?