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.

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.

Time to mock

Soap UI is great tool which allows us to quickly  mock SOAP services based on WSDL file.

  1. Open Soap UI
  2. Create project based on local or remote WSDL file
  3. Right click at service endpoint and from context menu choose Generate SOAP Mock Service
  4. In Generate MockService windows choose which operations you would like to mock. I will select only one operation GetObjectList

    Creating mock configuration for our service
    Creating mock configuration for our service
  5. In new windows double click on the operation you will mock and then in the next window click Response 1
  6. It is time to edit your mock response. It will be always returned in the way you define it here.

    Prepared SOAP response
    Prepared SOAP response
  7. Last thing is to start service by clicking green play button. Finally we are ready to write our service.

Code Sample

Flow calling external web service
Flow calling external web 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 />
&lt;v1:GetObjectList xmlns:v1=&quot;http://www.omninet.de/OtWebSvc/v1&quot;&gt;<br />
 &lt;v1:Get folderPath=&quot;Users&quot;&gt;<br />
 &lt;/v1:Get&gt;<br />
&lt;/v1:GetObjectList&gt;<br />

Next message processor proxy-wsSub_Flow invokes subflow. This subflow is divided into two steps:

  1. configuring Proxy client – it creates outbound endpoint to send raw xml content,
  2. 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.

CXF Proxy Client
CXF Proxy Client

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 –

    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==

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.

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.

CXF proxy client – consume SOAP web service
Tagged on:                 

Leave a Reply

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