标签:没有 add cep host mil ongl username tst 消息拦截器
开发消息拦截器的步骤跟开发简单插件步骤一样,要开发消息拦截器插件,首先继承PacketInterceptor包拦截类,然后在initializelPlugin()方法中注册拦截器,就可以实现interceptPackage()方法中拦截包(即此方法中的packet参数)了。并且,可以通过入参incoming来判断是服务器发送的包还是接受的包(注:true为服务器接收的包;false为发出的包)。processed参数用处暂不明,猜想是对请求做了什么处理的标识,但不影响我们对包进行拦截和处理。
这个扩展方式与前一种相比的好处在于,这种方式不仅没有修改原注册流程的代码,而且最大程度的使用了原注册流程。这样可以避免一些不必要的风险。
-
package com.bis.plugin.messageplugin;
-
-
-
-
import org.jivesoftware.openfire.container.Plugin;
-
import org.jivesoftware.openfire.container.PluginManager;
-
import org.jivesoftware.openfire.interceptor.InterceptorManager;
-
import org.jivesoftware.openfire.interceptor.PacketInterceptor;
-
import org.jivesoftware.openfire.interceptor.PacketRejectedException;
-
import org.jivesoftware.openfire.session.Session;
-
import org.xmpp.packet.Packet;
-
-
public class MessagePlugIn implements Plugin,PacketInterceptor {
-
private static PluginManager pluginManager;
-
private InterceptorManager interceptoerManager;
-
-
-
-
-
interceptoerManager = InterceptorManager.getInstance();
-
-
-
-
public void initializePlugin(PluginManager manager, File pluginDirectory) {
interceptoerManager = InterceptorManager.getInstance();
-
-
interceptoerManager.addInterceptor(this);
-
System.out.println("加载插件成功!");
-
-
-
-
public void destroyPlugin() {
-
interceptoerManager.removeInterceptor(this);
-
System.out.println("销毁插件成功!");
-
-
-
-
public void interceptPacket(Packet packet, Session session,
-
boolean incoming, boolean processed) throws PacketRejectedException {
-
System.out.println("接收到的消息内容:"+packet.toXML());
-
-
-
1、继承Plugin接口,就是在系统启动的时候会执行initializePlugin()方法,表示这是一个插件类
2、继承PacketInterceptor接口,表示这个类是一个拦截Message的消息类,当拦截的时候,会执行interceptPacket方法
关于openfire是如何管理消息拦截器的?
我们可以看看MessageRouter类的route(Message packet)方法
-
public void route(Message packet) {
-
-
throw new NullPointerException();
-
-
ClientSession session = sessionManager.getSession(packet.getFrom());
-
-
-
-
InterceptorManager.getInstance().invokeInterceptors(packet, session, true, false);
-
if (session == null || session.getStatus() == Session.STATUS_AUTHENTICATED) {
-
JID recipientJID = packet.getTo();
-
-
-
if (recipientJID != null && recipientJID.getNode() == null && recipientJID.getResource() == null &&
-
serverName.equals(recipientJID.getDomain())) {
-
if (packet.getElement().element("addresses") != null) {
-
-
-
multicastRouter.route(packet);
-
-
-
-
-
sendMessageToAdmins(packet);
-
-
-
-
-
-
-
routingTable.routePacket(recipientJID, packet, false);
-
-
-
log.error("Failed to route packet: " + packet.toXML(), e);
-
routingFailed(recipientJID, packet);
-
-
-
-
packet.setTo(session.getAddress());
-
packet.setFrom((JID)null);
-
packet.setError(PacketError.Condition.not_authorized);
-
-
-
-
-
InterceptorManager.getInstance().invokeInterceptors(packet, session, true, true);
-
} catch (PacketRejectedException e) {
-
-
if (session != null && e.getRejectionMessage() != null && e.getRejectionMessage().trim().length() > 0) {
-
-
Message reply = new Message();
-
reply.setID(packet.getID());
-
reply.setTo(session.getAddress());
-
reply.setFrom(packet.getTo());
-
reply.setType(packet.getType());
-
reply.setThread(packet.getThread());
-
reply.setBody(e.getRejectionMessage());
-
-
-
-
-
public void route(Message packet) {
-
-
throw new NullPointerException();
-
-
ClientSession session = sessionManager.getSession(packet.getFrom());
-
-
-
-
InterceptorManager.getInstance().invokeInterceptors(packet, session, true, false);
-
if (session == null || session.getStatus() == Session.STATUS_AUTHENTICATED) {
-
JID recipientJID = packet.getTo();
-
-
-
if (recipientJID != null && recipientJID.getNode() == null && recipientJID.getResource() == null &&
-
serverName.equals(recipientJID.getDomain())) {
-
if (packet.getElement().element("addresses") != null) {
-
-
-
multicastRouter.route(packet);
-
-
-
-
-
sendMessageToAdmins(packet);
-
-
-
-
-
-
-
routingTable.routePacket(recipientJID, packet, false);
-
-
-
log.error("Failed to route packet: " + packet.toXML(), e);
-
routingFailed(recipientJID, packet);
-
-
-
-
packet.setTo(session.getAddress());
-
packet.setFrom((JID)null);
-
packet.setError(PacketError.Condition.not_authorized);
-
-
-
-
-
InterceptorManager.getInstance().invokeInterceptors(packet, session, true, true);
-
} catch (PacketRejectedException e) {
-
-
if (session != null && e.getRejectionMessage() != null && e.getRejectionMessage().trim().length() > 0) {
-
-
Message reply = new Message();
-
reply.setID(packet.getID());
-
reply.setTo(session.getAddress());
-
reply.setFrom(packet.getTo());
-
reply.setType(packet.getType());
-
reply.setThread(packet.getThread());
-
reply.setBody(e.getRejectionMessage());
-
-
-
-
最后附上xmpp的相关协议:
-
-
-
<iq id="g0G4m-1" to="zhanglj" type="set">
-
<query xmlns="jabber:iq:register">
-
-
-
-
-
-
-
-
-
-
<iq type="result" id="g0G4m-1" from="zhanglj" to="re3@zhanglj/Spark 2.6.3"/>
-
-
-
-
<iq type="error" id="g0G4m-1" from="zhanglj" to="re3@zhanglj/Spark 2.6.3">
-
<query xmlns="jabber:iq:register">
-
-
-
-
-
-
-
<error code="500" type="wait">
-
<internal-server-error xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
-
-
-
-
-
<iq type="error" id="g0G4m-1" from="zhanglj" to="re3@zhanglj/Spark 2.6.3">
-
<query xmlns="jabber:iq:register">
-
-
-
-
-
-
<error code="406" type="modify"><not-acceptable xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
-
-
-
-
-
<iq id="g0G4m-1" to="re3@zhanglj/Spark 2.6.3" from="zhanglj" type="error">
-
<query xmlns="jabber:iq:register">
-
-
-
-
-
-
-
<error code="409" type="CANCEL">
-
<conflict xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
-
-
https://blog.csdn.net/zhonglunshun/article/details/84695804
开发openfire 消息拦截器插件PacketInterceptor
标签:没有 add cep host mil ongl username tst 消息拦截器
原文地址:https://www.cnblogs.com/tiancai/p/10064631.html