WSO2 ESB REST API Chaining issue - wso2

I am facing some issues when doing service chaining is WSO2 ESB. Below is the xml file.
Following is my use case. I need to call Service 1, get the response, do validation check on it and then call Service 2. Through the below code I am successfully able to call Service 1. For the service two request, I have hard coded the request in the payload. Issue is coming when setting the header parameters. The header properties are not getting set due to which call to Service 2 is not going. For testing purpose I have kept both the URLs same.
Please let me know the following:
1. How to set HTTP Header values.
2. Is there a way to persist the Initial input request and then use it in the second Service call.

Although your synapse configs are not there I'll answer your question.
You can do this two ways. One is by using the Header Mediator. You can reffer this Doc. Example code below,
<header name="Accept" value="image/jpeg" scope="transport"/>
The second approach is using the property mediator, You can set the Header value and set the scope to transport. So the Header property will be added.
What you simply need to do it assign the original request content to a Property, so you can use it later. There are many ways to do this, Following example is by using the enrich mediator
<enrich>
<source type="body" clone="true"/>
<target type="property" property="request"/>
</enrich>

Related

How to log request headers within script mediator in Wso2 API Manager 3.2?

I am trying to get all transport headers used when calling a specific API and log that for debugging, since we do not know what the name of the headers will be I want to log them all.
I know this can be done via a class mediator as well as by enabling wire logs but I am looking for an option to achieve this without having to do either of those.
I have tried using script mediator and then using: mc.getProperty("org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS") to fetch them from message context but it just returns null.
Any suggestions?
You are trying to get a property with the name "org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS" from the default Context, hence it's returning null. You can't really access TRANSPORT_HEADERS from the Axis2Context within a ScriptMediator. Your best option is to write a class mediator.
Use get-property from the transport layer.
Only you need to know the name of the variables.
<log level="headers" category="INFO">
<property name="inicio" value="------ begin -------"/>
<property name="X_Teste_FOS" expression="get-property('transport','X-Test-FOS')"/>
<property name="inicio" value="------ end -------"/>
</log>

How to add a URL parameter using a WSO2 Mediation Flow?

I have successfully deployed a WSO2 API Manager. I am already using mediation flows for setting Header information, but now I am adding an API that requires a key to be set as an URL parameter. However I would like this to be added in the background so that the end-users don't have to worry about this key.
How can this be done in a Message Mediation Policy/Flow? Obviously the other parameters that are already present should stay untouched.
Thanks in advance
Hope you can access the key inside the synapse context. Then you can assign the key value to the uri.var object as below.
<property name="uri.var.key" expression="get-property('userParames.key')"/>
Now you can simply construct the endpoint as,
<endpoint>
<http uri-template="https://{uri.var.hostname}:{uri.var.portnum}/abc/{uri.var.key}"/>
</endpoint>

WSO2 API Manager - Replace URLs in response body

I'm trying to setup a proxy for my RESTful API using WSO2 API Manager. My problem is that the responses from the backend API are left untouched so all the urls that connect to other endpoints still reference the backend server rather than the proxy. I need a way to replace those url values in the response body to point to the proxied api. I understand this can be accomplished via Mediation Extensions, using ESB Mediators.
I'm not familiar enough with them to pick the one better suited for the job. URLRewrite mediator looks pretty straightforward, but it doesn't seem to apply to the message body but the headers. Payload Factory seems to require a fixed structure for the message, which is not very convenient for me, since I need it to work on the different responses that my API provides (and I wouldn't want to maintain those structures in the mediator definition).
I've managed to solve it by setting the headers my application checks to build its urls:X-Forwarded-Host and X-Forwarded-Proto.
So I've created a Header Mediator that looks like:
<sequence xmlns="http://ws.apache.org/ns/synapse" name="WSO2AM--Ext--In">
<header name="X-Forwarded-Host" expression="get-property('transport','Host')" scope="transport"/>
<header name="X-Forwarded-Proto" value="https" scope="transport"/>
</sequence>
And that did the trick.

Dynamically constructing the endpoint in a WSO2ESB Callout Mediator

I am trying to use a Callout Mediator to call one of two endpoints (stored in the registry), based on a property of the message. The way I would like to do this would be essentially concatenating some properties to provide the path of an endpoint, or by using XPATH if that is not possible.
I know that, using a Send Mediator, XPATH is a possible option for choosing an endpoint, e.g.
<send xmlns:ns="http://ws.apache.org/ns/synapse">
<endpoint key-expression="//ns:abc"/>
</send>
However, this syntax doesn't seem to work using the Callout Mediator.
Unfortunately my current "solution" to this problem is to have a filter based on this property and n cases, where n is the number of different endpoints I am considering. However, I was hoping to not require a change to the sequence if I added a new endpoint- simply a difference in the message.
This doesn't seem possible in the current implementation of the Callout Mediator.
I ultimately decided to write my own mediator. It is essentially a copy of the CalloutMediator but adds the functionality of using xpath for endpoints.

Create a multipart response with WSO2 ESB

I'm trying to create a WSO2 ESB proxy that would generate an HTTP multipart response, basically with 2 parts: XML and an attached binary file (an image for example).
The sequence would be as follow:
the service is exposed as an HTTP GET request
we first call an endpoint that returns a binary file
we create an XML that describe the binary file
we mix the 2 elements together and provide the multipart response
(XML + binary file)
After several attempts and looking around on samples and forums I couldn't find how to solve this particular case.
I've managed to call the endpoint. I can see in the debug logs that the response is transferred as binary in the soap internal message.
I suspect I then need to use the MultipartFormDataFormatter. As far as I understand the code of the formatter, it takes all the child nodes of the body of the internal soap message to create one part by child.
So I've tried to append my XML content as a sibling node of the binary node (the message looks as expected in the logs), and force the ContentType with :
<property name="messageType" value="multipart/form-data" scope="axis2"/>
<property name="ContentType" value="multipart/form-data" scope="axis2"/>
to be sure to call the correct formatter.
Unfortunately this does not seem to work, the response is indeed in multipart/form-data, but with zero bytes data.
Any help?
Thanks
Yannick
You need to enable Binary Relay builders in axis2.xml file to use the multipart/form-data message formatting.
<messageBuilder contentType="multipart/form-data"
class="org.wso2.carbon.relay.BinaryRelayBuilder"/>
<messageFormatter contentType="multipart/form-data"
class="org.wso2.carbon.relay.ExpandingMessageFormatter"/>
Thanks for your answer.
I've made several tests, by enabling Binary Relay builders, but I did not get the exact multipart response I was expecting.
So, I've finally created my own Formatter. It constructs the multipart response from the body children, and looks for some specific properties to specify header information such as part content id, transfer encoding and content type.
Maybe this will be useful to someone else:
You can set the multipart builders by uncommenting the following property in /repository/conf/axis2/axis2.xml file
<messageBuilder contentType="multipart/related" class="org.wso2.carbon.relay.BinaryRelayBuilder"/>
As per our understanding requirement here is to make a single response message using an xml content and a png attachment received from different endpoints.
In order to accomplish your goal, we can use a custom mediator. By using a custom class mediator you can build a response with xml metadata and png images.
Writing a java class to build the message with both responses could be the best way to achieve this in WSO2 way. You can use message builder and crate message methods to create the message in the way that you want. And you can use the class mediator, which can be found in this documentation to use it with ESB
As a further clarification, please note that the enrich mediator attaches the given resources.