1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-09 00:59:39 +02:00

Introduce StanzaBuilder

As first step to immutable Stanza types.
This commit is contained in:
Florian Schmaus 2019-10-24 15:45:08 +02:00
parent 926c5892ad
commit 5db6191110
134 changed files with 2576 additions and 764 deletions

View file

@ -34,7 +34,7 @@ import org.jivesoftware.smack.filter.StanzaFilter;
import org.jivesoftware.smack.filter.StanzaTypeFilter;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.packet.StanzaBuilder;
import org.jivesoftware.smackx.workgroup.packet.AgentStatus;
import org.jivesoftware.smackx.workgroup.packet.AgentStatusRequest;
@ -213,8 +213,10 @@ public class AgentRoster {
Jid key = getPresenceMapKey(user);
Map<Resourcepart, Presence> userPresences = presenceMap.get(key);
if (userPresences == null) {
Presence presence = new Presence(Presence.Type.unavailable);
presence.setFrom(user);
Presence presence = StanzaBuilder.buildPresence()
.ofType(Presence.Type.unavailable)
.from(user)
.build();
return presence;
}
else {
@ -236,8 +238,7 @@ public class AgentRoster {
}
}
if (presence == null) {
presence = new Presence(Presence.Type.unavailable);
presence.setFrom(user);
presence = synthesizeUnvailablePresence(user);
return presence;
}
else {
@ -289,6 +290,13 @@ public class AgentRoster {
}
}
private static Presence synthesizeUnvailablePresence(Jid from) {
return StanzaBuilder.buildPresence()
.ofType(Presence.Type.unavailable)
.from(from)
.build();
}
/**
* Listens for all presence packets and processes them.
*/

View file

@ -45,9 +45,9 @@ import org.jivesoftware.smack.iqrequest.IQRequestHandler.Mode;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.PresenceBuilder;
import org.jivesoftware.smack.packet.StandardExtensionElement;
import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smackx.muc.packet.MUCUser;
import org.jivesoftware.smackx.search.ReportedData;
import org.jivesoftware.smackx.workgroup.MetaData;
@ -335,8 +335,11 @@ public class AgentSession {
// If the user is going online...
if (online) {
presence = new Presence(Presence.Type.available);
presence.setTo(workgroupJID);
presence = connection.getStanzaFactory().buildPresenceStanza()
.ofType(Presence.Type.available)
.to(workgroupJID)
.build();
presence.addExtension(new StandardExtensionElement(AgentStatus.ELEMENT_NAME,
AgentStatus.NAMESPACE));
@ -353,8 +356,10 @@ public class AgentSession {
// Update this iv now since we don't care at this point of any error
this.online = online;
presence = new Presence(Presence.Type.unavailable);
presence.setTo(workgroupJID);
presence = connection.getStanzaFactory().buildPresenceStanza()
.ofType(Presence.Type.unavailable)
.to(workgroupJID)
.build();
presence.addExtension(new StandardExtensionElement(AgentStatus.ELEMENT_NAME,
AgentStatus.NAMESPACE));
connection.sendStanza(presence);
@ -427,21 +432,21 @@ public class AgentSession {
this.presenceMode = presenceMode;
this.maxChats = maxChats;
Presence presence = new Presence(Presence.Type.available);
presence.setMode(presenceMode);
presence.setTo(this.getWorkgroupJID());
if (status != null) {
presence.setStatus(status);
}
PresenceBuilder presenceBuilder = connection.getStanzaFactory().buildPresenceStanza()
.ofType(Presence.Type.available)
.setMode(presenceMode)
.to(workgroupJID)
.setStatus(status)
;
// Send information about max chats and current chats as a packet extension.
StandardExtensionElement.Builder builder = StandardExtensionElement.builder(AgentStatus.ELEMENT_NAME,
AgentStatus.NAMESPACE);
builder.addElement("max_chats", Integer.toString(maxChats));
presence.addExtension(builder.build());
presence.addExtension(new MetaData(this.metaData));
presenceBuilder.addExtension(builder.build());
presenceBuilder.addExtension(new MetaData(this.metaData));
Presence presence = presenceBuilder.build();
StanzaCollector collector = this.connection.createStanzaCollectorAndSend(new AndFilter(
new StanzaTypeFilter(Presence.class),
FromMatchesFilter.create(workgroupJID)), presence);
@ -478,13 +483,16 @@ public class AgentSession {
}
this.presenceMode = presenceMode;
Presence presence = new Presence(Presence.Type.available);
presence.setMode(presenceMode);
presence.setTo(this.getWorkgroupJID());
PresenceBuilder presenceBuilder = connection.getStanzaFactory().buildPresenceStanza()
.ofType(Presence.Type.available)
.setMode(presenceMode)
.to(getWorkgroupJID());
if (status != null) {
presence.setStatus(status);
presenceBuilder.setStatus(status);
}
Presence presence = presenceBuilder.build();
presence.addExtension(new MetaData(this.metaData));
StanzaCollector collector = this.connection.createStanzaCollectorAndSend(new AndFilter(new StanzaTypeFilter(Presence.class),

View file

@ -188,8 +188,11 @@ public class Workgroup {
* @throws InterruptedException if the calling thread was interrupted.
*/
public boolean isAvailable() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
Presence directedPresence = new Presence(Presence.Type.available);
directedPresence.setTo(workgroupJID);
Presence directedPresence = connection.getStanzaFactory().buildPresenceStanza()
.ofType(Presence.Type.available)
.to(workgroupJID)
.build();
StanzaFilter typeFilter = new StanzaTypeFilter(Presence.class);
StanzaFilter fromFilter = FromMatchesFilter.create(workgroupJID);
StanzaCollector collector = connection.createStanzaCollectorAndSend(new AndFilter(fromFilter,

View file

@ -213,15 +213,18 @@ public final class MessageEventManager extends Manager {
* @throws InterruptedException if the calling thread was interrupted.
*/
public void sendDeliveredNotification(Jid to, String packetID) throws NotConnectedException, InterruptedException {
// Create the message to send
Message msg = new Message(to);
// Create a MessageEvent Package and add it to the message
MessageEvent messageEvent = new MessageEvent();
messageEvent.setDelivered(true);
messageEvent.setStanzaId(packetID);
msg.addExtension(messageEvent);
XMPPConnection connection = connection();
Message msg = connection.getStanzaFactory().buildMessageStanza()
.to(to)
.addExtension(messageEvent)
.build();
// Send the packet
connection().sendStanza(msg);
connection.sendStanza(msg);
}
/**
@ -233,15 +236,18 @@ public final class MessageEventManager extends Manager {
* @throws InterruptedException if the calling thread was interrupted.
*/
public void sendDisplayedNotification(Jid to, String packetID) throws NotConnectedException, InterruptedException {
// Create the message to send
Message msg = new Message(to);
// Create a MessageEvent Package and add it to the message
MessageEvent messageEvent = new MessageEvent();
messageEvent.setDisplayed(true);
messageEvent.setStanzaId(packetID);
msg.addExtension(messageEvent);
XMPPConnection connection = connection();
Message msg = connection.getStanzaFactory().buildMessageStanza()
.to(to)
.addExtension(messageEvent)
.build();
// Send the packet
connection().sendStanza(msg);
connection.sendStanza(msg);
}
/**
@ -253,15 +259,18 @@ public final class MessageEventManager extends Manager {
* @throws InterruptedException if the calling thread was interrupted.
*/
public void sendComposingNotification(Jid to, String packetID) throws NotConnectedException, InterruptedException {
// Create the message to send
Message msg = new Message(to);
// Create a MessageEvent Package and add it to the message
MessageEvent messageEvent = new MessageEvent();
messageEvent.setComposing(true);
messageEvent.setStanzaId(packetID);
msg.addExtension(messageEvent);
XMPPConnection connection = connection();
Message msg = connection.getStanzaFactory().buildMessageStanza()
.to(to)
.addExtension(messageEvent)
.build();
// Send the packet
connection().sendStanza(msg);
connection.sendStanza(msg);
}
/**
@ -273,13 +282,17 @@ public final class MessageEventManager extends Manager {
* @throws InterruptedException if the calling thread was interrupted.
*/
public void sendCancelledNotification(Jid to, String packetID) throws NotConnectedException, InterruptedException {
// Create the message to send
Message msg = new Message(to);
// Create a MessageEvent Package and add it to the message
MessageEvent messageEvent = new MessageEvent();
messageEvent.setCancelled(true);
messageEvent.setStanzaId(packetID);
msg.addExtension(messageEvent);
XMPPConnection connection = connection();
Message msg = connection.getStanzaFactory().buildMessageStanza()
.to(to)
.addExtension(messageEvent)
.build();
// Send the packet
connection().sendStanza(msg);
}

View file

@ -30,6 +30,7 @@ import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.StanzaExtensionFilter;
import org.jivesoftware.smack.filter.StanzaFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.MessageBuilder;
import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.roster.Roster;
import org.jivesoftware.smack.roster.RosterEntry;
@ -121,15 +122,16 @@ public class RosterExchangeManager {
* @throws InterruptedException if the calling thread was interrupted.
*/
public void send(Roster roster, Jid targetUserID) throws NotConnectedException, InterruptedException {
XMPPConnection connection = weakRefConnection.get();
// Create a new message to send the roster
Message msg = new Message(targetUserID);
MessageBuilder messageBuilder = connection.getStanzaFactory().buildMessageStanza().to(targetUserID);
// Create a RosterExchange Package and add it to the message
RosterExchange rosterExchange = new RosterExchange(roster);
msg.addExtension(rosterExchange);
messageBuilder.addExtension(rosterExchange);
XMPPConnection connection = weakRefConnection.get();
// Send the message that contains the roster
connection.sendStanza(msg);
connection.sendStanza(messageBuilder.build());
}
/**
@ -141,16 +143,17 @@ public class RosterExchangeManager {
* @throws InterruptedException if the calling thread was interrupted.
*/
public void send(RosterEntry rosterEntry, Jid targetUserID) throws NotConnectedException, InterruptedException {
XMPPConnection connection = weakRefConnection.get();
// Create a new message to send the roster
Message msg = new Message(targetUserID);
MessageBuilder messageBuilder = connection.getStanzaFactory().buildMessageStanza().to(targetUserID);
// Create a RosterExchange Package and add it to the message
RosterExchange rosterExchange = new RosterExchange();
rosterExchange.addRosterEntry(rosterEntry);
msg.addExtension(rosterExchange);
messageBuilder.addExtension(rosterExchange);
XMPPConnection connection = weakRefConnection.get();
// Send the message that contains the roster
connection.sendStanza(msg);
connection.sendStanza(messageBuilder.build());
}
/**
@ -163,8 +166,10 @@ public class RosterExchangeManager {
* @throws InterruptedException if the calling thread was interrupted.
*/
public void send(RosterGroup rosterGroup, Jid targetUserID) throws NotConnectedException, InterruptedException {
XMPPConnection connection = weakRefConnection.get();
// Create a new message to send the roster
Message msg = new Message(targetUserID);
MessageBuilder msg = connection.getStanzaFactory().buildMessageStanza().to(targetUserID);
// Create a RosterExchange Package and add it to the message
RosterExchange rosterExchange = new RosterExchange();
for (RosterEntry entry : rosterGroup.getEntries()) {
@ -172,9 +177,8 @@ public class RosterExchangeManager {
}
msg.addExtension(rosterExchange);
XMPPConnection connection = weakRefConnection.get();
// Send the message that contains the roster
connection.sendStanza(msg);
connection.sendStanza(msg.build());
}
/**