openfire 插件开发例子
生活随笔
收集整理的這篇文章主要介紹了
openfire 插件开发例子
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
? ? 好久都沒有寫東西了。今天總結一下之前開發的一些openfire插件。
? ? 這次的插件需要提供一個HTTP的接口。通過HTTP來對openfire做一些操作。
? ? 插件的目錄結構:項目名稱“exampleplugin"
src/main/javaorg/jivesoftware/openfire/plugin/ExamplePlugin.javaorg/jivesoftware/openfire/plugin/example/ExampleServlet.java src/main/resourceslogo_large.giflogo_small.gifplugin.xml webimages/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;@Overridepublic void destroyPlugin() {server = null;}@Overridepublic 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;@Overridepublic void init(ServletConfig servletConfig) throws ServletException {super.init(servletConfig);plugin = (ExamplePlugin) XMPPServer.getInstance().getPluginManager().getPlugin("exampleplugin");AuthCheckFilter.addExclude("exampleplugin/exampleplugin");}@Overrideprotected 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();}@Overrideprotected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}@Overridepublic 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
?
?
?
?
?
?
?
轉載于:https://my.oschina.net/u/145002/blog/60617
總結
以上是生活随笔為你收集整理的openfire 插件开发例子的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用HTML代码实现个人简历的编写
- 下一篇: TP5: 日志记录改造——4