ejabberd integration with XMLRPC API + JAVA

Attention

First of all you have to be sure everything is setted up and working properly from ejabberd shell, please take a look to ejabberd integration with XMLRPC API which describe this process.

Configuration

In order to be able to call to XMLRPC method you must install xmlrpc apache jars files please go to apache xmlrpc project and download the lastest release.

Those jars files must be placed in your class path

Usage

To perfom a XMLRPC call you need to know the methods structure you want to call

For example lets call to "echothis", which send back ours sent it parameter.

Call

Arguments

Returns

echothis

String

String

echothis

Simple echo implementation, using the old argument and result formatting.

String echothis(String echostring)

That mean, echothis, wait for an string as argument and return an string as result

Before to make our call we need to set up ours XMLRPC client

mlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL("http://localhost:4560"));
XmlRpcClient client = new XmlRpcClient();
client.setConfig(config)

this code will give us an important method to perform our call (execute).

execute([string method name], [an object array with all needed parameters])
Object params[] = new Object[] { new String("hello")};
String result = (String) client.execute("echothis", params);
system.out.println("Result: " + result);

full code will be

import java.net.URL;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;

public class EchoClient {

public static void main(String[] args) {
try {
      XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
      config.setServerURL(new URL("http://localhost:4560"));
      XmlRpcClient client = new XmlRpcClient();
      client.setConfig(config);
      Object params[] = new Object[] { new String("hello") };
      String result = (String) client.execute("echothis", params);
      System.out.println("Result: " + result);
      } catch (Exception e) {
        System.err.println(e);
    }
}
}

this is an basic example, let try it with another more complex method

get_roster

Retrieve the roster for a given user.

Call

Arguments

Returns

get_roster

struct[{user, String}, {server, String}]

struct[{contacts, {array, struct[{contact, {array, [{struct [{jid, String}, {group, String}, {nick, String}, {subscription, String}, {pending, String}]}]}}]}}]

The function returns a list of the contacts in a user roster.

The function also returns the state of the contact subscription. Subscription can be either "none", "from", "to", "both". Pending can be "in", "out" or "none".

In this case the first part of the code, setting process, is the same like before.

We need to build the structure which will be used like parameters in ourfunction call.

Assume you have a contact called "evento" in your roster list

Hashtable<String, Object> params = new Hashtable<String, Object>();
params.put("user", new String("evento"));
params.put("server", new String("localhost"));

List<Object> roster_params = new ArrayList<Object>();
roster_params.add(params);

we created a hashmap having a key value peer which will be matched against "user" and "server" function parameters

because execute method is waiting for an List<params> we need create a List with Hashmap into it

Object result = client.execute("get_roster", roster_params)
Attention

We put the result call in an object structure, but you can build your own object an map it.
Object result is a Hashmap with key value peer, in this example there are only two contacts in the "evento" roster list which are
subscription="both"
nick="event2"
pending="none"
jid="event2@localhost"
group= ""

subscription="both"
nick=""
pending="none"
jid="evento3@localhost"
group= ""

Full code will be

import java.net.URL;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;

public class GetRoster {
public static void main(String[] args) {
try {

    XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
    config.setServerURL(new URL("http://localhost:4560"));
    XmlRpcClient client = new XmlRpcClient();
    client.setConfig(config);

    Hashtable<String, Object> params = new Hashtable<String, Object>();
    params.put("user", new String("evento"));
    params.put("server", new String("localhost"));

    List<Object> roster_params = new ArrayList<Object>(;
    roster_params.add(params);

    Object result = client.execute("get_roster", roster_params);

} catch (Exception e) {
System.err.println(e);
}
}
}

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Ejabberd Complete Installation

Hi,'

Please share the ejabberd complete steps

Thanks for this

Pls share the full code now, or the api files necessary to use this without error in java

Syndicate content