powered by Jive Software

Questions ab custom XMPP extension

Hi guys, I am new to XMPP and smack. After playing around, I have my OpenFire server running and successfully get a simple IM example working using the smack library.

Now I want to do a XMPP extension example. I found a really good example online written by Johannes Grimm. I wanted to test this example on my own server. However, I have failed to do so. Below is the codes:

Receiver Code:

package net.smilingj.xmpp.smack.exampleextension.reciever;

import net.smilingj.xmpp.smack.exampleextension.common.ExamplePacketExtension;

import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.PacketExtensionFilter;
import org.jivesoftware.smack.packet.Packet;

public class Reciever {

/**
* @param args
*/
public static void main(String[] args) {
try {
XMPPConnection.DEBUG_ENABLED = true;

        XMPPConnection connection = new XMPPConnection("XXX.XXX.XXX.XXX"); // <- This is my own IP address
        connection.connect();
       
        connection.addPacketListener(new PacketListener() {

public void processPacket(Packet p) {
ExamplePacketExtension e = (ExamplePacketExtension) p.getExtension(ExamplePacketExtension.namespace);
System.out.println(“Extension content: (”+e.getExampleAttribut()+")");
}}, new PacketExtensionFilter(ExamplePacketExtension.namespace));

        connection.login("tester2", "tester2");           
       
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

Sender:

package net.smilingj.xmpp.smack.exampleextension.sender;

import net.smilingj.xmpp.smack.exampleextension.common.ExampleIQPacket;
import net.smilingj.xmpp.smack.exampleextension.common.ExamplePacketExtension;

import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;

public class Sender {

/**
* @param args
*/
public static void main(String[] args) {
try {
// set debugging enabled
XMPPConnection.DEBUG_ENABLED = true;

        // setup connection
        XMPPConnection connection = new XMPPConnection("XXX.XXX.XXX.XXX");
        connection.connect();
       
        // add packet listener for ExampleIQPacket
        connection.addPacketListener(new PacketListener() {
            public void processPacket(Packet p) {
                ExampleIQPacket ep = (ExampleIQPacket)p;
                System.out.println("Recieve ExampleIQPacket ["+ep.toXML()+"]");
            }}, new PacketTypeFilter(ExampleIQPacket.class));
       
        // perform login
        connection.login("tester1", "tester1");
       
        // create and send an ExampleIQPacket
        ExampleIQPacket p = new ExampleIQPacket();
        connection.sendPacket(p);
       
        // generate a new message and add a custom extension
        Message m = new Message();
        ExamplePacketExtension e = new ExamplePacketExtension();
       
        e.setExampleAttribut("Hello World!");
        m.addExtension(e);
        m.setTo("tester2@ming8832");
       
        connection.sendPacket(m);
       
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

Example IQPacket:

package net.smilingj.xmpp.smack.exampleextension.common;

import org.jivesoftware.smack.packet.IQ;

public class ExampleIQPacket extends IQ {

private String examplecontent = null;

public ExampleIQPacket() {
    // enpty constructor for introspection parser
}

public ExampleIQPacket(String examplecontent) {
this.examplecontent = examplecontent;
}

public String getExamplecontent() {
return this.examplecontent;
}

public void setExamplecontent(String examplecontent) {
this.examplecontent = examplecontent;
}

public String getChildElementXML() {
    StringBuilder buf = new StringBuilder();
    buf.append("<query xmlns=\"jabber:iq:exampleiqpacket\">");
    if(examplecontent != null) buf.append("<examplecontent>").append(examplecontent).append("</examplecontent> ");
    buf.append("</query>");
    return buf.toString();
}

}

ExamplePacketExtension:

package net.smilingj.xmpp.smack.exampleextension.common;

import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.provider.PacketExtensionProvider;
import org.xmlpull.v1.XmlPullParser;

public class ExamplePacketExtension implements PacketExtension {

private String exampleAttribut;

public static String namespace = "http://ming8832";  //<- what is namespace? what should it be?
public static String elementname = "exampleextension";

public ExamplePacketExtension() {
}

public void setExampleAttribut(String value) {
    exampleAttribut = value;
}

public String getExampleAttribut() {
    return exampleAttribut;
}

public String getElementName() {
return ExamplePacketExtension.elementname;
}

public String getNamespace() {
return ExamplePacketExtension.namespace;
}

public String toXML() {
StringBuffer buf = new StringBuffer();

    buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append("\">");
    if(exampleAttribut != null) buf.append("<content exampleattribut=\"").append(exampleAttribut).append("\"/>");
    buf.append("</").append(getElementName()).append(">");
   
    return buf.toString();
}

public static class Provider implements PacketExtensionProvider{

public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
ExamplePacketExtension extension = new ExamplePacketExtension();
boolean done = false;
while(!done) {
int eventType = parser.next();
if(eventType == XmlPullParser.START_TAG) {
if(parser.getName().equals(“content”)) {
extension.setExampleAttribut(parser.getAttributeValue("", “exampleattribut”));
done = true;
}
}
}
return extension;
}

}

}

There is also codes for the OpenFire server plug in. For simplicity, I will attach the two class at the end.

With the above code, I have two computers and one is login as a sender while the other is login as a reciever. They both login succesfully (update presence, receive roster info). The sender send out a IQ-get for the exampleiqpacket, and it received a “error message”:

The receiver side actually recieves the message but it coudn’t process it.

I think the main problem here is that the server is unable to process the custom IQ and message.

Does anyone find any problems from the above code or the attached server plug in?

I also have difficulty understanding the namespace concept. Let’s say my server domain is ming8832, then what should my name space be?

Any help will be appreciate! Thanks in advance!
ExampleExtension.java.zip (494 Bytes)
ExampleIQPacketHandler.java.zip (550 Bytes)