OF的插件开发有三种方式,关于如何开发OF的插件,请参考别的文章,本文就不具体描述了。下面讲述如何Register a plugin as anIQHandler。
我们如果要做自己的IQ包处理,可以自定义类如TestTemplateMethodHandler:class TestTemplateMethodHandler extends IQHandler。
以下需要注意:
1.IQ handlers相应包中特定的元素名或命名空间,在public IQHandlerInfo getInfo()方法中写上自己想要注册的元素名及命名空间,
2.在public IQ handlerIQ(IQ packet)方法中写上自己想要对丢进来的IQ包做什么样的处理(注意IQ包是基于问答形式的,所以应该有IQ包的reply)。
3.继承IQHandler的插件,需要注册到IQRouter中去。
以下是注册一个继承IQHandler的插件到IQRouter中的代码:
IQHandler myHandler = new MyIQHander();
IQRouter iqRouter = XMPPServer.getInstance().getIQRouter();
iqRouter.addHandler(myHandler);
接下来就是展示具体的实现继承IQHandler的插件两种方式:(区别主要是在把IQHandler的继承类和插件是否绑定在一起)
1.注册继承IQHandler的插件到IQrouter可以在这个插件初始化的时候:
package com.bis.plugins.iqhandler;
import java.io.File;
import org.jivesoftware.openfire.IQHandlerInfo;
import org.jivesoftware.openfire.IQRouter;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.auth.UnauthorizedException;
import org.jivesoftware.openfire.container.Plugin;
import org.jivesoftware.openfire.container.PluginManager;
import org.jivesoftware.openfire.handler.IQHandler;
import org.xmpp.packet.IQ;
public class MyIQHandler extends IQHandler implements Plugin{
private IQHandlerInfo info;
public MyIQHandler() {
super("MyIQHandler");
info = new IQHandlerInfo("query", "jabber:iq:roster");
}
@Override
public IQHandlerInfo getInfo() {
// TODO Auto-generated method stub
return info;
}
@Override
public IQ handleIQ(IQ packet) throws UnauthorizedException {
// TODO Auto-generated method stub
IQ reply = IQ.createResultIQ(packet);
System.out.println("【iq from】:" + reply.getFrom());
System.out.println("【iq to】:" + reply.getTo());
return reply;
}
@Override
public void initializePlugin(PluginManager manager, File pluginDirectory) {
IQRouter iqRouter = XMPPServer.getInstance().getIQRouter();
iqRouter.addHandler(this);
System.out.println("==init myIQHandler==");
}
@Override
public void destroyPlugin() {
// TODO Auto-generated method stub
}
}
或者,单独写一个IQHandler的实现类,再单独写一个插件专门用于把所有扩展的IQHandler继承类注册到IQRouter中:
IQHandler的继承类:
public class GroupTreeIQHander extends IQHandler
{
private static final String MODULE_NAME = "group tree handler";
private static final String NAME_SPACE = "com:im:group";
private IQHandlerInfo info;
public GroupTreeIQHander()
{
super(MODULE_NAME);
info = new IQHandlerInfo("gruops", NAME_SPACE);
}
/*
* (non-Javadoc)
*
* @see org.jivesoftware.openfire.handler.IQHandler#getInfo()
*/
@Override
public IQHandlerInfo getInfo()
{
return info;
}
/*
* (non-Javadoc)
*
* @see org.jivesoftware.openfire.handler.IQHandler#handleIQ(org.xmpp.packet.IQ)
*/
@Override
public IQ handleIQ(IQ packet) throws UnauthorizedException
{
IQ reply = IQ.createResultIQ(packet);
Element groups = packet.getChildElement();//1
if (!IQ.Type.get.equals(packet.getType()))
{
System.out.println("非法的请求类型");
reply.setChildElement(groups.createCopy());
reply.setError(PacketError.Condition.bad_request);
return reply;
}
String userName = StringUtils.substringBefore(packet.getFrom().toString(),"@");
GroupManager.getInstance().initElement(groups,userName);
reply.setChildElement(groups.createCopy());//2
System.out.println("返回的最终XML" + reply.toXML());
return reply;
}
}
再写一个插件,用于把扩展的IQHandler继承类注册到IQRouter中:
/**
* 服务器端插件类
*
* @author Phoenix
*
* Mar 14, 2008 11:03:11 AM
*
* version 0.1
*/
public class GroupTreePlugin implements Plugin
{
private XMPPServer server;
/*
* (non-Javadoc)
*
* @see org.jivesoftware.openfire.container.Plugin#destroyPlugin()
*/
public void destroyPlugin()
{
}
/*
* (non-Javadoc)
*
* @see org.jivesoftware.openfire.container.Plugin#initializePlugin(org.jivesoftware.openfire.container.PluginManager,
* java.io.File)
*/
public void initializePlugin(PluginManager manager, File pluginDirectory)
{
PluginLog.trace("注册群组树IQ处理器");
server = XMPPServer.getInstance();
server.getIQRouter().addHandler(new GroupTreeIQHander()); //1
server.getIQRouter().addHandler(new UserInfoIQHandler());
server.getIQRouter().addHandler(new DelUserIQHandler());
server.getIQRouter().addHandler(new CreateUserIQHandler());
server.getIQRouter().addHandler(new AddGroupUserIQHandler());
server.getIQRouter().addHandler(new SetRoleIQHandler());
}
}