Adding a header to a XML message in Synapse - wso2

Using Synapse 2.1, I am trying to transform an XML message with no header into a SOAP message with a header containing credentials to consume a web service. Something like this:
Synapse incoming message:
<SOAP-ENV:Envelope>
<SOAP-ENV:Body>
...TAGS...
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Synapse outgoing message:
<SOAP-ENV:Envelope>
<SOAP-ENV:Header>
<yta:Authentication>
<yta:UserName>srnm</yta:UserName>
<yta:Password>psswrd</yta:Password>
</yta:Authentication>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
...TAGS...
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
How could I configure Synapse to do it? I am successfully using a transform file to update the body of the message, but not to add a header to the output.
I tried using the header and property mediators in the configuration file, but I am not sure what is the way to go. Reading about the header mediator it says "At the moment set header only supports simple valued headers". Could this be the case?
Thanks

For the record, I ended up using a script mediator with an inline javascript script in the configuration file using the addHeader method. See below:
<script language="js">
<![CDATA[
var user = mc.getPayloadXML()..*::UserName.toString();
var psswd = mc.getPayloadXML()..*::Password.toString();
mc.addHeader(false, <yta:Authentication xmlns:yta="yta:namespace url"><yta:UserName>{user}</yta:UserName><yta:Password>{psswd}</yta:Password></yta:Authentication>);
]]>
</script>

You can use XSLT mediator to manipulate it. So add an XSLT transformation with required headers and it would add required headers. Or use Script mediator / Class mediator where you can manipulate message.
Please refer followings which will be useful.
http://wso2.org/forum/thread/10794
http://wso2.org/forum/thread/10843
If this xml structure is not needed.
you can use Http Headers you can use properties as below.
http://blog.thilinamb.com/2011/04/how-to-access-web-service-using-http.html

Looks like you want to secure the service. The easiest then, is to use username-token security. Go to the services dashboard in WSO2 ESB for your proxy service and secure it, using UT. Also see http://docs.wso2.org/wiki/display/ESB460/Sample+200%3A+Using+WS-Security+with+policy+attachments+for+proxy+services for a security sample

Related

WSO2 responds payload in binary

I have a corportative environment and make changes in axis2.xml and carbon.xml can generate impacts.
My problem is that WSO2 is responding the message in binary as an example:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<axis2ns1073:binary xmlns:axis2ns1073="http://ws.apache.org/commons/ns/payload">PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz48UzpFbnZlbG9wZSB4bWxuczpTPSJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy9zb2FwL2VudmVsb3BlLyI+PFM6Qm9keT48UzpGYXVsdCB4bWxuczpuczQ9Imh0dHA6Ly93d3cudzMub3JnLzIwMDMvMDUvc29hcC1lbnZlbG9wZSI+PGZhdWx0Y29kZT4wMDU8L2ZhdWx0Y29kZT48ZmF1bHRzdHJpbmc+RVJSTyBETyBDUk0gLSBFU1RBIE9TIEpBIEZPSSBJTlNFUklEQSBOQSBCQVNFIERPIFNQUzI8L2ZhdWx0c3RyaW5nPjxkZXRhaWw+PGlucHV0TWVzc2FnZVZhbGlkYXRpb25GYXVsdD5PUkEtMDAwMDE6IHVuaXF1ZSBjb25zdHJhaW50IFdTX0hJU1RPUklDT19FTlRSQURBX1BLIHZpb2xhZGE8L2lucHV0TWVzc2FnZVZhbGlkYXRpb25GYXVsdD48L2RldGFpbD48L1M6RmF1bHQ+PC9TOkJvZHk+PC9TOkVudmVsb3BlPg==</axis2ns1073:binary>
</soapenv:Body>
</soapenv:Envelope>
Is there any way or mediator I can resolve this without much impact on other projects within the ESB?
Looks like you have enabled the binary message builder/formatter..Is there any need to use them?
If you use ESB 4.7.0/4.8.X , all those versions are using Pass thru transport..
What is the ESB version you use? Try disable binary message builder and use default message builders/formatters.
If you use binary builders for any specific requirement then use builder mediator to build message if you want to process the message when it passing through the system.

wso2 esb: Construct one message from multiple web service calls

I have a number of web services each returning a list of user ids as follows:
<application name="abc">
<users>
<id>123</id>
<id>456</id>
<id>789</id>
</users>
</application>
I need to be able to
Call a proxy service with a specific id (for example 123);
Call each webservice and search for the ID;
Create a response for each webservice and finally
Aggregate all responses in one message which is sent to the client as follows:
<response>
<id>123</id>
<application name="abc">
found
</application>
<application name="lmn">
not found
</application>
<application name="xyz">
found
</application>
</response>
Its probably a mix of service chaining and aggregate, but I cannot figure out how to do it. I tried cloning a request and using send at the end with a receiving sequence which transforms the body using the payload factory. In the Out sequence I then used aggregate to combine the new messages. However it times out and I don't think it's a matter of timing. My main issue is how to create a new message from each webservice response the aggregate mediator can combine them.
Any help is appreciated.
Thanks
You need to follow this pattern, https://docs.wso2.com/display/IntegrationPatterns/Scatter-Gather and you are almost there. When you define receive sequence the response will be forwareded to that sequence and you wouldn't get the response message in outSequence. Use aggregator mediator inside the outSequence and Combine the responses rather than defining a receive sequence.
Once you aggregate the responses, you can use xslt mediator to transform the message.
I managed to solve my issue by creating a proxy service for each web servive. Each proxy service calls the actual web services and uses a filter in the out sequence to create a response likes this:
<application name="abc">
found
</application>
Then I created a REST API which takes the idno as a URI template. I then prepare a payload with this idno and clone the request to the proxy services I mentioned above. Then I aggregate the responses and add the idno in the payload.
If anybody has any questions let me know.

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 - How to build soapenv:Body for remote service call

I have the WSDL file of the remote web service I need to call from a proxy service in the WSO2 ESB and would like to know if I need to construct the soap:Body's elements manually through XSLT/Enrich or there is a way to generate the soapenv:Body's contains from the WSDL and maybe replace '?' for the values.
For example, if you've used soapUI before you'll know that when you import a WSDL file in a project a soapenv:Envelope gets generated automatically with all the XML elements and question marks for their values. Same goes for the TryIt tool in the WSO2 ESB.
Here is an example of an auto-generated soapenv:Envelope in soapUI after importing WSDL:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:abc="http://abc.com/">
<soapenv:Header/>
<soapenv:Body>
<abc:RegisterCandidate>
<abc:NameFirst>?</abc:NameFirst>
<abc:NameMiddle>?</abc:NameMiddle>
<abc:NameLast>?</abc:NameLast>
<abc:PhoneHome>?</abc:PhoneHome>
<abc:EmailAddress>?</abc:EmailAddress>
<abc:Address1>?</abc:Address1>
<abc:Address2>?</abc:Address2>
<abc:City>?</abc:City>
<abc:State>?</abc:State>
<abc:ZipCode>?</abc:ZipCode>
<abc:Country>?</abc:Country>
</abc:RegisterCandidate>
</soapenv:Body>
</soapenv:Envelope>
Is this possible in the Proxy Service through any of the mediators available to read a WSDL and generate soapenv:Body with its XML tags (in the code above it would be abc:RegisterCandidate with its children)? I've done it with the use of the XSL templates, but it's manual and not very elegant.
I've found a few articles/blogs online about writing proxy services in the WSO2 ESB that call remote web services and what the developers were doing in there was to insert the XML elements needed in the soapenv:Body with the use of XSL templates to have the correct/full SOAP message that is then sent (send mediator) to the remote web service server.
Thank you.
There is no way to generate the soap body from the remote service's wsdl as in your requirement. But there is an easier way than using xslt. That is to use the payload factory mediator. You can define the payload and assign values using xpath as shown in the sample.

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.