Create a multipart response with WSO2 ESB - wso2

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.

Related

Log Configuration in WSO2

So i'm working on PER-API log configuration in WSO2, in order to log REQUEST/RESPONSE for an API call, we need to
Modify the file log4j.properties in < APIM HOME >\repository\conf by adding the following lines to it
log4j.appender.TestAPI_APPENDER=org.apache.log4j.RollingFileAppender
log4j.appender.TestAPI_APPENDER.File=${carbon.home}/repository/logs/PerAPI/${instance.log}/wso2-APILogs-service${instance.log}.log
log4j.appender.TestAPI_APPENDER.MaxFileSize=1000KB
log4j.appender.TestAPI_APPENDER.MaxBackupIndex=10
log4j.appender.TestAPI_APPENDER.layout=org.apache.log4j.PatternLayout
log4j.appender.TestAPI_APPENDER.layout.ConversionPattern=%d{ISO8601}
[%X{ip}-%X{host}] [%t] %5p %c{1} %m%n%n
log4j.category.API_LOGGER.admin--APINAME= TRACE,
TestAPI_APPENDER
It logs the following details to a .txt file which is pretty comprehensive and cool
However i do have some questions that i'm stuck in and would like help in, here are they
The HTTP Status Code for the response is not being logged here which i badly need, I've looked here and it says to use %s - HTTP status code of the response however when i put the character %s in the log4j.properties ConversionPattern, those characters are printed as they are, how can i achieve this?
Secondly, does wso2 has some sort of portal in order to visualize these custom logs?
Thirdly, i need to put a line at the end of request/response (4 entries per API CALL) log lines and i'm unable to find any solution, if i hardcode a line into the log4j.properties ConversionPattern, i suppose it would print after each entry, please help?!
Thank you
Please find the below answers for your questions
By enabling wire logs settings you can easily track the HTTP Status Code for the response. You can enable the header and wirelogs by un-commenting following lines in log4j.properties file (located at: \repository\conf)
log4j.logger.org.apache.synapse.transport.http.headers=DEBUG
log4j.logger.org.apache.synapse.transport.http.wire=DEBUG
The third Question, you can achieve this by using log mediator in In-sequence(print a line in request) and out-sequence(print a line in response).For an example you can use custom log in your api synapse configuration file as follows (here I use this custom log in in sequence)
<sequence xmlns="http://ws.apache.org/ns/synapse" name="admin--TwitterSearch:v1.0.0--In">
<log level="custom">
<property name="test" value="your value goes here……….” />
</log>
</sequence>
You can enable wirelogs to track the request and responses of the API when it is getting invoked. To do that uncomment following line in log4j.properties file.
log4j.logger.org.apache.synapse.transport.http.wire=DEBUG
Furthermore if you want to log the http messages you can uncomment following line as well.
log4j.logger.org.apache.synapse.transport.http.headers=DEBUG
In WSO2 API Manager Analytics, a log Analyzer is included. You need to configure WSO2 API Manager Analytics with WSO2 API Manager to get that.
If you want to print a customized log for the API, you can use the Log mediator and add a Log mediator as a mediation extension per API with the required log to be printed in the synapse configuration of the API.

Custom error messages and HTTP return codes in WSO2DSS

I am using Wso2 DSS and building RESTful webservices from an oracle DB and trying to return custom error messages in case no records found or invoked using invalid parameters.
And I also want to control the HTTP return code for these error responses.
For example. Lets say there is a webservice: /getemployee/{empno} that returns employee details in JSON/XML format.
If I call getemployee with an empno that doesn't exists in the DB then
I want the response to be like something similar to below
<Error>
<Error_code>No employee found </Error_code>
</Error>
It should not have the normal employee details message structure that would go as response when an employee is found.
At the moment if record is not found by the underlying query DSS just returns empty message with just root element.
The HTTP return code for this response should be 404 as opposed to the success code 200.
By default all responses get HTTP return code 200 in DSS.
Is this possible at all just by using DSS?
Appreciate your help
You would need WSO2 ESB to do that. You can make a proxy service in ESB that will call DSS and then transform the response for you based on its contents and your needs. You can do so using XSLT.
You can set manually the desired HTTP return code, using the property HTTP_SC:
<property name="HTTP_SC" scope="axis2" type="STRING" value="404"/>
You can also use switch and payload factory, if you know what you will receive and the output will be a static one.

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>

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.

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.