Add XEP-0191 - Blocking Command implementation

SMACK-731
This commit is contained in:
Fernando Ramirez 2016-08-29 10:43:05 -03:00 committed by Florian Schmaus
parent 0679c89813
commit 9554c7be26
17 changed files with 1058 additions and 2 deletions

View File

@ -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<Jid> 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();
```

View File

@ -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

View File

@ -166,5 +166,5 @@
<namespace>urn:xmpp:iot:control</namespace>
<className>org.jivesoftware.smackx.iot.control.provider.IoTSetResponseProvider</className>
</iqProvider>
</smackProviders>

View File

@ -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 <a href="http://xmpp.org/extensions/xep-0191.html">XEP-0191: Blocking
* Command</a>
*/
public final class BlockingCommandManager extends Manager {
public static final String NAMESPACE = "urn:xmpp:blocking";
private volatile List<Jid> blockListCached;
static {
XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
@Override
public void connectionCreated(XMPPConnection connection) {
getInstanceFor(connection);
}
});
}
private static final Map<XMPPConnection, BlockingCommandManager> 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<Jid>();
}
List<Jid> 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<Jid>();
}
List<Jid> 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<Jid> blockedJids) {
for (Jid jid : blockedJids) {
if (searchJid(jid) == -1) {
blockListCached.add(jid);
}
}
}
private void removeFromBlockList(List<Jid> 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<Jid> 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<Jid> 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<Jid> 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<Jid> 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();
}
}

View File

@ -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 <a href="http://xmpp.org/extensions/xep-0191.html">XEP-0191: Blocking
* Command</a>
*/
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<Jid> jids;
/**
* Block list IQ constructor.
*
* @param jids
*/
public BlockContactsIQ(List<Jid> jids) {
super(ELEMENT, NAMESPACE);
this.setType(Type.set);
this.jids = jids;
}
/**
* Get the JID.
*
* @return the list of JIDs
*/
public List<Jid> 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;
}
}

View File

@ -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 <a href="http://xmpp.org/extensions/xep-0191.html">XEP-0191: Blocking
* Command</a>
*/
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<Jid> jids;
/**
* Block list IQ constructor.
*
* @param jids
*/
public BlockListIQ(List<Jid> jids) {
super(ELEMENT, NAMESPACE);
this.jids = jids;
}
/**
* Block list IQ constructor.
*/
public BlockListIQ() {
this(null);
}
/**
* Get the JIDs.
*
* @return the JIDs
*/
public List<Jid> 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;
}
}

View File

@ -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 <a href="http://xmpp.org/extensions/xep-0191.html">XEP-0191: Blocking
* Command</a>
*/
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<Jid> jids;
/**
* Unblock contacts IQ constructor.
*
* @param jids
*/
public UnblockContactsIQ(List<Jid> jids) {
super(ELEMENT, NAMESPACE);
this.setType(Type.set);
this.jids = jids;
}
/**
* Get the JIDs.
*
* @return the list of JIDs
*/
public List<Jid> 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;
}
}

View File

@ -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 <a href="http://xmpp.org/extensions/xep-0191.html">XEP-0191: Blocking
* Command</a>
*/
package org.jivesoftware.smackx.blocking.element;

View File

@ -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 <a href="http://xmpp.org/extensions/xep-0191.html">XEP-0191: Blocking
* Command</a>
*/
package org.jivesoftware.smackx.blocking;

View File

@ -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 <a href="http://xmpp.org/extensions/xep-0191.html">XEP-0191: Blocking
* Command</a>
*/
public class BlockContactsIQProvider extends IQProvider<BlockContactsIQ> {
@Override
public BlockContactsIQ parse(XmlPullParser parser, int initialDepth) throws Exception {
List<Jid> 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);
}
}

View File

@ -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 <a href="http://xmpp.org/extensions/xep-0191.html">XEP-0191: Blocking
* Command</a>
*/
public class BlockListIQProvider extends IQProvider<BlockListIQ> {
@Override
public BlockListIQ parse(XmlPullParser parser, int initialDepth) throws Exception {
List<Jid> 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;
}
}

View File

@ -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 <a href="http://xmpp.org/extensions/xep-0191.html">XEP-0191: Blocking
* Command</a>
*/
public class UnblockContactsIQProvider extends IQProvider<UnblockContactsIQ> {
@Override
public UnblockContactsIQ parse(XmlPullParser parser, int initialDepth) throws Exception {
List<Jid> 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);
}
}

View File

@ -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 <a href="http://xmpp.org/extensions/xep-0191.html">XEP-0191: Blocking
* Command</a>
*/
package org.jivesoftware.smackx.blocking.provider;

View File

@ -514,5 +514,23 @@
<namespace>urn:xmpp:message-correct:0</namespace>
<className>org.jivesoftware.smackx.message_correct.provider.MessageCorrectProvider</className>
</extensionProvider>
<!-- XEP-0191 Blocking Command -->
<iqProvider>
<elementName>blocklist</elementName>
<namespace>urn:xmpp:blocking</namespace>
<className>org.jivesoftware.smackx.blocking.provider.BlockListIQProvider</className>
</iqProvider>
<iqProvider>
<elementName>block</elementName>
<namespace>urn:xmpp:blocking</namespace>
<className>org.jivesoftware.smackx.blocking.provider.BlockContactsIQProvider</className>
</iqProvider>
<iqProvider>
<elementName>unblock</elementName>
<namespace>urn:xmpp:blocking</namespace>
<className>org.jivesoftware.smackx.blocking.provider.UnblockContactsIQProvider</className>
</iqProvider>
</smackProviders>

View File

@ -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 = "<iq id='block1' type='set'>" + "<block xmlns='urn:xmpp:blocking'>"
+ "<item jid='romeo@montague.net'/>" + "<item jid='pepe@montague.net'/>" + "</block>" + "</iq>";
String blockContactPushIQExample = "<iq to='juliet@capulet.com/chamber' type='set' id='push1'>"
+ "<block xmlns='urn:xmpp:blocking'>" + "<item jid='romeo@montague.net'/>"
+ "<item jid='pepe@montague.net'/>" + "</block>" + "</iq>";
@Test
public void checkBlockContactIQStanza() throws Exception {
List<Jid> 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));
}
}

View File

@ -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 = "<iq id='blocklist1' type='get'>"
+ "<blocklist xmlns='urn:xmpp:blocking'/>" + "</iq>";
String blockListIQExample = "<iq type='result' id='blocklist1'>" + "<blocklist xmlns='urn:xmpp:blocking'>"
+ "<item jid='romeo@montague.net'/>" + "<item jid='iago@shakespeare.lit'/>" + "</blocklist>" + "</iq>";
String emptyBlockListIQExample = "<iq type='result' id='blocklist1'>" + "<blocklist xmlns='urn:xmpp:blocking'/>"
+ "</iq>";
@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());
}
}

View File

@ -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 = "<iq id='unblock1' type='set'>" + "<unblock xmlns='urn:xmpp:blocking'>"
+ "<item jid='romeo@montague.net'/>" + "<item jid='pepe@montague.net'/>" + "</unblock>" + "</iq>";
String unblockContactPushIQExample = "<iq to='juliet@capulet.com/chamber' type='set' id='push3'>"
+ "<unblock xmlns='urn:xmpp:blocking'>" + "<item jid='romeo@montague.net'/>"
+ "<item jid='pepe@montague.net'/>" + "</unblock>" + "</iq>";
String unblockAllIQExample = "<iq id='unblock2' type='set'>" + "<unblock xmlns='urn:xmpp:blocking'></unblock>"
+ "</iq>";
String unblockAllPushIQExample = "<iq to='juliet@capulet.com/chamber' type='set' id='push5'>"
+ "<unblock xmlns='urn:xmpp:blocking'/>" + "</iq>";
@Test
public void checkUnblockContactIQStanza() throws Exception {
List<Jid> 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());
}
}