AdroitLogic Private Ltd.

  • Increase font size
  • Default font size
  • Decrease font size

Mock RESTfully with the UltraESB

Creating RESTful mock services is made a trivial task with the UltraESB, as RESTful services can easily be created with just a couple of lines of configuration. This article shows how a mock REST service maybe created that will respond to GET, POST, PUT and DELETE calls. The objective of the example is to introduce the user to the UltraESB for mock RESTful services, and not to convey the most efficient way to develop RESTful services.

 

The sample configuration used in this example is stored as "ultra-sample-106.xml" and can be started easily via the command line,  or through the UltraESB tab of the graphical console, ToolBox.

asankha@asankha:~/java/ultraesb-1.0-beta-1/bin$ ./ultraesb.sh -sample 106

Now, start the ToolBox, and issue a GET request to the following URLs. You could then try POST, DELETE and PUT methods over the same resources

  • http://localhost:8280/service/rest-mock/customers/1
  • http://localhost:8280/service/rest-mock/customers/2

The mock responses are generated by the following proxy service

    <u:proxy id="rest-mock">
<u:transport id="http-8280"/>
<u:target>
<u:inSequence>
<u:java import="org.adroitlogic.ultraesb.transport.http.*;"><![CDATA[
if ("GET".equals(msg.getMessageProperty(HttpConstants.HTTP_METHOD))) {
msg = msg.createDefaultResponseMessage();
if (msg.getDestinationURL().endsWith("customers/1")) {
Mediation.setPayloadFromFile(msg, "samples/resources/mock-response-1.xml");
} else if (msg.getDestinationURL().endsWith("customers/2")) {
Mediation.setPayloadFromFile(msg, "samples/resources/mock-response-2.xml");
} else {
Mediation.setPayloadFromFile(msg, "samples/resources/mock-response-3.xml");
}
Mediation.sendResponse(msg, 200);
} else if ("POST".equals(msg.getMessageProperty(HttpConstants.HTTP_METHOD))) {
msg = msg.createDefaultResponseMessage();
msg.addTransportHeader("Location", "http://localhost:8280/service/rest-mock/customers/1");
Mediation.sendResponse(msg, 201);
} else if ("PUT".equals(msg.getMessageProperty(HttpConstants.HTTP_METHOD))) {
msg = msg.createDefaultResponseMessage();
Mediation.sendResponse(msg, 204);
} else if ("DELETE".equals(msg.getMessageProperty(HttpConstants.HTTP_METHOD))) {
msg = msg.createDefaultResponseMessage();
Mediation.sendResponse(msg, 204);
} else {
msg.createDefaultResponseMessage();
Mediation.sendResponse(msg, 500);
}
]]></u:java>
</u:inSequence>
</u:target>
</u:proxy>

As can be seen, this example illustrates only the very basics on replying to RESTful requests using some of the mock service support. It would be very easy to extend this same sample to say - perform Content Base Routing (CBR) and/or then use XSLT transformation to generate the result to be returned etc. In addition, there is no compiling, no bunding or creation of JAR files, execution of Maven scripts etc.. Just edit the script for any changes, and run!