Consuming OData via JSP in Windows Azure

A colleague recently asked me if I knew anything about consuming OData from a Java deployment in Windows Azure. My answer at the time was “no”, but with a quick pointer to https://code.google.com/p/odata4j/ I know the answer would soon change to “yes”. In this short post, I’ll show you how I used this tutorial, Deploying a Java Application to Windows Azure with Command-line Ant, to quickly comsume OData from Java running in Windows Azure.

Note: This post is not an investigation of the odata4j API. It is simply a look at how to deploy odata4j to Windows Azure. However, I will note that I didn’t see any functionality in the odata4j API for generating classes from an OData feed (unlike the OData SDK for PHP). This means that you need to know the structure of the feed you are consuming when writing code.

As I mentioned in the intro, to get my OData/JSP page running in Windows Azure, I basically followed this tutorial, written by Ben Lobaugh. Using Ben’s tutorial as a guide, you really only need to change a few things:

1. Download and install odata4j. In the You can download odata4j here: https://code.google.com/p/odata4j/. After you have downloaded and unzipped the archive, move the odata4j-bundle-0.4.jar file to the /lib/ext directory of your Java installation.

2. Create your jre6.zip archive with the odata4j files included. In the Select the Java Runtime Environment section of Ben’s tutorial, you are directed to create a .zip archive from your Java installation. Make sure you have added the odata4j-bundle-0.4.jar file to the /lib/ext directory of your Java installation before creating the archive.

3. Change the HelloWorld.jsp code to use the odata4j API. In the Preparing your Java application section of Ben’s tutorial, you need to write code that uses the odata4j API. Here’s a very simple example:

    1: <%@ page language="java" 
    2:     contentType="text/html; charset = ISO-8859-1"
    3:     import="org.odata4j.consumer.*, org.odata4j.core.*, java.util.List"
    4: %>
    5:  
    6: <html>
    7: <head>
    8:     <title>Consuming OData with JSP/Java</title>
    9: </head>
   10: <body>
   11:     <h1>My Movies</h1>
   12:     <%   1:  

   2:         ODataConsumer c = ODataConsumer.create("https://odata.netflix.com/v1/Catalog/");

   3:         List<OEntity> titles = c.getEntities("Titles").filter("Name eq 'Into the Wild'").execute().toList();

   4:  

   5:         for(OEntity title : titles) 

   6:         {              

   7:             for(OProperty<?> p : title.getProperties()) 

   8:             {                  

   9:                 out.println(p.getName() + ": " + p.getValue() + "<br />");

  10:             }

  11:         }

  12:     
%>
   13: </body>
   14: </html>

Note the references to org.odata4j.consumer and org.odata4j.core at the top of the page.

That’s it. If you follow the rest of Ben’s tutorial for testing your application in the Compute Emulator and deploying it to Windows Azure, you should have your application up and running quickly.

Next up, the Restlet OData extension. :-)

Thanks.

-Brian

Share this on Twitter