AdroitLogic Private Ltd.

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

RESTful Proxy Services with the UltraESB

This example demonstrates a simple RESTful Proxy service, which will proxy RESTful messages to a JBoss RESTEasy [http://www.jboss.org/resteasy/] backend service implemtnation. The Proxy service is exposed over HTTP on port 8280, and forwards incoming messages to an endpoint specified as  an address prefix. The prefix endpoint is also configured to re-write location headers [Location & Content-Location] back on responses, to point clients back to the proxy service as expected, with the "switchLocationHeadersTo" property.

To run the example, start the UltraESB sample configuration 101 via the ToolBox or on the command line as follows

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

Now start the ToolBox, and fire-up the sample Jetty server that holds a RESTEasy sample service for testing. The sample configuration defines the following proxy service:

    <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 Proxy service endpoint will be http://localhost:8280/service/rest-proxy, while the original RESTEasy sample hosted on the Jetty server is available at http://localhost:9000/rest-services/customers. To create a new Customer record, first load a sample request using Preset "3" from the HTTP/S client of the ToolBox, and issue it against the RESTEasy service using POST. Your response will be as follows:

HTTP/1.1 201 Created
Location: http://localhost:9000/rest-services/customers/1
Connection: close
Server: Jetty(6.1.21)

Now change the URL to http://localhost:8280/service/rest-proxy/customers and issue the same request over POST against the UltraESB proxy service. The second Customer instance will now be created in the backend,and a response similar to the following returned.

HTTP/1.0 201 Created
Location: http://localhost:8280/service/rest-proxy/customers/2
Date: Wed, 13 Jan 2010 04:54:51 GMT
Server: UltraESB/1.0-beta-1
Content-Length: 0
Connection: close

Note that when the request was made through the Proxy Service, the "Location" header of the response has been re-written so that a subsequent call by the client will be directed to the Proxy service itself, instead of the Location returned by the RESTEasy sample - which can be seen in the first response.

To experiment with REST features, you can now perform a GET operation on the returned 'Location'. A GET on URL http://localhost:8280/service/rest-proxy/customers/2 will return the following:

HTTP/1.0 200 OK
Content-Type: application/xml
Date: Wed, 13 Jan 2010 04:56:53 GMT
Server: UltraESB/1.0-beta-1
Content-Length: 243
Connection: close

<customer id="2">
   <first-name>Asankha</first-name>
   <last-name>Perera</last-name>
   <street>12 A 1 Pirivena Road</street>
   <city>Mount Lavinia</city>
   <state>LK</state>
   <zip>10370</zip>
   <country>Sri Lanka</country>
</customer>

To modify a customer instance, issue a PUT on the URL with a request payload to replace the resource. For example, edit the Preset "3" request and set the "first-name" element to something different. As an example, issuing a PUT on the URL http://localhost:8280/service/rest-proxy/customers/2 with first-name set to "Chamath" will return the following response confirming the update.

HTTP/1.0 204 No Content
Date: Wed, 13 Jan 2010 05:01:53 GMT
Server: UltraESB/1.0-beta-1
Connection: close

A subsequent GET on the URL will now return the updated request as follows:

HTTP/1.0 200 OK
Content-Type: application/xml
Date: Wed, 13 Jan 2010 05:04:35 GMT
Server: UltraESB/1.0-beta-1
Content-Length: 243
Connection: close

<customer id="2">
<first-name>Chamath</first-name>
<last-name>Perera</last-name>
<street>12 A 1 Pirivena Road</street>
<city>Mount Lavinia</city>
<state>LK</state>
<zip>10370</zip>
<country>Sri Lanka</country>
</customer

Issuing a DELETE on the same URL will remove the Customer instance from the backend, with a response similar to the following,

HTTP/1.0 204 No Content
Date: Wed, 13 Jan 2010 05:05:31 GMT
Server: UltraESB/1.0-beta-1
Connection: close

Any subsequent operations (e.g. a GET) on the URL will return a "404 Not Found" response from RESTEasy

HTTP/1.0 404 Not Found
Date: Wed, 13 Jan 2010 05:06:56 GMT
Server: UltraESB/1.0-beta-1
Content-Length: 0
Connection: close