Trying the new RESTful api for Openfire 3.10.0 Beta

A trio of scripts used to perform RESTful operations on a Openfire 3.10.0 (Beta) server.

The latest Beta version of Openfire XMPP Server (3.10.0) features a new REST API Plugin. This allows developers and administrators to perform operations on the server using HTTP gets and puts. Following are three scripts using that API, in perl, python and php. All do the same thing: get the roster for a particular XMPP user from the Openfire server for their domain. Authentication is by secret for simplicity (the plugin also will accept Basic HTTP Authentication, which is preferable for security reasons).

This perl script uses Miles Crawford’s REST::Client module to do its work.

#!/usr/bin/perl

use strict;
use REST::Client;

my $secret = 'xxxxxxxxxxxxxxx';
my $url = "http://devchat.example.com:9090/plugins/restapi/v1/users/jsmith/roster";

my $client = REST::Client->new();

$client->addHeader('Authorization', $secret); 

my $response = $client->GET($url);

print $client->responseCode(), "\n";
print $client->responseContent();


__END__;

For the python variation on the same theme, I’ve used the urllib2 module. It’s no accident that this is the shortest of the three scripts. Makes me wonder what else I could do more efficiently in python.

#!/usr/bin/python
import urllib2

secret = 'xxxxxxxxxxxxx'

url = 'http://devchat.example.com:9090/plugins/restapi/v1/users/jsmith/roster'
opener = urllib2.build_opener()
opener.addheaders = [('Authorization', secret)]
response = opener.open(url).read()
print response

Although there are at least two REST client libraries for php (HTTPful and Guzzle), in the example below I’ve fallen back to a file_get_contents sample method I found on Decibel Developer (thus avoiding the horrors of curl). See the file_get_contents doc for background. This was the only explicitly streams oriented method I used.

<?php

$secret = 'xxxxxxxxxxxxxxx';
$url = "http://devchat.example.com:9090/plugins/restapi/v1/users/jsmith/roster";

$headers  = array(
   'http' => array(
       'method' => "GET",
       'header' => "Authorization: " . $secret . "\r\n"
    )
);

$context = stream_context_create($headers);
$response = file_get_contents($url, false, $context);

echo $response;

?>
This entry was posted in Development, Systems Analysis on by .

About phil

My name is Phil Lembo. In my day job I’m an enterprise IT architect for a leading distribution and services company. The rest of my time I try to maintain a semi-normal family life in the suburbs of Raleigh, NC. E-mail me at philipATlembobrothersDOTcom. The opinions expressed here are entirely my own and not those of my employers, past, present or future (except where I quote others, who will need to accept responsibility for their own rants).