openfire send message plug-in (description and source code)
Implementation function:
Send messages to users directly if they are online, and save them as offline messages when they are not online
Send a message to a group. If it is online, it will be sent directly. If it is not online, it will be saved as an offline message
The following is the plug-in configuration:
Description reference: http://www.igniterealtime.org/builds/openfire/docs/latest/documentation/plugin-dev-guide.html
<?xml version="1.0" encoding="UTF-8"?>
<plugin>
<class>org.jivesoftware.openfire.plugin.Msger</class>
<name>${msger.plugin.name}</name>
<description>${msger.plugin.description}</description>
<author>leon</author>
<version>1.0.0</version>
<date>2/20/2009</date>
<url>http://hi.baidu.com/jyleon</url>
<minServerVersion>3.6.3</minServerVersion>
<adminconsole>
<tab id="tab-session">
<sidebar id="sidebar-tools" description="${msger.sidebar.name}">
<item id="msger-send" name="${msger.sendToUser}" description="${msger.sendToUser.title}" url="msger-send.jsp"/>
<item id="msger-sendGroup" name="${msger.sendToGroup}" description="${msger.sendToGroup.title}" url="msger-sendGroup.jsp"/>
</sidebar>
</tab>
</adminconsole>
</plugin>
//Note that:
1,<sidebar id="sidebar-tools" description="${msger.sidebar.name}">
id="sidebar-tools" Put this plug-in in openfire Navigate under tools for sessions. ( openfire Navigation label reference admin-sidebar.xml ),${msger.sidebar.name} Internationalization settings, if not, can only be in English
2, <item id="msger-send" name="${msger.sendToUser}" description="${msger.sendToUser.title}" url="msger-send.jsp"/>
//The function is to add a new connection "send message to user" to connect to msger-send.jsp
//Note: openfire uses the Sitemesh page decoration framework, and the plug-in tag defines id = "MSG send". Then, the MSG send.jsp must be added in the head:<meta name="pageID" content="msger-send"/>,Otherwise, it will not display normally.
3, <minServerVersion>3.6.3</minServerVersion> Plug-in definition openfire The version of the service cannot be uploaded below 3.6.3 Version. If less than 3.6.3,Change here to and openfire The corresponding version number is enough.
4. jsp Code msger-send.jsp
< import="org.jivesoftware.openfire.XMPPServer"%>
<%@ page import="java.util.*" %>
<html>
<head>
<link rel="stylesheet" type="text/css" href="/style/global.css">
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt" %>
<title><fmt:message key="msger.sendToUser.title"/></title>
<meta name="pageID" content="msger-send"/>
</head>
<jsp:useBean id="webManager" class="org.jivesoftware.util.WebManager" />
<body>
<%@ page import="org.jivesoftware.openfire.PresenceManager,
org.jivesoftware.openfire.group.Group,
org.jivesoftware.openfire.group.GroupManager,
org.jivesoftware.openfire.user.User,
org.jivesoftware.openfire.user.UserManager,
org.jivesoftware.openfire.user.UserNotFoundException,
org.jivesoftware.util.ParamUtils,
org.jivesoftware.openfire.SessionManager,
org.xmpp.packet.JID,
org.xmpp.packet.Message,
java.util.Collection"
errorPage="error.jsp"
%>
<%
boolean send = ParamUtils.getBooleanParameter(request,"send");
String toUser = ParamUtils.getParameter(request,"toUser");
String fromUser = ParamUtils.getParameter(request,"fromUser");
String message = ParamUtils.getParameter(request,"message");
String success=null;
if (send) {
//send to user
SessionManager sessionManager = webManager.getSessionManager();
PresenceManager presenceManager = webManager.getPresenceManager();
UserManager userManager = webManager.getUserManager();
String serverDomainName =XMPPServer.getInstance().getServerInfo().getXMPPDomain();
User user=null;
JID toUserJid= null;
JID fromUserJid= null;
if(fromUser!=null)
fromUserJid=new JID(fromUser+"@"+serverDomainName);
else
fromUserJid=new JID(");
if(toUser!=null&&message!=null&&userManager.isRegisteredUser(toUser)==true){
try {
user = userManager.getUser(toUser);
if (presenceManager.isAvailable(user)) {
// online messages
toUserJid = presenceManager.getPresence(user).getFrom();
sessionManager.sendServerMessage(toUserJid , null, message);
success="true";
}else{
// offline message
toUserJid =new JID(toUser+"@"+serverDomainName);
if(toUserJid!=null){
Message msg = new Message();
msg.setBody(message);
msg.setTo(toUserJid);
msg.setFrom(fromUserJid);
XMPPServer.getInstance().getOfflineMessageStrategy().storeOffline(msg);
}
success="true";
}
}
catch (UserNotFoundException e) {
success="fail";
e.printStackTrace();
}
}else{
success="fail";
}
}
%>
<% if (success=="true") { %>
<div class="jive-success">
<table cellpadding="0" cellspacing="0" border="0">
<tbody>
<tr><td class="jive-icon"><img src="images/success-16x16.gif" width="16" height="16" border="0" alt=""></td>
<td class="jive-icon-label">
<fmt:message key="msger.sendReault.success" />
</td></tr>
</tbody>
</table>
</div><br>
<% }if(success=="fail"){ %>
<div class="jive-success">
<table cellpadding="0" cellspacing="0" border="0">
<tbody>
<tr><td class="jive-icon"><img src="images/error-16x16.gif" width="16" height="16" border="0" alt=""></td>
<td class="jive-icon-label">
<fmt:message key="msger.sendReault.fail" />! <fmt:message key="msger.sendReault.fail.nouser" /> , <fmt:message key="msger.sendReault.fail.noContent" />
</td></tr>
</tbody>
</table>
</div><br>
<%}%>
<table cellpadding="3">
<form action="msger-send.jsp" id="sendMsg" method="post">
<tr>
<td><fmt:message key="msger.sendToUser"/></td>
<td><input type="text" name="toUser" id="toUser"></td>
</tr>
<tr>
<td> <fmt:message key="msger.sendContent"/></td>
<td><textarea rows="10" name="message" cols="60"></textarea></td>
<tr>
<td></td>
<td>
<input type="submit" value="<fmt:message key="msger.send.submit"/>"><input type="hidden" name="send" value="true">
</td>
</tr>
</form>
</table>
</body>
</html>
openfire's plug-in development is a very interesting thing. It's just a summary and experience at the beginning of learning. It's suitable for novices. I hope more people will participate in the research.
Plug in download: Send message plug-in
https://download.csdn.net/download/jyleon/1288943
http://www.360doc.com/content/16/0316/22/16915_542809998.shtml