Mule – Building a webservice proxy

In this blogpost we’ll develop a simple webservice proxy using the latest Mule Studio Community Edition (currently version 1.3.1 which uses Mule ESB v.3.3), which can be downloaded here. The proxy will do some basic routing. No transformation will be done. That area will be explored in a future blogpost.

webservice proxy

mule proxy service

The webservice which will be used for proxying is a simple jax-ws webservice for retrieving employee data based on their department id. The webservice will be exposed via the following url: http://localhost:8083/HR/hrManagerService?wsdl. See this blogpost for building a simple jax-ws webservice on your own.

Creating a new Mule project

First we’ll create a new Mule project based on Maven. This can be done in 3 simple steps. First select File > New > Mule Project. Give the project a Name and a Description and click Next >.

Screenshot from 2013-01-03 12:35:35

New Mule Project – Step 1 of 3

Now, create a flow for the project. Give it a Name, File name and a Description and click Next >.

Screenshot from 2013-01-03 12:35:55

New Mule Project – Step 2 of 3

Finally select the option for creating the Maven pom.xml file and fill in a Group Id and Artifact Id and click Finish.

Screenshot from 2013-01-03 12:39:13

New Mule Project – Step 3 of 3

That’s all. We’ve created the skeleton for our web service proxy and are now ready to implement the flow.

Building the flow

Open the mule-proxy.mflow file and drag a Flow Scope into it. Give it an appropriate name, like MuleProxyFlow.

MuleProxyFlow-begin

MuleProxyFlow

Now let’s build the flow for the proxy service back to front. We’ll begin with adding a HTTP Endpoint to the flow which will resemble the webservice we’re gonna proxy. Right click the Endpoint and select Convert to an outbound endpoint. Next select Properties, clear the Host and Port values and fill in the endpoint of the webservice in the Address field.

Screenshot from 2013-01-03 14:34:20

HTTP Outbound Endpoint Properties – General

Screenshot from 2013-01-03 14:41:01

HTTP Outbound Endpoint Properties – Advanced

Next, we’ll create a client for the web service. Select a SOAP Component and drag it to the left of the HTTP Endpoint. Change the Operation field to Proxy client.

Screenshot from 2013-01-03 14:54:31

SOAP Component – Proxy client

Now for the actual proxy service. Select another SOAP Component and drag it to the left of the Proxy client SOAP Component. Change the Operation to Proxy service. Fill in the Namespace and Service as well. They have to match the targetNamespace and name of the webservice as defined in the wsdl we’re gonna use for the proxy. Make sure that the Payload of the Proxy service and Proxy client match (in this example I choose to only exchange the SOAP body).

Screenshot from 2013-01-03 15:12:52

SOAP Component – Proxy service – General

In the Advanced section we’ll fill in the WSDL Location of the actual wsdl, which will serve as the wsdl for the proxy service. In this simple example, the wsdl of the proxy will match the wsdl of the actual webservice. I’ve raken the liberty to copy the wsdl and xsd files to the src/main/resources/wsdl and src/main/resources/xsd directories of the project. But for the sake of simplicity, you could also pick up the wsdl from it’s actual location, i.e. http://localhost:8083/HR/hrManagerService?wsdl and use that as the WSDL Location in the case of this example.

Screenshot from 2013-01-03 17:57:42

SOAP Component – Proxy service – Advanced

Finally, we’ll need to add the HTTP inbound endpoint. Again, clear the Host and Port fields and add the Address you’d like to use for the proxy, in this example it will be http://localhost:8081/hrManagerServiceProxy.

Screenshot from 2013-01-03 15:27:38

HTTP Inbound Endpoint Properties – General

Screenshot from 2013-01-03 15:27:50

HTTP Inbound Endpoint Properties – Advanced

That’s it. If you followed these steps correctly, your flow should resemble the flow below.

MuleProxyFlow-end

MuleProxyFlow

The xml configuration (src/main/app/mule-proxy.xml) should look like the code below.

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:cxf="http://www.mulesoft.org/schema/mule/cxf" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" version="CE-3.3.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/cxf http://www.mulesoft.org/schema/mule/cxf/current/mule-cxf.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd ">
  <flow name="MuleProxyFlow" doc:name="MuleProxyFlow">
    <http:inbound-endpoint exchange-pattern="request-response" address="http://localhost:8081/hrManagerServiceProxy" doc:name="HTTP"/>
    <cxf:proxy-service namespace="http://service.hrmanager.hr.whitehorses.nl/" service="HRManagerServiceImplService" payload="body" doc:name="SOAP" enableMuleSoapHeaders="false" wsdlLocation="wsdl/hrManagerService.wsdl"/>
    <cxf:proxy-client doc:name="SOAP" payload="body"/>
    <http:outbound-endpoint exchange-pattern="request-response" address="http://localhost:8083/HR/hrManagerService" doc:name="HTTP"/>
  </flow>
</mule>

Testing the proxy

Mule Studio

Now, let’s run the Mule proxy. You can do this directly inside Mule Studio by choosing Run As > Mule Application.

Maven

You can also use maven via the command line:

mvn clean install

There’s a small issue with the generated pom.xml file in this version of the Community Edition. If you use it as is, you’ll get an error concerning missing dependencies. To fix this, open the pom.xml file and search for the following snippet:

<dependency>
  <groupId>com.mulesoft.muleesb</groupId>
  <artifactId>mule-core-ee</artifactId>
  <version>${mule.version}</version>
  <scope>provided</scope>
</dependency>
<!-- Xml configuration -->
<dependency>
  <groupId>com.mulesoft.muleesb.modules</groupId>
  <artifactId>mule-module-spring-config-ee</artifactId>
  <version>${mule.version}</version>
  <scope>provided</scope>
</dependency>

Replace it with this one:

<dependency>
  <groupId>org.mule</groupId>
  <artifactId>mule-core</artifactId>
  <version>${mule.version}</version>
  <scope>provided</scope>
</dependency>
<!-- Xml configuration -->
<dependency>
  <groupId>org.mule.modules</groupId>
  <artifactId>mule-module-spring-config</artifactId>
  <version>${mule.version}</version>
  <scope>provided</scope>
</dependency>

Also you need to have a mule server running for this to work. See this blogpost for details. The install goal will deploy the mule project to the $MULE_HOME/apps directory of the mule server installation and if the server is running, it wil startup the project automatically.

SoapUI

Regardless of the way you’re running the mule project, be it via Maven or via Mule Studio, you should be able to test the proxy now with soapUI. Create a New Project, point it to the wsdl of the proxy, i,e. http://localhost:8081/hrManagerServieProxy?wsdl, and add a default TestSuite. Now you can run a TestCase against the proxy and it should yield the same result as it would when you’d call the webservice behind it directly.

Screenshot from 2013-01-03 21:26:50

soapUI – Testing the proxy

Summary

In this blogpost we’ve created a simple webservice proxy which does some basic routing without any kind of transformation. In the next post we’ll add some transformation and basic logging to the mix.

%d bloggers like this: