Call mediator end point type(XPath) - wso2-api-manager

In call mediator we can form endpoint using XPath (from the response).But the problem is endpoint is not calling.
<?xml version="1.0" encoding="UTF-8"?>
<api context="/xpath" name="call"
xmlns="http://ws.apache.org/ns/synapse">
<resource methods="GET" protocol="http">
<inSequence>
<call>
<endpoint key-expression="/root/a"/>
</call>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>
following response is from postman.
<root>
<a>http://www.mocky.io/v2/5ca6db71340000132f76b192</a>
</root>
Expected response :
<root>
<name>abcd</name>
<no>82382832</no>
</root>

Firstly, when using the resolving endpoint (using key-expression), we cannot give the URL directly [1]. We have to have the endpoint defined beforehand and should give only the key of the endpoint in the payload.
Secondly, for the key-expression to parse the xpath, the message should be built beforehand. Since call mediator is content-unaware, it will not build the message. Therefore, we should use a content aware mediator to build the message.
Following is the sample inSequence that'd work.
<inSequence>
<log level="full"/>
<call>
<endpoint key-expression="//a"/>
</call>
<respond/>
</inSequence>
Now the payload should be like
<root>
<a>testEndpoint</a>
</root>
Edit:
The endpoint named 'testEndpoint' should be defined with the backend url.
You need to define an endpoint [2]. For example, I'm using an address endpoint.
<endpoint xmlns="http://ws.apache.org/ns/synapse" name="testEndpoint">
<address uri="http://www.mocky.io/v2/5ca6db71340000132f76b192">
<suspendOnFailure>
<progressionFactor>1.0</progressionFactor>
</suspendOnFailure>
<markForSuspension>
<retriesBeforeSuspension>0</retriesBeforeSuspension>
<retryDelay>0</retryDelay>
</markForSuspension>
</address>
</endpoint>

Try this. I receieved needed response in Postman, but you should use POST method as your request has a Body.
<api xmlns="http://ws.apache.org/ns/synapse" name="call" context="/xpath">
<resource methods="POST">
<inSequence>
<property name="uri.var.httpendpointurl" expression="$body/root/a" scope="default" type="STRING"/>
<call>
<endpoint>
<http uri-template="{uri.var.httpendpointurl}"/>
</endpoint>
</call>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>

Related

how to add mediation for get token of WSO2 API Manager?

I Have This API And I Used the Message Mediation Below to Authenticate and Get the Token:
curl -X POST "URL" -H "accept: /" -H "Content-Type: application/x-www-form-urlencoded" -d "username=user&password=password"
result is:
enter image description here
<payloadFactory media-type="x-www-form-urlencoded">
<format>
{
"username":"user",
"password":"password"
}
</format>
<args>
</args>
</payloadFactory>
<header name="Content-Type" value="application/x-www-form-urlencoded" scope="transport" />
<property name="messageType" value="application/json" scope="axis2" type="STRING" />
<property name="REST_URL_POSTFIX" value="" scope="axis2" type="STRING" />
<call blocking="true">
<endpoint>
<http method="POST" uri-template="URL" />
</endpoint>
</call>
<property name="bearer-token" expression="json-eval($..token)" scope="default" type="STRING" />
<property name="REST_URL_POSTFIX" expression="get-property('resource')" scope="axis2" type="STRING" />
<header name="Authorization" expression="fn:concat('Bearer ', get-property('bearer-token'))" scope="transport" />
<payloadFactory media-type="x-www-form-urlencoded">
<format>$1</format>
<args>
<arg evaluator="xml" expression="get-property('request-body')" />
</args>
</payloadFactory>
But i have this error:
enter image description here
WSO2 API Manager Logs
INFO - LogMediator STATUS = Message dispatched to the main sequence. Invalid URL., RESOURCE = url, HEALTH CHECK URL = url
[2021-07-28 16:22:39,696] INFO - LogMediator STATUS = Message dispatched to the main sequence. Invalid URL., RESOURCE = url, HEALTH CHECK URL = url
Make sure that, you have provided the correct token endpoint url for the Call mediator
<call blocking="true">
<endpoint>
<http method="POST" uri-template="https://your-token-endpoint/oauth" />
</endpoint>
</call>

WSO2 ESB-How to pass dates with rest url

url likes:
http://ict.esg.ilfseducation.com`/gtrapi/api/profilesystem?`d1=17-10-2018&d2=17-10-2018
Iam trying this code to pass with dates dynamically d1 and d2 but not working.but how to pass dates with dynamically please any one suggest me.i have try this another url with sigle parms working fine.but dates was not working any one please suggeat me.
api:
<?xml version="1.0" encoding="UTF-8"?>
<api context="/gtrapi/api/profilesystem?" name="SCR_profileapi" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="GET" protocol="http" uri-template="/{date1}/{date2}">
<inSequence>
<property description="http" name="HTTP_SC" scope="axis2" type="STRING" value="404"/>
<log level="full"/>
<send>
<endpoint key="SCR_profileendpoint"/>
</send>
</inSequence>
<outSequence>
<switch source="get-property('axis2','HTTP_SC')">
<case regex="200">
<log description="200log" level="custom">
<property expression="$ctx:ERROR_CODE" name="200reserrorcode"/>
</log>
<send/>
</case>
<default>
<log description="reslog" level="custom">
<property expression="$ctx:ERROR_CODE" name="reserrorcode"/>
<property expression="$ctx:ERROR_MESSAGE" name="reserrormessage"/>
<property expression="get-property('axis2','HTTP_SC')" name="reshttpsc"/>
</log>
</default>
</switch>
<send/>
</outSequence>
<faultSequence>
<switch source="get-property('axis2','HTTP_SC')">
<case regex="200">
<log description="200log" level="custom">
<property expression="$ctx:ERROR_CODE" name="200reserrorcode"/>
</log>
</case>
<default>
<log description="reslog" level="custom">
<property expression="$ctx:ERROR_CODE" name="reserrorcode"/>
<property expression="$ctx:ERROR_MESSAGE" name="reserrormessage"/>
<property expression="get-property('axis2','HTTP_SC')" name="reshttpsc"/>
</log>
</default>
</switch>
<send/>
</faultSequence>
</resource>
</api>
endpoint:
<?xml version="1.0" encoding="UTF-8"?>
<endpoint name="SCR_profileendpoint" xmlns="http://ws.apache.org/ns/synapse">
<http uri-template="http://ict.esg.ilfseducation.com/gtrapi/api/profilesystem?d1={uri.var.service.date1}&d2={uri.var.service.d2}" method="get">
</http>"/>
</endpoint>

getting "org.apache.http.nohttpresponseexception the target server failed to respond" while calling wso2 http endpoint

I am getting below error while calling http endpoint:-
The endpoint is HTTP endpoint which I am using.
Also added timeout parameters in axis2.xml and axis2.blocking.xml.
But it did not work.
Can anyone provide any solution to it?
HTTPSender Unable to sendViaPost to url[http://*********]
org.apache.commons.httpclient.NoHttpResponseException: The server ****** failed to respond
at org.apache.commons.httpclient.HttpMethodBase.readStatusLine(HttpMethodBase.java:1976)
at org.apache.commons.httpclient.HttpMethodBase.readResponse(HttpMethodBase.java:1735)
at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:1098)
the esb proxy looks like below:--
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="mqListener"
startOnLoad="true"
statistics="disable"
trace="disable"
transports="http,https">
<target>
<inSequence>
<log description="print message" level="full">
<property name="message" value=""In listenAbc1Mq""/>
</log>
<payloadFactory media-type="json">
<format>
{
"orderId": ""
}
</format>
<args>
<arg evaluator="xml" expression="//verkkopvc/text()"/>
</payloadFactory>
<log description="print message" level="full">
<property name="message" value=""after payload factory""/>
</log>
<property name="OUT_ONLY" value="true"/>
<property name="DISABLE_CHUNKING"
scope="axis2"
type="STRING"
value="true"/>
<call blocking="true">
<endpoint key="abc"/>
</call>
<log description="print message" level="full">
<property name="message" value=""Out sequence""/>
</log>
</inSequence>
<outSequence/>
<description/>
</proxy>
I believe the error is printed by your http client. To fix it, do these.
1) Remove <property name="OUT_ONLY" value="true"/>
2) Add <respond/> mediator at the end of inSequence.

How to catch tcp client request message in wso2 esb proxy service

Im new comer to Wso2 esb. Now I'm working with tcp protocol and proxy service. I have some question. I send some message to the proxy service through tcp protocol. I need get this message to the property mediator in the proxy service. But my way not catch that value in the property mediator.
Here is my proxy service
<?xml version="1.0" encoding="UTF-8"?>
<proxy name="TCPProxyService" startOnLoad="true" transports="tcp" xmlns="http://ws.apache.org/ns/synapse">
<target>
<inSequence>
<property description="" expression="get-property('message')" name="MessageValue" scope="default" type="STRING"/>
<log description="" level="custom">
<property name="property_name" value="============================="/>
<property expression="get-property('MessageValue')" name="inputValue"/>
</log>
<sequence key="in-sequence"/>
<log/>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</target>
<parameter name="transport.tcp.responseClient">true</parameter>
<parameter name="transport.tcp.inputType">string</parameter>
<parameter name="transport.tcp.recordDelimiter">|</parameter>
<parameter name="transport.tcp.contentType">text/plain</parameter>
<parameter name="transport.tcp.port">6789</parameter>
<parameter name="transport.tcp.recordDelimiterType">character</parameter>
</proxy>
Here is my Java client for send tcp request
import java.io.*;
import java.net.*;
class TCPClient {
public static void main(String argv[]) throws Exception {
String delimiter = "|";
int modifiedSentence;
Socket clientSocket = new Socket("localhost", 6789);
DataOutputStream outToServer = new
DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
String message = "Pietoo,Klaas,also" + delimiter;
outToServer.writeBytes(message);
outToServer.flush();
while ((modifiedSentence = inFromServer.read()) > -1){
System.out.print((char)modifiedSentence);
}
clientSocket.close();
}
}
I tried to add below property mediator with get-property expression. What is the wrong with this.
property description="" expression="get-property('message')" name="MessageValue" scope="default" type="STRING"/>
try a <log level="full"/> in your proxy def, send it a message with your java client and have a look to wso2-esb-service.log : you should see a soap message like this
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<text xmlns="http://ws.apache.org/commons/ns/payload">your|data</text>
</soapenv:Body>
</soapenv:Envelope>
You can get the text value with :
<property xmlns:tp="http://ws.apache.org/commons/ns/payload" name="MessageValue" expression="$body/tp:text"/>

WSO2 APIM The system cannot infer the transport information from the /devtest/1.0/users/admin

Hi I have an API with a custom sequence (dblookup & switch sequence) that will verify if a user is existing on the database, if not it should send a http status 401: Unauthorized, below is the case code but I always get an error cannot infer the transport information. What is the correct way of sending the back the response message.
THanks,
Drew
<case regex="0">
<log level="custom">
<property name="ucount" expression="fn:concat('User login - ', get-property('employee_count'))"/>
</log>
<send>
<property name="RESPONSE" value="true" scope="default"/>
<property name="NO_ENTITY_BODY" action="remove" scope="axis2"/>
<property name="HTTP_SC" value="401" scope="axis2"/>
<header name="To" action="remove"/>
<payloadFactory media-type="xml">
<format>
<response>
<result>Unauthorized</result>
</response>
</format>
<args/>
</payloadFactory>
<send/>
</case>
ERROR:
TID: [0] [AM] [2014-12-02 08:16:30,852] ERROR {org.apache.synapse.core.axis2.Axis2Sender} - Unexpected error during sending message out {org.apache.synapse.core.axis2.Axis2Sender}
org.apache.axis2.AxisFault: The system cannot infer the transport information from the /devtest/1.0/users/admin URL.
at org.apache.axis2.description.ClientUtils.inferOutTransport(ClientUtils.java:81)
at org.apache.synapse.core.axis2.DynamicAxisOperation$DynamicOperationClient.executeImpl(DynamicAxisOperation.java:115)
at org.apache.axis2.client.OperationClient.execute(OperationClient.java:149)
at org.apache.synapse.core.axis2.Axis2FlexibleMEPClient.send(Axis2FlexibleMEPClient.java:482)
at org.apache.synapse.core.axis2.Axis2Sender.sendOn(Axis2Sender.java:59)
at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.send(Axis2SynapseEnvironment.java:338)
at org.apache.synapse.mediators.builtin.SendMediator.mediate(SendMediator.java:94)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:77)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:47)
at org.apache.synapse.config.xml.AnonymousListMediator.mediate(AnonymousListMediator.java:30)
at org.apache.synapse.config.xml.SwitchCase.mediate(SwitchCase.java:66)
at org.apache.synapse.mediators.filters.SwitchMediator.mediate(SwitchMediator.java:123)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:77)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:47)
at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:131)
at org.wso2.carbon.apimgt.gateway.handlers.ext.APIManagerExtensionHandler.mediate(APIManagerExtensionHandler.java:64)
at org.wso2.carbon.apimgt.gateway.handlers.ext.APIManagerExtensionHandler.handleRequest(APIManagerExtensionHandler.java:180)
at org.apache.synapse.rest.API.process(API.java:285)
at org.apache.synapse.rest.RESTRequestHandler.dispatchToAPI(RESTRequestHandler.java:83)
at org.apache.synapse.rest.RESTRequestHandler.process(RESTRequestHandler.java:64)
at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.injectMessage(Axis2SynapseEnvironment.java:220)
at org.apache.synapse.core.axis2.SynapseMessageReceiver.receive(SynapseMessageReceiver.java:83)
at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
at org.apache.synapse.transport.passthru.ServerWorker.processNonEntityEnclosingRESTHandler(ServerWorker.java:344)
at org.apache.synapse.transport.passthru.ServerWorker.run(ServerWorker.java:168)
at org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
TID: [0] [AM] [2014-12-02 08:16:30,853] INFO {org.apache.synapse.mediators.builtin.LogMediator} - STATUS = Executing default 'fault' sequence, ERROR_CODE = 0, ERROR_MESSAGE = Unexpected error during sending message out {org.apache.synapse.mediators.builtin.LogMediator}
Within send mediator you can not define other mediators.
Correct like this;
<property name="RESPONSE" value="true" scope="default"/>
<property name="NO_ENTITY_BODY" action="remove" scope="axis2"/>
<property name="HTTP_SC" value="401" scope="axis2"/>
<header name="To" action="remove"/>
<payloadFactory media-type="xml">
<format>
<response>
<result>Unauthorized</result>
</response>
</format>
<args/>
</payloadFactory>
<send>
endpoint>
<address uri="ADDRESS OF YOUR ENDPOINT"/>
</endpoint>
</send>
In my case there were invisible characters prepended to otherwise correct (Production/Sandbox) URL.
WSO2 seems very strict and didn't manage to interpret such definition, resulting in vague error:
The system cannot infer the transport information from the https://...
Notice the two spaces between the and https. It may have been a tabulator character.

Resources