When it is time to consume SOAP Web Service mule has a couple of ways to handle this task. There was a time when I would generate stub classes based on WSDL file, but I wanted to create flows without generating a single line of code. So I found out that we could use cxf proxy client to handle my wish.
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.
Soap UI is great tool which allows us to quickly mock SOAP services based on WSDL file.
- Open Soap UI
- Create project based on local or remote WSDL file
- Right click at service endpoint and from context menu choose Generate SOAP Mock Service
- In Generate MockService windows choose which operations you would like to mock. I will select only one operation GetObjectList
- In new windows double click on the operation you will mock and then in the next window click Response 1
- It is time to edit your mock response. It will be always returned in the way you define it here.
- Last thing is to start service by clicking green play button. Finally we are ready to write our service.
We start out with the HTTP Listener configured to listen at port 8081. When user sends GET request for resource /users , flow will be invoked. Then we are setting payload statically – here we could either set soap envelope or soap body – it is up to you which one is better for specific use case. I have decided to set only SOAP body.
<br /> <v1:GetObjectList xmlns:v1="http://www.omninet.de/OtWebSvc/v1"><br /> <v1:Get folderPath="Users"><br /> </v1:Get><br /> </v1:GetObjectList><br />
Next message processor proxy-wsSub_Flow invokes subflow. This subflow is divided into two steps:
- configuring Proxy client – it creates outbound endpoint to send raw xml content,
- sending HTTP request to web service endpoint.
To setup proxy client you have to set Operation to Proxy client. In Proxy Client section you should specify what part of SOAP message you are going to send. The body is default option – as I mentioned earlier it could be set to envelope.
To send prepared xml payload we should configure HTTP request connector – like setting host, port, path and method (always POST). 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 content
- Authorization – username and password in case of basic authorization (optional)
In order to set up custom headers in HTTP request connector go to Parameters section and click button Add Parameter. It will allow add headers one by one or you can use builder which will hold all custom headers in one place.
Here are examples for mentioned headers:
- SoapAction – http://www.omninet.de/OtWebSvc/v1/ModifyObject
- Content-Type – application/soap+xml;charset=UTF-8
- Authorization –
To hash user name and password we are using Base64’s method encodeBytes. Header after evaluation: Basic c3VlcjpwYXNzd29yZA==
The part with Authorization seems to be a bit complicated so let’s remove it from properties. Http Request Connector’s configuration has tab allowing to configure authentication type. In order to do it edit connector configuration.
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.