SPARK-2355: Use getExtension(Class) and hasExtension(Class)

This commit is contained in:
Sergey Ponomarev 2025-10-04 20:54:48 +03:00 committed by Guus der Kinderen
parent efda43bf5d
commit dc0f625f25
19 changed files with 31 additions and 41 deletions

View File

@ -46,6 +46,7 @@ import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smackx.debugger.EnhancedDebuggerWindow; import org.jivesoftware.smackx.debugger.EnhancedDebuggerWindow;
import org.jivesoftware.smackx.delay.packet.DelayInformation; import org.jivesoftware.smackx.delay.packet.DelayInformation;
import org.jivesoftware.smackx.jiveproperties.packet.JivePropertiesExtension; import org.jivesoftware.smackx.jiveproperties.packet.JivePropertiesExtension;
import org.jivesoftware.smackx.muc.packet.GroupChatInvitation;
import org.jivesoftware.smackx.vcardtemp.packet.VCard; import org.jivesoftware.smackx.vcardtemp.packet.VCard;
import org.jivesoftware.spark.component.tabbedPane.SparkTabbedPane; import org.jivesoftware.spark.component.tabbedPane.SparkTabbedPane;
import org.jivesoftware.spark.filetransfer.SparkTransferManager; import org.jivesoftware.spark.filetransfer.SparkTransferManager;
@ -347,7 +348,7 @@ public class Workspace extends JPanel implements StanzaListener {
boolean isGroupChat = message.getType() == Message.Type.groupchat; boolean isGroupChat = message.getType() == Message.Type.groupchat;
// Check if Conference invite. If so, do not handle here. // Check if Conference invite. If so, do not handle here.
if (message.hasExtension("x", "jabber:x:conference")) { if (message.hasExtension(GroupChatInvitation.class)) {
return; return;
} }
@ -356,7 +357,7 @@ public class Workspace extends JPanel implements StanzaListener {
final boolean broadcast = extension != null && extension.getProperty( "broadcast" ) != null; final boolean broadcast = extension != null && extension.getProperty( "broadcast" ) != null;
// Handle offline message. // Handle offline message.
DelayInformation offlineInformation = message.getExtension("delay", "urn:xmpp:delay"); DelayInformation offlineInformation = message.getExtension(DelayInformation.class);
if (offlineInformation != null && (Message.Type.chat == message.getType() || if (offlineInformation != null && (Message.Type.chat == message.getType() ||
Message.Type.normal == message.getType())) { Message.Type.normal == message.getType())) {
handleOfflineMessage(message); handleOfflineMessage(message);

View File

@ -341,25 +341,19 @@ public class ContactItem extends JPanel {
* @param presence the presence. * @param presence the presence.
*/ */
public void setPresence(Presence presence) { public void setPresence(Presence presence) {
this.presence = presence; this.presence = presence;
final VCardUpdateExtension extension = presence.getExtension(VCardUpdateExtension.class);
final VCardUpdateExtension extension = presence.getExtension("x", "vcard-temp:x:update");
// Handle vCard update packet. // Handle vCard update packet.
if (extension != null) { if (extension != null) {
String hash = extension.getPhotoHash(); String hash = extension.getPhotoHash();
if (hash != null) { if (hash != null) {
this.hash = hash; this.hash = hash;
if (!hashExists(hash)) { if (!hashExists(hash)) {
updateAvatar(); updateAvatar();
updateAvatarInSideIcon(); updateAvatarInSideIcon();
} }
} }
} }
updatePresenceIcon(presence); updatePresenceIcon(presence);
} }

View File

@ -247,7 +247,7 @@ public class TranscriptWindow extends ChatArea implements ContextMenuListener
String body = message.getBody(); String body = message.getBody();
// Verify the timestamp of this message. Determine if it is a 'live' message, or one that was sent earlier. // Verify the timestamp of this message. Determine if it is a 'live' message, or one that was sent earlier.
final DelayInformation inf = message.getExtension( "delay", "urn:xmpp:delay" ); final DelayInformation inf = message.getExtension(DelayInformation.class);
final ZonedDateTime sentDate; final ZonedDateTime sentDate;
final boolean isDelayed; final boolean isDelayed;
if ( inf != null ) if ( inf != null )

View File

@ -194,7 +194,7 @@ public class GroupChatParticipantList extends JPanel {
// When joining a room, check if the current user is an owner/admin. If so, the UI should allow the current // When joining a room, check if the current user is an owner/admin. If so, the UI should allow the current
// user to change settings of this MUC. // user to change settings of this MUC.
final MUCUser mucUserEx = p.getExtension(MUCUser.ELEMENT, MUCUser.NAMESPACE); final MUCUser mucUserEx = p.getExtension(MUCUser.class);
if ( mucUserEx != null && mucUserEx.getStatus().contains(MUCUser.Status.create(110)) ) // 110 = Inform user that presence refers to itself if ( mucUserEx != null && mucUserEx.getStatus().contains(MUCUser.Status.create(110)) ) // 110 = Inform user that presence refers to itself
{ {
final MUCItem item = mucUserEx.getItem(); final MUCItem item = mucUserEx.getItem();
@ -270,7 +270,7 @@ public class GroupChatParticipantList extends JPanel {
MUCAffiliation affiliation = null; MUCAffiliation affiliation = null;
MUCRole role = null; MUCRole role = null;
final MUCUser extension = (MUCUser) presence.getExtension( MUCUser.NAMESPACE ); final MUCUser extension = presence.getExtension(MUCUser.class);
if ( extension != null && extension.getItem() != null ) if ( extension != null && extension.getItem() != null )
{ {
affiliation = extension.getItem().getAffiliation(); affiliation = extension.getItem().getAffiliation();

View File

@ -521,7 +521,7 @@ public class ChatRoomImpl extends ChatRoom {
} }
// Do not Handle offline messages. Offline messages are handling by Workspace. // Do not Handle offline messages. Offline messages are handling by Workspace.
if (message.hasExtension("delay", "urn:xmpp:delay") && if (message.hasExtension(DelayInformation.class) &&
(message.getType() == Message.Type.chat || (message.getType() == Message.Type.chat ||
message.getType() == Message.Type.normal)) { message.getType() == Message.Type.normal)) {
return; return;
@ -601,7 +601,7 @@ public class ChatRoomImpl extends ChatRoom {
public void insertMessage(Message message) { public void insertMessage(Message message) {
// Debug info // Debug info
super.insertMessage(message); super.insertMessage(message);
MessageEvent messageEvent = message.getExtension("x", "jabber:x:event"); MessageEvent messageEvent = message.getExtension(MessageEvent.class);
if (messageEvent != null) { if (messageEvent != null) {
checkEvents(message.getFrom(), message.getStanzaId(), messageEvent); checkEvents(message.getFrom(), message.getStanzaId(), messageEvent);
} }

View File

@ -614,7 +614,7 @@ public class GroupChatRoom extends ChatRoom
lastMessage = message; lastMessage = message;
if ( message.getType() == Message.Type.groupchat ) if ( message.getType() == Message.Type.groupchat )
{ {
final DelayInformation inf = message.getExtension( "delay", "urn:xmpp:delay" ); final DelayInformation inf = message.getExtension(DelayInformation.class);
final Date sentDate = inf != null ? inf.getStamp() : new Date(); final Date sentDate = inf != null ? inf.getStamp() : new Date();
// Do not accept Administrative messages. // Do not accept Administrative messages.
@ -725,7 +725,7 @@ public class GroupChatRoom extends ChatRoom
final Resourcepart nickname = from.getResourcepart(); final Resourcepart nickname = from.getResourcepart();
final MUCUser mucUser = stanza.getExtension( "x", "http://jabber.org/protocol/muc#user" ); final MUCUser mucUser = stanza.getExtension(MUCUser.class);
final Set<MUCUser.Status> status = new HashSet<>(); final Set<MUCUser.Status> status = new HashSet<>();
if ( mucUser != null ) if ( mucUser != null )
{ {

View File

@ -76,6 +76,7 @@ import org.jivesoftware.sparkimpl.plugin.privacy.list.SparkPrivacyList;
import org.jivesoftware.sparkimpl.profile.VCardEditor; import org.jivesoftware.sparkimpl.profile.VCardEditor;
import org.jivesoftware.sparkimpl.profile.VCardListener; import org.jivesoftware.sparkimpl.profile.VCardListener;
import org.jivesoftware.sparkimpl.profile.VCardManager; import org.jivesoftware.sparkimpl.profile.VCardManager;
import org.jivesoftware.sparkimpl.profile.ext.VCardUpdateExtension;
import org.jivesoftware.sparkimpl.settings.local.SettingsManager; import org.jivesoftware.sparkimpl.settings.local.SettingsManager;
//TODO: I need to remove the presence logic from this class. //TODO: I need to remove the presence logic from this class.
@ -391,7 +392,7 @@ public class StatusBar extends JPanel implements VCardListener {
} }
if ((presence.getMode() == currentPresence.getMode()) && (presence.getType() == currentPresence.getType()) && (presence.getStatus().equals(currentPresence.getStatus()))) { if ((presence.getMode() == currentPresence.getMode()) && (presence.getType() == currentPresence.getType()) && (presence.getStatus().equals(currentPresence.getStatus()))) {
if (presence.hasExtension("x", "vcard-temp:x:update")) { if (presence.hasExtension(VCardUpdateExtension.class)) {
// Update VCard // Update VCard
loadVCard(); loadVCard();
} }

View File

@ -201,9 +201,8 @@ public class BroadcastPlugin extends SparkTabHandler implements Plugin, StanzaLi
SwingUtilities.invokeLater(() -> { SwingUtilities.invokeLater(() -> {
try { try {
final Message message = (Message) stanza; final Message message = (Message) stanza;
// Do not handle errors or offline messages // Do not handle errors or offline messages
if (message.hasExtension("delay", "urn:xmpp:delay") || message.getError() != null) { if (message.hasExtension(DelayInformation.class) || message.getError() != null) {
return; return;
} }
@ -336,7 +335,7 @@ public class BroadcastPlugin extends SparkTabHandler implements Plugin, StanzaLi
SparkManager.getChatManager().fireGlobalMessageReceievedListeners(chatRoom, message); SparkManager.getChatManager().fireGlobalMessageReceievedListeners(chatRoom, message);
if (message.hasExtension("delay", "urn:xmpp:delay")) { if (message.hasExtension(DelayInformation.class)) {
SoundPreference soundPreference = (SoundPreference) SparkManager.getPreferenceManager().getPreference(new SoundPreference().getNamespace()); SoundPreference soundPreference = (SoundPreference) SparkManager.getPreferenceManager().getPreference(new SoundPreference().getNamespace());
SoundPreferences preferences = soundPreference.getPreferences(); SoundPreferences preferences = soundPreference.getPreferences();
if (preferences.isPlayIncomingSound()) { if (preferences.isPlayIncomingSound()) {

View File

@ -74,7 +74,7 @@ public class SoundPlugin implements Plugin, MessageListener, ChatRoomListener {
@Override @Override
public void messageReceived(ChatRoom room, Message message) { public void messageReceived(ChatRoom room, Message message) {
// Do not play sounds on history updates. // Do not play sounds on history updates.
if (message.hasExtension("delay", "urn:xmpp:delay")) { if (message.hasExtension(DelayInformation.class)) {
return; return;
} }

View File

@ -81,8 +81,7 @@ public class GUI extends JPanel implements Observer{
@Override @Override
public void processPacket(Stanza stanza) { public void processPacket(Stanza stanza) {
MovePacket move = (MovePacket) stanza.getExtension( MovePacket move = stanza.getExtension(MovePacket.class);
MovePacket.ELEMENT_NAME, MovePacket.NAMESPACE);
if (move.getGameID() == _gameID) { if (move.getGameID() == _gameID) {
boolean opponentMadeHit = _gameboard.placeBomb(move.getPositionX(), move.getPositionY()); boolean opponentMadeHit = _gameboard.placeBomb(move.getPositionX(), move.getPositionY());

View File

@ -41,9 +41,10 @@ import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.Presence; import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smackx.muc.MultiUserChat; import org.jivesoftware.smackx.muc.MultiUserChat;
import org.jivesoftware.smackx.workgroup.packet.QueueOverview;
import org.jivesoftware.smackx.workgroup.packet.WorkgroupInformation;
import org.jivesoftware.smackx.xdata.FormField; import org.jivesoftware.smackx.xdata.FormField;
import org.jivesoftware.smackx.xdata.form.FillableForm; import org.jivesoftware.smackx.xdata.form.FillableForm;
import org.jivesoftware.smackx.xdata.form.FilledForm; import org.jivesoftware.smackx.xdata.form.FilledForm;
@ -269,11 +270,9 @@ public class WorkgroupManager {
public void handleContactItem(final ContactItem contactItem) { public void handleContactItem(final ContactItem contactItem) {
Presence presence = contactItem.getPresence(); Presence presence = contactItem.getPresence();
// TODO It probably can be WorkgroupInformation.class but it has namespace http://jabber.org/protocol/workgroup // TODO It probably can be WorkgroupInformation.class but it has namespace http://jabber.org/protocol/workgroup
boolean hasWorkgroup = presence.hasExtension("workgroup", "http://jivesoftware.com/protocol/workgroup"); boolean hasWorkgroup = presence.hasExtension("workgroup", "http://jivesoftware.com/protocol/workgroup");
boolean hasNotifyQueue = presence.hasExtension("notify-queue", "http://jabber.org/protocol/workgroup"); boolean hasNotifyQueue = presence.hasExtension(QueueOverview.class);
if (!hasWorkgroup && !hasNotifyQueue) { if (!hasWorkgroup && !hasNotifyQueue) {
return; return;
} }
@ -404,7 +403,7 @@ public class WorkgroupManager {
public boolean handleInvitation(final XMPPConnection conn, final MultiUserChat room, final EntityBareJid inviter, final String reason, final String password, final Message message) { public boolean handleInvitation(final XMPPConnection conn, final MultiUserChat room, final EntityBareJid inviter, final String reason, final String password, final Message message) {
invites.add(inviter); invites.add(inviter);
if (message.hasExtension("workgroup", "http://jabber.org/protocol/workgroup")) { if (message.hasExtension(WorkgroupInformation.class)) {
Localpart workgroupName = inviter.getLocalpart(); Localpart workgroupName = inviter.getLocalpart();
GroupChatRoom groupChatRoom = ConferenceUtils.enterRoomOnSameThread(workgroupName, room.getRoom(), null, password); GroupChatRoom groupChatRoom = ConferenceUtils.enterRoomOnSameThread(workgroupName, room.getRoom(), null, password);

View File

@ -614,7 +614,7 @@ public class Workpane {
return true; return true;
} }
else if (message != null) { else if (message != null) {
MetaData metaDataExt = message.getExtension(MetaData.ELEMENT_NAME, MetaData.NAMESPACE); MetaData metaDataExt = message.getExtension(MetaData.class);
if (metaDataExt != null) { if (metaDataExt != null) {
Map<String, List<String>> metadata = metaDataExt.getMetaData(); Map<String, List<String>> metadata = metaDataExt.getMetaData();
List<String> values = new ArrayList<>(); List<String> values = new ArrayList<>();

View File

@ -186,7 +186,7 @@ public final class AgentConversations extends JPanel implements ChangeListener {
@Override @Override
public void presenceChanged(Presence presence) { public void presenceChanged(Presence presence) {
EntityBareJid agentJID = presence.getFrom().asEntityBareJidOrThrow(); EntityBareJid agentJID = presence.getFrom().asEntityBareJidOrThrow();
AgentStatus agentStatus = presence.getExtension("agent-status", "http://jabber.org/protocol/workgroup"); AgentStatus agentStatus = presence.getExtension(AgentStatus.class);
if (agentStatus != null) { if (agentStatus != null) {
List<AgentStatus.ChatInfo> list = agentStatus.getCurrentChats(); List<AgentStatus.ChatInfo> list = agentStatus.getCurrentChats();
@ -231,7 +231,7 @@ public final class AgentConversations extends JPanel implements ChangeListener {
for (EntityBareJid agent : agentRoster.getAgents()) { for (EntityBareJid agent : agentRoster.getAgents()) {
Presence presence = agentRoster.getPresence(agent); Presence presence = agentRoster.getPresence(agent);
if (presence.isAvailable()) { if (presence.isAvailable()) {
AgentStatus agentStatus = presence.getExtension("agent-status", "http://jabber.org/protocol/workgroup"); AgentStatus agentStatus = presence.getExtension(AgentStatus.class);
if (agentStatus != null) { if (agentStatus != null) {
counter += agentStatus.getCurrentChats().size(); counter += agentStatus.getCurrentChats().size();
} }

View File

@ -73,7 +73,7 @@ public class ChatViewer extends JPanel {
if (stanza instanceof Message) { if (stanza instanceof Message) {
Message message = (Message) stanza; Message message = (Message) stanza;
String from = message.getFrom().getResourceOrThrow().toString(); String from = message.getFrom().getResourceOrThrow().toString();
DelayInformation delayInformation = message.getExtension("delay", "urn:xmpp:delay"); DelayInformation delayInformation = message.getExtension(DelayInformation.class);
Date stamp = null; Date stamp = null;
if (delayInformation != null) { if (delayInformation != null) {
stamp = delayInformation.getStamp(); stamp = delayInformation.getStamp();

View File

@ -170,8 +170,7 @@ public final class CurrentActivity extends JPanel {
@Override @Override
public void presenceChanged(Presence presence) { public void presenceChanged(Presence presence) {
BareJid agentJID = presence.getFrom().asBareJid(); BareJid agentJID = presence.getFrom().asBareJid();
AgentStatus agentStatus = presence.getExtension("agent-status", "http://jabber.org/protocol/workgroup"); AgentStatus agentStatus = presence.getExtension(AgentStatus.class);
if (agentStatus != null) { if (agentStatus != null) {
List<ChatInfo> list = agentStatus.getCurrentChats(); List<ChatInfo> list = agentStatus.getCurrentChats();

View File

@ -124,7 +124,7 @@ public class InvitationPane {
transcriptAlert.add(new JLabel(), new GridBagConstraints(2, 5, 1, 1, 0.0, 1.0, GridBagConstraints.SOUTH, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0)); transcriptAlert.add(new JLabel(), new GridBagConstraints(2, 5, 1, 1, 0.0, 1.0, GridBagConstraints.SOUTH, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0));
MetaData metaDataExt = message.getExtension(MetaData.ELEMENT_NAME, MetaData.NAMESPACE); MetaData metaDataExt = message.getExtension(MetaData.class);
if (metaDataExt != null) { if (metaDataExt != null) {
metadata = metaDataExt.getMetaData(); metadata = metaDataExt.getMetaData();
} }

View File

@ -209,7 +209,7 @@ public final class OnlineAgents extends JPanel {
return FpRes.getString("message.user.not.logged.in"); return FpRes.getString("message.user.not.logged.in");
} }
AgentStatus agentStatus = presence.getExtension("agent-status", "http://jabber.org/protocol/workgroup"); AgentStatus agentStatus = presence.getExtension(AgentStatus.class);
List<AgentStatus.ChatInfo> list = agentStatus.getCurrentChats(); List<AgentStatus.ChatInfo> list = agentStatus.getCurrentChats();
// Add new ones. // Add new ones.

View File

@ -93,7 +93,7 @@ public class ReversiPanel extends JPanel {
if (connection != null) { if (connection != null) {
gameMoveListener = stanza -> { gameMoveListener = stanza -> {
GameMove move = stanza.getExtension(GameMove.ELEMENT_NAME, GameMove.NAMESPACE); GameMove move = stanza.getExtension(GameMove.class);
// If this is a move for the current game. // If this is a move for the current game.
if (move.getGameID() == gameID) { if (move.getGameID() == gameID) {
int position = move.getPosition(); int position = move.getPosition();

View File

@ -86,9 +86,7 @@ public class GamePanel extends JPanel {
add(_playerdisplay, BorderLayout.SOUTH); add(_playerdisplay, BorderLayout.SOUTH);
_connection.addAsyncStanzaListener(stanza -> { _connection.addAsyncStanzaListener(stanza -> {
MovePacket move = stanza.getExtension( MovePacket move = stanza.getExtension(MovePacket.class);
MovePacket.ELEMENT_NAME, MovePacket.NAMESPACE);
if (move.getGameID() == _gameID) { if (move.getGameID() == _gameID) {
if (_gameboard.isValidMove(getYourMark(), move.getPositionX(), move.getPositionY())) { if (_gameboard.isValidMove(getYourMark(), move.getPositionX(), move.getPositionY())) {
_gameboardpanel.placeMark(getYourMark(), move.getPositionX(), move.getPositionY()); _gameboardpanel.placeMark(getYourMark(), move.getPositionX(), move.getPositionY());
@ -109,7 +107,7 @@ public class GamePanel extends JPanel {
}, new StanzaExtensionFilter(MovePacket.ELEMENT_NAME, MovePacket.NAMESPACE)); }, new StanzaExtensionFilter(MovePacket.ELEMENT_NAME, MovePacket.NAMESPACE));
_connection.addAsyncStanzaListener(stanza -> { _connection.addAsyncStanzaListener(stanza -> {
//InvalidMove im = (InvalidMove)packet.getExtension(InvalidMove.ELEMENT_NAME, InvalidMove.NAMESPACE); //InvalidMove im = packet.getExtension(InvalidMove.class);
ChatRoom cr = SparkManager.getChatManager().getChatRoom(_opponent.asEntityBareJid()); ChatRoom cr = SparkManager.getChatManager().getChatRoom(_opponent.asEntityBareJid());
cr.getTranscriptWindow().insertCustomText("You seem to be Cheating\n" + cr.getTranscriptWindow().insertCustomText("You seem to be Cheating\n" +
"You placed a wrong Move", true, false, Color.red); "You placed a wrong Move", true, false, Color.red);