Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Write Your First Google Wave Robot in Java : Page 2

Creating useful content and services is expensive and time consuming. Why not amortize your efforts by publishing those creations to multiple platforms? The Google Wave platform enables you to do just that.


WEBINAR: On-Demand

Unleash Your DevOps Strategy by Synchronizing Application and Database Changes REGISTER >

Setting Up Your Development Environment

Follow these recommendations to get the most from the demo in this article:
Figure 3. Using the Google Plug-in to Create a New App Engine Project: Here is a screenshot of a new App Engine project created with the Google Plug-in for Eclipse.

  1. Spend at least a few minutes reading Google's documentation for installing the required software for writing Java robots. These instructions show you how to set up a project from scratch.
  2. Use Eclipse with the Google App Engine plug-in. (Not a requirement, but I strongly recommend this for serious robot development and for general Java AppEngine development.)
  3. Download the ZIP file with the demo robot code and configuration.
  4. Refer to the API documentation for the RobotMessageBundle interface. This is a good page to bookmark when developing Wave robots because most of the APIs that you will need are either on this page or can be reached from links on this page.

Figure 3 shows how you create a project using the Google Plug-in for Eclipse.

Figure 4. App Engine Project Tree in Eclipse IDE: Here is the project tree in Eclipse after creating a new App Engine project and copying in the required JAR files to war/WEB-INF/lib.

When you set up your own projects—even if you use the Google Plug-in for Eclipse—you need to start by downloading copies of the wave-robot-api.jar, jsonrpc.jar, and the json.jar files from Google's Robot Java Client Libraries and copying them to the directory war/WEB-INF/lib in your project directory. Figure 4 shows the project tree in Eclipse after creating a new App Engine project and copying in the three JAR files to war/WEB-INF/lib. (Note the wave-robot-api.jar file name includes a date stamp: wave-robot-api-20090916.jar).

Implementing a Proxy in App Engine

For this demo, the proxy must be able to complete three steps:
  1. Make a web service call to the test server that I use for running CookingSpace.
  2. Process the JSON data returned from the web service call. (You can find the documentation for the Java JSON library here.)
  3. Process Wave events.

Making a Web Service Call

One of the App Engine platform's restrictions is you must use a special URL fetch service to make external web service calls. Here is the demo robot code that performs the web service call:

String data = URLEncoder.encode("search_text", "UTF-8") + "=" + URLEncoder.encode(query, "UTF-8"); data += "&" + URLEncoder.encode("format", "UTF-8") + "=" + URLEncoder.encode("json", "UTF-8"); URL url = new URL("http://cookingspace.com"); URLConnection conn = url.openConnection(); conn.setDoOutput(true); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write(data); wr.flush(); BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line = rd.readLine(); System.out.println("line: " + line); wr.close(); rd.close(); JSONTokener jt = new JSONTokener(line); // process returned JSON payload

Author's Note: The REST architectural style promotes using an HTTP GET to fetch a representation of data from a resource and using HTTP PUT requests to create and update remote resources. That said, PUT is more general and I decided to use it in this example in order to show you how to set up a PUT request. GET requests like http://cookingspace.com/?search_text=rice&format=json work fine also.

The demo robot source code includes a new Java package com.cookingspace.rest and a new class ServiceCall that is hardwired to access CookingSpace. If you look at the source code in file ServiceCall.java, you will see that it uses the data in the JSON payload to create a return string value that contains a formatted list of recipes (with ingredients, directions, and nutrition data) for recipes that match the search query. Here is an example of what a JSON return payload looks like (with lots of text removed for brevity):

[[{"rating": 2.0, "name": "Arroz con Pollo", "directions": "Heat oil in a large skillet .....", "num_served": 4, }, ["Chicken (fryer)", "1 ", "unit ( yield from 1 lb ready-to-cook chicken)", "vegetable oil", ...], [["carbohydrate", 43.60446125], ["energy_in_kcal", 1208.3971164], ["fat_mono_unsaturated", 17.0170589726], ["fat_poly_unsaturated", 8.7905745974], ["fat_saturated", 5.7239387098], ["fiber", 1.82729], ...], ....]]]

Next, you will see how the demo Wave robot uses the code in the class file ServiceCall.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date