[ Implementation | Development | Conclusion ]

The Client API

This section shows how you can access Nabu from your application. To make use of Nabu in your application, you need a Jabber client library to connect to Nabu, and an RDF parser to parse replies.

For Java, the Nabu project has a simple API you can use to listen to chat events for user observation and to communicate with the Nabu server via requests. The API encapsulates both the connection handling and the encoding/parsing of request and responses. The API does not include an RDF parser to avoid dependencies to a specific RDF implementation. To parse the RDF , you can use the Jena framework (it will be used in the examples below).


To use the client API in your Java application, download, unpack it and add the contained JAR files (nabu-client.jar, nabu-remote.jar, smack.jar, smackx.jar) to your classpath.

If you build from source, go to the nabuclient/ directory and run

ant nabuclient_jar

After build, you can find the JAR files in nabuclient/target/lib. Add them to your classpath.

Connect to the server

The following code snippet shows how to connect to the server:


// [...]

   String USER = "alice"; // the user name (node part of the Jabber ID)
   String RESOURCE = "nabu"; // the resource to be used, can be any 
                             // resource that is not in use e.g. by 
                             // your graphical client
   String PASS = "topsecret"; // the jabber password of alice
   String HOST = ""; // the server, 
   int PORT = 5222; // the port, usually 5222 for unsecured 
                    //connections and 5223 for SSL
   String BOT = ""; // the JID of the nabu bot, 
                                               // as listed in the contact list

   Client client = new ClientImpl(); // for a SSL connection, use 
                                     // "new ClientSSLImpl()" (note that the port
                                     // will change too, e.g. to 5223)
      client.login(USER, RESOURCE, PASS, HOST, PORT);
   catch (Exception e) {}


   // here you can send requests, start observation etc. (see below)
   // [...]

User Observation

To use user observation, implement the Listener interface:


public class MyListener implements Listener

    public void receiveObservationNotification(String uri, String cbd)
        System.out.println("Received message with URI " + uri);
        System.out.println("Description in RDF:");
        // Act on notification

    public void receiveResponse(Response response)
        System.out.println("A new response arrived! Response code: "
                           + response.getCode());

Create an listener instance and register it at the client and start the observation:

   client.addListener(new MyListener()); // adds the listener
   client.startObservation(); // starts the observation

Now observation is activated and every received notification is passed to !MyListener::receiveObservationNotification().

To stop the observation, call client.stopObservation();

Sending Requests

The client API also offers a convenient way to send request to Nabu and to receive responses without building request strings and parsing responses. To send a request, simply create a request object and pass it to the client. Let's send the annotation example from the handbook via the API.

CREATESTATEMENT RESOURCE http://foo/Messages/someMessage http://foo/Categories/Projects/Nabu


int type = CreateStatementRequest.ObjectType.Resource;
String subject = "http://foo/Messages/someMessage";
String predicate = "";
String object = "http://foo/Categories/Projects/Nabu";

CreateStatementRequest req = 
   new CreateStatementRequest(null, type, subject, predicate, object);


To handle responses, reimplement receiveResponse() from the Listener interface. To handle the different sub classes, you might also implement the ResponseVisitor interface:

public class MyListener implements Listener, ResponseVisitor
    public void receiveResponse(Response response)

    public void visitMessageLoggingStatusResponse(MessageLoggingStatusResponse response)
        System.out.println("message logging is " + (response.getStatus() 
                           ? " enabled " : "disabled") );
    public void visitQueryResultResponse(QueryResultResponse response);
        Model m = ModelMaker.createDefaultModel(); // create a Jena Model of 
                                                   // the RDF StringReader(response.getResult(), null);
        // do something with the model
    // ...

By implementing the visitX class for every response type X, you can handle all the response types you are interested in. For instance, the visitQueryResultResponse() implementation in the example uses Jena to parse the RDF sent in the response.

[ Implementation | Development | Conclusion ]

Last modified 12 years ago Last modified on 08/01/05 00:46:33