| Java, Eclipse and Web programming Tutorials |
Version 1.5
Copyright © 2009 - 2010 Lars Vogel
04.03.2010
| Revision History | ||
|---|---|---|
| Revision 0.1 | 29.08.2009 | Lars Vogel |
| First version | ||
| Revision 0.2 | 30.08.2009 | Lars Vogel |
| Added embedded | ||
| Revision 0.3 | 01.09.2009 | Lars Vogel |
| clean-up | ||
| Revision 0.4 | 02.09.2009 | Lars Vogel |
| more information | ||
| Revision 0.5 | 03.09.2009 | Lars Vogel |
| Screenshot of Wave, first working robot | ||
| Revision 0.6 | 06.09.2009 | Lars Vogel |
| Clean-up of description, better GAE description | ||
| Revision 0.7 | 07.09.2009 | Lars Vogel |
| Added buzzword bot from Oliver Gierke | ||
| Revision 0.8 | 11.09.2009 | Lars Vogel |
| Added link to Google Wave example apps | ||
| Revision 0.9 | 29.10.2009 | Lars Vogel |
| Added link to the standard Google Wave system | ||
| Revision 1.0 | 25.12.2009 | Lars Vogel |
| General re-work | ||
| Revision 1.1 | 28.12.2009 | Lars Vogel |
| improved robot description | ||
| Revision 1.2 | 30.12.2009 | Lars Vogel |
| added one week in average to wait for account | ||
| Revision 1.3 | 02.01.2010 | Lars Vogel |
| added logging | ||
| Revision 1.4 | 03.03.2010 | Lars Vogel |
| Started update to new Robot API V2 | ||
| Revision 1.5 | 04.03.2010 | Lars Vogel |
| Updated to new Robot API V2 | ||
Table of Contents
Google Wave is a communication platform which merges the mediums email, forum, chat, instance messaging and wiki.
Google Wave can be separated into the product, the platform and the protocol. The product is what people can use, the platform allows developers to extend the Wave product and the protocol takes care of the synchronization between wave documents and other services.
The Google Wave product looks similar to the following screenshot.

Currently Google is running a beta program so not everyone can join Google Wave. You can sign-up to a waiting list on the Google Wave homepage . Google tries to grand access to Wave as fast as possible; currently access is usually given within a week after sign-up.
Google Wave has two environments. The Wave sandbox and the standard Wave Server. The standard wave server can be found https://wave.google.com/wave . The Wave sandbox system can be found here https://www.wavesandbox.com .
A wave is a threated conversation between one or several parties (people or programs (robots)).
A wave can be seen as an envelop which contains wavelets. A wavelet is a subset of a larger conversation (== wave). Access control can be given based on wavelets and all participants in a wavelet have full read and write access to the wavelet.
Wavelets contain blips. A Blip is a single, individual message. Blips can be have the status "published" or "draft". Blip stores their content in a XML document.
The Google Wave API operates either on wavelets or on blips.
The Google Wave platform supports the development of Robots and Gadgets. A robot runs on a server while the gadget runs on the client. The gadget will manipulate the Wave XML locally and the delta is send to the server.
A robot can get added to a wave. After you added a robot to a Wave the robot will get notified if the Wave changes and can then react to the changes. A robot must be currently hosted on the Google App Engine and is an application which interfaces with a Wave via the Wave protocol.
The following tutorial will focus on developing Google Wave Robots.
For an introduction into Eclipse please see Eclipse Introduction .
Google offers a Eclipse plugin that provides both Google App Engine and GWT development capabilities. Install the plugins from http://dl.google.com/eclipse/plugin/3.5 via the update manager of eclipse (please see Using the update manager of Eclipse for details).
Install all available features from the Google update site.
Download the required libraries from from http://code.google.com/p/wave-robot-java-client/ the following libraries
wave-robot-api.jar
gson.jar
oauth.jar
wave-model.jar
Currently a robot needs to run on the Google App Engine to participate in a Wave. We use the Google App Engine for Java which is also called GAE/J.
You need to
Register yourself on the Google App Engine
Create an application which will become your Wave robot (this will be later part of this tutorial).
To learn about the Google App Engine please see Google App Engine Development with Java
Create an account at the Google App Engine Homepage in case you have not yet done this.
Create an application which we will later use for our robot. My application will be called "vogellawave".

The following will develop a robot for Google Wave. This robot will be a "buzzword" detector. If you add the robot to a wave you can register certain words as buzzword. If buzzwords are used more then 3 times the robot will add "bingo" to the wave conversation.
Create a new "Web Application Project" "de.vogella.google.wave.firstrobot". Select only GAE support. The procedure for creating a project is described in Google App Engine .
Put the wave jars you downloaded from http://code.google.com/p/wave-robot-java-client/ in the folder "/war/WEB-INF/lib" and add them to the project build path.
Create the following Java class.
package de.vogella.google.wave.firstrobot;
import com.google.wave.api.AbstractRobot;
import com.google.wave.api.event.WaveletParticipantsChangedEvent;
import com.google.wave.api.event.WaveletSelfAddedEvent;
/**
* Small sample bot for Google Wave to demonstrate bot behavior. Just says hello
* and goodbye
*
* @author Lars Vogel - Updated to new Wave API version
*/
public class MyWaveServlet extends AbstractRobot {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected String getRobotName() {
return "My first robot";
}
@Override
protected String getRobotAvatarUrl() {
return "http://www.vogella.de/img/common/LarsVogelAbout.jpg";
}
@Override
public void onWaveletSelfAdded(WaveletSelfAddedEvent event) {
event.getWavelet().reply("Hello Wave. I have been added");
}
@Override
public void onWaveletParticipantsChanged(
WaveletParticipantsChangedEvent event) {
for (String participant : event.getParticipantsAdded()) {
event.getWavelet().reply("\nHello " + participant);
}
for (String participant : event.getParticipantsRemoved()) {
event.getWavelet().reply("\nGoodbye " + participant);
}
}
}
Map your servlet to the path "/_wave/" by changing the file "war\WEB-INF\web.xml" to the following.
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <servlet> <servlet-name>WaveRobot</servlet-name> <servlet-class>de.vogella.google.wave.firstrobot.MyWaveServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>WaveRobot</servlet-name> <url-pattern>/_wave/*</url-pattern> </servlet-mapping> </web-app>
The file "appengine-web.xml" needs to have the correction application id. You find this file in the folder "war/WEB-INF"
In step Google App Engine and Robots you should have created an application on the Google App Engine. In the file "appengine-web.xml" enter this application. For example I have registered "vogellawave" therefore my file looks like the following.
<?xml version="1.0" encoding="utf-8"?> <appengine-web-app xmlns="http://appengine.google.com/ns/1.0"> <application>vogellawave</application> <version>3</version> <precompilation-enabled>true</precompilation-enabled> <!-- Configure java.util.logging --> <system-properties> <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/> </system-properties> </appengine-web-app>
Deploy your application to the Google app engine by pressing the highlighted button.

See Google App Engine for Java for details on deployment.
The Google Wave Java library will automatically create on the GAE/J server a configuration file which describes the capacities of your robot based on the implemented methods. The the GAE/J server you find this file at "http://yourApplicatioinName.appspot.com/_wave/capacities.xml". Use a browser to check if this is available.
Create a new wave. Press add participant and type yourapplication@appspot.com, e.g. in my example vogellawave@appspot.com. The system will asked you if you want to add the robot to your contacts. Select yes. Once you added the robot to your wave it should add his greetings to your wave.

We will extend the simple robot. The enhanced robot will allow to register buzzwords via the "!addbuzzword word" command, show the available buzzwords via "!showbuzzwords" and reset the buzzword counter via "!reset". See later for an example. Thanks to Oliver Gierke who provided the initial implementation of the following robot.
Change your servlet to the following.
package de.vogella.google.wave.firstrobot;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.ServletException;
import com.google.wave.api.AbstractRobot;
import com.google.wave.api.Blip;
import com.google.wave.api.event.BlipSubmittedEvent;
import com.google.wave.api.event.WaveletSelfAddedEvent;
/**
* Small sample bot for Google Wave to demonstrate bot behavior. Just says hello
* and goodbye
*
* @author Lars Vogel - Updated to new Wave API version
*/
public class MyWaveServlet extends AbstractRobot {
private static final long serialVersionUID = 1L;
private static final String COMMAND = "\n!add";
private static final String RESET_COMMAND = "\n!reset";
private static final int COUNT_TO_WIN = 3;
private List<String> buzzwords;
private int count;
// This defines the robot name for Google Wave
@Override
protected String getRobotName() {
return "Buzzword Bot";
}
// This defines the picture for the Google Wave robot
@Override
protected String getRobotAvatarUrl() {
return "http://www.vogella.de/img/common/LarsVogelAbout.jpg";
}
@Override
public void init() throws ServletException {
buzzwords = new ArrayList<String>();
buzzwords.addAll(Arrays.asList("Wave", "Google", "Android", "GTUG",
"DevDusk"));
count = 0;
}
// By defining an event listener Wave knows that it should send these events
// to your robot, here we listening the event that the robot is added to a
// wave
@Override
public void onWaveletSelfAdded(WaveletSelfAddedEvent event) {
// If you add text to a wavelet (creating a blip) you have to start the
// text
// with "\n"
event.getWavelet().reply("\nBuzzword Bot has been added.");
}
@Override
public void onBlipSubmitted(BlipSubmittedEvent event) {
Blip blip = event.getBlip();
if (!maybeLearnBuzzword(blip)) {
detectBuzzwordIn(blip);
bingoWon(blip);
}
eventuallyReset(blip);
}
/**
* @param blip
*/
private void bingoWon(Blip blip) {
if (count >= COUNT_TO_WIN) {
blip.getWavelet().reply("\nBuzzword BINGO! ");
count = 0;
}
}
/**
* @param blip
*/
private boolean maybeLearnBuzzword(Blip blip) {
String text = blip.getContent();
if (text.startsWith(COMMAND)) {
String buzzword = text.substring(COMMAND.length(), text.length())
.trim();
buzzwords.add(buzzword);
blip
.getWavelet()
.reply(
String
.format(
"\nAdded %s to buzzwords! Owning %s buzzwords now!",
buzzword, buzzwords.size()));
return true;
}
return false;
}
private void detectBuzzwordIn(Blip blip) {
String text = blip.getContent();
for (String buzzword : buzzwords) {
if (text.contains(buzzword)) {
blip.getWavelet().reply("\nGotcha!");
count++;
break;
}
}
}
private void eventuallyReset(Blip blip) {
if (blip.getContent().startsWith(RESET_COMMAND)) {
count = 0;
blip.getWavelet().reply("\nReseted!");
}
}
}
Re-deploy your servelet.
In Google Wave create a new Wave and add your robot to the wave.
See the following screenshot for a possible usage of the robot.

In case of problems you can check the log on the GAE. Please see How to access the log in Google App Engine .
Thank you for practicing with this tutorial.
Please note that I maintain this website in my private time. If you like the information I'm providing please help me by donating.For questions and discussion around this article please use the www.vogella.de Google Group. Also if you note an error in this article please post the error and if possible the correction to the Group.
I believe the following is a very good guideline for asking questions in general and also for the Google group How To Ask Questions The Smart Way.
https://wave.google.com/wave Google Wave System
http://code.google.com/apis/wave/extensions/robots/java-tutorial.html Google Wave Robots: Java Tutorial
http://code.google.com/apis/wave/guide.html Google Wave Developer Guide
http://wave-samples-gallery.appspot.com/ Google Wave Code Examples
http://code.google.com/p/wave-protocol/wiki/Installation Installation of your own Google Wave server
http://googlewavedev.blogspot.com/ Google Wave Blog
http://code.google.com/events/io/sessions/ProgrammingWithAndForGoogleWave.html Google Wave Technical Talk