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 3

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.


Implementing the Demo Wave Robot

Figure 5. App Engine Project with All Demo Files Added: Here is an Eclipse IDE project view with all the required files specific to this demo.

Figure 5 shows another Eclipse IDE project view, this time with all the required files for this example. Note the added ServiceCall.java file as well as the files in war/_wave.

Eclipse creates the file src/META-INF/jdoconfig.xml automatically when you use the Google App Engine Eclipse plug-in to create a new App Engine project. This demo doesn't use a persistent store, so you will leave this file alone (see my previous DevX App Engine article for an example of using the App Engine data store).

The directory war/_wave contains two XML files that are worth noting: capabilities.xml and profile.xml. The capabilities.xml file is used to register which Wave events you want your robot to receive from the Wave platform:

<?xml version="1.0" encoding="utf-8"?>
<w:robot xmlns:w="http://wave.google.com/extensions/robots/1.0">
    <w:capability name="wavelet_participants_changed" content="true" /> 
  <w:profile name="CookingSpace" />

This capabilities specification requests that the robot be notified whenever someone is added to a wave. (An upcoming discussion of the wave robot servlet will explain how to process this event.) In other examples of wave robots, additional events are registered for notification (although not in this demo):

<w:capability name="wavelet_blip_created" content="true" /> 
    <w:capability name="blip_submitted" content="true" />

Figure 6. Creating a New Wave: Here is an example of creating a new wave with the title "salmon rice."

The file war/_wave/profile.xml provides the name for the demo robot:

<?xml version="1.0"?>

The CookingSpace robot uses the wave title as a search term for finding matching recipes (see Figure 6 for an example with "salmon rice.").

CookingSpace Robot Servlet

You now come to the servlet that handles Wave event notifications from the Wave platform when the CookingSpace robot is added as a participant to a wave. Because most of the event-handling behavior is handled in the class ServiceCall, the implementation of this servlet is simple:
package com.cookingspace;

import com.cookingspace.rest.ServiceCall;

import java.util.logging.Logger;
import com.google.wave.api.AbstractRobotServlet;
import com.google.wave.api.Blip;
import com.google.wave.api.RobotMessageBundle;
import com.google.wave.api.TextView;
import com.google.wave.api.Wavelet;

public class CookingSpaceProxyServlet extends AbstractRobotServlet {
  private static final Logger log = Logger.getLogger(CookingSpaceProxyServlet.class.getName());

  public void processEvents(RobotMessageBundle events) {
    log.info("Entered processEvents " + events);
    Wavelet wavelet = events.getWavelet();

    if (events.wasSelfAdded()) {
      Blip blip = wavelet.appendBlip();
      TextView textView = blip.getDocument();
      textView.append("I'm alive and ready to provide recipes. For more information: ");
      String title = wavelet.getTitle();
      log.info("CookingSpace.com Recipe lookup web service: " + title);
      // String text = eventBlip.getDocument().getText().trim(); // we don't need blip text
      if (title.length() > 3 && title.length() < 30) {
        String results = new ServiceCall(title).results();
        textView.append("CookingSpace.com Recipe lookup web service: " + title + "\n\n" + results);

The logger is used to insert useful messages into the App Engine logs for this application. Whether you are writing plain App Engine applications or Wave robots, you will find the App Engine management console's log viewer to be very useful for both tracking down application bugs and generally understanding what occurs during the processing of requests. I treat these logger calls as debug printouts and usually remove them when I am satisfied that my App Engine applications are running correctly.

Figure 7. Adding the CookingSpace Robot to a New Wave: Here is an example of adding the CookingSpace robot to a new wave whose title is "salmon rice."

The method call events.getWavelet() returns the wavelet on the Wave system that triggered the event being processed. A wavelet is part of a wave document. It is made up of one or more blips. A blip is the smallest component of a document.

The method call events.wasSelfAdded() returns a true Boolean value if the event was triggered by the CookingSpace robot being added to a wave. If that is the case, then you want to get the title of the wave. A "blip" is a part of a wave, such as the initial wave contents and so forth. In this case, the blip is new information that the CookingSpace robot will add to the original wave (see Figure 7).

A text view of a blip shows its title and text. The code above uses the blip title and ignores the body text of the blip. It passes the title to the ServiceCall utility class to make the web service call to cookingspace.com and to format the returned recipes (if any are found). It then adds this recipe text to the new blip created by calling Blip blip = wavelet.appendBlip().

That's it! You are done implementing the robot. Figure 8 shows the Wave web client after the CookingSpace robot has been added, Figure 9 shows the client displaying a recipe added by the robot, and Figure 10 shows the nutrition data for the recipe in Figure 9.

Figure 8. After the CookingSpace Robot Has Been Added: Here's the Wave web client after the CookingSpace robot has been added.
Figure 9. Showing a Recipe Added by the Robot: Here's the Wave web client showing a recipe added by the robot for the search query "salmon rice."
Figure 10. Showing Nutrients for Recipe Added by the Robot: Here's the Wave web client showing the nutrition data for the recipe in Figure 9.

This simple demo provided you with a quick-start if you want to reuse any of your applications as Wave robots. Almost all of the work for the demo was in developing and testing the ServiceCall utility class. When you become more experienced with Wave robots, you will need only a few minutes to complete the "boilerplate" work of starting new Wave robot projects:

  1. Creating a new Eclipse project
  2. Adding the required Wave client JAR files
  3. Writing the event-handing servlet

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