| Java, Eclipse and Web programming Tutorials |
Version 1.3
Copyright © 2009 - 2010 Lars Vogel
02.01.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 | ||
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.
The following assumes that you have experience using Eclipse. I assume your are using Eclipse 3.5. For an introduction please see Introduction to Eclipse .
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
jsonrpc.jar
json.jar
Currently a robot needs to run on the Google App Engine to participate in a Wave. We will use the Google App Engine for Java which is also called GAE/J.
Therefore 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 and how to create please a application please see see Google App Engine Development with Java
Create now a account at the Google App Engine Homepage in case you have not yet done this.
Create also an application which we will later use to deploy our robot to. 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 three waves jars you downloaded earlier 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.AbstractRobotServlet;
import com.google.wave.api.Blip;
import com.google.wave.api.Event;
import com.google.wave.api.EventType;
import com.google.wave.api.RobotMessageBundle;
import com.google.wave.api.TextView;
import com.google.wave.api.Wavelet;
public class MyWaveServlet extends AbstractRobotServlet {
private static final long serialVersionUID = 1L;
@Override
public void processEvents(RobotMessageBundle bundle) {
Wavelet wavelet = bundle.getWavelet();
if (bundle.wasSelfAdded()){
Blip blip = wavelet.appendBlip();
TextView textView = blip.getDocument();
textView.append("Moin, moin wave");
}
for (Event e: bundle.getEvents()){
if (e.getType()== EventType.WAVELET_PARTICIPANTS_CHANGED){
Blip blip = wavelet.appendBlip();
TextView textView = blip.getDocument();
textView.append("Hello, wave");
}
}
}
}
Map your servlet to the path "/_wave/robot/jsonrpc" by changing the 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/robot/jsonrpc</url-pattern> </servlet-mapping> </web-app>
Google Wave requires that a cofiguration file "capacities.xml". This file will tell Wave about the capabilities and authorizations of the robot.
In folder /war create the folder "_wave" and create the file "capabilities.xml".
<?xml version="1.0" encoding="utf-8"?>
<w:robot xmlns:w="http://wave.google.com/extensions/robots/1.0">
<w:capabilities>
<w:capability name="WAVELET_PARTICIPANTS_CHANGED" content="true" />
</w:capabilities>
<w:version>1</w:version>
</w:robot>
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>1</version> <!-- 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.
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.

To test this you can use the existing bots "vogellawave@appspot.com" (from Lars) or "wavebuzzbot@appspot.com" (from Oliver).
This 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.
Add the event BLIP_SUBMITTED to your capabilities.xml
<?xml version="1.0" encoding="utf-8"?>
<w:robot xmlns:w="http://wave.google.com/extensions/robots/1.0">
<w:capabilities>
<w:capability name="WAVELET_PARTICIPANTS_CHANGED" content="true" />
<w:capability name="BLIP_SUBMITTED" content="true" />
</w:capabilities>
<w:version>2</w:version>
</w: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.AbstractRobotServlet;
import com.google.wave.api.Blip;
import com.google.wave.api.Event;
import com.google.wave.api.EventType;
import com.google.wave.api.RobotMessageBundle;
/**
* Small sample bot for Google Wave to demonstrate bot behavior. This one will
* allow registering buzzwords and react on occurrence of them afterwards. After
* having found 5 words it will shout BINGO!
*
* @author Oliver Gierke
*/
public class MyWaveServlet extends AbstractRobotServlet {
private static final long serialVersionUID = -8582338473962188402L;
private static final String COMMAND = "!addbuzz";
private static final String RESET_COMMAND = "!reset";
private static final int COUNT_TO_WIN = 5;
private List<String> buzzwords;
private int count;
@Override
public void init() throws ServletException {
buzzwords = new ArrayList<String>();
buzzwords.addAll(Arrays.asList("Wave", "Google", "Android", "GTUG",
"DevDusk"));
count = 0;
}
@Override
public void processEvents(RobotMessageBundle bundle) {
for (Event event : bundle.getBlipSubmittedEvents()) {
if (event.getType() == EventType.BLIP_SUBMITTED) {
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.createChild().getDocument().append("BINGO!");
count = 0;
}
}
/**
* @param blip
*/
private boolean maybeLearnBuzzword(Blip blip) {
String text = blip.getDocument().getText();
if (text.startsWith(COMMAND)) {
String buzzword =
text.substring(COMMAND.length(), text.length() - 1).trim();
buzzwords.add(buzzword);
blip.createChild().getDocument().append(
String.format(
"Added %s to buzzwords! Owning %s buzzwords now!",
buzzword, buzzwords.size()));
return true;
}
return false;
}
private void detectBuzzwordIn(Blip blip) {
String text = blip.getDocument().getText();
for (String buzzword : buzzwords) {
if (text.contains(buzzword)) {
Blip child = blip.createChild();
child.getDocument().append("Gotcha!");
count++;
break;
}
}
}
private void eventuallyReset(Blip blip) {
if (blip.getDocument().getText().startsWith(RESET_COMMAND)) {
count = 0;
blip.createChild().getDocument().append("Reseted!");
}
}
}
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