openfire 插件开发例子

别等时光非礼了梦想. 提交于 2020-03-06 10:23:48

    好久都没有写东西了。今天总结一下之前开发的一些openfire插件。

    这次的插件需要提供一个HTTP的接口。通过HTTP来对openfire做一些操作。

    插件的目录结构:项目名称“exampleplugin"

src/main/java
    org/jivesoftware/openfire/plugin/ExamplePlugin.java
    org/jivesoftware/openfire/plugin/example/ExampleServlet.java
src/main/resources
    logo_large.gif
    logo_small.gif
    plugin.xml
web
    images/
    WEB-INF/
        web-custom.xml

ExamplePlugin.java 实现了openfire Plugin接口。它提供了一个handle方法。我们的主要逻辑代码在这个方法里面实现:

package org.jivesoftware.openfire.plugin;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import org.jivesoftware.openfire.SessionManager;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.auth.UnauthorizedException;
import org.jivesoftware.openfire.container.Plugin;
import org.jivesoftware.openfire.container.PluginManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



public class ExamplePlugin implements Plugin{

	private static final long serialVersionUID = 4609828712383867663L;

	private static final Logger log = LoggerFactory.getLogger(ExamplePlugin.class);
	private XMPPServer server = null;
	
	@Override
	public void destroyPlugin() {
		server = null;
	}

	@Override
	public void initializePlugin(PluginManager arg0, File arg1) {
		log.info("enter--initializePlugin");
		server = XMPPServer.getInstance();
		log.info("exit---initializePlugin");
	}
	
	public String handle(String input) {
		//your logic code
	}
}

ExampleServlet.java 实现了HttpServlet。它的主要功能是提供HTTP接口,通过HTTP接口来操作ExamplePlugin类中的handle方法。

package org.jivesoftware.openfire.plugin.example;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jivesoftware.admin.AuthCheckFilter;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.plugin.ExamplePlugin;
import org.jivesoftware.openfire.user.UserAlreadyExistsException;
import org.jivesoftware.openfire.user.UserNotFoundException;
import org.jivesoftware.util.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xmpp.packet.JID;

public class ExampleServlet extends HttpServlet {

	private static final long serialVersionUID = 5815016363356541081L;
	private static final Logger log = LoggerFactory.getLogger(ExampleServlet.class);
	
	private ExamplePlugin plugin;

	@Override
	public void init(ServletConfig servletConfig) throws ServletException {
		super.init(servletConfig);
		plugin = (ExamplePlugin) XMPPServer.getInstance()
				.getPluginManager().getPlugin("exampleplugin");
		AuthCheckFilter.addExclude("exampleplugin/exampleplugin");
	}

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		try {
			String input = request.getParameter("input");
			plugin.handle(input);
		} catch (Exception e) {
			replyError(e.toString(), response, out);
		}
	}

	private void replyError(String error, HttpServletResponse response,
			PrintWriter out) {
		response.setContentType("text/xml");
		out.println("<error>" + error + "</error>");
		out.flush();
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
	}

	@Override
	public void destroy() {
		super.destroy();
		AuthCheckFilter.removeExclude("exampleplugin/exampleplugin");
	}
}

plugin.xml描述了插件的信息:

<?xml version="1.0" encoding="UTF-8"?>

<plugin>
    <class>org.jivesoftware.openfire.plugin.ExamplePlugin</class>
    <name>Example</name>
    <description>Example</description>
    <author>ilxlf</author>
    <version>1.0.0</version>
    <date>06/05/2012</date>
    <minServerVersion>3.7.1</minServerVersion>
</plugin>

web-custom.xml描述了ExampleServlet的一些信息:

<?xml version='1.0' encoding='ISO-8859-1'?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
    <!-- Servlets -->
    <servlet>
        <servlet-name>ExampleServlet</servlet-name>
        <servlet-class>org.jivesoftware.openfire.plugin.example.ExampleServlet</servlet-class>
    </servlet>

    <!-- Servlet mappings -->
    <servlet-mapping>
        <servlet-name>ExampleServlet</servlet-name>
        <url-pattern>/exampleplugin</url-pattern>
    </servlet-mapping>

</web-app>

项目的名称叫: ”exampleplugin"

这个项目用gradle来提供编译,打包。这里就不写了。

编译的结果是一个exampleplugin.jar。 openfire安装目录下面有一个plugins目录。把这个jar包拷贝到该目录下就可以了。

然后我们就可以在openfire admin console界面 plugin标签页里面看到我们这个插件了。

HTTP的对外接口就是:

http://your_openfire_ip:9090/plugins/exampleplugin/exampleplugin/test_input

 

 

 

 

 

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!