Last Updated on 03/05/2021 by Patryk Bandurski
In my previous post, I have described the usage of the CXF proxy client to consume soap web service. We will create a similar service but this time we will use Web Service Consumer.
Background
We are going to create in mule simple service which consumes SOAP Web Service. The client can call our flow by sending a GET request. Then we route to the service, consume its payload, and based on the response’s status we return two different kinds of response.
Web Service Consumer
You should have in mind that this message processor is dedicated only to SOAP web services. It has constraints on the allowed list of connectors.
Therefore you are able to use it with the following connectors:
- File/(S)FTP
- HTTP(S)
- JMS
More about this connector you can find on MuleSoft documentation.
Code Sample
We start with the HTTP Listener configured to listen at port 8091. When a user sends a GET request for resource /weather, the flow is triggered. Then we are setting payload statically – here we set the SOAP body. If you would decided otherwise you receive error like System.Web.Services.Protocols.SoapException: Server was unable to process request.
<web:GetCitiesByCountry xmlns:web="http://www.webserviceX.NET">
<web:CountryName>Poland</web:CountryName>
</web:GetCitiesByCountry>
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.
As you can see, I have specified:
- location to WSDL file
as 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 the 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 the 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 the Property message processor.
To sent custom headers we can either add Property or Message Properties message processor.
Basic authentication
To set a basic authorization header you need to compute the 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 the References tab. Select checkbox Http Configuration Reference and create a new configuration. In HTTP configuration you only need to fill the Authentication tab.
As you can see, any transformation is not needed. We provide simply user name and password. One thing to bear in mind is selecting a Preemptive checkbox.
Code
Source code is available at github.