Creating a generic RESTful gateway on the OSB

In this recipe, we will show you how to implement a RESTful gateway on the OSB, which will accept RESTful requests and then routes these requests to the real implementation of the service that can also be a RESTful service. This adds an additional layer of processing logic, which can be used for things such as auditing, security, and SLA monitoring.

We will use an external public service called Brewery DB, which offers a RESTful API (http://www.brewerydb.com/api/documentation) to execute queries for breweries, beers, styles, and other information about beer.

The gateway we implement will be generic in such a way that it accepts all the different requests through one single proxy service and then routes the request directly to the business service, wrapping the external RESTful API of the Brewery DB service.

Creating a generic RESTful gateway on the OSB

Getting ready

To use the Brewery DB service API, an API key is needed. Just register for such an API key here: http://www.brewerydb.com/api/register.

Make sure to have access to the Internet and check that the Brewery DB API is accessible, by executing the following request from a browser: http://www.brewerydb.com/api/styles?apikey=<api-key>. Make sure that you replace the <api-key> with your own key. The service should respond with an XML document holding the different styles of beers.

How to do it...

We will start with the business service, which will wrap the Brewery DB service API. In Eclipse OEPE, perform the following steps:

  1. Create a new OSB project, name it creating-generic-restful-gateway and add a business and proxy folder to the project.
  2. In the business folder of the project, create a new business service and name it BreweryService.
  3. On the General tab select Messaging Service for the Service Type.
  4. Navigate to the Messaging tab and select XML for both the Request Message Type and the Response Message Type.
  5. Navigate to the Transport tab, enter http://www.brewerydb.com/api into the Endpoint URI field and click Add.

    By doing that the business service wrapping the Brewery DB service API is in place.

    Next, let's create the proxy service that will offer the same RESTful API and just passes request messages through to the Brewery DB service using the business service created previously. In Eclipse OEPE, perform the following steps:

  6. Right-click on the business service BreweryService.biz and select Oracle Service Bus | Generate Proxy Service.
  7. Enter BreweryService into the File name field and select the proxy folder for the location of the proxy service from the tree.
  8. Click Finish.
  9. Navigate to the Transport tab and enter /BreweryService into the EndpointURI field.
  10. Navigate to the Message Flow tab and check that a Route node with a Routing action, which invokes the business service, has been generated.
  11. Insert an Insert action into the Request Action flow of the Routing action.
  12. On the Properties of the Insert action, enter outbound into the In Variable field.
  13. Click <Expression>, enter <http:http-method>{$inbound/ctx:transport/ctx:request/http:http-method}</http:http-method> into the Expression field and click OK.
  14. Click <XPath>, enter ./ctx:transport/ctx:request into the Expression field and click OK.
  15. Leave the Location on as first child of.
  16. Duplicate the Insert action by copying and pasting it into the Request Action.
  17. On the Properties of the second Insert action, click on the link to the right of Expression, enter <http:query-string>{$inbound/ctx:transport/ctx:request/http:query-string}</http:query-string> into the Expression field and click OK.
  18. Duplicate the Insert action by copying and pasting it into the Request Action.
  19. On the Properties of the third Insert action, click on the link to the right of Expression, enter <http:relative-URI>{$inbound/ctx:transport/ctx:request/http:relative-URI}</http:relative-URI> into the Expression field and click OK.
  20. Insert a Log action into the Response Action flow of the Routing action.
  21. On the Properties tab of the Log action, click <Expresssion>, enter $body and click OK.
  22. Select Warning for the Severity drop-down listbox.
  23. The message flow of the proxy service should look as shown in the following screenshot:
    How to do it...
  24. Deploy the project to the OSB server.

    Our generic RESTful service gateway is now ready and can be tested. Open a browser window and perform the following steps:

  25. Send a request using the following URL (replace the <api-key> with your key): http://localhost:7001/BreweryService/beers?apikey=<api-key>&format=xml.
  26. The response should be shown in the browser window and holds a XML document with a list of beers as shown in the following screenshot:
    How to do it...
  27. The response should also be shown in the OSB console log window.
  28. Send a request using the following URL (replace the <api-key> with your key): http://localhost:7001/BreweryService/beers?apikey=<api-key>&format=json.
  29. The response in the browser window will now contain a list of beers in JSON format.
  30. Send a request using the following URL (replace the <api-key> with your key): http://localhost:7001/BreweryService/breweries?apikey=<api-key>.
  31. The response in the browser window will contain a list of breweries formatted as XML.

How it works...

In this recipe, we created a generic RESTful service gateway that accepts RESTful requests and passes it through to the Brewery DB RESTful service API.

We have created one single proxy service, accepting requests on the (base) Endpoint URI: http:\<osbserver>:<osb-port>BreweryService. By doing that, the message flow of the proxy service will be triggered for all requests using this base URI, such as:

  • http://localhost:7001/BreweryService/beers?apikey=<api-key>&format=xml
  • http://localhost:7001/BreweryService/breweries?apikey=<api-key>

By adding the three Insert actions into the message flow of the proxy service we make sure that the HTTP method, the relative part of the URI (such as /beers and /breweries), as well as the query parameters (such as apikey and format) are copied from the inbound into the outbound variable. This is important, as the generic business service also only holds the base part of the Endpoint URI: http://www.brewerydb.com/api. When sending a request to the Brewery DB service, the values of the <http:relative-URI> and <http:query-string> elements in the outbound variable are combined with the Endpoint URI.

So far only the response of the Brewery DB service is logged. But this centralized, additional layer of processing logic can be useful for many other things, such as:

  • monitoring the response time of the different services centrally and alerting an administrator if an SLA is violated
  • routing to other external services, depending on the request sent to the proxy service
  • mapping other security credentials to the API keys used by the external service
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset