Blogs

Contact us

Do you have problems with site or you want to give us a feedback? Use our mailing list.

This tutorial shows how to build a Wave Robot using Dailydev Wave Robot library and Spring MVC framework. It describes steps to build simple calendar robot, which recognizes date pattern in blip's document. The date is then replaced by link to add event to Google Calendar.

Robot's sources can be downloaded from Download/Examples section.

Robot is also deployed so you can try it from your Wave. It's address is: calendar-robot@appspot.com

This tutorial assumes that the reader has a basic knowledge of Google's Wave Java Client Library .

Requirements for Java 6, currently defined by Google, are valid here also. For more information read Google's Tutorial .

This tutorial also assumes that you already registered your application with Google App Engine (GAE) and has your Eclipse set and standard GAE project is created. For more information read Setting Up App Engine section from Google's Tutorial.

Libraries

To update standard GAE application into DailyDev Wave Robot Application with Spring support you should add following libraries into ${project.dir}/war/WEB-INF/lib  folder.
  1. wave-robot-api.jar
  2. json.jar
  3. jsonrpc.jar
  4. dailydev-wave-robot-0.2.0.jar
  5. spring-webmvc-2.5.6.SEC01.jar
    • spring-core-2.5.6.SEC01.jar
    • spring-beans-2.5.6.SEC01.jar
    • spring-context-2.5.6.SEC01.jar
    • spring-context-support-2.5.6.SEC01.jar
    • spring-web-2.5.6.SEC01.jar
    • commons-logging-1.1.1.jar
    • aopalliance-1.0.jar
  6. joda-time-1.6.jar

First three libraries are standard Google Wave libraries . The fourth library is DailyDev extension which can be downloaded from Download section of this site. Fifth library is Spring MVC library, which is part of Spring Framework , plus it's mandatory dependencies. Finally the sixth library is Joda Time - implementation of Date / Time classes used by robot.

To make libraries available on classpath of the project do Refresh and add libraries through project Properties > Java Build Path > Libraries.

Event Handling

package org.dailydev.wave.calendar;

import static com.google.wave.api.EventType.*;

import java.util.List;
import java.util.regex.Pattern;

import org.dailydev.wave.robot.EventHandlerOperation;
import org.dailydev.wave.robot.filter.ContentMatch;

import com.google.wave.api.Blip;
import com.google.wave.api.Event;
import com.google.wave.api.RobotMessageBundle;

public class CalendarRobotHandler {

    private static final String DATE_PATTERN =
            "((19|20)\\d\\d)[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])";

    private static final Pattern datePattern = Pattern.compile(DATE_PATTERN);

    @EventHandlerOperation(eventTypes = { BLIP_SUBMITTED, DOCUMENT_CHANGED },
                           content = true)
    @ContentMatch(DATE_PATTERN)
    public void handleDateInDocument(Event event, RobotMessageBundle bundle)
            throws UnsupportedEncodingException {
        Blip blip = event.getBlip();
        handleBlip(blip);
    }

    @EventHandlerOperation(eventTypes = { WAVELET_SELF_ADDED }, content = true)
    @ContentMatch(DATE_PATTERN)
    public void handleDateInWavelet(Event event, Wavelet wavelet)
            throws
UnsupportedEncodingException {
        Blip rootBlip = wavelet.getRootBlip();
        handleBlipRecursive(rootBlip);
    }

    protected void handleBlipRecursive(Blip blip) {
        handleBlip(blip);

        List<Blip> children = blip.getChildren();
        for (Blip child : children) {
            handleBlipRecursive(child);
        }
    }

    protected void handleBlip(Blip blip) {
        // for concrete implementation of the method check source code repository
        // of spring-wave-calendar example project: CalendarRobotHandler.java
    }
}
For concrete implementation of the method check source code repository of spring-wave-calendar example project: CalendarRobotHandler.java

Servlet Mapping

Since the robot is based on Spring MVC framework, it's servlet mapping is standard for such case. It means that we have to simply map just Spring's DispatcherServlet to the context under which every robot lives.
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
    <servlet>
        <servlet-name>robot</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet

        </servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>robot</servlet-name>
        <url-pattern>/_wave/*</url-pattern>
    </servlet-mapping>
</web-app>

Application Context

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <bean name="calendarHandler"
            class="org.dailydev.wave.calendar.CalendarRobotHandler" />

    <bean name="robot" class="org.dailydev.wave.robot.Robot">
        <property name="robotName" value="Calendar Robot" />
        <property name="robotVersion" value="2.0" />
        <property name="robotAvatar" value="/images/calendar.gif" />
        <property name="handlers">
            <set>
                <ref bean="calendarHandler" />
            </set>
        </property>
        <property name="filterManager" ref="filterManager" />
    </bean>

    <bean name="filterManager"
            
class
="org.dailydev.wave.robot.filter.SimpleFilterManager">
        <property name="filters">
            <bean class="org.dailydev.wave.robot.filter.ContentMatchFilter" />
        </property>
    </bean>

    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <value>
                /robot/jsonrpc=robotController
                /capabilities.xml=capabilitiesController
                /robot/profile=profileController
            </value>
        </property>
    </bean>

    <bean name="robotController"
            class="org.dailydev.wave.robot.spring.RobotController">
        <property name="robot" ref="robot" />
    </bean>

    <bean name="capabilitiesController"
            class="org.dailydev.wave.robot.spring.CapabilitiesController">
        <property name="robot" ref="robot" />
    </bean>

    <bean name="profileController"
            class="org.dailydev.wave.robot.spring.ProfileController">
        <property name="robot" ref="robot" />
    </bean>
</beans>

Sign in  |  Recent Site Activity  |  Terms  |  Report Abuse  |  Print page  |  Powered by Google Sites