Creating a simple Activity Stream with Apache Abdera2, Java, and Netbeans 7 (with Maven)

Post to Twitter

The Apache Abdera team is hard at work on Abdera2. Abdera2 has a bunch of new features, check out this email by James Snell to get a sample of whats new and improved. One of the new features is Activity Streams support. If your not familiar with Activity Streams visit the main site to get an idea but suffice to say if you know how the Facebook Wall works and looks then you get the basic idea of what an activity stream is. Today I’m going to show you how to build a simple activity stream with Netbeans 7 and Apache Abdera2.


The first step you need to do since at the time of this writing Abdera2 is only available via Subversion is to get the source code onto your computer. You can do this by going into the Netbeans 7 Team menu and selecting: Subversion -> Checkout

For the Repository URL use this: http://svn.apache.org/repos/asf/abdera/abdera2

You will need to build the Abdera2 project which in turn will get all the dependencies gathered. When it is fully built you can move onto the rest of this article.

Note: If by the time you read this article and the Abdera2 jars are available you can simply add those to your project through the Netbeans 7 Maven support.

With Netbeans 7 create a new Maven Java Application called ActivityStreamExample

With the new project right-click on the Dependencies and select: Add Dependencies

When the Add Dependency dialog opens up select the Open Projects tab and select the Abdera2 Activities project.

Your POM file should look like the following:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.giantflyingsaucer</groupId>
  <artifactId>ActivityStreamExample</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>ActivityStreamExample</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.abdera2</groupId>
      <artifactId>abdera2-activities</artifactId>
      <version>2.0-SNAPSHOT</version>
    </dependency>
  </dependencies>
</project>

With that out of the way we can finally begin to create the activity stream.

Please note that I have taken some of the Abdera2 example source code and modified it for this article.

Now we will create a few activities. First, we will create a person called John Doe whom will make Sarah Jones his friend. Then John Doe will follow George Black. Next, John Doe will like a movie and the movie director. Finally he will go on vacation. The last activity I’ll use a custom verb not supplied by Abdera2 but since you can add new verbs on the fly it will be trivial to do.

Let’s examine the flow first:

John Doe -> makes friend -> Sarah Jones
John -> follows -> George Black
John Doe -> likes movie and director -> E.T directed by Steven Spielberg
John Doe -> goes on vacation -> Austria

Note: Keep in mind I’m going to be verbose in the code to better show what is going on.

Inside the App.java file (created by Maven by default) add the following code:

package com.giantflyingsaucer;

import org.apache.abdera2.activities.model.Activity;
import org.apache.abdera2.activities.model.Collection;
import org.apache.abdera2.activities.model.IO;
import org.apache.abdera2.activities.model.Verb;
import org.apache.abdera2.activities.model.objects.PersonObject;

import static org.apache.abdera2.activities.model.Verb.LIKE;
import static org.apache.abdera2.activities.model.Verb.FOLLOW;
import static org.apache.abdera2.activities.model.Verb.MAKE_FRIEND;
import static org.apache.abdera2.activities.model.Activity.makeActivity;
import static org.apache.abdera2.activities.model.objects.PersonObject.makePerson;
import static org.apache.abdera2.activities.model.objects.MovieObject.makeMovie;
import static org.apache.abdera2.activities.model.objects.NoteObject.makeNote;


public class App {
    public static void main(String[] args) {
        final IO io = IO.get();
        final PersonObject johnDoe = makePerson("John D. Doe").email("john.doe@example.org").get();

        Activity makeFriendActivity =
                makeActivity().actor(johnDoe).verb(MAKE_FRIEND).object(
                makePerson("Sarah Jones").email("sarah.jones@example.org").get()).get();
        
        Activity followActivity =
                makeActivity().actor(johnDoe).verb(FOLLOW).object(
                makePerson("George Black").email("george.black@example.org").get()).get();         
        
        Activity likeMovieActivity =
                makeActivity().actor(johnDoe).verb(LIKE).object(
                makeMovie().displayName("E.T").director(makePerson("Steven Spielberg").get())).get();
        
        Activity vacationActivity =
                makeActivity().actor(johnDoe).verb(Verb.get("GOES_ON_VACATION"))
                .object(makeNote().content("I just left for Austria!").get()).get();

        System.out.println("\n\n");
        makeFriendActivity.writeTo(io, System.out);
        followActivity.writeTo(io, System.out);
        likeMovieActivity.writeTo(io, System.out);
        vacationActivity.writeTo(io, System.out);
        System.out.println("\n\n");
        
        Collection<Activity> collection =
                Collection.<Activity>makeCollection()
                .item(makeFriendActivity)
                .item(followActivity)
                .item(likeMovieActivity)
                .item(vacationActivity).get();

        collection.writeTo(System.out);
        System.out.println("\n\n");
    }
}

There will be two types of output. The first will be object type of activity output:

{
   "objectType":"activity",
   "actor":{
      "objectType":"person",
      "displayName":"John D. Doe",
      "emails":[
         "john.doe@example.org"
      ]
   },
   "verb":"make-friend",
   "object":{
      "objectType":"person",
      "displayName":"Sarah Jones",
      "emails":[
         "sarah.jones@example.org"
      ]
   }
}{
   "objectType":"activity",
   "actor":{
      "objectType":"person",
      "displayName":"John D. Doe",
      "emails":[
         "john.doe@example.org"
      ]
   },
   "verb":"follow",
   "object":{
      "objectType":"person",
      "displayName":"George Black",
      "emails":[
         "george.black@example.org"
      ]
   }
}{
   "objectType":"activity",
   "actor":{
      "objectType":"person",
      "displayName":"John D. Doe",
      "emails":[
         "john.doe@example.org"
      ]
   },
   "verb":"like",
   "object":{
      "objectType":"movie",
      "displayName":"E.T",
      "director":{
         "objectType":"person",
         "displayName":"Steven Spielberg"
      }
   }
}{
   "objectType":"activity",
   "actor":{
      "objectType":"person",
      "displayName":"John D. Doe",
      "emails":[
         "john.doe@example.org"
      ]
   },
   "verb":"GOES_ON_VACATION",
   "object":{
      "objectType":"note",
      "content":"I just left for Austria!"
   }
}

The second will be the object type of collection output:

{
   "objectType":"collection",
   "totalItems":4,
   "items":[
      {
         "objectType":"activity",
         "actor":{
            "objectType":"person",
            "displayName":"John D. Doe",
            "emails":[
               "john.doe@example.org"
            ]
         },
         "verb":"make-friend",
         "object":{
            "objectType":"person",
            "displayName":"Sarah Jones",
            "emails":[
               "sarah.jones@example.org"
            ]
         }
      },
      {
         "objectType":"activity",
         "actor":{
            "objectType":"person",
            "displayName":"John D. Doe",
            "emails":[
               "john.doe@example.org"
            ]
         },
         "verb":"follow",
         "object":{
            "objectType":"person",
            "displayName":"George Black",
            "emails":[
               "george.black@example.org"
            ]
         }
      },
      {
         "objectType":"activity",
         "actor":{
            "objectType":"person",
            "displayName":"John D. Doe",
            "emails":[
               "john.doe@example.org"
            ]
         },
         "verb":"like",
         "object":{
            "objectType":"movie",
            "displayName":"E.T",
            "director":{
               "objectType":"person",
               "displayName":"Steven Spielberg"
            }
         }
      },
      {
         "objectType":"activity",
         "actor":{
            "objectType":"person",
            "displayName":"John D. Doe",
            "emails":[
               "john.doe@example.org"
            ]
         },
         "verb":"GOES_ON_VACATION",
         "object":{
            "objectType":"note",
            "content":"I just left for Austria!"
         }
      }
   ]
}

It’s a pretty simple example. If you dig around in the Abdera2 code and examples you’ll see there are a lot of things to experiment with and to add to Activity Streams.

Post to Twitter

This entry was posted in Apache Abdera, Java, Netbeans. Bookmark the permalink.