How to register Plugin as IQHandler?

I am interested in write a plugin which could handle my IQ messages.

I see the code to Register a plugin as an IQHandler in the link

http://www.jivesoftware.org/builds/wildfire/docs/latest/documentation/plugin-dev -guide.html

IQHandler myHandler = new MyIQHander();

IQRouter iqRouter = XMPPServer.getInstance().getIQRouter();

iqRouter.addHandler(myHandler);[/code]

Where do you I need to add this code in the plugin? Or have to write a separate IQHandler? Can someone point me to an example for this?

My IQHandler class looks something like this:

This is good to use as an IQHandler plugin or am I missing something?


package org.jivesoftware.wildfire.plugin;

import java.io.File;

import org.dom4j.Element;

import org.indent.wildfire.auth.UserRandomList;

import org.jivesoftware.wildfire.ClientSession;

import org.jivesoftware.wildfire.SessionManager;

import org.jivesoftware.wildfire.XMPPServer;

import org.jivesoftware.wildfire.container.Plugin;

import org.jivesoftware.wildfire.container.PluginManager;

import org.xmpp.packet.IQ;

import org.xmpp.packet.JID;

import org.xmpp.packet.Packet;

import org.indent.wildfire.auth.ClientIDExtension;

import org.jivesoftware.wildfire.Session;

public class ClientRandomPlugin implements Plugin {

private XMPPServer server;

private UserManager userManager;

private PluginManager pluginManager;

private String serviceName;

private static String serverName;

public ClientRandomPlugin() {

}

public void initializePlugin(PluginManager manager, File pluginDirectory) {

pluginManager = manager;

}

public void initialize(JID jid, ComponentManager componentManager) {

}

public void start() {

}

public void destroyPlugin() {

pluginManager = null;

server = null;

userManager = null;

}

public void shutdown() {

}

public void processPacket(Packet p) {

}

private IQ handleIQ(IQ packet) {

}

}[/code]

Just add yourself as an IQHandler in the init method of your plugin. It should be as simple as that.

-Matt

Example:

public class AddressbookPlugin implements Plugin{

public AddressbookPlugin() {

IQHandler iqAddressbookHandler = new IQAddressbookHandler();

IQRouter iqRouter = XMPPServer.getInstance().getIQRouter();

iqRouter.addHandler(iqAddressbookHandler);

}

public void initializePlugin(PluginManager manager, File pluginDirectory) {

}

public void destroyPlugin() {

}

}

protected class IQAddressbookHandler extends IQHandler {

public IQAddressbookHandler () {

super(“Addressbook Handler”);

}

/* (non-Javadoc)

  • @see org.jivesoftware.wildfire.handler.IQHandler#getInfo()

*/

public IQHandlerInfo getInfo() {

// TODO Auto-generated method stub

           return new IQHandlerInfo("query","http://www.hupoo.net/protocol/addressbook");

}

/* (non-Javadoc)

*/

public IQ handleIQ(IQ packet) throws UnauthorizedException {

System.out.println("pa: "+packet);

return null;

}

}

/code

null

Hello zhuam,

Thanks very much.

Mahaveer

May be this is useful for others as well. To receive the response on the client side of your custom namespace iq hander you have to register your iq stuff as well on the client side (smack).

//register custom namespace handler

providerMgr.addIQProvider(“query”, SessionCount.NAMESPACE, new SessionCount.Provider());

//the hanlder itself looks like this

public class SessionCount extends IQ {

public static final String NAMESPACE = "iq:user:sessioncount";
static final String NAMESPACE_ELEMENT = "<query xmlns=\"" + NAMESPACE + "\"/>";
private int numberOfSessions;

public SessionCount() {
    setType(IQ.Type.GET);
}

@Override
public String getChildElementXML() {
    return NAMESPACE_ELEMENT;
}

/**
 * inner class
 */
public static class Provider implements IQProvider {

    /** Creates a new Provider. ProviderManager requires that every PacketExtensionProvider
     *  has a public,no-argument constructor
     */
    public Provider() {
        /***/
    }

    public IQ parseIQ(XmlPullParser parser) throws Exception {

        SessionCount sCount = new SessionCount();
        boolean done = false;
        while (!done) {
            int eventType = parser.next();
            if (eventType == XmlPullParser.START_TAG) {
                if (parser.getName().equals("sessioncount")) {
                    String sessions = parser.nextText();
                    sCount.setNumberOfSessions(Integer.valueOf(sessions).intValue());
                    done = true;
                }
            }
        }
        return sCount;
    }
}

I’ve been trying to write an IQHandler plugin for days now with no luck. I’ve read all the forum post and to my knowledge I’ve done everything correctly. I have written 2 classes, one class is the plugin and the other is the IQHandlers. I’ve added logging information and notice that the plugin is initialized but the handler handleIQ method is not called. My sample code is posted below.

import org.jivesoftware.openfire.IQRouter;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.container.Plugin;
import org.jivesoftware.openfire.container.PluginManager;
import org.jivesoftware.openfire.handler.IQHandler;
import org.jivesoftware.util.Log;

import java.io.File;
import java.io.IOException;

/**

  • A sample plugin for Openfire.
    */
    public class MyTestPlugin implements Plugin {

    public MyTestPlugin()
    {
    IQHandler testHandler = new IQMyTestHandler();
    IQRouter iqRouter = XMPPServer.getInstance().getIQRouter();
    iqRouter.addHandler(testHandler);
    }

    public void initializePlugin(PluginManager manager, File pluginDirectory) {
    try {
    Log.info("plugin called " + pluginDirectory.getCanonicalPath());
    } catch (IOException e) {
    e.printStackTrace();
    }
    /*
    IQHandler locatorHandler = new IQMyTestHandler();
    IQRouter iqRouter = XMPPServer.getInstance().getIQRouter();
    iqRouter.addHandler(locatorHandler);
    * */
    }

public void destroyPlugin() {
// Your code goes here
}
}

import org.jivesoftware.openfire.IQHandlerInfo;
import org.jivesoftware.openfire.auth.UnauthorizedException;
import org.jivesoftware.openfire.handler.IQHandler;
import org.jivesoftware.util.Log;
import org.xmpp.packet.IQ;
import org.xmpp.packet.PacketError;

public class IQMyTestHandler extends IQHandler{

private IQHandlerInfo info;

public IQMyTestHandler() {
super(“XMPP Test Handler”);
info = new IQHandlerInfo(“query”, “jabber:iq:testhandler”);
Log.info(“IQMyTestHandler instance created”);
}

@Override
public IQHandlerInfo getInfo() {
    Log.info("IQMyTestHandler getInfo called");
    return info;
}

@Override
public IQ handleIQ(IQ packet) throws UnauthorizedException {
Log.info("packet called " + packet.toString());
IQ result = IQ.createResultIQ(packet);
result.setChildElement(packet.getChildElement().createCopy());
result.setError(PacketError.Condition.not_acceptable);
return result;
}

}

The client sends a message as follows:

But it seems that the server never gets this message. Any idea what i’m doing wrong?

hi, you register your handler twice which may confuses the server. If you do not have success just try the example above which works.

guido

iqRouter.addHandler(testHandler);

iqRouter.addHandler(locatorHandler);

I have the second handler commented out. As for the example I tried that example and it didn’t work for me. I will try again.

Problem solved, xml from client was malformed,

was

should have been

Thanks for the reply. Developing is so much more fun when you have help.