From 96046a063add57c20b0974f1aaeb6e758ff425e8 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Wed, 18 Apr 2018 08:27:10 +0200 Subject: [PATCH] Make Agent an EntityBareJid in smackx.workgroup --- .../smackx/workgroup/agent/AgentRoster.java | 39 +++++++------------ .../workgroup/agent/AgentRosterListener.java | 6 ++- .../smackx/workgroup/agent/AgentSession.java | 4 +- .../workgroup/agent/InvitationRequest.java | 13 ++++--- .../ext/history/AgentChatHistory.java | 7 ++-- .../workgroup/packet/AgentStatusRequest.java | 10 +++-- .../workgroup/packet/RoomInvitation.java | 25 +++++++----- 7 files changed, 55 insertions(+), 49 deletions(-) diff --git a/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/agent/AgentRoster.java b/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/agent/AgentRoster.java index 3824bdf44..71188d91a 100644 --- a/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/agent/AgentRoster.java +++ b/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/agent/AgentRoster.java @@ -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 entries = new ArrayList<>(); + private final List entries = new ArrayList<>(); private final List listeners = new ArrayList<>(); private final Map> 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 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 getAgents() { - Set agents = new HashSet<>(); + public Set getAgents() { + Set agents = new HashSet<>(); synchronized (entries) { - for (Iterator i = entries.iterator(); i.hasNext();) { + for (Iterator 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 i = entries.iterator(); i.hasNext();) { - String entry = i.next(); + for (Iterator 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 i = entries.iterator(); i.hasNext();) { - String entry = i.next(); + for (Iterator 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 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 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); } diff --git a/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/agent/AgentRosterListener.java b/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/agent/AgentRosterListener.java index 35747420f..e25efedc6 100644 --- a/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/agent/AgentRosterListener.java +++ b/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/agent/AgentRosterListener.java @@ -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); } diff --git a/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/agent/AgentSession.java b/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/agent/AgentSession.java index 55dafdce6..89f52a2c3 100644 --- a/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/agent/AgentSession.java +++ b/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/agent/AgentSession.java @@ -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); diff --git a/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/agent/InvitationRequest.java b/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/agent/InvitationRequest.java index 8b189013f..25739c493 100644 --- a/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/agent/InvitationRequest.java +++ b/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/agent/InvitationRequest.java @@ -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; } diff --git a/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/ext/history/AgentChatHistory.java b/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/ext/history/AgentChatHistory.java index 3e7829883..7f4eae815 100644 --- a/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/ext/history/AgentChatHistory.java +++ b/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/ext/history/AgentChatHistory.java @@ -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 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; diff --git a/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/packet/AgentStatusRequest.java b/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/packet/AgentStatusRequest.java index f26a0f041..291b16f89 100644 --- a/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/packet/AgentStatusRequest.java +++ b/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/packet/AgentStatusRequest.java @@ -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) { diff --git a/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/packet/RoomInvitation.java b/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/packet/RoomInvitation.java index 4275c5b30..2f9fdd244 100644 --- a/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/packet/RoomInvitation.java +++ b/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/packet/RoomInvitation.java @@ -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)) {