WSO2 API Manager - Replace URLs in response body - web-services

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.

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>

In wso2 esb how to read endpoint from a file

In wso2 esb what is the best practice for Endpoint maintenance. From some article I got to know that we can read from file So, if this the best approach how achieve this one.
In a scenario where the endpoint is saved as a file within the carbon registry (You can even upload the file via management console or can use WSO2 EI Tooling to create an endpoint template), we can read the content of the endpoint as follows.
Add the endpoint registry resource.
Log the endpoint content using the following synapse configuration. (Can even retrieve specific attributes within the endpoint using their xpath expressions)
<log level="custom">
<property name="Endpoint Content:" expression="$ctx:endpointFile//*"/>
<property name="Endpoint URL:" expression="$ctx:endpointFile//*[local-name() = 'address'][1]/#uri"/>
</log>
You can see the logged endpoint content as below.
There are 3 ways to Endpoint Maintenance.
Using ESB Tooling
From Command Line
Using a Script
More details can be found in WSO2 Documentation.

wso2am Elastic Search lookup

I switched from Apigee to WSO2 2.1 but on piece of functionality is missing.
When my Oauth users make API calls, I want to add an additional Header
to the backend request. E.g.: "X-Customer-Name: CUST_NAME
I have the Java code to lookup "CUST_NAME" in ElasticSearch based on
the user's consumer key & secret. How would I integrate that code into
WSO2 to be able to lookup the values and send the extra X-Customer-Name header to my backend?
Java classes can be integrated / used in WSO2 mediations using <class/> mediator.
To achieve this, the custom java class can extend AbstractMediator class and can implement the logic in the mediate method.
Create a new insequence, like the following and invoke the custom class, this way the custom property can be set to the message context.
<sequence name="TokenExchange" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
<class description="" name="com.customer.CustomerName"/>
<property expression="get-property('Customer-Name','custName')" name="cs" scope="default" type="STRING"/>
</sequence>
Use this newly built inSequence into your API In-Flow message mediation flow and pass the appropriate properties to the backend.

WSO2 ESB REST API Chaining issue

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>

Usage of SoapActionEndpointMapping in Spring-ws

I'm trying to create a WS based on a WSDL that defines one Request and one Response. The incoming request should be mapped to an endpoint depending on the SOAPAction defined in the SOAP message. To achieve this I'm trying to use the SoapActionEndpointMapping in my servlet.xml config file and define the mappings, as described in the Spring documentation.
<bean id="endpointMapping" class="org.springframework.ws.soap.server.endpoint.mapping.SoapActionEndpointMapping">
<property name="mappings">
<props>
<prop key="http://myCompany/MyService/MyRequest/mySoapActionOne">myFirstEndpoint</prop>
<prop key="http://myCompany/MyService/MyRequest/mySoapActionTwo">mySecondEndpoint</prop>
</props>
</property>
My endpoint extends AbstractMarshallingPayloadEndpoint and should be able to handle the requests.
The problem is that when I try to send a request (with SoapUI) i get the following error in the log:
WARN [EndpointNotFound] No endpoint mapping found for [SaajSoapMessage {http://schemas.mycompany/MyService}MyRequest]
I have used the PayloadRootQNameEndpointMapping with great success earlier but can not this to work.
Any help is appreciated.
Regards.
Do you have a handler adapter bean defined also? You'll need one in order to use a MarshallingPayloadEndpoint, so that spring knows how to perform the marshalling. The adapter is called something like MarshallingEndpointHandlerAdapter, or similar.
In your SOAP client (SOAPUI), you'll need to add the SOAPAction header to your request, to supply spring with the SOAP action to use in its mapping.
E.g. SOAPAction=http://myCompany/MyService/MyRequest/mySoapActionOne
It shouldn't make any difference what type of Endpoint you're using, because currently, you're receiving a 404 response - your request isn't finding its way to any endpoint.