说明
内部组件就是openfire的插件,因为它必须和openfire所在的进程一起运行,被openfire动态加载,所以叫做openfire插件。
一般来说,都是通过admin管理界面进行导入
添加插件
删除插件
项目结构
- src
- plugin.xml
- changelog.html:发布日志,记录插件各版本的变化情况,就是纯HTML,方便阅读即可
- readme.html:项目描述
- logo_large.gif:插件大logo
- logo_small.gif :插件小logo
- pom.xml:打包的项目
src
不用多说,自然是源码路径,内部就是一个web项目,内部包含java:资源代码,resources:项目配置资源等,webapp:web服务资源、服务配置web.xml等。
最为重要的是assembly,其重要配置项目的xml信息,是否见插件jar的关键
assembly.xml
<!--
~ Copyright (C) 2018 Ignite Realtime Foundation. All rights reserved.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<id>openfire-plugin-ms-filter</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<!--
The OFMeet plugin is an aggregation of various Jitsi projects. These projects have some conflicting dependencies.
The OFMeet plugin handles these by using a class loading mechanism that loads dependencies for each Jitsi project
from a different 'lib' folder (as opposed to a plugin that contains all dependencies in one 'lib' folder). This
assembly descriptor creates these different 'lib' folders. Note that moduleSets are used to capture each of the
Jitsi projects, and explicitly not dependencySets. Using dependencySets instead of moduleSets introduces a problem,
as dependencySets use only the dependencies that have been de-duplicated and filtered for conflict by the Maven
dependency management mechanism. This causes conflicting dependencies to not be included (which typically is a
good thing, but is the exact opposite of what we're trying to do with this very specific plugin). -->
<moduleSets>
<moduleSet>
<useAllReactorProjects>true</useAllReactorProjects>
<includes>
<include>org.igniterealtime.openfire.ofmeet:videobridge</include>
</includes>
<binaries>
<includeDependencies>false</includeDependencies>
<unpack>true</unpack>
<outputDirectory>lib-videobridge</outputDirectory>
</binaries>
</moduleSet>
<moduleSet>
<useAllReactorProjects>true</useAllReactorProjects>
<includes>
<include>org.igniterealtime.openfire.ofmeet:offocus</include>
</includes>
<binaries>
<includeDependencies>false</includeDependencies>
<unpack>true</unpack>
<outputDirectory>lib-jicofo</outputDirectory>
</binaries>
</moduleSet>
<moduleSet>
<useAllReactorProjects>true</useAllReactorProjects>
<includes>
<include>org.igniterealtime.openfire.ofmeet:ofgasi</include>
</includes>
<binaries>
<includeDependencies>false</includeDependencies>
<unpack>true</unpack>
<outputDirectory>lib-jigasi</outputDirectory>
</binaries>
</moduleSet>
</moduleSets>
<fileSets>
<!-- metadata files (readme, icons, etc). -->
<fileSet>
<outputDirectory/>
<directory>${project.build.sourceDirectory}/../..</directory>
<includes>
<include>*.gif</include>
<include>*.png</include>
<include>lib/**</include>
</includes>
</fileSet>
<!-- filtered metadata files (html, plugin.xml) -->
<fileSet>
<outputDirectory/>
<directory>${project.build.sourceDirectory}/../..</directory>
<filtered>true</filtered>
<includes>
<include>*.html</include>
<include>plugin.xml</include>
</includes>
</fileSet>
<!-- database, i18n and web directory -->
<fileSet>
<outputDirectory/>
<directory>${project.build.sourceDirectory}/..</directory>
<includes>
<include>database/**</include>
<include>i18n/**</include>
<include>web/**</include>
</includes>
<!-- Exclude jsp pages (they will be compiled) and filterable resources -->
<excludes>
<exclude>**/*.jsp</exclude>
<exclude>i18n/**/*.properties</exclude>
<exclude>web/**/*.xml</exclude>
<exclude>web/**/*.html</exclude>
<exclude>web/**/*.properties</exclude>
</excludes>
</fileSet>
<!-- database, i18n and web directory (filtered) -->
<fileSet>
<outputDirectory/>
<directory>${project.build.sourceDirectory}/..</directory>
<filtered>true</filtered>
<includes>
<include>i18n/**/*.properties</include>
<include>web/**/*.xml</include>
<include>web/**/*.html</include>
<include>web/**/*.properties</include>
</includes>
<excludes>
<!-- Exclude the web.xml, it will be generated by JspC -->
<exclude>web/WEB-INF/web.xml</exclude>
</excludes>
</fileSet>
<!-- web.xml (it will be modified by JspC) -->
<fileSet>
<outputDirectory>web/WEB-INF</outputDirectory>
<directory>${project.build.directory}</directory>
<includes>
<include>web.xml</include>
</includes>
</fileSet>
<!-- Include the classes folder (filtered) -->
<fileSet>
<outputDirectory>classes</outputDirectory>
<directory>classes</directory>
<filtered>true</filtered>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</fileSet>
<!-- Include the classes folder -->
<fileSet>
<outputDirectory>classes</outputDirectory>
<directory>classes</directory>
<filtered>false</filtered>
<excludes>
<exclude>**/*.xml</exclude>
<exclude>**/*.properties</exclude>
</excludes>
</fileSet>
</fileSets>
<!-- Bundle the dependencies of this plugin -->
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<!-- Exclude the modules (and their transitive dependencies) that go in separate 'lib' folders. -->
<excludes>
<exclude>org.igniterealtime.openfire.ofmeet:videobridge</exclude>
<exclude>org.igniterealtime.openfire.ofmeet:offocus</exclude>
<exclude>org.igniterealtime.openfire.ofmeet:ofgasi</exclude>
</excludes>
<useTransitiveDependencies>true</useTransitiveDependencies>
<useTransitiveFiltering>true</useTransitiveFiltering>
</dependencySet>
</dependencySets>
</assembly>
plugin.xml
是最重要的插件的配置文件。这个文件非常重要,openfire需要读取插件信息,例如插件的启动类是哪一个,需要在plugin.xml中定义
标签 | 说明 |
---|---|
class | 插件的启动类,也可以理解为插件的main函数 |
name | 插件的名字 |
description | 插件功能描述字段 |
author | 作者名称 |
version | 插件版本 |
date | 插件书写的日期,这个自己知道就可以了 |
url | 插件的官网 |
minServerVersion | openfire的最小版本 |
minJavaVersion | JDK最小版本 |
<plugin>
<class>com.demo.plugin.MessageFilterPlugin</class>
<name>文本消息过滤插件</name>
<description>过滤一些敏感词、非法词汇</description>
<author>机制的我</author>
<version>${project.version}</version>
<date>06/06/2019</date>
<minServerVersion>4.3.2</minServerVersion>
<minJavaVersion>1.8</minJavaVersion>
</plugin>
实战
1、在父项目中的plugin模块下,创建子项目,然后按照上面的结构,创建项目按照上面结构
2、复制assembly.xml
3、然后编写主要业务类
4、补上pom的配置
一个小插件项目就完成了,然后就是打包命令,在父项目目录下
mvn verify -f plugins/openfire-plugin-ms-filter/pom.xml
最后打包出来的项目openfire-plugin-ms-filter.jar
,通过上面添加插件的步骤,通过admin界面进行添加。
下面粘贴部分需要用到的项目的代码和xml。
业务类
package com.demo.plugin;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xmpp.packet.Message;
import org.xmpp.packet.Packet;
import java.io.File;
public class MessageFilterPlugin implements Plugin, PacketInterceptor {
private static final Logger Log = LoggerFactory.getLogger(MessageFilterPlugin.class);
// 消息拦截器
private InterceptorManager interceptorManager;
// 插件初始化函数
@Override
public void initializePlugin(PluginManager pluginManager, File file) {
// 将当前插件加入到消息拦截管理器(interceptorManager )中,当消息到来或者发送出去的时候,会触发本插件的interceptPacket方法。
interceptorManager = InterceptorManager.getInstance();
interceptorManager.addInterceptor(this);
}
// 插件销毁函数
@Override
public void destroyPlugin() {
// 当插件被卸载的时候,主要通过openfire管理控制台卸载插件时,被调用。注意interceptorManager的addInterceptor和removeInterceptor需要成对调用。
interceptorManager.removeInterceptor(this);
}
// 插件拦截处理函数
@Override
public void interceptPacket(Packet packet, Session session, boolean incoming, boolean processed) throws PacketRejectedException {
// incoming表示本条消息刚进入openfire。processed为false,表示本条消息没有被openfire处理过。这说明这是一条处女消息,也就是没有被处理过的消息。
if (incoming && processed == false) {
// packet可能是IQ、Presence、Message,这里当packet是message的时候,进行处理。
if (packet instanceof Message) {
// 将packet强制转换为Message
Message msg = (Message) packet;
// 取得message中的body内容,就是消息正文
String body = msg.getBody();
// 如果内容中包含fuck,则拒绝处理消息
if (body != null && body.contains("fuck")) {
// 这里通过抛出异常的方式,来阻止程序流程继续执行下去。
PacketRejectedException rejectedException = new PacketRejectedException();
// 设置返回信息
rejectedException.setRejectionMessage("信息发送失败,包含侮辱性词语");
throw rejectedException;
}
}
}
}
}
配置pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.igniterealtime.openfire</groupId>
<artifactId>plugins</artifactId>
<version>4.3.2</version>
</parent>
<groupId>com.openfire.plugin.demo</groupId>
<artifactId>openfire-plugin-ms-filter</artifactId>
<version>1.0</version>
<build>
<sourceDirectory>src/java</sourceDirectory>
<testSourceDirectory>src/test</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<finalName>${project.artifactId}</finalName>
<attach>false</attach>
<descriptors>
<descriptor>src/assembly/ms-filter-plugin-assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
<!-- Compile the JSP pages -->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jspc-maven-plugin</artifactId>
<version>9.2.14.v20151106</version>
<configuration>
<webAppSourceDirectory>${project.build.sourceDirectory}/../java</webAppSourceDirectory>
<webXml>${project.build.sourceDirectory}/../java/WEB-INF/web.xml</webXml>
</configuration>
<executions>
<execution>
<id>jspc</id>
<goals>
<goal>jspc</goal>
</goals>
<configuration>
<jspc>
<package>org.jivesoftware.openfire.plugin.${project.artifactId}</package>
</jspc>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.igniterealtime.openfire</groupId>
<artifactId>xmppserver</artifactId>
<version>${openfire.version}</version>
</dependency>
</dependencies>
</plugin>
<!-- Fix for Mina 2.x -->
<!-- https://issues.apache.org/jira/browse/DIRMINA-919 -->
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>3.0.0</version>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
<dependencies>
</dependencies>
</project>