Question

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

Was it helpful?

Solution

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>

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top