Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Create Mock REST services, or Proxy REST services and mediate them, through the UltraESB

DZone's Guide to

Create Mock REST services, or Proxy REST services and mediate them, through the UltraESB

· Integration Zone ·
Free Resource

Are your API program basics covered? Read the 5 Pillars of Full Lifecycle API Management eBook

The UltraESB from AdroitLogic includes full support for REST - so that uses could easily use it as a Proxy Service to front existing REST services of an enterprise. This allows the UltraESB to perform SSL decryption, HTTP basic/digest authentication etc, and then proxy the calls received from outside, safely to the real services. The UltraESB supports GET, POST, PUT, DELETE, HEAD and OPTIONS proxying, and performs 'Location' header re-writing on responses, so that the end-clients will always be directed to the proxy services as the entry point.

The UltraESB ships a sample RESTful service in ready-to-run state, and an easy to use graphical toolkit that support all forms of REST interactions. Thus it is trivial for a user to start the sample configuration, and use the ToolBox HTTP/S client to interact with the REST proxy service. A full article "RESTFul Proxy Services with the UltraESB" shows complete steps to interact with the REST service or its Proxy service interactively. Here is a full configuration for REST proxy service with the UltraESB. The Proxy service directs all REST requests to the RESTeasy sample service hosted at the http://localhost:9000/rest-services URL.

    <u:proxy id="rest-proxy">
<u:transport id="http-8280"/>
<u:target>
<u:inDestination>
<u:address type="prefix">http://localhost:9000/rest-services</u:address>
<u:property name="switchLocationHeadersTo" value="http://localhost:8280/service/rest-proxy"/>
</u:inDestination>
<u:outDestination>
<u:address type="response"/>
</u:outDestination>
</u:target>
</u:proxy>

The Mock Services support of the UltraESB allows one to easily write Mock services for REST! Mock responses maybe selected by Content Based Routing [CBR] on the request or its headers, and maybe even picked up from sample files on the file system. The article "Mock RESTfully with the UltraESB" shows how one could run the sample. Mock Service. As a reference, the following is the full configuration used to implement the Mock REST service used in the example.

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

The service shows how mock responses are read from the file system and returned to the user for GET, POST, PUT and DELETE requests. The mediation within the "inSequence" though written as a Java fragment - does not need to be compiled, bundled or deployed. The UltraESB considers it as "configuration" and compiles the code at runtime into byte code and executes with extreme performance. The user may specify mediation as Java code, a Sping bean, or any JSR 233 scripting language such as Javascript, Groovy, Ruby etc.

The full article explains how one could easily start the sample, and try out the scenarios end-to-end within less than 10 minutes. REST is only one of the message formats supported by the UltraESB. It can proxy and mediate SOAP, POX, Hessian, Text, HTML or any other form of payload over HTTP/. It also allows the user to switch between any of its transports such as HTTP/S, JMS, Email, File, S/FTP, FTPS etc or even to-or-from AS2 for B2B messaging such as with EDI. Read more about it at http://adroitlogic.org. 

Establish API creation, publishing and discovery as a master practice with the API Management Playbook.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}