說明
內(nèi)部組件就是openfire的插件澈魄,因為它必須和openfire所在的進程一起運行筒狠,被openfire動態(tài)加載溜徙,所以叫做openfire插件葛超。
一般來說包晰,都是通過admin管理界面進行導(dǎo)入
添加插件
添加操作說明
刪除插件
刪除操作說明
項目結(jié)構(gòu)
- src
- plugin.xml
- changelog.html:發(fā)布日志筒愚,記錄插件各版本的變化情況贡歧,就是純HTML怨绣,方便閱讀即可
- readme.html:項目描述
- logo_large.gif:插件大logo
- logo_small.gif :插件小logo
- pom.xml:打包的項目
src
不用多說整胃,自然是源碼路徑颗圣,內(nèi)部就是一個web項目,內(nèi)部包含java:資源代碼屁使,resources:項目配置資源等在岂,webapp:web服務(wù)資源、服務(wù)配置web.xml等蛮寂。
最為重要的是assembly蔽午,其重要配置項目的xml信息,是否見插件jar的關(guān)鍵
源碼結(jié)構(gòu)
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函數(shù) |
name | 插件的名字 |
description | 插件功能描述字段 |
author | 作者名稱 |
version | 插件版本 |
date | 插件書寫的日期,這個自己知道就可以了 |
url | 插件的官網(wǎng) |
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>
實戰(zhàn)
1僧鲁、在父項目中的plugin模塊下,創(chuàng)建子項目象泵,然后按照上面的結(jié)構(gòu)寞秃,創(chuàng)建項目按照上面結(jié)構(gòu)
2、復(fù)制assembly.xml
3偶惠、然后編寫主要業(yè)務(wù)類
4春寿、補上pom的配置
一個小插件項目就完成了,然后就是打包命令忽孽,在父項目目錄下
mvn verify -f plugins/openfire-plugin-ms-filter/pom.xml
最后打包出來的項目openfire-plugin-ms-filter.jar
绑改,通過上面添加插件的步驟,通過admin界面進行添加扒腕。
插件
下面粘貼部分需要用到的項目的代碼和xml绢淀。
業(yè)務(wù)類
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;
// 插件初始化函數(shù)
@Override
public void initializePlugin(PluginManager pluginManager, File file) {
// 將當(dāng)前插件加入到消息攔截管理器(interceptorManager )中,當(dāng)消息到來或者發(fā)送出去的時候瘾腰,會觸發(fā)本插件的interceptPacket方法皆的。
interceptorManager = InterceptorManager.getInstance();
interceptorManager.addInterceptor(this);
}
// 插件銷毀函數(shù)
@Override
public void destroyPlugin() {
// 當(dāng)插件被卸載的時候,主要通過openfire管理控制臺卸載插件時蹋盆,被調(diào)用费薄。注意interceptorManager的addInterceptor和removeInterceptor需要成對調(diào)用硝全。
interceptorManager.removeInterceptor(this);
}
// 插件攔截處理函數(shù)
@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竞慢,這里當(dāng)packet是message的時候先紫,進行處理。
if (packet instanceof Message) {
// 將packet強制轉(zhuǎn)換為Message
Message msg = (Message) packet;
// 取得message中的body內(nèi)容筹煮,就是消息正文
String body = msg.getBody();
// 如果內(nèi)容中包含fuck遮精,則拒絕處理消息
if (body != null && body.contains("fuck")) {
// 這里通過拋出異常的方式,來阻止程序流程繼續(xù)執(zhí)行下去败潦。
PacketRejectedException rejectedException = new PacketRejectedException();
// 設(shè)置返回信息
rejectedException.setRejectionMessage("信息發(fā)送失敗本冲,包含侮辱性詞語");
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>