In my previous post I have described usage of CXF proxy client to consume soap web service. We will create similar service but this time we will use Web Service Consumer.

Mule version

Tools

I am going to use following tools:

  • Postman to make http calls
  • Soap UI to mock soap service
  • Anypoint Studio

Background

We are going to create in mule simple service which consumes SOAP Web Service. Client can call our flow by sending GET request. Than we will route to the service, consume its payload and based on response’s status we will return two different kind of response.

Web Service Consumer

You should have in mind that this message processors is dedicated only to SOAP web services. It has constraints on allowed list of connectors.
Therefore you are able to use it with following connectors:

  • File/(S)FTP
  • HTTP(S)
  • JMS

Time to mock

Mock web service with soapUI

Code Sample

Flow calling external web service
Flow calling external web service

We start out with the HTTP Listener configured to listen at port 8091. When user sends GET request for resource /weather, flow will be invoked. Then we are setting payload statically – here we must to  set SOAP body. If you would decided otherwise you receive error like System.Web.Services.Protocols.SoapException: Server was unable to process request.

<br />
&lt;web:GetCitiesByCountry xmlns:web=&quot;http://www.webserviceX.NET&quot;&gt;<br />
 &lt;web:CountryName&gt;Poland&lt;/web:CountryName&gt;<br />
&lt;/web:GetCitiesByCountry&gt;<br />

Next message processor, Web Service Consumer invokes specified operation. But how this piece of flow is configured? To do that we need to specify connector configuration and operation to call.

Connector configuration
Connector configuration

As you can see we specified:

  • location to WSDL file

as it is in a classpath, only file name is needed

  • service to call

this is a drop down filed automatically by mule based on WSDL file

  • endpoint (port) to call

this is an endpoint specifying a single address for binding. Like service is automatically populated by mule

  • address to call

actual URL to call. It can be hardcoded or taken from properties file.

When invoking web service we are required to provide headers like:

  • SoapAction – which operation we would like to invoke
  • Content-Type – what is the type of the content
  • Authorization – username and password in case of basic authorization (optional)

Comparing it to cxf proxy client example we do not need to specify SoapAction and Content-Type. However in order to set basic authorization we need to set it before using Property message processor.

To sent custom headers we can either add Property or Message Properties message processor.

Basic authentication

To set basic authorization header you need to compute value, as shown below, and assign it in Authorization property.

Basic #[org.mule.util.Base64.encodeBytes("user:password".getBytes())]

To hash user name and password we are using Base64’s method encodeBytes. Header after evaluation: Basic c3VlcjpwYXNzd29yZA==

However there is a simpler way. In order to set authentication open Web Service Consumer’s connector configuration and navigate to References tab. Select checkbox Http Configuration Reference and create new configuration. In Http configuration you only need to fill Authentication tab.

Basic authentication settings
Basic authentication settings

As it can be seen, any transformation is not needed. We provide simply user name and password. One thing to bear in mind is selecting Preemptive checkbox, otherwise your credentials won’t be sent and we will receive 401 error.

Code

Code is available at github.

Web Service Consumer – a simple way to handle SOAP service
Tagged on:         

Leave a Reply

Your email address will not be published. Required fields are marked *