1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-22 03:52:06 +01:00

Make Agent an EntityBareJid in smackx.workgroup

This commit is contained in:
Florian Schmaus 2018-04-18 08:27:10 +02:00
parent f69cd55970
commit 96046a063a
7 changed files with 55 additions and 49 deletions

View file

@ -38,6 +38,7 @@ import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smackx.workgroup.packet.AgentStatus;
import org.jivesoftware.smackx.workgroup.packet.AgentStatusRequest;
import org.jxmpp.jid.EntityBareJid;
import org.jxmpp.jid.EntityFullJid;
import org.jxmpp.jid.Jid;
import org.jxmpp.jid.impl.JidCreate;
@ -58,7 +59,7 @@ public class AgentRoster {
private final XMPPConnection connection;
private final Jid workgroupJID;
private final List<String> entries = new ArrayList<>();
private final List<EntityBareJid> entries = new ArrayList<>();
private final List<AgentRosterListener> listeners = new ArrayList<>();
private final Map<Jid, Map<Resourcepart, Presence>> presenceMap = new HashMap<>();
// The roster is marked as initialized when at least a single roster packet
@ -113,8 +114,7 @@ public class AgentRoster {
listeners.add(listener);
// Fire events for the existing entries and presences in the roster
for (Iterator<String> it = getAgents().iterator(); it.hasNext();) {
String jid = it.next();
for (EntityBareJid jid : getAgents()) {
// Check again in case the agent is no longer in the roster (highly unlikely
// but possible)
if (entries.contains(jid)) {
@ -167,10 +167,10 @@ public class AgentRoster {
*
* @return all entries in the roster.
*/
public Set<String> getAgents() {
Set<String> agents = new HashSet<>();
public Set<EntityBareJid> getAgents() {
Set<EntityBareJid> agents = new HashSet<>();
synchronized (entries) {
for (Iterator<String> i = entries.iterator(); i.hasNext();) {
for (Iterator<EntityBareJid> i = entries.iterator(); i.hasNext();) {
agents.add(i.next());
}
}
@ -185,14 +185,13 @@ public class AgentRoster {
* or "user@domain/resource").
* @return true if the XMPP address is an agent in the workgroup.
*/
@SuppressWarnings("EqualsIncompatibleType")
public boolean contains(Jid jid) {
if (jid == null) {
return false;
}
synchronized (entries) {
for (Iterator<String> i = entries.iterator(); i.hasNext();) {
String entry = i.next();
for (Iterator<EntityBareJid> i = entries.iterator(); i.hasNext();) {
EntityBareJid entry = i.next();
if (entry.equals(jid)) {
return true;
}
@ -278,10 +277,10 @@ public class AgentRoster {
for (int i = 0; i < listeners.length; i++) {
switch (eventType) {
case EVENT_AGENT_ADDED:
listeners[i].agentAdded((String) eventObject);
listeners[i].agentAdded((EntityBareJid) eventObject);
break;
case EVENT_AGENT_REMOVED:
listeners[i].agentRemoved((String) eventObject);
listeners[i].agentRemoved((EntityBareJid) eventObject);
break;
case EVENT_PRESENCE_CHANGED:
listeners[i].presenceChanged((Presence) eventObject);
@ -335,8 +334,8 @@ public class AgentRoster {
}
// Fire an event.
synchronized (entries) {
for (Iterator<String> i = entries.iterator(); i.hasNext();) {
String entry = i.next();
for (Iterator<EntityBareJid> i = entries.iterator(); i.hasNext();) {
EntityBareJid entry = i.next();
if (entry.equals(key.asEntityBareJidIfPossible())) {
fireEvent(EVENT_PRESENCE_CHANGED, packet);
}
@ -356,8 +355,7 @@ public class AgentRoster {
}
// Fire an event.
synchronized (entries) {
for (Iterator<String> i = entries.iterator(); i.hasNext();) {
String entry = i.next();
for (EntityBareJid entry : entries) {
if (entry.equals(key.asEntityBareJidIfPossible())) {
fireEvent(EVENT_PRESENCE_CHANGED, packet);
}
@ -378,19 +376,12 @@ public class AgentRoster {
AgentStatusRequest statusRequest = (AgentStatusRequest) packet;
for (Iterator<AgentStatusRequest.Item> i = statusRequest.getAgents().iterator(); i.hasNext();) {
AgentStatusRequest.Item item = i.next();
String agentJID = item.getJID();
EntityBareJid agentJID = item.getJID();
if ("remove".equals(item.getType())) {
// Removing the user from the roster, so remove any presence information
// about them.
Jid agentJid;
try {
agentJid = JidCreate.from(agentJID);
}
catch (XmppStringprepException e) {
throw new IllegalStateException(e);
}
presenceMap.remove(agentJid.asBareJid());
presenceMap.remove(agentJID.asBareJid());
// Fire event for roster listeners.
fireEvent(EVENT_AGENT_REMOVED, agentJID);
}

View file

@ -19,15 +19,17 @@ package org.jivesoftware.smackx.workgroup.agent;
import org.jivesoftware.smack.packet.Presence;
import org.jxmpp.jid.EntityBareJid;
/**
* Agent roster listener.
* @author Matt Tucker
*/
public interface AgentRosterListener {
void agentAdded(String jid);
void agentAdded(EntityBareJid jid);
void agentRemoved(String jid);
void agentRemoved(EntityBareJid jid);
void presenceChanged(Presence presence);
}

View file

@ -891,7 +891,7 @@ public class AgentSession {
* @throws NotConnectedException
* @throws InterruptedException
*/
public AgentChatHistory getAgentHistory(String jid, int maxSessions, Date startDate) throws XMPPException, NotConnectedException, InterruptedException {
public AgentChatHistory getAgentHistory(EntityBareJid jid, int maxSessions, Date startDate) throws XMPPException, NotConnectedException, InterruptedException {
AgentChatHistory request;
if (startDate != null) {
request = new AgentChatHistory(jid, maxSessions, startDate);
@ -1015,7 +1015,7 @@ public class AgentSession {
* @throws NotConnectedException
* @throws InterruptedException
*/
public void sendRoomInvitation(RoomInvitation.Type type, String invitee, String sessionID, String reason) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
public void sendRoomInvitation(RoomInvitation.Type type, Jid invitee, String sessionID, String reason) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
final RoomInvitation invitation = new RoomInvitation(type, invitee, sessionID, reason);
IQ iq = new RoomInvitation.RoomInvitationIQ(invitation);
iq.setType(IQ.Type.set);

View file

@ -17,6 +17,9 @@
package org.jivesoftware.smackx.workgroup.agent;
import org.jxmpp.jid.EntityBareJid;
import org.jxmpp.jid.EntityJid;
/**
* Request sent by an agent to invite another agent or user.
*
@ -24,21 +27,21 @@ package org.jivesoftware.smackx.workgroup.agent;
*/
public class InvitationRequest extends OfferContent {
private final String inviter;
private final String room;
private final EntityJid inviter;
private final EntityBareJid room;
private final String reason;
public InvitationRequest(String inviter, String room, String reason) {
public InvitationRequest(EntityJid inviter, EntityBareJid room, String reason) {
this.inviter = inviter;
this.room = room;
this.reason = reason;
}
public String getInviter() {
public EntityJid getInviter() {
return inviter;
}
public String getRoom() {
public EntityBareJid getRoom() {
return room;
}

View file

@ -26,6 +26,7 @@ import java.util.List;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.provider.IQProvider;
import org.jxmpp.jid.EntityBareJid;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
@ -45,20 +46,20 @@ public class AgentChatHistory extends IQ {
*/
public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";
private String agentJID;
private EntityBareJid agentJID;
private int maxSessions;
private long startDate;
private final List<AgentChatSession> agentChatSessions = new ArrayList<>();
public AgentChatHistory(String agentJID, int maxSessions, Date startDate) {
public AgentChatHistory(EntityBareJid agentJID, int maxSessions, Date startDate) {
this();
this.agentJID = agentJID;
this.maxSessions = maxSessions;
this.startDate = startDate.getTime();
}
public AgentChatHistory(String agentJID, int maxSessions) {
public AgentChatHistory(EntityBareJid agentJID, int maxSessions) {
this();
this.agentJID = agentJID;
this.maxSessions = maxSessions;

View file

@ -25,7 +25,9 @@ import java.util.Set;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.provider.IQProvider;
import org.jivesoftware.smack.util.ParserUtils;
import org.jxmpp.jid.EntityBareJid;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
@ -90,17 +92,17 @@ public class AgentStatusRequest extends IQ {
public static class Item {
private final String jid;
private final EntityBareJid jid;
private final String type;
private final String name;
public Item(String jid, String type, String name) {
public Item(EntityBareJid jid, String type, String name) {
this.jid = jid;
this.type = type;
this.name = name;
}
public String getJID() {
public EntityBareJid getJID() {
return jid;
}
@ -139,7 +141,7 @@ public class AgentStatusRequest extends IQ {
private Item parseAgent(XmlPullParser parser) throws XmlPullParserException, IOException {
boolean done = false;
String jid = parser.getAttributeValue("", "jid");
EntityBareJid jid = ParserUtils.getBareJidAttribute(parser);
String type = parser.getAttributeValue("", "type");
String name = null;
while (!done) {

View file

@ -25,6 +25,10 @@ import org.jivesoftware.smack.packet.IQ.IQChildElementXmlStringBuilder;
import org.jivesoftware.smack.provider.ExtensionElementProvider;
import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jxmpp.jid.EntityBareJid;
import org.jxmpp.jid.EntityJid;
import org.jxmpp.jid.Jid;
import org.jxmpp.jid.impl.JidCreate;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
@ -53,11 +57,11 @@ public class RoomInvitation implements ExtensionElement {
* JID of the entity being invited. The entity could be another agent, user , a queue or a workgroup. In
* the case of a queue or a workgroup the server will select the best agent to invite.
*/
private String invitee;
private Jid invitee;
/**
* Full JID of the user that sent the invitation.
*/
private String inviter;
private EntityJid inviter;
/**
* ID of the session that originated the initial user request.
*/
@ -65,13 +69,13 @@ public class RoomInvitation implements ExtensionElement {
/**
* JID of the room to join if offer is accepted.
*/
private String room;
private EntityBareJid room;
/**
* Text provided by the inviter explaining the reason why the invitee is invited.
*/
private String reason;
public RoomInvitation(Type type, String invitee, String sessionID, String reason) {
public RoomInvitation(Type type, Jid invitee, String sessionID, String reason) {
this.type = type;
this.invitee = invitee;
this.sessionID = sessionID;
@ -91,11 +95,11 @@ public class RoomInvitation implements ExtensionElement {
return NAMESPACE;
}
public String getInviter() {
public EntityJid getInviter() {
return inviter;
}
public String getRoom() {
public EntityBareJid getRoom() {
return room;
}
@ -179,16 +183,19 @@ public class RoomInvitation implements ExtensionElement {
invitation.sessionID = parser.getAttributeValue("", "id");
}
else if ("invitee".equals(elementName)) {
invitation.invitee = parser.nextText();
String inviteeString = parser.nextText();
invitation.invitee = JidCreate.from(inviteeString);
}
else if ("inviter".equals(elementName)) {
invitation.inviter = parser.nextText();
String inviterString = parser.nextText();
invitation.inviter = JidCreate.entityFrom(inviterString);
}
else if ("reason".equals(elementName)) {
invitation.reason = parser.nextText();
}
else if ("room".equals(elementName)) {
invitation.room = parser.nextText();
String roomString = parser.nextText();
invitation.room = JidCreate.entityBareFrom(roomString);
}
}
else if (parser.getEventType() == XmlPullParser.END_TAG && ELEMENT_NAME.equals(elementName)) {