How to modify header wso2 - soap

I am creating a POC . I am trying to send "Authorization : Bearer " to a webservice proxy .
How can i do this using Class Mediation in WSO2ESB .
And also how can i modify the old payload by adding the new payload .... with in class Mediation

Please try the following:
<property name="Authorization" value="Bearer" scope="transport"/>
Changing the payload can be done in a number of ways, one way to change the whole payload is using the payloadFactory.
<payloadFactory>
<format>
<m:getquote xmlns:m="http://services.samples">
<m:request>
<m:symbol>$1</m:symbol>
</m:request>
</m:getquote>
</format>
<args>
<arg expression="//m0:Code" xmlns:m0="http://services.samples"/>
</args>
</payloadFactory>
Or the same can be achieved with the enrich mediator:
<enrich>
<source type="inline" clone="false">
<m:getquote xmlns:m="http://services.samples">
<m:request>
<m:symbol>$1</m:symbol>
</m:request>
</m:getquote>
</source>
<target type="body" action="replace" />
</enrich>
<enrich>
<source type="property" clone="true" property="PATIENTS"/>
<target xmlns:m="http://services.samples" xpath="//m:symbol"/>
</enrich>
Hope this helps
Regards, nidkil

Related

REST WSO2 Kafka

I've installed WSO2 Integration Studio version 6.5.0 in my Windows workstation and created a project using the Kafka Consumer and Producer built-in template.
api.xml:
<?xml version="1.0" encoding="UTF-8"?>
<api context="/publishweatherdata" name="WeatherDataPublishAPI" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST">
<inSequence>
<kafkaTransport.init>
<bootstrapServers>localhost:9092</bootstrapServers>
<keySerializerClass>org.apache.kafka.common.serialization.StringSerializer</keySerializerClass>
<valueSerializerClass>org.apache.kafka.common.serialization.StringSerializer</valueSerializerClass>
<acks>all</acks>
<requestTimeout>10000</requestTimeout>
<timeout>8000</timeout>
<metadataFetchTimeout>5000</metadataFetchTimeout>
<maxPoolSize>50</maxPoolSize>
</kafkaTransport.init>
<kafkaTransport.publishMessages>
<topic>weatherdatatopic</topic>
</kafkaTransport.publishMessages>
<payloadFactory media-type="json">
<format>
{"topic":"$1", "partition":"$2", "offset":"$3"}
</format>
<args>
<arg evaluator="xml" expression="$ctx:topic"/>
<arg evaluator="xml" expression="$ctx:partition"/>
<arg evaluator="xml" expression="$ctx:offset"/>
</args>
</payloadFactory>
<property name="messageType" scope="axis2" type="STRING" value="application/json"/>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>
weatherdatatransmitinboundEP.xml:
<?xml version="1.0" encoding="UTF-8"?>
<inboundEndpoint class="org.wso2.carbon.inbound.kafka.KafkaMessageConsumer" name="WeatherDataTransmitInboundEP" onError="WeatherDataErrorSeq" sequence="WeatherDataProcessSeq" suspend="false" xmlns="http://ws.apache.org/ns/synapse">
<parameters>
<parameter name="sequential">true</parameter>
<parameter name="interval">10</parameter>
<parameter name="coordination">true</parameter>
<parameter name="inbound.behavior">polling</parameter>
<parameter name="value.deserializer">org.apache.kafka.common.serialization.StringDeserializer</parameter>
<parameter name="topic.name">weatherdatatopic</parameter>
<parameter name="poll.timeout">100</parameter>
<parameter name="bootstrap.servers">localhost:9092</parameter>
<parameter name="group.id">hello</parameter>
<parameter name="contentType">application/json</parameter>
<parameter name="key.deserializer">org.apache.kafka.common.serialization.StringDeserializer</parameter>
<parameter name="class">org.wso2.carbon.inbound.kafka.KafkaMessageConsumer</parameter>
</parameters>
</inboundEndpoint>
WeatherDataPublishService.xml:
<?xml version="1.0" encoding="UTF-8"?>
<proxy name="WeatherDataPublishService" startOnLoad="true" transports="http https" xmlns="http://ws.apache.org/ns/synapse">
<target>
<inSequence>
<kafkaTransport.init>
<bootstrapServers>localhost:9092</bootstrapServers>
<keySerializerClass>org.apache.kafka.common.serialization.StringSerializer</keySerializerClass>
<valueSerializerClass>org.apache.kafka.common.serialization.StringSerializer</valueSerializerClass>
<acks>all</acks>
<requestTimeout>10000</requestTimeout>
<timeout>8000</timeout>
<metadataFetchTimeout>5000</metadataFetchTimeout>
<maxPoolSize>50</maxPoolSize>
</kafkaTransport.init>
<kafkaTransport.publishMessages>
<topic>weatherdatatopic</topic>
</kafkaTransport.publishMessages>
<payloadFactory media-type="json">
<format>
{"topic":"$1", "partition":"$2", "offset":"$3"}
</format>
<args>
<arg evaluator="xml" expression="$ctx:topic"/>
<arg evaluator="xml" expression="$ctx:partition"/>
<arg evaluator="xml" expression="$ctx:offset"/>
</args>
</payloadFactory>
<property name="messageType" scope="axis2" type="STRING" value="application/json"/>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</target>
</proxy>
now I can send post request to http://localhost:8290/publishweatherdata and get it in the kafka topic. Also I can receive message produced by kafka in wso2. How can I send message to external service from wso2? I think I should to use
<endpoint [name="string"] [key="string"]>
address-endpoint | default-endpoint | wsdl-endpoint | load-balanced-endpoint | fail-over-endpoint
</endpoint>
but I have no idea where it must be added and how configured
You can use a call mediator [1] or a send mediator [2] to achieve your use case. Within the mediator, you can define the desired endpoint you want to invoke. Please refer to the following sample configuration. Here we have used a call mediator to invoke the external endpoint http://run.mocky.io/v3/9cf4b844-57c1-4fa5-a101-881dc36385bd.
<call>
<endpoint>
<address uri="http://run.mocky.io/v3/9cf4b844-57c1-4fa5-a101-881dc36385bd"/>
</endpoint>
</call>
In your use case, if you have finish building the desired payload after the payload factory mediator, you can use the call mediator after the payload factory mediator to invoke the external endpoint. Here the payload build by the payload factory mediator will be used to invoke the external endpoint.
<payloadFactory media-type="json">
<format>
{"topic":"$1", "partition":"$2", "offset":"$3"}
</format>
<args>
<arg evaluator="xml" expression="$ctx:topic"/>
<arg evaluator="xml" expression="$ctx:partition"/>
<arg evaluator="xml" expression="$ctx:offset"/>
</args>
</payloadFactory>
<call>
<endpoint>
<address uri="http://run.mocky.io/v3/9cf4b844-57c1-4fa5-a101-881dc36385bd"/>
</endpoint>
</call>
<property name="messageType" scope="axis2" type="STRING" value="application/json"/>
<respond/>
Further, in the case of WeatherDataTransmitInboundEP(inbound endpoint), it will read the messages published to Kafka, and then the message is sent to the sequence defined in the inbound endpoint. If you want to send the messages consumed by WeatherDataTransmitInboundEP to an external endpoint you have to follow a different approach.
In your case, WeatherDataProcessSeq is invoked after reading messages from Kafka. So if your requirement is to send the messages in Kafka you will need to define the call or the send mediator in the WeatherDataProcessSeq.
If you want further clarification regarding the call/send mediator please refer to the blog post [3].
[1]-https://docs.wso2.com/display/EI6xx/Call+Mediator
[2]-https://docs.wso2.com/display/EI600/Send+Mediator
[3]-https://www.yenlo.com/blog/wso2torial-to-send-or-not-to-send-that-is-your-choice
I just add
<send>
<endpoint>
<http method="post" statistics="enable" trace="enable" uri-template="http://localhost:8081/api">
<property name="name" scope="axis2" value="messageValue"/>
<suspendOnFailure>
<initialDuration>-1</initialDuration>
<progressionFactor>-1</progressionFactor>
<maximumDuration>0</maximumDuration>
</suspendOnFailure>
<markForSuspension>
<retriesBeforeSuspension>0</retriesBeforeSuspension>
</markForSuspension>
</http>
</endpoint>
</send>
to sequence and get what I want

How to use the Fault Mediator to Respond with a Fault

I am trying to use the WSO2 EI Fault mediator but the Fault Response always gives me a void response message. The point here is to try to build custom Fault messages. My Sample API code:
<?xml version="1.0" encoding="UTF-8"?>
<api context="/test" name="TestFaultAPI" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="GET" uri-template="/{teste}">
<inSequence>
<log description="" level="full" separator=";">
<property expression="get-property('uri.var.teste')" name="Path"/>
<property expression="get-property('transport','Content-Type')" name="ContentType"/>
</log>
<filter xpath="get-property('uri.var.teste') != '1234'">
<then>
<log description="" level="full">
<property name="Entrada" value=""Fault Way""/>
</log>
<makefault description="" version="soap11">
<code value="soap11Env:Client" xmlns:soap11Env="http://schemas.xmlsoap.org/soap/envelope/"/>
<reason value="Recruso incorreto"/>
<detail>incorrect Resource</detail>
</makefault>
<respond/>
</then>
<else>
<log level="full">
<property name="Caminho" value=""Corret Way""/>
</log>
<payloadFactory media-type="json">
<format>{
"Status":"Correct Resource"
}</format>
<args/>
</payloadFactory>
<respond/>
</else>
</filter>
</inSequence>
<outSequence/>
<faultSequence>
<log level="full" separator=";">
<property name="Caminho" value=""Entrou Fault Sequence""/>
</log>
<property description="" name="HTTP_SC" scope="axis2" type="INTEGER" value="400"/>
<payloadFactory media-type="json">
<format>{
"codigoErro":$1,
"detalhe":$2
}</format>
<args>
<arg evaluator="xml" expression="get-property('HTTP_SC','axis2')"/>
<arg evaluator="xml" expression="get-property('ERROR_DETAIL')"/>
</args>
</payloadFactory>
<respond/>
</faultSequence>
</resource>
</api>
So I have two questions:
1 - How can I make the custom fault messages response?
2 - Is it possible to use the fault mediator to change the sequence to the 'Fault Sequence' ?
I will put the answers here. Those are results of my study and experiences:
1 - How can I make the custom fault messages response?
The way to do it is use filter mediator for test a rule and build the messages to respond your request.
2 - Is it possible to use the fault mediator to change the sequence to the 'Fault Sequence'?
No. The fault sequence only start to be executed if a fault occurs. You can´t change the execution path by yourself using a mediator.
So It´s possible that other people with more experience then I answer different informations.
[],s
Marcello

WSO2 ESB with x-www-form-urlencoded messaage type service call

I have used following code for sending x-www-form-urlencoded message to backed server.
<endpoint xmlns="http://ws.apache.org/ns/synapse" name="FormDataReceiver">
<http uri-template="http://www.eaipatterns.com/MessageEndpoint.html" method="post">
<suspendOnFailure>
<progressionFactor>1.0</progressionFactor>
</suspendOnFailure>
<markForSuspension>
<retriesBeforeSuspension>0</retriesBeforeSuspension>
<retryDelay>0</retryDelay>
</markForSuspension>
</http>
</endpoint>
Also used following API code.
<api xmlns="http://ws.apache.org/ns/synapse" name="FORM" context="/Service">
<resource methods="POST">
<inSequence>
<log level="full"></log>
<property name="name" value="Mark" scope="default" type="STRING"></property>
<property name="company" value="wso2" scope="default" type="STRING"></property>
<property name="country" value="US" scope="default" type="STRING"></property>
<payloadFactory media-type="xml">
<format>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<root xmlns="">
<name>$1</name>
<company>$2</company>
<country>$3</country>
</root>
</soapenv:Body>
</soapenv:Envelope>
</format>
<args>
<arg evaluator="xml" expression="$ctx:name"></arg>
<arg evaluator="xml" expression="$ctx:company"></arg>
<arg evaluator="xml" expression="$ctx:country"></arg>
</args>
</payloadFactory>
<log level="full"></log>
<property name="messageType" value="application/x-www-form-urlencoded" scope="axis2" type="STRING"></property>
<property name="DISABLE_CHUNKING" value="true" scope="axis2" type="STRING"></property>
<call>
<endpoint key="FormDataReceiver"></endpoint>
</call>
<respond></respond>
</inSequence>
</resource>
</api>
But still that message send as rest call manner.
I want something like following message body into backed server. This example message should send through the message body. In here I have used WSO2 Given Example. I have added same wso2esb link for your further reference. wso2. Actually I need to send message XML={{my_xml_message_here}}. Please help me to continue this. Thanks lot.
name=Mark&company=wso2
There is missing Content-Type in header for this type of request. Need to add:
<header name="Content-Type" scope="transport" value="application/x-www-form-urlencoded"/>

WSO2 REST to SOAP passing operation parameters

Using WSO2 ESB 4.8.1, I have configured a WSDL proxy that I want to access over REST. the proxy points to the SOAP WSDL URI and has publish WSDL turned on. This seem to work fine and I can see the service and its various operations in the WSO2 admin UI. Likewise if I go to localhost:8280/services/
The questions is how do I pass operation specific parameters when accessing over HTTP REST?
Let's say my FooService OperationX expects a "p1" parameter, can I pass this directly when accessing localhost:8280/services/FooService/OperationX in a browser?
I tried for example localhost:8280/services/FooService/SomeOperation?p1=somevalue, but always get a validation error that the required parameter is missing:
cvc-complex-type.2.4.b: The content of element 'axis2ns15:OperationXRequest' is not complete. One of '{"somenamespace":p1}' is expected.
Can this be supported by a basic WSDL proxy? Or do I need to use the API?
I think the better option for your scenario is to use api to access over REST. Here I have created an api (I used http://jsonplaceholder.typicode.com/comments as my REST back end) which gets the query parameter(postId) which was sent in REST request (http://172.22.99.96:8290/test/comments?postId=1) and assign that value to a property called mypostId inside the api.
Then I am modifying the payload by adding the mypostId property using payload factory mediator which will match to the echo service request(I have used echo service as the SOAP backend).
Then I use enrich mediator to change my soap envelope to match the echo service request soap envelope by adding "xmlns:echo="http://echo.services.core.carbon.wso2.org"" name space. Finally I am sending my created request to echo service proxy.
<api xmlns="http://ws.apache.org/ns/synapse" name="test" context="/test">
<resource methods="GET" uri-template="/comments?postId={postId}">
<inSequence>
<log level="custom">
<property name="Message Flow" value="--- Order GET ---"></property>
</log>
<log level="custom">
<property name="postId" expression="$url:postId"></property>
</log>
<property name="mypostId" expression="$url:postId"></property>
<call>
<endpoint>
<http method="GET" uri-template="http://jsonplaceholder.typicode.com/comments?postId={uri.var.postId}"></http>
</endpoint>
</call>
<payloadFactory media-type="xml">
<format>
<echo:echoInt xmlns:echo="http://echo.services.core.carbon.wso2.org">
<in>$1</in>
</echo:echoInt>
</format>
<args>
<arg evaluator="xml" expression="get-property('mypostId')"></arg>
</args>
</payloadFactory>
<log level="full"></log>
<log level="custom">
<property name="Message Flow" value="--- After Palyload factory---"></property>
</log>
<property name="extarctedBody" expression="$body"></property>
<log level="custom">
<property name="MyextarctedBody" expression="get-property('extarctedBody')"></property>
</log>
<log level="full"></log>
<enrich>
<source type="inline" clone="true">
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:echo="http://echo.services.core.carbon.wso2.org"></soapenv:Envelope>
</source>
<target type="envelope"></target>
</enrich>
<log level="custom">
<property name="Message Flow" value="--- Order GET2 ---"></property>
</log>
<log level="full"></log>
<enrich>
<source type="property" clone="true" property="extarctedBody"></source>
<target xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:echo="http://echo.services.core.carbon.wso2.org" action="child" xpath="//soapenv:Envelope"></target>
</enrich>
<log level="full"></log>
<send>
<endpoint>
<address uri="http://localhost:8290/services/echo"></address>
</endpoint>
</send>
</inSequence>
<outSequence>
<send></send>
</outSequence>
</resource>
</api>
Hope this may help you .

WSO2 esb: how to proxy REST service with numeric operation name

We are trying to implement basic SOAP-to-REST proxy service on WSO2 ESB. Our 3rd-party REST service accepts the requests in the following format:
http://<MYURL>/simpleQuery/16783484?oslc.select=value1
The problem is that operation name has numeric only format - "16783484" in our case. payloadFactory mediator does not allow having <16783484> as XML element, since XML spec restricts numeric-only element names.
<proxy xmlns="http://ws.apache.org/ns/synapse" name="CQProxy" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
<target>
<inSequence>
<payloadFactory>
<format>
<16783484>
<oslc.select>$1</oslc.select>
</16783484>
</format>
<args>
<arg value="myvalue1"/>
</args>
</payloadFactory>
<send>
<endpoint>
<address uri="http://<MYURL>/simpleQuery" format="get"/>
</endpoint>
</send>
<drop/>
</inSequence>
<outSequence>
<log level="full"/>
<send/>
</outSequence>
</target>
</proxy>
How can this be overcome?
Appreciate your help!
WSO2 support team suggested the following solution. Thank you Sandapa!
In that case, you have to set endpoint format as 'rest'. And if it's a GET request you have to set 'HTTP_METHOD' as as GET. Please refer the example given below.
Example:
<proxy xmlns="http://ws.apache.org/ns/synapse" name="CQProxy" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
<target>
<inSequence>
<property name="REST_URL_POSTFIX" value="/getSimpleQuote?symbol=IBM" scope="axis2" type="STRING"/>
<property name="HTTP_METHOD" value="GET" scope="axis2" type="STRING"/>
<send>
<endpoint>
<address uri="http://localhost:9000/services/SimpleStockQuoteService/" format="rest"/>
</endpoint>
</send>
<drop/>
</inSequence>
<outSequence>
<log level="full"/>
<send/>
</outSequence>
</target>
<description></description>
</proxy>
Although this comment is not going to suggest you a solution, I can say that this is a bad idea :-) You can try using XSLT instead of PayloadFactory to transform, but again that might choke up the XML parser. Problem is a lot of open source projects/libs that WSO2 products use and you might bump into elsewhere will honour the spec. In the long run, complying to the spec will give you less headaches when you integrate with other external tools/systems. Is it possible to change your rest service so that the service name at least have an underscore in front?