HipChat Atlassian HipChat

Being big Atlassian fans here at Nuxeo, we recently started using HipChat. It's an enterprise chat room. One of the cool things about HipChat is its very simple web API. It makes it really easy to send notifications to a chat room.

To show you how dead easy it is, I did a project showing how to send Nuxeo events to a Hipchat room using their web API. I did it using Nuxeo IDE to generate my plugin structure, using the Nuxeo Plugin Project and Nuxeo Listener wizards. My listener only listens to documentCreated and documentModified events. Each time they occur, we send a small message to a HipChat room, containing the URL of the document, its title, date and creator. The code is really simple (especially because most of it is generated by Nuxeo IDE):

/*
* (C) Copyright ${year} Nuxeo SA (http://nuxeo.com/) and contributors.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser General Public License
* (LGPL) version 2.1 which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl.html
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* Contributors:
* ldoguin
*/

package org.nuxeo.sample;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.event.Event;
import org.nuxeo.ecm.core.event.EventContext;
import org.nuxeo.ecm.core.event.EventListener;
import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
import org.nuxeo.ecm.platform.url.DocumentViewImpl;
import org.nuxeo.ecm.platform.url.api.DocumentView;
import org.nuxeo.ecm.platform.url.api.DocumentViewCodecManager;
import org.nuxeo.runtime.api.Framework;

/**
* @author ldoguin
*/
public class HipChatListener implements EventListener {

public static Log log = LogFactory.getLog(HipChatListener.class);

public static final String MESSAGE_TEMPLATE = "Document <a href="%s">%s</a> has been modified by %s on the %s";

public static final String BASE_URL = "https://api.hipchat.com/v1/rooms/message?format=json&auth_token=%s";

public void handleEvent(Event event) throws ClientException {
// First we make sure that the event is about a document
EventContext ctx = event.getContext();
if (!(ctx instanceof DocumentEventContext)) {
return;
}
DocumentEventContext docCtx = (DocumentEventContext) ctx;
DocumentModel doc = docCtx.getSourceDocument();
// Once we have the document, we can start building the message we'll send to HipChat

// First we build the document's URL
DocumentView docView = new DocumentViewImpl(doc);
DocumentViewCodecManager docLocator = Framework.getLocalService(DocumentViewCodecManager.class);
String nuxeoUrl = Framework.getProperty("nuxeo.url");
String docUrl = docLocator.getUrlFromDocumentView(docView, true,
nuxeoUrl + "/");
// Than the message body
Date date = new Date(event.getTime());
String chatMessage = String.format(MESSAGE_TEMPLATE, docUrl,
doc.getTitle(), docCtx.getPrincipal().getName(),
date.toString());
log.debug(chatMessage);
try {
// now we can send it to HipChat
sendMessage(chatMessage);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public void sendMessage(String message) throws IOException {
// We retrieve every needed property from nuxeo.conf
String roomId = Framework.getProperty("hipchat.roomId");
String authToken = Framework.getProperty("hipchat.authToken");
String userName = Framework.getProperty("hipchat.userName");
String notify = Framework.getProperty("hipchat.notify");
String color = Framework.getProperty("hipchat.color");

// then we do the actual rest call to HipChat web API
String url = String.format(BASE_URL, authToken);
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(url);
try {
post.addParameter("from", userName);
post.addParameter("room_id", roomId);
post.addParameter("message", message);
post.addParameter("color", color);
post.addParameter("notify", notify);
post.getParams().setContentCharset("UTF-8");
client.executeMethod(post);
} finally {
post.releaseConnection();
}
}
}

What it does is simply relay the modify or create events to a room. This is the simplest integration possible, but there are many other cool use cases you can implement. You could have a dedicated room per Nuxeo group, you could integrate this with the notification module and send personal notifications, etc. Let us know what you think about this -- are you using the same kind of tools to collaborate? Would like to see integrations between other collaboration tools and Nuxeo?