openfire 消息拦截器

文档参考 :http://blog.csdn.net/majian_1987/article/details/9814185

 

package com.bis.plugin.messageplugin;

import java.io.File;
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;
	
	
	
	public MessagePlugIn() {
		interceptoerManager = InterceptorManager.getInstance();
	}

	@Override
	public void initializePlugin(PluginManager manager, File pluginDirectory) {
		pluginManager = manager;
		//将拦截器注册到消息邮局中
		//InterceptorManager.getInstance().addInterceptor(this);
		interceptoerManager.addInterceptor(this);
		System.out.println("加载插件成功!");
	}

	@Override
	public void destroyPlugin() {
		interceptoerManager.removeInterceptor(this);
		System.out.println("销毁插件成功!");
	}

	@Override
	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) {
	if (packet == null) {
		throw new NullPointerException();
	}
	ClientSession session = sessionManager.getSession(packet.getFrom());
	try {
		// Invoke the interceptors before we process the read packet
		//系统的拦截器就是在这里调用的,遍历动态添加的所有拦截器
		InterceptorManager.getInstance().invokeInterceptors(packet, session, true, false);
		if (session == null || session.getStatus() == Session.STATUS_AUTHENTICATED) {
			JID recipientJID = packet.getTo();

			// Check if the message was sent to the server hostname
			if (recipientJID != null && recipientJID.getNode() == null && recipientJID.getResource() == null &&
					serverName.equals(recipientJID.getDomain())) {
				if (packet.getElement().element("addresses") != null) {
					// Message includes multicast processing instructions. Ask the multicastRouter
					// to route this packet
					multicastRouter.route(packet);
				}
				else {
					// Message was sent to the server hostname so forward it to a configurable
					// set of JID's (probably admin users)
					sendMessageToAdmins(packet);
				}
				return;
			}

			try {
				// Deliver stanza to requested route
				routingTable.routePacket(recipientJID, packet, false);
			}
			catch (Exception e) {
				log.error("Failed to route packet: " + packet.toXML(), e);
				routingFailed(recipientJID, packet);
			}
		}
		else {
			packet.setTo(session.getAddress());
			packet.setFrom((JID)null);
			packet.setError(PacketError.Condition.not_authorized);
			session.process(packet);
		}
		// Invoke the interceptors after we have processed the read packet
		//系统的拦截器就是在这里调用的,遍历动态添加的所有拦截器
		InterceptorManager.getInstance().invokeInterceptors(packet, session, true, true);
	} catch (PacketRejectedException e) {
		// An interceptor rejected this packet
		if (session != null && e.getRejectionMessage() != null && e.getRejectionMessage().trim().length() > 0) {
			// A message for the rejection will be sent to the sender of the rejected packet
			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());
			session.process(reply);
		}
	}
}

 

备注:拦截器本身只是拦截发送过来的消息,但是本身并没有修改消息的内容

openfire 消息拦截器_第1张图片

 

 

你可能感兴趣的:(openfire)