From 5c086eeefa2c4470f3295d26e7cfc1db94e1e2ab Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Thu, 1 Jan 2015 14:56:12 +0100 Subject: [PATCH] Add Node.getSubscriptionsAsOwner() in PubSub API To retrieve the subscriptions of a PubSub node as owner. Fixes SMACK-623. --- .../org/jivesoftware/smackx/pubsub/Node.java | 52 ++++++++++++++- .../extensions.providers | 12 ++++ .../pubsub/provider/PubSubProviderTest.java | 63 +++++++++++++++++++ 3 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 smack-extensions/src/test/java/org/jivesoftware/smackx/pubsub/provider/PubSubProviderTest.java diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/Node.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/Node.java index abca27920..efe26f53e 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/Node.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/Node.java @@ -166,8 +166,56 @@ abstract public class Node */ public List getSubscriptions(List additionalExtensions, Collection returnedExtensions) throws NoResponseException, XMPPErrorException, NotConnectedException { - PubSub pubSub = createPubsubPacket(Type.get, new NodeExtension( - PubSubElementType.SUBSCRIPTIONS, getId())); + return getSubscriptions(additionalExtensions, returnedExtensions, null); + } + + /** + * Get the subscriptions currently associated with this node as owner. + * + * @return List of {@link Subscription} + * @throws XMPPErrorException + * @throws NoResponseException + * @throws NotConnectedException + * @see #getSubscriptionsAsOwner(List, Collection) + * @since 4.1 + */ + public List getSubscriptionsAsOwner() throws NoResponseException, XMPPErrorException, + NotConnectedException { + return getSubscriptionsAsOwner(null, null); + } + + /** + * Get the subscriptions currently associated with this node as owner. + *

+ * Unlike {@link #getSubscriptions(List, Collection)}, which only retrieves the subscriptions of the current entity + * ("user"), this method returns a list of all subscriptions. This requires the entity to have the sufficient + * privileges to manage subscriptions. + *

+ *

+ * {@code additionalExtensions} can be used e.g. to add a "Result Set Management" extension. + * {@code returnedExtensions} will be filled with the packet extensions found in the answer. + *

+ * + * @param additionalExtensions + * @param returnedExtensions a collection that will be filled with the returned packet extensions + * @return List of {@link Subscription} + * @throws NoResponseException + * @throws XMPPErrorException + * @throws NotConnectedException + * @see XEP-60 ยง 8.8.1 - + * Retrieve Subscriptions List + * @since 4.1 + */ + public List getSubscriptionsAsOwner(List additionalExtensions, + Collection returnedExtensions) throws NoResponseException, XMPPErrorException, + NotConnectedException { + return getSubscriptions(additionalExtensions, returnedExtensions, PubSubNamespace.OWNER); + } + + private List getSubscriptions(List additionalExtensions, + Collection returnedExtensions, PubSubNamespace pubSubNamespace) + throws NoResponseException, XMPPErrorException, NotConnectedException { + PubSub pubSub = createPubsubPacket(Type.get, new NodeExtension(PubSubElementType.SUBSCRIPTIONS, getId()), pubSubNamespace); if (additionalExtensions != null) { for (PacketExtension pe : additionalExtensions) { pubSub.addExtension(pe); diff --git a/smack-extensions/src/main/resources/org.jivesoftware.smackx/extensions.providers b/smack-extensions/src/main/resources/org.jivesoftware.smackx/extensions.providers index 97c283665..1df8ce353 100644 --- a/smack-extensions/src/main/resources/org.jivesoftware.smackx/extensions.providers +++ b/smack-extensions/src/main/resources/org.jivesoftware.smackx/extensions.providers @@ -339,6 +339,18 @@ org.jivesoftware.smackx.pubsub.provider.FormNodeProvider + + subscriptions + http://jabber.org/protocol/pubsub#owner + org.jivesoftware.smackx.pubsub.provider.SubscriptionsProvider + + + + subscription + http://jabber.org/protocol/pubsub#owner + org.jivesoftware.smackx.pubsub.provider.SubscriptionProvider + + event diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/pubsub/provider/PubSubProviderTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/pubsub/provider/PubSubProviderTest.java new file mode 100644 index 000000000..09b6943ab --- /dev/null +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/pubsub/provider/PubSubProviderTest.java @@ -0,0 +1,63 @@ +/** + * + * Copyright 2014 Florian Schmaus + * + * 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.pubsub.provider; + +import static org.junit.Assert.assertEquals; + +import java.util.List; + +import org.jivesoftware.smack.test.util.TestUtils; +import org.jivesoftware.smack.util.PacketParserUtils; +import org.jivesoftware.smackx.pubsub.PubSubElementType; +import org.jivesoftware.smackx.pubsub.Subscription; +import org.jivesoftware.smackx.pubsub.SubscriptionsExtension; +import org.jivesoftware.smackx.pubsub.packet.PubSub; +import org.junit.Test; +import org.xmlpull.v1.XmlPullParser; + +public class PubSubProviderTest { + + @Test + public void subscriptionsOwnerResultTest() throws Exception { + // @formatter:off + final String resultStanza = + "" + + "" + + "" + + "" + + "" + + "" + + "" + + ""; + // @formatter:on + XmlPullParser parser = TestUtils.getIQParser(resultStanza); + PubSub pubsubResult = (PubSub) PacketParserUtils.parse(parser, null); + SubscriptionsExtension subElem = pubsubResult.getExtension(PubSubElementType.SUBSCRIPTIONS); + List subscriptions = subElem.getSubscriptions(); + assertEquals(2, subscriptions.size()); + + Subscription sub1 = subscriptions.get(0); + assertEquals("foo@example.org/Smack", sub1.getJid()); + assertEquals(Subscription.State.subscribed, sub1.getState()); + assertEquals("58C1A6F99F2A7", sub1.getId()); + + Subscription sub2 = subscriptions.get(1); + assertEquals("julia@example.org/Smack", sub2.getJid()); + assertEquals(Subscription.State.subscribed, sub2.getState()); + assertEquals("58C18F8917321", sub2.getId()); + } +}