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.
I am going to use following tools:
- Postman to make http calls
- Soap UI to mock soap service
- Anypoint Studio
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:
Time to mock
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 /> <web:GetCitiesByCountry xmlns:web="http://www.webserviceX.NET"><br /> <web:CountryName>Poland</web:CountryName><br /> </web:GetCitiesByCountry><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.
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.
To set basic authorization header you need to compute value, as shown below, and assign it in Authorization property.
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.
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 is available at github.