After teaching math (and a little computer science) for 14 years, Brian changed careers in 2006 with the idea that getting out of his comfort zone would be good. So now he works as a writer and programmer at Microsoft who specializes in PHP and Windows Azure. Brian is a DZone MVB and is not an employee of DZone and has posted 82 posts at DZone. You can read more from them at their website. View Full User Profile

Consuming OData via JSP in Windows Azure

05.25.2011
| 8435 views |
  • submit to reddit

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 http://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: http://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:

<%@ page language="java" 

     contentType="text/html; charset = ISO-8859-1"

     import="org.odata4j.consumer.*, org.odata4j.core.*, java.util.List"

 %>  


    <html>

    <head>

        <title>Consuming OData with JSP/Java</title>

    </head>

    <body>

       <h1>My Movies</h1>

       <%

    

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

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

              {              

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

                {                  

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

  </body>

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

References
Published at DZone with permission of Brian Swan, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)