文章目录
- 1、openfire服务端下载安装
- 1.1、openfire解压运行
- 1.2、Spark安装和登录
- 2、openfire插件开发
- 2.1、基于servlet开发http接口
- 2.2、基于Jersey开发http接口
- 2.3、WEB UI页面开发
- 2.4、上传插件
- 2.4.1、访问servlet接口效果
- 2.4.2、访问Jersey接口效果
- 2.4.3、访问页面效果
Openfire 采用Java开发,开源的实时协作(RTC)服务器基于XMPP(Jabber)协议。如果要搭建企业内部IM服务、内部系统集成、或对消息数据有安全要求(数据必须保存在内部),Openfire还是一个最好的选择之一,而且开源免费、二次扩展也方便。
Spark是Openfire的pc客户端应用。
本文仅介绍Openfire的基本安装与扩展开发
1、openfire服务端下载安装
官网下载地址:https://igniterealtime.org/downloads/
1.1、openfire解压运行
openfire_4_7_5.zip:https://igniterealtime.org/downloadServlet?filename=openfire/openfire_4_7_5.zip
openfire_4_7_5.zip解压
window下运行openfire服务器端,在命令行下输入以下命令:
bin\openfire.bat
浏览器访问:http://localhost:9090/,然后按界面提示配置数据库、管理员帐号密码等安装步骤。
1.2、Spark安装和登录
本文使用版本spark_3_0_2-64bit.exe:https://igniterealtime.org/downloadServlet?filename=spark/spark_3_0_2-64bit.exe
双击安装即可
安装后的启动界面:
默认情况下,是登录失败,报certificate_unkown错误,需要在高级设置里面做设置,选择下边两行:
登录前需要先在Openfire服务器端创建帐号:
2、openfire插件开发
本文分别介绍基于servlet和Jersey开发http插件,插件的开发可以参考官方的例子:https://igniterealtime.org/projects/openfire/plugins.jsp
完成后的插件代码目录如下:
- pom.xml
<?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>
<artifactId>plugins</artifactId>
<groupId>org.igniterealtime.openfire</groupId>
<version>4.7.0</version>
</parent>
<groupId>org.igniterealtime.openfire.plugins</groupId>
<artifactId>dbcdc</artifactId>
<version>1.0.0</version>
<name>dbcdc Plugin</name>
<description>Openfire plugin to CDC database</description>
<properties>
<jersey.version>2.36</jersey.version>
</properties>
<build>
<sourceDirectory>src/java</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jspc-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jetty-http</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jetty-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-jaxb</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey.version}</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>igniterealtime</id>
<name>Ignite Realtime Repository</name>
<url>https://igniterealtime.org/archiva/repository/maven/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>igniterealtime</id>
<name>Ignite Realtime Repository</name>
<url>https://igniterealtime.org/archiva/repository/maven/</url>
</pluginRepository>
</pluginRepositories>
</project>
- 所有插件都要实现org.jivesoftware.openfire.container.Plugin接口的java类,DbCdcPlugin.java
package org.igniterealtime.openfire.exampleplugin;
import org.jivesoftware.openfire.PresenceManager;
import org.jivesoftware.openfire.SessionManager;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.container.Plugin;
import org.jivesoftware.openfire.container.PluginManager;
import org.jivesoftware.openfire.group.Group;
import org.jivesoftware.openfire.group.GroupManager;
import org.jivesoftware.openfire.interceptor.InterceptorManager;
import org.jivesoftware.openfire.interceptor.PacketInterceptor;
import org.jivesoftware.openfire.interceptor.PacketRejectedException;
import org.jivesoftware.openfire.muc.MultiUserChatService;
import org.jivesoftware.openfire.roster.RosterManager;
import org.jivesoftware.openfire.session.Session;
import org.jivesoftware.openfire.spi.PresenceManagerImpl;
import org.jivesoftware.openfire.user.UserManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xmpp.packet.Message;
import org.xmpp.packet.Packet;
import java.io.File;
import java.util.Collection;
import java.util.List;
public class DbCdcPlugin implements Plugin, PacketInterceptor {
private static final Logger log = LoggerFactory.getLogger(DbCdcPlugin.class);
public final UserManager userManager;
public final RosterManager rosterManager;
public final GroupManager groupManager;
public final XMPPServer server;
public final SessionManager sessionManager;
public final PresenceManager presenceManager;
private InterceptorManager interceptorManager;
public DbCdcPlugin(){
server = XMPPServer.getInstance();
userManager = server.getUserManager();
// 所有花名册管理。
rosterManager = server.getRosterManager();
groupManager = GroupManager.getInstance();
// 用户组
groupManager.getGroups().forEach(group->{
log.info("DbPlugin group===========" + group.getName());
});
server.getMultiUserChatManager().getMultiUserChatServices().forEach(muchat->{
log.info("DbPlugin muchat===========" + muchat.getServiceName() + "," + muchat.getDescription() + "," + muchat.getServiceDomain());
});
// 所有会话管理
sessionManager = server.getSessionManager();
// 在线状态管理
presenceManager = server.getPresenceManager();
interceptorManager = InterceptorManager.getInstance();
interceptorManager.addInterceptor(this);
}
public void initializePlugin(PluginManager pluginManager, File file) {
log.info("DbCdcPlugin================" );
}
public void destroyPlugin() {
interceptorManager.removeInterceptor(this);
log.info("DbCdcPlugin destroyPlugin================" );
}
public void sendServerMessage(String msg){
SessionManager.getInstance().sendServerMessage(null, msg);
}
public Collection<Group> getGroups(){
return groupManager.getGroups();
}
public List<MultiUserChatService> getRooms(){
return server.getMultiUserChatManager().getMultiUserChatServices();
}
@Override
public void interceptPacket(Packet packet, Session session, boolean incoming, boolean processed) throws PacketRejectedException {
if(packet instanceof Message){
Message message = (Message)packet;
if(message.getType().equals(Message.Type.chat)){
// 打印日志
log.info("groupchat========incoming:" + incoming + ",processed:" + processed + "," + message.toXML());
}
}
}
}
plugin.xml配置插件启动类
<?xml version="1.0" encoding="UTF-8"?>
<plugin>
<class>org.igniterealtime.openfire.exampleplugin.DbCdcPlugin</class>
<name>Db CDC</name>
<description>An Db CDC plugin that does nothing interesting</description>
<author>penngo</author>
<version>1.0.15</version>
<date>09/24/2023</date>
<minServerVersion>4.7.0</minServerVersion>
<adminconsole>
<tab id="tab-server">
<sidebar id="sidebar-server-settings">
<item id="dbcdc-plugin-page" name="Db CDC" url="dbcdcplugin-page.jsp" description="Db CDC Plugin" />
</sidebar>
</tab>
</adminconsole>
</plugin>
2.1、基于servlet开发http接口
接口访问地址:http://localhost:9090/plugins/dbcdc/user?user=test1
DataServlet.java
package org.igniterealtime.openfire.service;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.igniterealtime.openfire.exampleplugin.DbCdcPlugin;
import org.jivesoftware.admin.AuthCheckFilter;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.user.User;
import org.jivesoftware.openfire.user.UserNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
public class UserServlet extends HttpServlet {
private static final Logger log = LoggerFactory.getLogger(UserServlet.class);
private DbCdcPlugin plugin;
@Override
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
plugin = (DbCdcPlugin) XMPPServer.getInstance().getPluginManager().getPlugin( "dbcdc" );
AuthCheckFilter.addExclude("dbcdc/user");
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
handle(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
handle(request, response);
}
private void handle(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Map<String, String[]> parame = request.getParameterMap();
String name = request.getParameter("user");
Collection<User> users = plugin.userManager.getUsers();
Map<String, Object> data = new HashMap<>();
List<Object> list = new ArrayList<>();
for(User user:users){
Map obj = new HashMap();
obj.put("name", user.getName());
obj.put("username", user.getUsername());
list.add(obj);
}
Map obj = new HashMap();
try{
User user = plugin.userManager.getUser(name);
obj.put("name", user.getName());
obj.put("username", user.getUsername());
}
catch (UserNotFoundException e) {
//throw new RuntimeException(e);
log.error("UserNotFoundException=====" + name, e);
}
data.put("search", obj);
data.put("users", list);
replyMessage(toJson(data), response);
}
private void replyMessage(String message, HttpServletResponse response) throws IOException{
PrintWriter out = response.getWriter();
response.setContentType("text/json");
out.println(message);
out.flush();
}
public String toJson(Object obj){
String json = null;
try{
ObjectMapper mapper=new ObjectMapper();
json = mapper.writeValueAsString(obj);
}
catch(Exception e){
}
return json;
}
@Override
public void destroy() {
super.destroy();
AuthCheckFilter.removeExclude("dbcdc/user");
}
}
web-custom.xml则是对servlet等动态注册的配置页面,类似Tomcat中的web.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>UserServlet</servlet-name>
<servlet-class>org.igniterealtime.openfire.service.UserServlet</servlet-class>
</servlet>
<!-- Servlet mappings -->
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/user</url-pattern>
</servlet-mapping>
</web-app>
2.2、基于Jersey开发http接口
Jersey是一个用于构建RESTful Web服务的开源框架。它基于Java语言和JAX-RS(Java API for RESTful Web Services)标准,并提供了简洁的API和丰富的功能。
JerseyWrapper.java
public class JerseyWrapper extends ResourceConfig {
private static final org.slf4j.Logger log = LoggerFactory.getLogger(JerseyWrapper.class);
private static String loadingStatusMessage = null;
public JerseyWrapper(@Context ServletConfig servletConfig) {
log.info("JerseyWrapper========");
registerClasses(
UserService.class
);
}
}
接口实现类
package org.igniterealtime.openfire.service;
import org.igniterealtime.openfire.exampleplugin.DbCdcPlugin;
import org.jivesoftware.admin.AuthCheckFilter;
import org.jivesoftware.openfire.MessageRouter;
import org.jivesoftware.openfire.SessionManager;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.group.Group;
import org.jivesoftware.openfire.muc.MUCRoom;
import org.jivesoftware.openfire.muc.MultiUserChatService;
import org.jivesoftware.openfire.user.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xmpp.packet.JID;
import org.xmpp.packet.Message;
import javax.annotation.PostConstruct;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.*;
@Path("dbcdc/v1/user")
public class UserService {
private static final Logger log = LoggerFactory.getLogger(UserService.class);
private DbCdcPlugin plugin;
@PostConstruct
public void init() {
plugin = (DbCdcPlugin) XMPPServer.getInstance().getPluginManager().getPlugin( "dbcdc" );
// 允许非登录也能访问http接口
AuthCheckFilter.addExclude("dbcdc/v1/user/getUsers");
}
// 访问地址:http://localhost:9090/plugins/dbcdc/v1/user/getUsers?name=test1
@GET
@Path("/getUsers")
@Produces(MediaType.APPLICATION_JSON)
public Map<String, Object> getUsers(@QueryParam("user") String name) throws Exception{
Boolean isEsist = false;
Collection<User> users = plugin.userManager.getUsers();
Map<String, Object> data = new HashMap<>();
List<Object> list = new ArrayList<>();
for(User user:users){
Map obj = new HashMap();
obj.put("name", user.getName());
obj.put("username", user.getUsername());
list.add(obj);
}
User user = plugin.userManager.getUser(name);
Map obj = new HashMap();
obj.put("name", user.getName());
obj.put("username", user.getUsername());
data.put("search", obj);
data.put("users", list);
return data;
}
}
2.3、WEB UI页面开发
openfire的页面是支持jsp技术开发的。
dbcdcplugin-page.jsp页面代码
<%@ page
import="org.jivesoftware.openfire.XMPPServer,
org.igniterealtime.openfire.exampleplugin.DbCdcPlugin,
org.jivesoftware.util.CookieUtils,
org.jivesoftware.util.ParamUtils,
org.jivesoftware.util.StringUtils,
java.util.HashMap"
errorPage="error.jsp"%>
<%@ page import="java.util.Map" %>
<%@ taglib uri="admin" prefix="admin" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%
final boolean save = request.getParameter( "save" ) != null;
final Cookie csrfCookie = CookieUtils.getCookie( request, "csrf" );
final String csrfParam = ParamUtils.getParameter( request, "csrf" );
final String exampleText = ParamUtils.getParameter( request, "exampletext" );
final DbCdcPlugin plugin = (DbCdcPlugin) XMPPServer.getInstance().getPluginManager().getPlugin( "dbcdcplugin" );
final Map<String, String> errors = new HashMap<>();
if ( save )
{
// CSRF has to be done manually.
if ( csrfCookie == null || csrfParam == null || !csrfCookie.getValue().equals( csrfParam ) )
{
errors.put( "csrf", "CSRF Failure!" );
}
if ( exampleText == null || exampleText.isEmpty() )
{
errors.put( "exampleText", "" );
}
if ( errors.isEmpty() )
{
response.sendRedirect( "dbcdc-page.jsp?settingsSaved=true" );
return;
}
}
final String csrf = StringUtils.randomString( 15 );
CookieUtils.setCookie( request, response, "csrf", csrf, -1 );
pageContext.setAttribute( "csrf", csrf );
pageContext.setAttribute( "errors", errors );
pageContext.setAttribute( "exampleText", exampleText );
%>
<html>
<head>
<title><fmt:message key="dbcdcplugin.title" /></title>
<meta name="pageID" content="dbcdc-plugin-page"/>
</head>
<body>
<c:choose>
<c:when test="${not empty param.settingsSaved and empty errors}">
<admin:infoBox type="success"><fmt:message key="dbcdcplugin.saved.success" /></admin:infoBox>
</c:when>
<c:otherwise>
<c:forEach var="err" items="${errors}">
<admin:infobox type="error">
<c:choose>
<c:when test="${err.key eq 'exampleText'}"><fmt:message key="dbcdcplugin.text.missing"/></c:when>
<c:otherwise>
<c:if test="${not empty err.value}">
<c:out value="${err.value}"/>
</c:if>
(<c:out value="${err.key}"/>)
</c:otherwise>
</c:choose>
</admin:infobox>
</c:forEach>
</c:otherwise>
</c:choose>
<p><fmt:message key="dbcdcplugin.directions" /></p>
<form action="dbcdc-page.jsp?save" method="post">
<fmt:message key="dbcdcplugin.options" var="boxtitle"/>
<admin:contentBox title="${boxtitle}">
<table cellpadding="3" cellspacing="0" border="0" width="100%">
<tr>
<td width="5%" valign="top"><fmt:message key="dbcdcplugin.text_label" />: *</td>
<td width="95%"><textarea cols="120" rows="20" wrap="virtual" name="exampletext"><c:out value="${exampleText}"/></textarea></td>
</tr>
<tr>
<td colspan="2" style="padding-top: 10px"><input type="submit" value="<fmt:message key="dbcdcplugin.button.save" />"/></td>
</tr>
</table>
</admin:contentBox>
<span class="jive-description">
* <fmt:message key="dbcdcplugin.required" />
</span>
<input type="hidden" name="csrf" value="${csrf}">
</form>
</body>
</html>
多语言配置dbcdc_i18n.properties
dbcdcplugin.title=数据同步插件
dbcdcplugin.options=Example Plugin Options
dbcdcplugin.saved.success=Settings saved successfully.
dbcdcplugin.required=Required fields.
dbcdcplugin.directions=Enter the Example Text
dbcdcplugin.text_label=XML
dbcdcplugin.text.missing=Please enter the dbcdc text!
dbcdcplugin.button.save=Save Settings
2.4、上传插件
在命令行下输入:mvn package,将会生成dbcdc-openfire-plugin-assembly.jar插件包,把插件包名改为dbcdc.jar,通过后台上传安装。
访问:http://localhost:9090/plugin-admin.jsp 上传。