openfire-IntelliJ IDEA插件环境搭建
1.建立Java工程
2.目录结构介绍
目录解释
build
|lib ant lib目录
|build.xml ant xml
|build.properties ant 配置文件
lib 存放jar包
src 存放插件源码
|- database 存放数据库文件
|- i18n 存放标签国际化
|- java 存放源码
|- test 存放测试代码
|- web
|- images 图片
|- scripts 脚本
|- style CSS
|- WEB-INFO
|-web-custom.xml servlet配置地址
|- xxxx.jsp
changelog.html 日志记录
logo_large.gif 大图标
logo_small.gif 小图标
plugin.xml 插件xml
readme.html 介绍
3.简单插件开发
3.1 java环境配置
把openfire.jar导入,请上网上下载
3.2 Plugin
public interface Plugin {
//初始化插件
public void initializePlugin(PluginManager manager, File pluginDirectory);
// 销毁插件
public void destroyPlugin();
}
public interface PacketInterceptor {
//消息拦截器
void interceptPacket(Packet packet, Session session, boolean incoming, boolean processed)
throws PacketRejectedException;
}
3.3plugin.xml文件
l Name 该插件的名称
l Description 该插件说明
l Author 该插件作者
l Version 该插件的版本标识
l Date 该插件的发布日期
l Url 该插件说明网址
l minServerVersion 该插件需要最低版本Opfenfire的支持
l licenseType 显示许可协议,常用的显示值如下:
“commercial”:commercial “商业” :插件是下发布的商业许可协议。
“gpl”: 通用公共许可证,插件发布使用GNU公共授权
“apache”:Apache许可证internal
“internal”:插件是提供内部组织使用
“other”: 如果许可未设置就会假定为其他
l databaseKey 如果插件需要它自己的数据表,该databaseKey内容应设立一个架构
4.简单应用
4.1 创建插件实体类
package com.openfire.plugins.messageFilter;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xmpp.packet.Message;
import org.xmpp.packet.Packet;
/**
* 过滤插件:当body中有fuck时,将消息截断,不转发消息。
*
*/
public class MessageFilterPlugin implements Plugin ,PacketInterceptor{
// A:生成一个日志实例,用于打印日志,日志被打印在openfire_src\target\openfire\logs目录中
private static final Logger Log = LoggerFactory.getLogger(MessageFilterPlugin.class);
//B: 消息拦截器
private InterceptorManager interceptorManager;
//C: 插件初始化函数
@Override
public void initializePlugin(PluginManager manager, File pluginDirectory) {
Log.info("MessageFilter init");
// 将当前插件加入到消息拦截管理器(interceptorManager )中,当消息到来或者发送出去的时候,会触发本插件的interceptPacket方法。
interceptorManager = InterceptorManager.getInstance();
interceptorManager.addInterceptor(this);
}
//D: 插件销毁函数
@Override
public void destroyPlugin() {
Log.info("MessageFilter destory");
// 当插件被卸载的时候,主要通过openfire管理控制台卸载插件时,被调用。注意interceptorManager的addInterceptor和removeInterceptor需要成对调用。
interceptorManager.removeInterceptor(this);
}
//E 插件拦截处理函数
@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")){
// F: 这里通过抛出异常的方式,来阻止程序流程继续执行下去。
PacketRejectedException rejectedException = new PacketRejectedException();
rejectedException.setRejectionMessage("fuck is error");
throw rejectedException;
}
}
}
}
}
4.2 带jsp设置
在web下创建messageFilter-index.jsp文件
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>hello world: 你好openfire</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!--和plugin.xml里面的 <sidebar id="sidebar-server-settings">中的ID相同-->
<meta name="pageID" content="messageFilter-index"/>
</head>
<body>
<h3>hello world jsp!! <a href="/plugins/messageFilter/messageFilterServlet">messageFilterServlet</a></h3>
<div class="jive-contentBoxHeader">jive-contentBoxHeader</div>
<div class="jive-contentBox">jive-contentBox</div>
<div class="jive-table">
<table cellpadding="0" cellspacing="0" border="0" width="100%">
<thead>
<tr>
<th> sss</th>
<th nowrap>a</th>
<th nowrap>b</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">asdf</td>
<td align="center">asdf</td>
<td align="center">asdf</td>
</tr>
<tr class="jive-even">
<td align="center">asdf</td>
<td align="center">asdf</td>
<td align="center">asdf</td>
</tr>
<tr class="jive-odd">
<td align="center">asdf</td>
<td align="center">asdf</td>
<td align="center">asdf</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
4.3 创建servlet
package com.openfire.plugins.messageFilter;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author 曲修成
* @className SampleServlet
* @description
* @date 2017-03-25 13:57:00
*/
public class MessageFilterServlet extends HttpServlet {
private static final long serialVersionUID = -5404916983906926869L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getOutputStream().write("哈哈".toString().getBytes());
response.getOutputStream().close();
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
4.4 创建web-custom.xml
web-custom.xml
<?xml version='1.0' encoding='ISO-8859-1'?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!-- Servlets -->
<servlet>
<servlet-name>messageFilterServlet</servlet-name>
<servlet-class>com.openfire.plugins.messageFilter.MessageFilterServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Servlet mappings -->
<servlet-mapping>
<servlet-name>messageFilterServlet</servlet-name>
<url-pattern>/messageFilterServlet</url-pattern>
</servlet-mapping>
</web-app>
4.5创建plugin.xml
<?xml version="1.0" encoding="UTF-8"?>
<plugin>
<!-- 插件的启动类,也可以理解为插件的main函数 -->
<class>com.openfire.plugins.messageFilter.MessageFilterPlugin</class>
<!-- 插件的名字,显示在上图的插件中-->
<name>MessageFilter</name>
<!--显示在描述字段中-->
<description>过滤会话</description>
<!--显示在作者-->
<author>曲修成</author>
<!--显示在插件版本-->
<version>1.0.0</version>
<!--插件书写的日期-->
<date>2017/03/25</date>
<!--openfire的最小版本-->
<minServerVersion>4.1.3</minServerVersion>
<!-- UI extension -->
<adminconsole>
<tab id="tab-server">
<sidebar id="sidebar-server-settings">
<item id="messageFilter-index" name="过滤脏话显示" url="messageFilter-index.jsp"
description="对话过滤插件" />
</sidebar>
</tab>
</adminconsole>
</plugin>
5.编译插件
5.1 build.xml解释
build.properties
plugin.name=messageFilter
plugin.path=E:\\OpenfirePlugins
build.xml
<project name="Webapp Precompilation" default="openfire-plugins" basedir=".">
<!--加载配置文件-->
<property file="build.properties" />
<!-- java servlet相关文件编译jar存放位置 -->
<property name="java.jar.dir" value="${plugin.path}/java-dist"/>
<!-- jsp servlet编译后jar存放位置 -->
<property name="jsp.jar.dir" value="${plugin.path}/jsp-dist/lib"/>
<!-- 定义java servlet和jsp servlet的jar包名称 -->
<property name="java.jar" value="${java.jar.dir}/plugin-${plugin.name}.jar"/>
<property name="jsp.jar" value="${jsp.jar.dir}/plugin-${plugin.name}-jsp.jar"/>
<!-- jsp servlet配置到web.xml中 -->
<property name="plugin.web.xml" value="${plugin.path}/jsp-dist/web.xml"/>
<!-- 编译jsp 并生成相关java文件和web.xml文件 -->
<target name="jspc">
<taskdef classname="org.apache.jasper.JspC" name="jasper2">
<classpath id="jspc.classpath">
<!--配置编译jsp的jar文件-->
<pathelement location="${java.home}/../lib/tools.jar" />
<fileset dir="${plugin.path}/build/lib">
<include name="**/*.jar" />
</fileset>
<fileset dir="${plugin.path}/lib">
<include name="**/*.jar" />
</fileset>
</classpath>
</taskdef>
<!--配置编译后jsp package路径-->
<jasper2 javaEncoding="UTF-8" validateXml="false"
uriroot="${plugin.path}/src/web"
outputDir="${plugin.path}/jsp-dist/src"
package="com.openfire.plugins.${plugin.name}" />
<!--配置编译后jsp web.xml路径-->
<jasper2
validateXml="false"
uriroot="${plugin.path}/src/web"
outputDir="${plugin.path}/jsp-dist/src"
package="com.openfire.plugins.${plugin.name}"
webXml="${plugin.web.xml}"/>
</target>
<!-- 编译jsp 并打jar包 -->
<target name="compile">
<mkdir dir="${plugin.path}/jsp-dist/classes" />
<mkdir dir="${plugin.path}/jsp-dist/lib" />
<!-- <mkdir dir="${plugin.path}/jsp-dist/src" />-->
<javac destdir="${plugin.path}/jsp-dist/classes" optimize="off"
encoding="UTF-8" debug="on" failonerror="false" includeantruntime="true"
srcdir="${plugin.path}/jsp-dist/src" excludes="**/*.smap">
<!-- compilerarg value="-Xlint:unchecked"/
<compilerarg value="-Xlint"/>-->
<classpath>
<pathelement location="${plugin.path}/jsp-dist/classes" />
<fileset dir="${plugin.path}/jsp-dist/lib">
<include name="*.jar" />
</fileset>
<fileset dir="${plugin.path}/lib">
<include name="**/*.jar" />
</fileset>
<fileset dir="${plugin.path}/build/lib">
<include name="**/*.jar" />
</fileset>
</classpath>
<include name="**" />
<exclude name="tags/**" />
</javac>
<jar jarfile="${jsp.jar}" basedir="${plugin.path}/jsp-dist/classes" />
</target>
<!-- 将java servlet打包成jar -->
<target name="java-jar">
<mkdir dir="${java.jar.dir}"/>
<mkdir dir="${java.jar.dir}/classes" />
<javac destdir="${java.jar.dir}/classes" optimize="off"
encoding="UTF-8" debug="on" failonerror="false" includeantruntime="true"
srcdir="${plugin.path}/src/java" excludes="**/*.smap">
<classpath>
<fileset dir="${plugin.path}/lib">
<include name="*.jar" />
</fileset>
<fileset dir="${plugin.path}/build/lib">
<include name="**/*.jar" />
</fileset>
</classpath>
<include name="**" />
</javac>
<jar jarfile="${java.jar}">
<fileset dir="${java.jar.dir}/classes" includes="**/*.class"/>
</jar>
</target>
<!-- 生成可部署的插件包 -->
<target name="plug-jar">
<!-- 插件插件包相关lib、 web目录 -->
<mkdir dir="${plugin.path}/${plugin.name}/lib"/>
<mkdir dir="${plugin.path}/${plugin.name}/web/WEB-INF"/>
<!-- 复制jsp servlet的jar和java servlet的相关jar包到插件包的lib目录下 -->
<copy file="${java.jar}" todir="${plugin.path}/${plugin.name}/lib"/>
<copy file="${jsp.jar}" todir="${plugin.path}/${plugin.name}/lib"/>
<copy todir="${plugin.path}/${plugin.name}/lib">
<fileset dir="${plugin.path}/lib" includes="*.jar"/>
</copy>
<!--数据库信息-->
<copy todir="${plugin.path}/${plugin.name}/database">
<fileset dir="${plugin.path}/src/database" includes="*.*"/>
</copy>
<!-- 将相关的图片、帮助文档、修改日志等文件复制到插件目录下 -->
<copy todir="${plugin.path}/${plugin.name}">
<fileset dir="${plugin.path}" includes="*.*"/>
</copy>
<copy todir="${plugin.path}/${plugin.name}/web">
<fileset dir="${plugin.path}/src/web">
<include name="*"/>
<include name="**/*.*"/>
<exclude name="**/*.xml"/>
<exclude name="**/*.jsp"/>
</fileset>
</copy>
<!-- jsp servlet的web复制到插件目录下 -->
<copy file="${plugin.web.xml}" todir="${plugin.path}/${plugin.name}/web/WEB-INF"/>
<copy todir="${plugin.path}/${plugin.name}/web">
<fileset dir="${plugin.path}/src/web" includes="**/*.xml"/>
</copy>
<!-- 将国际化相关资源文件复制到插件目录下 -->
<!--<copy file="${plugin.path}/bin/i18n" todir="${plugin.path}/${plugin.name}"/>-->
<!-- 产生可部署插件包 -->
<jar jarfile="${plugin.path}/${plugin.name}.jar">
<fileset dir="${plugin.path}/${plugin.name}" includes="**/**"/>
</jar>
</target>
<!-- 生成没有Web资源的可部署插件包 -->
<target name="java-plug-jar">
<!-- 插件插件包相关lib、 web目录 -->
<mkdir dir="${plugin.path}/${plugin.name}/lib"/>
<!-- 复制java servlet的相关jar包到插件包的lib目录下 -->
<copy file="${java.jar}" todir="${plugin.path}/${plugin.name}/lib"/>
<!-- 将相关的图片、帮助文档、修改日志等文件复制到插件目录下 -->
<copy todir="${plugin.path}/${plugin.name}">
<fileset dir="${plugin.path}" includes="*.*"/>
</copy>
<!-- 产生可部署插件包 -->
<jar jarfile="${plugin.path}/${plugin.name}.jar">
<fileset dir="${plugin.path}/${plugin.name}" includes="**/**"/>
</jar>
</target>
<!-- 清理生成的文件 -->
<target name="clean">
<delete file="${plugin.path}/${plugin.name}.jar"/>
<delete dir="${plugin.path}/${plugin.name}"/>
<delete dir="${plugin.path}/jsp-dist"/>
<delete dir="${plugin.path}/java-dist"/>
</target>
<target name="all" depends="clean,jspc,compile"/>
<target name="openfire-plugin" depends="jspc,java-jar"/>
<target name="openfire-plugins" depends="all,java-jar,plug-jar"/>
<target name="openfire-plugin-java" depends="clean,java-jar,java-plug-jar"/>
</project>
5.2编译
5.3目录解释
java-dist:servlet目录和java源码目录
jsp-dist:jsp目录
plugins.name : 编译后源码目录
plugins.name.jar 编译后打的jar包