Adding user defined headers in wso2 esb custom proxy - wso2

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..

Related

Is it possible to add request/response handlers on SOAP requests/responses using SaaJ?

in my project I have to use SaaJ to create requests and to consume the SOAP responses.
In JAX_WS there was a notion of handlers with which on the port you can add additional information for example to add Sequrity header. I was wondering if this possible in the SaaJ case.
SO my idea is to add WS-Security header before sending the message on SOAPConnection is it possible? Or I have to add special method for example on the SOAPConnection class which to add the header?
You can use this package provided by apache to create your WS-Security header as a Document and then you can add it to your existing SOAPEnvelope.

WSO2 API Message Mediation based on accept headers

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

URI templates in WSO2 API Publisher

Is it possible to register endpoints with URI templates in the API Publisher that ships with WSO2 API Manager version 1.4.0? For example, I would like to register an endpoint such as:
/jobs/{jobid}/pems
After saving an API, I can see this bit in the API config file:
<resource methods="GET" url-mapping="/jobs/{jobid}/pems">
I have tried manually changing the XML file from url-mapping to uri-template, i.e., to:
<resource methods="GET" uri-template="/jobs/{jobid}/pems">
but I still get "No matching resource found in the API for the given request". In any case, this is not a good work around since we need developers to be able to register these endpoints from the publisher application. Whenever changes are saved to an API from within the API Publisher interface, 'uri-template' gets reverted back to 'url-mapping'.
Thanks in advance.
This is due to APIPublisher app only supports URL-Mappings when defining API resources.
If you want to define a uri-template ,instead of a url-mapping,you can achieve this from changing the api xml manually.
But before that make sure,when creating the API,you have define the API with a valid url-mapping.So in your trying API,first please edit the API from APIPublisher as keeping a valid url-mapping pattern for API resources and then change its xml to match with your mentioned uri-template pattern.
Thanks;
we don't currently support uri-template patterns. We support only url-mappings.
Even , if you edit the backend API configuration (that is, api configuration file ) to url-mappings, it wont work, because we validate request before the request get hits the mediation engine.
You can use ESB RESTApi as your backend service endpoint.That is, define the RESTAPI in wso2esb, with uri-templates(it is supported out of the in ESB) and point that API as the service endpoint from wso2APIManager with url-mappings pattern.
I would like to add some more points.As I mentioned earlier,WSO2 APIManager supports uri-templates.Only api-publisher app UI is not allowing to add uri-templates.
When creating the API from the Publisher UI, you have to give the resource mapping as /* or valid url-mapping. Then when the synapse api is created, you have to change that xml file to uri-template and then change the /* to the uri-template pattern.
Hope above will help you to resolve your issue.
Thanks;

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.

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..