开发openfire 消息拦截器插件PacketInterceptor
开发消息拦截器的步骤跟开发简单插件步骤一样,要开发消息拦截器插件,首先继承PacketInterceptor包拦截类,然后在initializelPlugin()方法中注册拦截器,就可以实现interceptPackage()方法中拦截包(即此方法中的packet参数)了。并且,可以通过入参incoming来判断是服务器发送的包还是接受的包(注:true为服务器接收的包;false为发出的包)。processed参数用处暂不明,猜想是对请求做了什么处理的标识,但不影响我们对包进行拦截和处理。
这个扩展方式与前一种相比的好处在于,这种方式不仅没有修改原注册流程的代码,而且最大程度的使用了原注册流程。这样可以避免一些不必要的风险。
- 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) {
interceptoerManager = InterceptorManager.getInstance();
- pluginManager = manager;
- 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);
- }
- }
- }
- 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);
- }
- }
- }
最后附上xmpp的相关协议:
- .openfire注册相关协议
- 1./*用户注册,原本的协议中没有province字段,这里为扩展*/
- <iq id="g0G4m-1" to="zhanglj" type="set">
- <query xmlns="jabber:iq:register">
- <username>re3</username>
- <email></email>
- <name></name>
- <password>1</password>
- <province>合肥</province>
- </query>
- </iq>
- 2.用户注册成功
- <iq type="result" id="g0G4m-1" from="zhanglj" to="re3@zhanglj/Spark 2.6.3"/>
- 3.用户注册失败
- i. 用户名为空:code='500"
- <iq type="error" id="g0G4m-1" from="zhanglj" to="re3@zhanglj/Spark 2.6.3">
- <query xmlns="jabber:iq:register">
- <username/>
- <email/>
- <name/>
- <password>1</password>
- <province>合肥</province>
- </query>
- <error code="500" type="wait">
- <internal-server-error xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
- </error>
- </iq>
- ii.密码为空:code='406"
- <iq type="error" id="g0G4m-1" from="zhanglj" to="re3@zhanglj/Spark 2.6.3">
- <query xmlns="jabber:iq:register">
- <username>r</username>
- <email/>
- <name/>
- <password/>
- </query>
- <error code="406" type="modify"><not-acceptable xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
- </error>
- </iq>
- iii.用户已存在:code='409"
- <iq id="g0G4m-1" to="re3@zhanglj/Spark 2.6.3" from="zhanglj" type="error">
- <query xmlns="jabber:iq:register">
- <username>re5</username>
- <email/>
- <name/>
- <province>合肥</province>
- <password>1</password>
- </query>
- <error code="409" type="CANCEL">
- <conflict xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
- </error>
- </iq>
https://blog.csdn.net/zhonglunshun/article/details/84695804
开发openfire 消息拦截器插件PacketInterceptor的更多相关文章
- 系统开发中使用拦截器校验是否登录并使用MD5对用户登录密码进行加密
项目名称:客户管理系统 项目描述: 项目基于javaEE平台,B/S模式开发.使用Struts2.Hibernate/Spring进行项目框架搭建.使用Struts中的Action 控制器进行用户访问 ...
- (转)C#制作一个消息拦截器
首先,我们先要制作一个自定义Attribute,让他可以具有上下文读取功能,所以我们这个Attribute类要同时继承Attribute和IContextAttribute. 接口IContextAt ...
- 4_4.springboot之Web开发登录和拦截器
1.登录处理 1).禁用模板引擎的缓存 # 禁用缓存 spring.thymeleaf.cache=false 2).页面修改完用ctrl+f9:重新编译: LoginController @Cont ...
- C#制作一个消息拦截器(intercept)1
首先,我们先要制作一个自己定义Attribute,让他能够具有上下文读取功能,所以我们这个Attribute类要同一时候继承Attribute和IContextAttribute. 接口IContex ...
- C# 通过Attribute制作的一个消息拦截器
首先,我们先要制作一个自己定义Attribute,让他能够具有上下文读取功能,所以我们这个Attribute类要同一时候继承Attribute和IContextAttribute. 接口IContex ...
- SpringBoot开发案例之拦截器注入Bean
前言 由于业务需要,需要在拦截器中操作Redis缓存,按照 controller,service层配置发现无法注入,一直报空指针异常. 解决方案 @Configuration public class ...
- wepy 小程序开发(interceptor拦截器 && WXS)
WePY全局拦截器可对原生API的请求进行拦截. import wepy from 'wepy'; export default class extends wepy.app { constructo ...
- 基于Bootstrap和Knockout.js的ASP.NET MVC开发实战 关于 拦截器的 学习 部分
先贴一段: 下面贴代码: 上面这段代码呢,有几个点迷糊.可以找找看
- 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_5-10.Springboot2.x用户登录拦截器开发实战
笔记 10.Springboot2.x用户登录拦截器开发实战 简介:实战开发用户登录拦截器拦截器 LoginInterceptor 1.实现接口 LoginI ...
随机推荐
- HDUOJ-----Climbing Worm
Climbing Worm Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- PHPNow升级PHP版本为5.3.5的方法(转)
PHPNow升级PHP版本为5.3.5的方法 原文:http://sharebar.org/1142.html 在WIN上有时候需要测试一些PHP程序,又不会自行独立配置环境,那么PHPNow是非常好 ...
- NSNotificationCenter实现原理
# 前言 Cocoa中使用NSNotification.NSNotificationCenter和KVO来实现观察者模式,实现对象间一对多的依赖关系. 本篇文章主要来讨论NSNotification和 ...
- 树莓派进阶之路 (021) - 3.2inch RPi LCD (B)
参考文档:http://www.waveshare.net/wiki/3.2inch_RPi_LCD_(B) 产品特点 320x240分辨率 电阻式触摸控制 兼容并可直接插入任何版本树莓派 提供Ras ...
- Docker(一):Docker入门教程
如今Docker的使用已经非常普遍,特别在一线互联网公司.使用Docker技术可以帮助企业快速水平扩展服务,从而到达弹性部署业务的能力.在云服务概念兴起之后,Docker的使用场景和范围进一步发展,如 ...
- Intel Edison学习笔记(二)—— 入门环境配置
一.安装Screen sudo apt-get install screen 二.配置 1.连接USB,等待出现 2.测试串口是否存在: ls /dev/ttyUSB0 输出/dev/ttyUSB0, ...
- 1503: [NOI2004]郁闷的出纳员 (SBT)
1503: [NOI2004]郁闷的出纳员 http://www.lydsy.com/JudgeOnline/problem.php?id=1503 Time Limit: 5 Sec Memory ...
- 【Oracle】Oracle的内外连接
目录结构: contents structure [+] Oracle的内外连接 内连接 等值连接 非等值连接 自连接 外连接 外连接的特点 如何实现外连接 SQL99的内外连接 SQL99的内连接 ...
- mysql 1449 : The user specified as a definer ('root'@'%') does not exist
1)创建试图时抛出此错误信息,如下图所示: 2)从网上搜索了一下,是SQL权限问题,通过如下的方式便可以解决: 3)再次执行创建视图的语句,验证一下问题是否已经解决,可以了,如下所示: 4)参考如下所 ...
- 从android aidl理解Proxy/stub模式
在小7写的上一篇文章<android IPC通信机制梳理>里,我讲到了如果activity要想和一个跨进程的Service进行通信就需要通过Binder框架,获取到IBinder对象,并调 ...