From 9554c7be26344baf2412c78b6bdf5fc18f0c5e6a Mon Sep 17 00:00:00 2001 From: Fernando Ramirez Date: Mon, 29 Aug 2016 10:43:05 -0300 Subject: [PATCH] Add XEP-0191 - Blocking Command implementation SMACK-731 --- documentation/extensions/blockingcommand.md | 63 +++++ documentation/extensions/index.md | 3 +- .../experimental.providers | 2 +- .../blocking/BlockingCommandManager.java | 248 ++++++++++++++++++ .../blocking/element/BlockContactsIQ.java | 81 ++++++ .../smackx/blocking/element/BlockListIQ.java | 91 +++++++ .../blocking/element/UnblockContactsIQ.java | 81 ++++++ .../smackx/blocking/element/package-info.java | 24 ++ .../smackx/blocking/package-info.java | 24 ++ .../provider/BlockContactsIQProvider.java | 67 +++++ .../provider/BlockListIQProvider.java | 70 +++++ .../provider/UnblockContactsIQProvider.java | 67 +++++ .../blocking/provider/package-info.java | 24 ++ .../extensions.providers | 18 ++ .../smackx/blocking/BlockContactsIQTest.java | 59 +++++ .../smackx/blocking/GetBlockingListTest.java | 59 +++++ .../blocking/UnblockContactsIQTest.java | 79 ++++++ 17 files changed, 1058 insertions(+), 2 deletions(-) create mode 100644 documentation/extensions/blockingcommand.md create mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/BlockingCommandManager.java create mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/element/BlockContactsIQ.java create mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/element/BlockListIQ.java create mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/element/UnblockContactsIQ.java create mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/element/package-info.java create mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/package-info.java create mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/provider/BlockContactsIQProvider.java create mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/provider/BlockListIQProvider.java create mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/provider/UnblockContactsIQProvider.java create mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/provider/package-info.java create mode 100644 smack-extensions/src/test/java/org/jivesoftware/smackx/blocking/BlockContactsIQTest.java create mode 100644 smack-extensions/src/test/java/org/jivesoftware/smackx/blocking/GetBlockingListTest.java create mode 100644 smack-extensions/src/test/java/org/jivesoftware/smackx/blocking/UnblockContactsIQTest.java diff --git a/documentation/extensions/blockingcommand.md b/documentation/extensions/blockingcommand.md new file mode 100644 index 000000000..3e7e369ce --- /dev/null +++ b/documentation/extensions/blockingcommand.md @@ -0,0 +1,63 @@ +Blocking Command +================ + +Allows to manage communications blocking. + + * Check push notifications support + * Get blocking list + * Block contact + * Unblock contact + * Unblock all + + +**XEP related:** [XEP-0191](http://xmpp.org/extensions/xep-0191.html) + + +Get an instance of Blocking Command Manager +------------------------------------------- + +``` +BlockingCommandManager blockingCommandManager = BlockingCommandManager.getInstanceFor(connection); +``` + + +Check blocking command support +------------------------------ + +``` +boolean isSupported = blockingCommandManager.isSupportedByServer(); +``` + + +Get block list +-------------- + +``` +List blockList = blockingCommandManager.getBlockList(); +``` + + +Block contact +------------- + +``` +blockingCommandManager.blockContact(jid); +``` +*jid* is a `Jid` + + +Unblock contact +--------------- + +``` +blockingCommandManager.unblockContact(jid); +``` +*jid* is a `Jid` + + +Unblock all +----------- + +``` +blockingCommandManager.unblockAll(); +``` diff --git a/documentation/extensions/index.md b/documentation/extensions/index.md index 6ab64d5de..7a669a407 100644 --- a/documentation/extensions/index.md +++ b/documentation/extensions/index.md @@ -69,6 +69,7 @@ Smack Extensions and currently supported XEPs of smack-extensions | Last Message Correction | [XEP-0308](http://xmpp.org/extensions/xep-0308.html) | Provides a method for indicating that a message is a correction of the last sent message. | | [Group Chat Invitations](invitation.md) | n/a | Send invitations to other users to join a group chat room. | | [Jive Properties](properties.md) | n/a | TODO | +| [Blocking Command](blockingcommand.md) | [XEP-0191](http://xmpp.org/extensions/xep-0191.html) | Communications blocking that is intended to be simpler than privacy lists (XEP-0016). | Experimental Smack Extensions and currently supported XEPs of smack-experimental @@ -85,7 +86,7 @@ Experimental Smack Extensions and currently supported XEPs of smack-experimental | [Internet of Things - Discovery](iot.md) | [XEP-0347](http://xmpp.org/extensions/xep-0347.html) | Describes how Things can be installed and discovered by their owners. | | Google GCM JSON payload | n/a | Semantically the same as XEP-0335: JSON Containers | | Client State Indication | [XEP-0352](http://xmpp.org/extensions/xep-0352.html) | A way for the client to indicate its active/inactive state. | -| [Message Archive Management](mam.md) | [XEP-0313](http://xmpp.org/extensions/xep-0313.html) | Query and control an archive of messages stored on a server. | +| [Message Archive Management](mam.md) | [XEP-0313](http://xmpp.org/extensions/xep-0313.html) | Query and control an archive of messages stored on a server. | Legacy Smack Extensions and currently supported XEPs of smack-legacy diff --git a/smack-experimental/src/main/resources/org.jivesoftware.smack.experimental/experimental.providers b/smack-experimental/src/main/resources/org.jivesoftware.smack.experimental/experimental.providers index 145309c84..9d7066499 100644 --- a/smack-experimental/src/main/resources/org.jivesoftware.smack.experimental/experimental.providers +++ b/smack-experimental/src/main/resources/org.jivesoftware.smack.experimental/experimental.providers @@ -166,5 +166,5 @@ urn:xmpp:iot:control org.jivesoftware.smackx.iot.control.provider.IoTSetResponseProvider - + 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 new file mode 100644 index 000000000..5a364e0ba --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/BlockingCommandManager.java @@ -0,0 +1,248 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.blocking; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.WeakHashMap; + +import org.jivesoftware.smack.AbstractConnectionListener; +import org.jivesoftware.smack.ConnectionCreationListener; +import org.jivesoftware.smack.Manager; +import org.jivesoftware.smack.SmackException.NoResponseException; +import org.jivesoftware.smack.SmackException.NotConnectedException; +import org.jivesoftware.smack.XMPPConnection; +import org.jivesoftware.smack.XMPPConnectionRegistry; +import org.jivesoftware.smack.XMPPException.XMPPErrorException; +import org.jivesoftware.smack.iqrequest.AbstractIqRequestHandler; +import org.jivesoftware.smack.iqrequest.IQRequestHandler.Mode; +import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smack.packet.IQ.Type; +import org.jivesoftware.smackx.blocking.element.BlockContactsIQ; +import org.jivesoftware.smackx.blocking.element.BlockListIQ; +import org.jivesoftware.smackx.blocking.element.UnblockContactsIQ; +import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; +import org.jxmpp.jid.Jid; + +/** + * Blocking command manager class. + * + * @author Fernando Ramirez + * @see XEP-0191: Blocking + * Command + */ +public final class BlockingCommandManager extends Manager { + + public static final String NAMESPACE = "urn:xmpp:blocking"; + + private volatile List blockListCached; + + static { + XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() { + @Override + public void connectionCreated(XMPPConnection connection) { + getInstanceFor(connection); + } + }); + } + + private static final Map INSTANCES = new WeakHashMap<>(); + + /** + * Get the singleton instance of BlockingCommandManager. + * + * @param connection + * @return the instance of BlockingCommandManager + */ + public static synchronized BlockingCommandManager getInstanceFor(XMPPConnection connection) { + BlockingCommandManager blockingCommandManager = INSTANCES.get(connection); + + if (blockingCommandManager == null) { + blockingCommandManager = new BlockingCommandManager(connection); + INSTANCES.put(connection, blockingCommandManager); + } + + return blockingCommandManager; + } + + private BlockingCommandManager(XMPPConnection connection) { + super(connection); + + // block IQ handler + connection.registerIQRequestHandler( + new AbstractIqRequestHandler(BlockContactsIQ.ELEMENT, BlockContactsIQ.NAMESPACE, Type.set, Mode.sync) { + @Override + public IQ handleIQRequest(IQ iqRequest) { + BlockContactsIQ blockContactIQ = (BlockContactsIQ) iqRequest; + + if (blockListCached == null) { + blockListCached = new ArrayList(); + } + + List blockedJids = blockContactIQ.getJids(); + addToBlockList(blockedJids); + + return IQ.createResultIQ(blockContactIQ); + } + }); + + // unblock IQ handler + connection.registerIQRequestHandler(new AbstractIqRequestHandler(UnblockContactsIQ.ELEMENT, + UnblockContactsIQ.NAMESPACE, Type.set, Mode.sync) { + @Override + public IQ handleIQRequest(IQ iqRequest) { + UnblockContactsIQ unblockContactIQ = (UnblockContactsIQ) iqRequest; + + if (blockListCached == null) { + blockListCached = new ArrayList(); + } + + List unblockedJids = unblockContactIQ.getJids(); + if (unblockedJids == null) { // remove all + blockListCached.clear(); + } else { // remove only some + removeFromBlockList(unblockedJids); + } + + return IQ.createResultIQ(unblockContactIQ); + } + }); + + connection.addConnectionListener(new AbstractConnectionListener() { + @Override + public void authenticated(XMPPConnection connection, boolean resumed) { + // No need to reset the cache if the connection got resumed. + if (resumed) { + return; + } + blockListCached = null; + } + }); + } + + private void addToBlockList(List blockedJids) { + for (Jid jid : blockedJids) { + if (searchJid(jid) == -1) { + blockListCached.add(jid); + } + } + } + + private void removeFromBlockList(List unblockedJids) { + for (Jid jid : unblockedJids) { + int position = searchJid(jid); + if (position != -1) { + blockListCached.remove(position); + } + } + } + + private int searchJid(Jid jid) { + int i = -1; + for (int j = 0; j < blockListCached.size(); j++) { + if (blockListCached.get(j).equals(jid)) { + i = j; + } + } + return i; + } + + /** + * Returns true if Blocking Command is supported by the server. + * + * @return true if Blocking Command is supported by the server. + * @throws NoResponseException + * @throws XMPPErrorException + * @throws NotConnectedException + * @throws InterruptedException + */ + public boolean isSupportedByServer() + throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + return ServiceDiscoveryManager.getInstanceFor(connection()).serverSupportsFeature(NAMESPACE); + } + + /** + * Returns the block list. + * + * @return the blocking list + * @throws NoResponseException + * @throws XMPPErrorException + * @throws NotConnectedException + * @throws InterruptedException + */ + public List getBlockList() + throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + + if (blockListCached != null) { + return Collections.unmodifiableList(blockListCached); + } + + BlockListIQ blockListIQ = new BlockListIQ(); + BlockListIQ blockListIQResult = connection().createPacketCollectorAndSend(blockListIQ).nextResultOrThrow(); + blockListCached = blockListIQResult.getJids(); + + List emptyList = Collections.emptyList(); + return (blockListCached == null) ? emptyList : Collections.unmodifiableList(blockListCached); + } + + /** + * Block contacts. + * + * @param jids + * @throws NoResponseException + * @throws XMPPErrorException + * @throws NotConnectedException + * @throws InterruptedException + */ + public void blockContacts(List jids) + throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + BlockContactsIQ blockContactIQ = new BlockContactsIQ(jids); + connection().createPacketCollectorAndSend(blockContactIQ).nextResultOrThrow(); + } + + /** + * Unblock contacts. + * + * @param jids + * @throws NoResponseException + * @throws XMPPErrorException + * @throws NotConnectedException + * @throws InterruptedException + */ + public void unblockContacts(List jids) + throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + UnblockContactsIQ unblockContactIQ = new UnblockContactsIQ(jids); + connection().createPacketCollectorAndSend(unblockContactIQ).nextResultOrThrow(); + } + + /** + * Unblock all. + * + * @throws NoResponseException + * @throws XMPPErrorException + * @throws NotConnectedException + * @throws InterruptedException + */ + public void unblockAll() + throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + UnblockContactsIQ unblockContactIQ = new UnblockContactsIQ(null); + connection().createPacketCollectorAndSend(unblockContactIQ).nextResultOrThrow(); + } + +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/element/BlockContactsIQ.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/element/BlockContactsIQ.java new file mode 100644 index 000000000..19ca0eb72 --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/element/BlockContactsIQ.java @@ -0,0 +1,81 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.blocking.element; + +import java.util.List; + +import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smackx.blocking.BlockingCommandManager; +import org.jxmpp.jid.Jid; + +/** + * Block contact IQ class. + * + * @author Fernando Ramirez + * @see XEP-0191: Blocking + * Command + */ +public class BlockContactsIQ extends IQ { + + /** + * block element. + */ + public static final String ELEMENT = "block"; + + /** + * the IQ NAMESPACE. + */ + public static final String NAMESPACE = BlockingCommandManager.NAMESPACE; + + private final List jids; + + /** + * Block list IQ constructor. + * + * @param jids + */ + public BlockContactsIQ(List jids) { + super(ELEMENT, NAMESPACE); + this.setType(Type.set); + this.jids = jids; + } + + /** + * Get the JID. + * + * @return the list of JIDs + */ + public List getJids() { + return jids; + } + + @Override + protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) { + xml.rightAngleBracket(); + + if (jids != null) { + for (Jid jid : jids) { + xml.halfOpenElement("item"); + xml.attribute("jid", jid); + xml.closeEmptyElement(); + } + } + + return xml; + } + +} 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 new file mode 100644 index 000000000..52de9f04d --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/element/BlockListIQ.java @@ -0,0 +1,91 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.blocking.element; + +import java.util.List; + +import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smackx.blocking.BlockingCommandManager; +import org.jxmpp.jid.Jid; + +/** + * Block list IQ class. + * + * @author Fernando Ramirez + * @see XEP-0191: Blocking + * Command + */ +public class BlockListIQ extends IQ { + + /** + * block list element. + */ + public static final String ELEMENT = "blocklist"; + + /** + * the IQ NAMESPACE. + */ + public static final String NAMESPACE = BlockingCommandManager.NAMESPACE; + + private final List jids; + + /** + * Block list IQ constructor. + * + * @param jids + */ + public BlockListIQ(List jids) { + super(ELEMENT, NAMESPACE); + this.jids = jids; + } + + /** + * Block list IQ constructor. + */ + public BlockListIQ() { + this(null); + } + + /** + * Get the JIDs. + * + * @return the JIDs + */ + public List getJids() { + return jids; + } + + @Override + protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) { + + if (jids == null) { + xml.setEmptyElement(); + + } else { + xml.rightAngleBracket(); + + for (Jid jid : jids) { + xml.halfOpenElement("item"); + xml.attribute("jid", jid); + xml.closeEmptyElement(); + } + } + + return xml; + } + +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/element/UnblockContactsIQ.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/element/UnblockContactsIQ.java new file mode 100644 index 000000000..d0269222e --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/element/UnblockContactsIQ.java @@ -0,0 +1,81 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.blocking.element; + +import java.util.List; + +import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smackx.blocking.BlockingCommandManager; +import org.jxmpp.jid.Jid; + +/** + * Unblock contact IQ class. + * + * @author Fernando Ramirez + * @see XEP-0191: Blocking + * Command + */ +public class UnblockContactsIQ extends IQ { + + /** + * unblock element. + */ + public static final String ELEMENT = "unblock"; + + /** + * the IQ NAMESPACE. + */ + public static final String NAMESPACE = BlockingCommandManager.NAMESPACE; + + private final List jids; + + /** + * Unblock contacts IQ constructor. + * + * @param jids + */ + public UnblockContactsIQ(List jids) { + super(ELEMENT, NAMESPACE); + this.setType(Type.set); + this.jids = jids; + } + + /** + * Get the JIDs. + * + * @return the list of JIDs + */ + public List getJids() { + return jids; + } + + @Override + protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) { + xml.rightAngleBracket(); + + if (jids != null) { + for (Jid jid : jids) { + xml.halfOpenElement("item"); + xml.attribute("jid", jid); + xml.closeEmptyElement(); + } + } + + return xml; + } + +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/element/package-info.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/element/package-info.java new file mode 100644 index 000000000..129e93b6a --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/element/package-info.java @@ -0,0 +1,24 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * Blocking command elements. + * + * @author Fernando Ramirez + * @see XEP-0191: Blocking + * Command + */ +package org.jivesoftware.smackx.blocking.element; diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/package-info.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/package-info.java new file mode 100644 index 000000000..8521595e4 --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/package-info.java @@ -0,0 +1,24 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * Classes and interfaces of Blocking command. + * + * @author Fernando Ramirez + * @see XEP-0191: Blocking + * Command + */ +package org.jivesoftware.smackx.blocking; diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/provider/BlockContactsIQProvider.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/provider/BlockContactsIQProvider.java new file mode 100644 index 000000000..ad45f5c4b --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/provider/BlockContactsIQProvider.java @@ -0,0 +1,67 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.blocking.provider; + +import java.util.ArrayList; +import java.util.List; + +import org.jivesoftware.smack.provider.IQProvider; +import org.jivesoftware.smackx.blocking.element.BlockContactsIQ; +import org.jxmpp.jid.Jid; +import org.jxmpp.jid.impl.JidCreate; +import org.xmlpull.v1.XmlPullParser; + +/** + * Block contact IQ provider class. + * + * @author Fernando Ramirez + * @see XEP-0191: Blocking + * Command + */ +public class BlockContactsIQProvider extends IQProvider { + + @Override + public BlockContactsIQ parse(XmlPullParser parser, int initialDepth) throws Exception { + List jids = null; + + outerloop: while (true) { + int eventType = parser.next(); + + switch (eventType) { + + case XmlPullParser.START_TAG: + if (parser.getName().equals("item")) { + if (jids == null) { + jids = new ArrayList<>(); + } + jids.add(JidCreate.from(parser.getAttributeValue("", "jid"))); + } + break; + + case XmlPullParser.END_TAG: + if (parser.getDepth() == initialDepth) { + break outerloop; + } + break; + + } + } + + return new BlockContactsIQ(jids); + } + +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/provider/BlockListIQProvider.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/provider/BlockListIQProvider.java new file mode 100644 index 000000000..2073f5528 --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/provider/BlockListIQProvider.java @@ -0,0 +1,70 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.blocking.provider; + +import java.util.ArrayList; +import java.util.List; + +import org.jivesoftware.smack.packet.IQ.Type; +import org.jivesoftware.smack.provider.IQProvider; +import org.jivesoftware.smackx.blocking.element.BlockListIQ; +import org.jxmpp.jid.Jid; +import org.jxmpp.jid.impl.JidCreate; +import org.xmlpull.v1.XmlPullParser; + +/** + * Block list IQ provider class. + * + * @author Fernando Ramirez + * @see XEP-0191: Blocking + * Command + */ +public class BlockListIQProvider extends IQProvider { + + @Override + public BlockListIQ parse(XmlPullParser parser, int initialDepth) throws Exception { + List jids = null; + + outerloop: while (true) { + int eventType = parser.next(); + + switch (eventType) { + + case XmlPullParser.START_TAG: + if (parser.getName().equals("item")) { + if (jids == null) { + jids = new ArrayList<>(); + } + jids.add(JidCreate.from(parser.getAttributeValue("", "jid"))); + } + break; + + case XmlPullParser.END_TAG: + if (parser.getDepth() == initialDepth) { + break outerloop; + } + break; + + } + } + + BlockListIQ blockListIQ = new BlockListIQ(jids); + blockListIQ.setType(Type.result); + return blockListIQ; + } + +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/provider/UnblockContactsIQProvider.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/provider/UnblockContactsIQProvider.java new file mode 100644 index 000000000..976236f13 --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/provider/UnblockContactsIQProvider.java @@ -0,0 +1,67 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.blocking.provider; + +import java.util.ArrayList; +import java.util.List; + +import org.jivesoftware.smack.provider.IQProvider; +import org.jivesoftware.smackx.blocking.element.UnblockContactsIQ; +import org.jxmpp.jid.Jid; +import org.jxmpp.jid.impl.JidCreate; +import org.xmlpull.v1.XmlPullParser; + +/** + * Unblock contact IQ provider class. + * + * @author Fernando Ramirez + * @see XEP-0191: Blocking + * Command + */ +public class UnblockContactsIQProvider extends IQProvider { + + @Override + public UnblockContactsIQ parse(XmlPullParser parser, int initialDepth) throws Exception { + List jids = null; + + outerloop: while (true) { + int eventType = parser.next(); + + switch (eventType) { + + case XmlPullParser.START_TAG: + if (parser.getName().equals("item")) { + if (jids == null) { + jids = new ArrayList<>(); + } + jids.add(JidCreate.from(parser.getAttributeValue("", "jid"))); + } + break; + + case XmlPullParser.END_TAG: + if (parser.getDepth() == initialDepth) { + break outerloop; + } + break; + + } + } + + return new UnblockContactsIQ(jids); + } + +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/provider/package-info.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/provider/package-info.java new file mode 100644 index 000000000..054954ef7 --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/blocking/provider/package-info.java @@ -0,0 +1,24 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * Blocking command providers. + * + * @author Fernando Ramirez + * @see XEP-0191: Blocking + * Command + */ +package org.jivesoftware.smackx.blocking.provider; diff --git a/smack-extensions/src/main/resources/org.jivesoftware.smack.extensions/extensions.providers b/smack-extensions/src/main/resources/org.jivesoftware.smack.extensions/extensions.providers index 2257b04d0..2cef339a9 100644 --- a/smack-extensions/src/main/resources/org.jivesoftware.smack.extensions/extensions.providers +++ b/smack-extensions/src/main/resources/org.jivesoftware.smack.extensions/extensions.providers @@ -514,5 +514,23 @@ urn:xmpp:message-correct:0 org.jivesoftware.smackx.message_correct.provider.MessageCorrectProvider + + + + blocklist + urn:xmpp:blocking + org.jivesoftware.smackx.blocking.provider.BlockListIQProvider + + + block + urn:xmpp:blocking + org.jivesoftware.smackx.blocking.provider.BlockContactsIQProvider + + + unblock + urn:xmpp:blocking + org.jivesoftware.smackx.blocking.provider.UnblockContactsIQProvider + + diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/blocking/BlockContactsIQTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/blocking/BlockContactsIQTest.java new file mode 100644 index 000000000..2e765b97b --- /dev/null +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/blocking/BlockContactsIQTest.java @@ -0,0 +1,59 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.blocking; + +import java.util.ArrayList; +import java.util.List; + +import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smack.util.PacketParserUtils; +import org.jivesoftware.smackx.blocking.element.BlockContactsIQ; +import org.junit.Assert; +import org.junit.Test; +import org.jxmpp.jid.Jid; +import org.jxmpp.jid.impl.JidCreate; + +public class BlockContactsIQTest { + + String blockContactIQExample = "" + "" + + "" + "" + "" + ""; + + String blockContactPushIQExample = "" + + "" + "" + + "" + "" + ""; + + @Test + public void checkBlockContactIQStanza() throws Exception { + List jids = new ArrayList<>(); + jids.add(JidCreate.from("romeo@montague.net")); + jids.add(JidCreate.from("pepe@montague.net")); + + BlockContactsIQ blockContactIQ = new BlockContactsIQ(jids); + blockContactIQ.setStanzaId("block1"); + + Assert.assertEquals(blockContactIQExample, blockContactIQ.toXML().toString()); + } + + @Test + public void checkBlockContactPushIQ() throws Exception { + IQ iq = (IQ) PacketParserUtils.parseStanza(blockContactPushIQExample); + BlockContactsIQ blockContactIQ = (BlockContactsIQ) iq; + Assert.assertEquals(JidCreate.from("romeo@montague.net"), blockContactIQ.getJids().get(0)); + Assert.assertEquals(JidCreate.from("pepe@montague.net"), blockContactIQ.getJids().get(1)); + } + +} 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 new file mode 100644 index 000000000..dd3062414 --- /dev/null +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/blocking/GetBlockingListTest.java @@ -0,0 +1,59 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.blocking; + +import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smack.packet.IQ.Type; +import org.jivesoftware.smack.util.PacketParserUtils; +import org.jivesoftware.smackx.blocking.element.BlockListIQ; +import org.junit.Assert; +import org.junit.Test; +import org.jxmpp.jid.impl.JidCreate; + +public class GetBlockingListTest { + + String getBlockingListIQExample = "" + + "" + ""; + + String blockListIQExample = "" + "" + + "" + "" + "" + ""; + + String emptyBlockListIQExample = "" + "" + + ""; + + @Test + public void checkGetBlockingListIQStanza() throws Exception { + BlockListIQ getBlockListIQ = new BlockListIQ(null); + getBlockListIQ.setType(Type.get); + getBlockListIQ.setStanzaId("blocklist1"); + Assert.assertEquals(getBlockingListIQExample, getBlockListIQ.toXML().toString()); + } + + @Test + 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)); + + IQ iq2 = (IQ) PacketParserUtils.parseStanza(emptyBlockListIQExample); + BlockListIQ emptyBlockListIQ = (BlockListIQ) iq2; + Assert.assertNull(emptyBlockListIQ.getJids()); + } + +} diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/blocking/UnblockContactsIQTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/blocking/UnblockContactsIQTest.java new file mode 100644 index 000000000..3d4f8a943 --- /dev/null +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/blocking/UnblockContactsIQTest.java @@ -0,0 +1,79 @@ +/** + * + * Copyright 2016 Fernando Ramirez + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.blocking; + +import java.util.ArrayList; +import java.util.List; + +import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smack.util.PacketParserUtils; +import org.jivesoftware.smackx.blocking.element.UnblockContactsIQ; +import org.junit.Assert; +import org.junit.Test; +import org.jxmpp.jid.Jid; +import org.jxmpp.jid.impl.JidCreate; + +public class UnblockContactsIQTest { + + String unblockContactIQExample = "" + "" + + "" + "" + "" + ""; + + String unblockContactPushIQExample = "" + + "" + "" + + "" + "" + ""; + + String unblockAllIQExample = "" + "" + + ""; + + String unblockAllPushIQExample = "" + + "" + ""; + + @Test + public void checkUnblockContactIQStanza() throws Exception { + List jids = new ArrayList<>(); + jids.add(JidCreate.from("romeo@montague.net")); + jids.add(JidCreate.from("pepe@montague.net")); + + UnblockContactsIQ unblockContactIQ = new UnblockContactsIQ(jids); + unblockContactIQ.setStanzaId("unblock1"); + + Assert.assertEquals(unblockContactIQExample, unblockContactIQ.toXML().toString()); + } + + @Test + public void checkUnblockContactPushIQ() throws Exception { + IQ iq = (IQ) PacketParserUtils.parseStanza(unblockContactPushIQExample); + UnblockContactsIQ unblockContactIQ = (UnblockContactsIQ) iq; + Assert.assertEquals(JidCreate.from("romeo@montague.net"), unblockContactIQ.getJids().get(0)); + Assert.assertEquals(JidCreate.from("pepe@montague.net"), unblockContactIQ.getJids().get(1)); + } + + @Test + public void checkUnblockAllIQStanza() throws Exception { + UnblockContactsIQ unblockAllIQ = new UnblockContactsIQ(null); + unblockAllIQ.setStanzaId("unblock2"); + Assert.assertEquals(unblockAllIQExample, unblockAllIQ.toXML().toString()); + } + + @Test + public void checkUnblockAllPushIQ() throws Exception { + IQ iq = (IQ) PacketParserUtils.parseStanza(unblockAllPushIQExample); + UnblockContactsIQ unblockAllIQ = (UnblockContactsIQ) iq; + Assert.assertNull(unblockAllIQ.getJids()); + } + +}