From 1b1e88c4c340afcbe75b9f0db5ff4572245c996c Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Wed, 23 Nov 2016 17:52:04 +0100 Subject: [PATCH] Fix UOE being thrown in BlockingCommandManager the manager must use a copy of the BlockListIQ's JID list, since it may be the empty list which is not modifiable. Also rename the getter methods from getJids() to getBlockedJids(). --- .../blocking/BlockingCommandManager.java | 2 +- .../smackx/blocking/element/BlockListIQ.java | 21 ++++++++++++++----- .../smackx/blocking/GetBlockingListTest.java | 8 +++---- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/BlockingCommandManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/BlockingCommandManager.java index 6ee6bdce7..55044394d 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/BlockingCommandManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/BlockingCommandManager.java @@ -169,7 +169,7 @@ public final class BlockingCommandManager extends Manager { BlockListIQ blockListIQ = new BlockListIQ(); BlockListIQ blockListIQResult = connection().createPacketCollectorAndSend(blockListIQ).nextResultOrThrow(); - blockListCached = blockListIQResult.getJids(); + blockListCached = blockListIQResult.getBlockedJidsCopy(); return Collections.unmodifiableList(blockListCached); } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/element/BlockListIQ.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/element/BlockListIQ.java index c24da5718..60267d3fb 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/element/BlockListIQ.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/element/BlockListIQ.java @@ -16,6 +16,7 @@ */ package org.jivesoftware.smackx.blocking.element; +import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -52,9 +53,10 @@ public class BlockListIQ extends IQ { public BlockListIQ(List jids) { super(ELEMENT, NAMESPACE); if (jids == null) { - jids = Collections.emptyList(); + this.jids = Collections.emptyList(); + } else { + this.jids = Collections.unmodifiableList(jids); } - this.jids = jids; } /** @@ -65,14 +67,23 @@ public class BlockListIQ extends IQ { } /** - * Get the JIDs. + * Get the JIDs as unmodifiable list. * - * @return the JIDs + * @return the blocked JIDs */ - public List getJids() { + public List getBlockedJids() { return jids; } + /** + * Get a copy of the blocked list JIDs. This copy is modifiable. + * + * @return the blocked JIDs + */ + public List getBlockedJidsCopy() { + return new ArrayList<>(getBlockedJids()); + } + @Override protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) { if (jids.isEmpty()) { diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/blocking/GetBlockingListTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/blocking/GetBlockingListTest.java index a14381ccf..877661eb7 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/blocking/GetBlockingListTest.java +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/blocking/GetBlockingListTest.java @@ -47,13 +47,13 @@ public class GetBlockingListTest { public void checkBlockListIQ() throws Exception { IQ iq = (IQ) PacketParserUtils.parseStanza(blockListIQExample); BlockListIQ blockListIQ = (BlockListIQ) iq; - Assert.assertEquals(2, blockListIQ.getJids().size()); - Assert.assertEquals(JidCreate.from("romeo@montague.net"), blockListIQ.getJids().get(0)); - Assert.assertEquals(JidCreate.from("iago@shakespeare.lit"), blockListIQ.getJids().get(1)); + Assert.assertEquals(2, blockListIQ.getBlockedJids().size()); + Assert.assertEquals(JidCreate.from("romeo@montague.net"), blockListIQ.getBlockedJids().get(0)); + Assert.assertEquals(JidCreate.from("iago@shakespeare.lit"), blockListIQ.getBlockedJids().get(1)); IQ iq2 = (IQ) PacketParserUtils.parseStanza(emptyBlockListIQExample); BlockListIQ emptyBlockListIQ = (BlockListIQ) iq2; - Assert.assertEquals(0, emptyBlockListIQ.getJids().size()); + Assert.assertEquals(0, emptyBlockListIQ.getBlockedJids().size()); } }