Google Wave - Tutorial: Vogella - de
Google Wave - Tutorial: Vogella - de
de
Home
Blog
Java
Eclipse
Web
Technology
Algorithms
Source Code
Browse
Revision
1. Google Wave
1.1. What is Google Wave
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.
1.4. Protocol
The Wave protocol is based on XMPP ("Extensible Messaging and Presence Protocol"). The Google Wave protocol will immediately update all clients in case a change occurs. Changes in a wave the are communicated as deltas between client and server, e.g. only the changes are communicated.
2. Extensions
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.
This installation will also in include the GWT and App Engine SDK into your Eclipse preferences. Check Window -> Preferences -> Google -> App Engine / Web Toolkit. The SDK is included in your Eclipse installation directory under "/plugins/". Here you find Eclipse plugins for GWT and App Engine which contain the SDK.
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>
Every time you change this file, e.g. by adding new capabilities, you have to increase the version in this file.
This buzzword bot has been developed and provided by Oliver Gierke and slightly modified by Lars Vogel . . To test this you can use the existing bots "[email protected]" (from Lars) or "[email protected]" (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>
/** * 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 private private private static static static static final final final final long serialVersionUID = -8582338473962188402L; String COMMAND = "!addbuzz"; String RESET_COMMAND = "!reset"; 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.
7. Logging
7.1. App Engine
In case of problems you can check the log on the GAE. Please see How to access the log in Google App Engine .
8. Thank you
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.
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.
Java Log Viewer Powerful Log viewer for Java apps Automatic anomaly detection on logs www.log -viewer.com/Java Java Database Accelerate Java Coding Pure Java database. Improve data access Read & Navigate Code Faster Stop and increase profits. Try It Wasting Time, Focus on Coding! web.progress.com/objectstore nWireSoftware.com/Eclipse_Java_Tool Distributed Java Cache Scale out application performance on server farms! Free download. www.scaleoutsoftware.com