Exposing RESTful Interface With Mule pt.1

Update (20.10.2013) - When not supported HTTP method is used, return more appropriate HTTP status (405 instead of 400).

As it’s my first blog entry I’d like to welcome everyone. If you’d like to know more about me click on the tab above. If not.. I’ll go straight to the point.

Recently, I had to create Mule’s application, which exposes itself via a simple RESTful API. However, when it comes to REST, Mule ESB seems to be quite limited in viable options.
The only, official approach is to use the REST Component, which relies on Jersey, which is the Reference Implementation of JAX-RS. Sounds good, but it ties you to the Java code instead of having fun with the Mule’s message processing :) This can be overcome as well, but still some Java needs to be written.

Fortunately, there are some other possibilites, which would make simple REST API creation easier. Nevertheless, I’d like to present you all the options (of which I’m aware of) with description and a working example.

All presented examples were tested against Mule ESB 3.4.0 EE. However, they should run without any problems on CE and even on previous versions.

1. “Poor man’s” REST:

First approach is the most straightforward one - handling HTTP properties available in Mule manually. Quick overview over HTTP properties is available here - (1.4.7 Use Case) in archived Mule Developer Resources.

In that case, we need to take care of detecting the HTTP method, URL and fetching URL variable parameters by ourselves. We can accomplish that using Mule’s choice component and setting flow variables (if needed).
An example will accept URL (on 8088 port) as follows: /client/{accountID}/{userID}/get on both GET and POST HTTP methods, where {accountID} and {userID} are the variable parameters.

General overview of the flow:

First step is not necessary, but helpful for quick suppresion of favicon.ico requests when accesing service via browsers:

1<message-filter doc:name="Filter favicon">  
2  <not-filter>  
3    <wildcard-filter casesensitive="true" pattern="/favicon.ico">  
4  </wildcard-filter></not-filter>  
5</message-filter>  

To match URL let’s use choice component with regular expression:

 1<choice doc:name="Choice">  
 2  <when expression="#[regex('/client/\\w+/\\w+/get/?', message.inboundProperties['http.request.path'])]">  
 3   ...  
 4 </when>  
 5  <otherwise>  
 6    <http:response-builder status="400" doc:name="Return 400 For bad URL">  
 7      <set-payload value="Unknown resource: #[message.inboundProperties['http.request.path']]" />  
 8    </http:response-builder>  
 9  </otherwise>  
10</choice>  

We retrieve URL path using Mule’s inbound property - ‘http.request.path’, then match it with the regexp ‘/client/\w+/\w+/get/?’. If we don’t have a match we just simply return 400 HTTP status.
Otherwise, we can proceed and determine which HTTP method was used to send the request:

 1  <when expression="message.inboundProperties['http.method'] == 'GET'">  
 2    <flow-ref name="RetrievingParameters" doc:name="Retrieve Parameters"/>  
 3    <flow-ref name="ProcessGET" doc:name="Process GET"/>  
 4  </when>  
 5  <when expression="message.inboundProperties['http.method'] == 'POST'">  
 6    <flow-ref name="RetrievingParameters" doc:name="Retrieve Parameters"/>  
 7    <flow-ref name="ProcessPOST" doc:name="Process POST"/>  
 8  </when>  
 9  <otherwise>  
10    <http:response-builder status="405" doc:name="Return 405 For bad HTTP method">  
11      <set-payload value="Unknown HTTP method: #[message.inboundProperties['http.method']]" />  
12    </http:response-builder>  
13  </otherwise>  
14</choice>  

This time we are checking another inbound parameter - ‘http.method’. If it’s not the method we want to proceed with, we return 405 HTTP status (Method Not Allowed).

For all supported methods we want to fetch the URL parameters. Hence, this functionality is extracted to a reusable sub-flow:

1<sub-flow name="RetrievingParameters" doc:name="RetrievingParameters">  
2  <set-variable variableName="accountID" value="#[StringUtils.splitAndTrim(message.inboundProperties['http.request.path'], '/')[1]]" doc:name="Set Account ID"/>  
3  <set-variable variableName="userID" value="#[StringUtils.splitAndTrim(message.inboundProperties['http.request.path'], '/')[2]]" doc:name="Set User ID"/>
4</sub-flow>

As I’m not good at regular expressions, I decided to go with Mule’s StringUtils method, which splits the URL and gets me the parameters I’m looking for.

To avoid providing fully qualified name of the class over and over again, we can define global import for Mule expressions:

1<configuration doc:name="Configuration">  
2  <expression-language>  
3    <import class="org.mule.util.StringUtils" />  
4  </expression-language>  
5</configuration>  

That is basically everything we needed. We can now process GET / POST requests. For readability and maintainability reasons we will do that in separate flows:

 1<flow name="ProcessPOST" doc:name="ProcessPOST">  
 2  <http:response-builder status="200"doc:name="Return OK">  
 3    <set-payload value="Processing POST with account id: #[accountID] and user id: #[userID] and body: #[payload]" doc:name="Set Payload"/>  
 4  </http:response-builder>  
 5</flow>  
 6<flow name="ProcessGET" doc:name="ProcessGET">  
 7  <http:response-builder status="200"doc:name="Return OK">  
 8    <set-payload value="Processing GET with account id: #[accountID] and user id: #[userID]" doc:name="Set Payload"/>  
 9  </http:response-builder>  
10</flow>

To test my service I’ve prepared a test class with couple of Mule functional tests. Instead of using default FunctionalTestCases, I suggest using MUnit. From what I know, it is supposed to replace current Mule testing solution in future. With MUnit you can write your tests using Java (JUnit) or XML (Mule code). It has features like mocking endpoints, processors and ability to call flow directly, skipping inbound endpoints. However, development of MUnit seemed to slow down recently.
To keep the test concise, I used JUnitParams extension, as I wanted to use same test methods, but with different input (URLs).

Additionally, we could’ve checked for content-type etc., but I didn’t want to clutter the code with insignificant details.
Having in mind next example, I’m not exactly sure, if anyone would opt for this approach. But still, there it is, probably for demonstration purposes only ;)

Full example at GitHub: PoorMansREST

2. REST Router

To make our lives easier Mule REST Router Module has been developed. Code is available at GitHub.
Almost everything we did in previous approach is wrapped into one message processor. Installation and configuration is well explained in the links provided.

Flow overview:

Basically, to implement same behaviour, we need to configure rest-router as follows:

1<rest-router:router templateUri="/client/{accountID}/{userID}/get">  
2  <rest-router:get>  
3    <flow-ref name="ProcessGET" doc:name="Process GET" />  
4  </rest-router:get>  
5  <rest-router:post>  
6    <flow-ref name="ProcessPOST" doc:name="Process POST" />  
7  </rest-router:post>  
8</rest-router:router>  

REST Router automatically assign parameters specified in templateUri to flow variables. However, I found out that it passes through empty parameters, so it is advisable to validate them explicitly in your code.</div>

Outstanding part is to cover invalid URLs and return something meaningful. For requests not matching our template we return standard 400 HTTP status (placed just after </rest-router:router>) :

1<http:response-builder status="400" doc:name="Return 400 For bad URL">  
2  <set-payload value="Unknown resource: #[message.inboundProperties['http.request.path']]" />  
3</http:response-builder>  

For HTTP methods not supported REST Router should (per documentation) throw UnsupportedHttpVerbException. However, there is an issue I submitted here. Unfortunately, the project looks abandoned as nobody cared :) I did some more research and it appears that the issue is not in the REST Router itself, but how the DevKit java code is generated.
There are two solutions for that:
1) Always implement whole set of methods in REST Router and return appriopriate message / status / throw exception in those you don’t want to support.
2) As I don’t like to have redundancy in my code I used a dirty hack of replacing compiled class with a fixed one directly in the jar file. Amended version of the module is available here. Just install it in your local Maven repository and switch module version in your pom.xml file to 1.2-fixed.

Fix description:

HTTP method recognition is implemented as in the following snippet: (RestRouterModule.java)

 1if (get != null && method.equalsIgnoreCase("get")) {  
 2 return get.processWithExtraProperties(properties);  
 3} else if (put != null && method.equalsIgnoreCase("put")) {  
 4 return put.processWithExtraProperties(properties);  
 5} else if (post != null && method.equalsIgnoreCase("post")) {  
 6 return post.processWithExtraProperties(properties);  
 7} else if (delete != null && method.equalsIgnoreCase("delete")) {  
 8 return delete.processWithExtraProperties(properties);  
 9} else if (patch != null && method.equalsIgnoreCase("patch")) {  
10 return patch.processWithExtraProperties(properties);  
11} else {  
12 throw new UnsupportedHttpVerbException(method);  
13}

Variables: get, put, post… are instances of NestedProcessor class, marked @Optional.
However, code generated by DevKit always propagates those variables to the RestRouterModule as instances of NestedProcessorChain (RouterMessageProcessor.java):

1final NestedProcessor _transformedGet = new NestedProcessorChain(event, getMuleContext(), ((MessageProcessor) get));  
2final NestedProcessor _transformedPut = new NestedProcessorChain(event, getMuleContext(), ((MessageProcessor) put));  
3final NestedProcessor _transformedPost = new NestedProcessorChain(event, getMuleContext(), ((MessageProcessor) post));  
4final NestedProcessor _transformedDelete = new NestedProcessorChain(event, getMuleContext(), ((MessageProcessor) delete));  
5final NestedProcessor _transformedPatch = new NestedProcessorChain(event, getMuleContext(), ((MessageProcessor) patch));  

Those _transformed method variables are passed into the RestRouterModule class, hence it always tries to process the processWithExtraProperties(properties) method and eventually finishes with NullPointerException as the last argument in NestedProcessorChain contructor is null.

My fixed version of RouterMessageProcessor.java includes following change:

1final NestedProcessor _transformedGet = (get == null) ? null : new NestedProcessorChain(event, getMuleContext(), ((MessageProcessor) get));  
2final NestedProcessor _transformedPut = (put == null) ? null : new NestedProcessorChain(event, getMuleContext(), ((MessageProcessor) put));  
3final NestedProcessor _transformedPost = (post == null) ? null : new NestedProcessorChain(event, getMuleContext(), ((MessageProcessor) post));  
4final NestedProcessor _transformedDelete = (delete == null) ? null : new NestedProcessorChain(event, getMuleContext(), ((MessageProcessor) delete));  
5final NestedProcessor _transformedPatch = (patch == null) ? null : new NestedProcessorChain(event, getMuleContext(), ((MessageProcessor) patch));

Not much to explain here. Now, if a REST module HTTP method is not defined it will propagate null to RestRouterModule.java and throw UnsupportedHttpVerbException as expected.</div>

In the flow we use standard choice exception strategy and catch the UnsupportedHttpVerbException to return 405 HTTP status:

1<choice-exception-strategy doc:name="Choice Exception Strategy">  
2   <catch-exception-strategy doc:name="Catch Exception Strategy" enablenotifications="false" when="#[exception.causedBy(UnsupportedHttpVerbException)]">  
3       <http:response-builder doc:name="Return 405 For bad HTTP method" status="405"><set-payload value="Unknown HTTP method: #[message.inboundProperties['http.method']]"></set-payload></http:response-builder> 
4   </catch-exception-strategy>
5</choice-exception-strategy>  

For testing I used the same set of tests as in previous example. As you can see, it’s much quicker and cleaner approach that saves you a lot of time and lines of code :)

Full example (using modified version of the module) at GitHub: RestWithRouter

In the next part I will present how to achieve the same, but using Jersey with writing as little Java as possible. Go here for the second part!</div>

Comments