开发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 ...
随机推荐
- 一个死去的网站shige.laiyo.com
2017年4月份的时候,研一下刚刚开始. 爬了这个网站,现在这个网站已经关闭了,这些爬虫代码也就没用了,面向特定网站爬虫本身就是没有意义的. 爬author import requests from ...
- (org.jbehave.core.failures.BeforeOrAfterFailed: webdriver selenium错误解决。
(org.jbehave.core.failures.BeforeOrAfterFailed: Method initWebDriver (annotated with @BeforeStory in ...
- iOS block分析
block是代码块,其本质和变量类似.不同的是代码块存储的数据是一个函数体.使用Block,就可以像其他标准函数一样,传入参数,并得到返回值. 可以把Block当做Objective-C的匿名函数,B ...
- 什么是分表和分区 MySql数据库分区和分表方法
1.为什么要分表和分区 日常开发中我们经常会遇到大表的情况,所谓的大表是指存储了百万级乃至千万级条记录的表.这样的表过于庞大,导致数据库在查询和插入的时候耗时太长,性能低下,如果涉及联合查询的情况,性 ...
- 树莓派进阶之路 (019) - 树莓派通过filezilla,samba与PC文件共享(转)
虽然我们可以很方便的通过ssh譬如putty或者vnc连接操控树莓派,但是毕竟树莓派资源没那么高,在上面编程,调试要吃力的多.所以还是想在pc上编程上传到树莓派或者最好,文件共享,可以直接读写共同的文 ...
- ubuntu(14.04) 下安装yaf拓展
#wget http://pecl.PHP.net/get/yaf-2.2.9.tgz #tar zxvf yaf-.tgz #cd yaf- [root@bogon yaf-]# whereis p ...
- 防盗链之URL参数签名 总结
一.概述 传统的 IP 禁用.referer 防盗链.User-Agent 防盗链.地区访问控制等防盗链措施已经无法完全满足用户要求,所以开发出URL参数签名方式来防盗链 二.实现 Token防盗链是 ...
- SQL如何获得本季度第一天、一年的第一天、本月的最后一天
nterval 参数,具有以下设定值: 设置 描述 Year yy, yyyy 年 quarter qq, q 季 Month mm, m 月 dayofyear dy, y 一年的日数 Day dd ...
- [译]async/await中使用阻塞式代码导致死锁 百万数据排序:优化的选择排序(堆排序)
[译]async/await中使用阻塞式代码导致死锁 这篇博文主要是讲解在async/await中使用阻塞式代码导致死锁的问题,以及如何避免出现这种死锁.内容主要是从作者Stephen Cleary的 ...
- easyui textbox combobox 设置只读不可编辑状态
输入框 textbox $("#xx").textbox('setValue','value'); //设置输入框的值 $('#xx').textbox('textbox').a ...