📄 workgrouppresence.java
字号:
/**
* $RCSfile$
* $Revision: 32833 $
* $Date: 2006-08-02 15:52:36 -0700 (Wed, 02 Aug 2006) $
*
* Copyright (C) 2004-2008 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution, or a commercial license
* agreement with Jive.
*/
package org.jivesoftware.xmpp.workgroup;
import org.jivesoftware.openfire.fastpath.util.TaskEngine;
import org.jivesoftware.xmpp.workgroup.interceptor.AgentInterceptorManager;
import org.jivesoftware.xmpp.workgroup.interceptor.InterceptorManager;
import org.jivesoftware.xmpp.workgroup.interceptor.PacketRejectedException;
import org.dom4j.Element;
import org.jivesoftware.database.DbConnectionManager;
import org.jivesoftware.util.ConcurrentHashSet;
import org.jivesoftware.util.FastDateFormat;
import org.jivesoftware.util.JiveConstants;
import org.xmpp.component.ComponentManagerFactory;
import org.xmpp.packet.JID;
import org.xmpp.packet.PacketError;
import org.xmpp.packet.Presence;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* <p>The Workgroup's presence handler processes all incoming
* presence packets sent to the workgroup.</p>
* <p/>
* <p>Currently the workgroup recognizes:</p>
* <ul>
* <li>Presence from Agents</li>
* <ul>
* <li>available - Join the workgoup and create agent session</li>
* <li>unavailable - Depart the workgroup and delete agent session</li>
* </ul>
* </ul>
*
* @author Derek DeMoro
*/
public class WorkgroupPresence {
private static final FastDateFormat UTC_FORMAT = FastDateFormat
.getInstance(JiveConstants.XMPP_DELAY_DATETIME_FORMAT, TimeZone.getTimeZone("UTC"));
private static final String LOAD_ROSTER =
"SELECT jid FROM fpWorkgroupRoster WHERE workgroupID=?";
private static final String CREATE_ROSTER_ITEM =
"INSERT INTO fpWorkgroupRoster (workgroupID, jid) VALUES (?, ?)";
private static final String DELETE_ROSTER_ITEM =
"DELETE FROM fpWorkgroupRoster WHERE workgroupID=? AND jid=?";
private static final String DELETE_ROSTER_ITEMS =
"DELETE FROM fpWorkgroupRoster WHERE workgroupID=?";
private Workgroup workgroup;
/**
* Holds the JID of users that sent an available presence to the workgroup as a way to be
* notified when the workgroup changes his presence status. This is like a temporary way
* to susbcribe to the workgroup presence. The temporary subscription will be removed
* when an unavailable presence is sent to the workgroup.
*/
private final Set<JID> listeners = new ConcurrentHashSet<JID>();
/**
* Holds the bare JID address of all the users that subscribed to the presence of the
* workgroup. Users may want to track the presence of the workgroup to find out when agents
* are available. Users that send a presence subscribe packet will automatically get their
* requests accepted.
*/
private List<String> presenceSubscribers = new CopyOnWriteArrayList<String>();
public WorkgroupPresence(Workgroup workgroup) {
this.workgroup = workgroup;
// Load the bare JIDs of the users that are interested in the workgroup presence
loadRosterItems();
}
public void process(Presence packet) {
try {
JID sender = packet.getFrom();
// Handling Subscription
if (Presence.Type.subscribe == packet.getType()) {
// User wants to track the workgroup presence so accept the request
// Add sender to the workgroup roster
// Only create item if user was not already subscribed to workgroup's presence
if (!presenceSubscribers.contains(sender.toBareJID())) {
createRosterItem(sender);
}
// Reply that the subscription request was approved
Presence reply = new Presence();
reply.setTo(sender);
reply.setFrom(workgroup.getJID());
reply.setType(Presence.Type.subscribed);
workgroup.send(reply);
// Send the presence of the workgroup to the user that requested it
sendPresence(packet.getFrom());
}
else if (Presence.Type.unsubscribe == packet.getType()) {
// Remove sender from the workgroup roster
deleteRosterItem(sender);
// Send confirmation of unsubscription
Presence reply = new Presence();
reply.setTo(sender);
reply.setFrom(workgroup.getJID());
reply.setType(Presence.Type.unsubscribed);
workgroup.send(reply);
// Send unavailable presence of the workgroup
reply = new Presence();
reply.setTo(sender);
reply.setFrom(workgroup.getJID());
reply.setType(Presence.Type.unavailable);
workgroup.send(reply);
}
else if (Presence.Type.subscribed == packet.getType()) {
// ignore
}
else if (Presence.Type.unsubscribed == packet.getType()) {
// ignore
}
else if (Presence.Type.probe == packet.getType()) {
// Send the presence of the workgroup to the user that requested it
sendPresence(packet.getFrom());
}
else {
try {
agentToWorkgroup(packet);
}
catch (AgentNotFoundException e) {
Presence reply = new Presence();
reply.setError(new PacketError(PacketError.Condition.not_authorized));
reply.setTo(packet.getFrom());
reply.setFrom(workgroup.getJID());
workgroup.send(reply);
StringBuilder errorMessage = new StringBuilder();
errorMessage.append("Sender: ");
errorMessage.append(packet.getFrom().toString());
errorMessage.append(" Workgroup: ");
errorMessage.append(workgroup.getJID().toString());
ComponentManagerFactory.getComponentManager().getLog().debug(errorMessage.toString(), e);
}
}
}
catch (Exception e) {
ComponentManagerFactory.getComponentManager().getLog().error(e);
Presence reply = new Presence();
reply.setError(new PacketError(PacketError.Condition.internal_server_error));
reply.setTo(packet.getFrom());
reply.setFrom(workgroup.getJID());
workgroup.send(reply);
}
}
/**
* Sends the presence of the workgroup to the specified JID address.
*
* @param address the XMPP address that will receive the presence of the workgroup.
*/
public void sendPresence(JID address) {
Presence presence = new Presence();
presence.setTo(address);
presence.setFrom(workgroup.getJID());
Presence.Type type;
if (workgroup.isAvailable()) {
type = null;
// Add the a child element that will contain information about the workgroup
Element child = presence.addChildElement("workgroup",
"http://jivesoftware.com/protocol/workgroup");
// Add the last modification date of the workgroup
child.addElement("lastModified").setText(UTC_FORMAT.format(workgroup.getModificationDate()));
}
else {
type = Presence.Type.unavailable;
// Add the a child element that will contain information about the workgroup
Element child = presence.addChildElement("workgroup",
"http://jivesoftware.com/protocol/workgroup");
// Add the last modification date of the workgroup
child.addElement("lastModified").setText(UTC_FORMAT.format(workgroup.getModificationDate()));
}
presence.setType(type);
workgroup.send(presence);
}
/**
* Send the workgroup presence to the users that subscribed to the workgroup's presence.
*/
void broadcastWorkgroupPresence() {
TaskEngine.getInstance().submit(new Runnable() {
public void run() {
try {
// Send the workgroup presence to the workgroup subscribers
for (String bareJID : presenceSubscribers) {
sendPresence(new JID(bareJID));
}
// Send the workgroup presence to the users that temporary subscribed
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -