OpenFire学习(二)内部组件

说明

内部组件就是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>

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,470评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,393评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,577评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,176评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,189评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,155评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,041评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,903评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,319评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,539评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,703评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,417评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,013评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,664评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,818评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,711评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,601评论 2 353

推荐阅读更多精彩内容