WSO2 API Message Mediation based on accept headers - wso2

I am using WSO2 Api Manager as a proxy between a client and the server. The server sends XML responses that I successfully converted to a JSON response with a custom PayloadFactory message mediation. Problem that I now am facing is the following: How do I mediate based on the accept headers of a request?
When I have in the header of a request:
Accept application/json
I want to use my mediator to convert the response from the server to JSON, but when I have
Accept text/xml or application/xml
I don't want to use my mediator and just forward the response as is.
Edit:
Basicly the only thing a really need is to save my "Accept ... " header. I can forward it to the server, but the server doesn't do anything with it. Are there global variables or something that I can use per proxy request?
Any advice will be appreciated.

You can use Filter mediator or Switch mediator to do a if-then-else kind of operations inside the insequence and implement your first requirement. You can get the header using synapse xpath variables.
You can define a custom sequence and add it to the api
for the second question, you can use property mediator to define values and use them later. set the 'Scope' parameter according to your need

Related

wso2:esb: What is the basic difference between call mediator and send mediator

Anybody would please through some light on difference between call and send mediator and what use cases these two mediators are used.
Send mediator - used to send messages out of Synapse to some endpoint. Then the response is returned to the OutSequence where you can send it back to the client
Use cases - When you only need to send a message to one back end and return the response back to the client.
Call mediator - Also used to send messages out of the Synapse to some endpoint, but the response does not come to the OutSequence. When we send a request using the call mediator, the response will arrive to the mediator that is placed right after the call mediator.So this will allow us to specify all the service invocations one after the other in a chain within a single sequence
Use cases - Service chaining. i.e.- Vehicle license renewal service
Another characteristic is that the callout mediator is blocking, send mediator and call mediator are not blocking. This answer is in regards to WSO2 versions until 4.8.0

Adding user defined headers in wso2 esb custom proxy

I have created a custom proxy for a given address endpoint. The custom proxy exposes the methods which are there in the actual endpoint. However I wanted the custom proxy to expose some custom headers in the SOAP header to the end user. The end user would then pass the data in these custom headers and these values would be used in the mediation logic. How would I do this ?
Thanks.
To manipulate header values, you can add a Header mediator (Transform->Header) in the in/out sequences. Use a Property mediator(Core->Property) to set/remove property values in the message context. These can be retrieved by get-property(proerty-name) later.
You can use script mediators to carry out mediation on the message headers. You can extract message headers using the following code
<property name="authheader" expression="get-property('transport','Accept')"/>
In the wsdl, you can add soap:header elements, that are transmitted inside the Header element of the SOAP Envelope. I believe you have a custom wsdl, which is exposed via the proxy..So you can edit that..

WSO2-ESB: SOAP Mediator from wsdl

I have seen some of the SOAP- Example- Mediators. I have not found a transformation based on the endpoint-WSDL.
I want to send some nested named array in json or POX and that data should go into a complete namespaced headered (username, password) SOAP-Request based on the names.
All the examples I have found had either a very simple wsdl or the namespaces were static in the XSL-Transformation.
It should be possible to do that, as I see in for example php-NuSOAP. You feed it with a wsdl-endpoint, the operation you want to execute and the parameter-array, and it calls the Webservice.
I am looking for a solution which is not too much hardcoded for every single service, so the proxy still works when the wsdl changes and Server Clients get changed.
As far as I understand the payload factory mediator in (https://stackoverflow.com/a/12969814/2277620) you would have to hardcode the soap-format in the mediator.
If WSO2 is the wrong tool for that I'd like to have a hint which tool could help.
Thanks in advance!
Marco.
For my understanding, you want to have a proxy, but it's backend service/wsdl may vary..
What , you can do is, you can save the wsdl (dynamic wsdl)in registry and point that in your proxy. whenever you edit the wsdl, proxy will automatically adopt to that..But the request, which you send to your backend should follow the wsdl definitions..It is totally client side responsibility..

How to validate input and output messages in proxy service against wsdl

Is there any way to validate input and output messages in proxy service against specific wsdl?
Have a situation, when proxy generates invalid output message, so i want to catch this inside my proxy just before responding to client.
Also i want to check if input message is valid.
You can use a tool like SoapUI to validate scenario like this. You can generate a test using particular wsdl. And you can use tool like tcpmon to validate the message going between proxy and the particular endpoint.
As a example use one tcpmon listening to out port to the proxy and redirect it to the actual port.
You can find soapui from [1] and if your using wso2 ESB the tcpmon is available at /bin directory and you can start it using
sh wso2esb-4.5.0/bin/tcpmon.sh
By looking at the message passing through tcpmon you can validate the message going out from your proxy.
[1]. http://sourceforge.net/projects/loadui/files/latest/download?source=files
You can validate the payload of the wso2 esb proxy using the Validate mediator.
Example,
<validate source="//xpath_of_element_to_validate">
<schema key="local_entry_name_of_your_schema_file"/>
<on-fail>
*This part contains the code to handle validation failure*
</on-fail>
</validate>
This Validate mediator can be used anywhere in the proxy. To validate the request, use it as the first line and to validate the response, use it before the respond mediator
You can refer the below link for more details,
https://docs.wso2.com/display/ESB490/Validate+Mediator
Hope this helps!!
Thanks!!

Changing Wso2 HTTP Method from POST to GET

How can i chnage the HTTP Method in wso2 esb using java class mediator
currently I am using
mc.setDoingGET(false);
mc.setDoingPOX(true);
I want to take a post request to ESB and then send to a webservice via GET or vice versa
but the above code is not working at all
Use the property mediator with property name: HTTP_METHOD and http method name as the value (GET,POST etc) and scope as 'axis2'.
If you want to change this property in the java class level, you may do the following:
msgCtx.setProperty(Constants.Configuration.HTTP_METHOD, "GET");