Add support for PubSub affiliation actions as owner

SMACK-675.
This commit is contained in:
Florian Schmaus 2015-06-06 11:14:26 +02:00
parent f7782aa466
commit fce0aeadef
3 changed files with 116 additions and 24 deletions

View File

@ -130,6 +130,20 @@ public class Affiliation implements ExtensionElement
return namespace;
}
/**
* Check if this is an affiliation element to modify affiliations on a node.
*
* @return <code>true</code> if this is an affiliation element to modify affiliations on a node, <code>false</code> otherwise.
* @since 4.2
*/
public boolean isAffiliationModification() {
if (jid != null && affiliation != null) {
assert(node == null && namespace == PubSubNamespace.OWNER);
return true;
}
return false;
}
@Override
public XmlStringBuilder toXML() {
XmlStringBuilder xml = new XmlStringBuilder(this);

View File

@ -19,26 +19,33 @@ package org.jivesoftware.smackx.pubsub;
import java.util.Collections;
import java.util.List;
import org.jivesoftware.smack.util.XmlStringBuilder;
/**
* Represents the <b>affiliations</b> element of the reply to a request for affiliations.
* It is defined in the specification in section <a href="http://xmpp.org/extensions/xep-0060.html#entity-affiliations">5.7 Retrieve Affiliations</a>.
* It is defined in the specification in section <a href="http://xmpp.org/extensions/xep-0060.html#entity-affiliations">5.7 Retrieve Affiliations</a> and
* <a href="http://www.xmpp.org/extensions/xep-0060.html#owner-affiliations">8.9 Manage Affiliations</a>.
*
* @author Robin Collier
*/
public class AffiliationsExtension extends NodeExtension
{
protected List<Affiliation> items = Collections.emptyList();
private final String node;
public AffiliationsExtension()
{
super(PubSubElementType.AFFILIATIONS);
}
public AffiliationsExtension() {
this(null, null);
}
public AffiliationsExtension(List<Affiliation> subList)
{
super(PubSubElementType.AFFILIATIONS);
items = subList;
}
public AffiliationsExtension(List<Affiliation> subList) {
this(subList, null);
}
public AffiliationsExtension(List<Affiliation> subList, String node) {
super(PubSubElementType.AFFILIATIONS);
items = subList;
this.node = node;
}
public List<Affiliation> getAffiliations()
{
@ -54,19 +61,14 @@ public class AffiliationsExtension extends NodeExtension
}
else
{
StringBuilder builder = new StringBuilder("<");
builder.append(getElementName());
builder.append(">");
for (Affiliation item : items)
{
builder.append(item.toXML());
}
builder.append("</");
builder.append(getElementName());
builder.append(">");
return builder.toString();
// Can't use XmlStringBuilder(this), because we don't want the namespace to be included
XmlStringBuilder xml = new XmlStringBuilder();
xml.openElement(getElementName());
xml.optAttribute("node", node);
xml.rightAngleBracket();
xml.append(items);
xml.closeElement(this);
return xml;
}
}
}

View File

@ -256,7 +256,54 @@ abstract public class Node
public List<Affiliation> getAffiliations(List<ExtensionElement> additionalExtensions, Collection<ExtensionElement> returnedExtensions)
throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
PubSub pubSub = createPubsubPacket(Type.get, new NodeExtension(PubSubElementType.AFFILIATIONS, getId()));
return getAffiliations(PubSubNamespace.BASIC, additionalExtensions, returnedExtensions);
}
/**
* Retrieve the affiliation list for this node as owner.
*
* @return list of entities whose affiliation is not 'none'.
* @throws NoResponseException
* @throws XMPPErrorException
* @throws NotConnectedException
* @throws InterruptedException
* @see #getAffiliations(List, Collection)
* @since 4.2
*/
public List<Affiliation> getAffiliationsAsOwner()
throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
return getAffiliationsAsOwner(null, null);
}
/**
* Retrieve the affiliation list for this node as owner.
* <p>
* Note that this is an <b>optional</b> PubSub feature ('pubusb#modify-affiliations').
* </p>
*
* @param additionalExtensions optional additional extension elements add to the request.
* @param returnedExtensions an optional collection that will be filled with the returned
* extension elements.
* @return list of entities whose affiliation is not 'none'.
* @throws NoResponseException
* @throws XMPPErrorException
* @throws NotConnectedException
* @throws InterruptedException
* @see <a href="http://www.xmpp.org/extensions/xep-0060.html#owner-affiliations-retrieve">XEP-60 § 8.9.1 Retrieve Affiliations List</a>
* @since 4.2
*/
public List<Affiliation> getAffiliationsAsOwner(List<ExtensionElement> additionalExtensions, Collection<ExtensionElement> returnedExtensions)
throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
return getAffiliations(PubSubNamespace.OWNER, additionalExtensions, returnedExtensions);
}
private List<Affiliation> getAffiliations(PubSubNamespace namespace, List<ExtensionElement> additionalExtensions,
Collection<ExtensionElement> returnedExtensions) throws NoResponseException, XMPPErrorException,
NotConnectedException, InterruptedException {
PubSub pubSub = createPubsubPacket(Type.get, new NodeExtension(PubSubElementType.AFFILIATIONS, getId()), namespace);
if (additionalExtensions != null) {
for (ExtensionElement pe : additionalExtensions) {
pubSub.addExtension(pe);
@ -270,6 +317,35 @@ abstract public class Node
return affilElem.getAffiliations();
}
/**
* Modify the affiliations for this PubSub node as owner. The {@link Affiliation}s given must be created with the
* {@link Affiliation#Affiliation(org.jxmpp.jid.BareJid, Affiliation.Type)} constructor.
* <p>
* Note that this is an <b>optional</b> PubSub feature ('pubusb#modify-affiliations').
* </p>
*
* @param affiliations
* @return <code>null</code> or a PubSub stanza with additional information on success.
* @throws NoResponseException
* @throws XMPPErrorException
* @throws NotConnectedException
* @throws InterruptedException
* @see <a href="http://www.xmpp.org/extensions/xep-0060.html#owner-affiliations-modify">XEP-60 § 8.9.2 Modify Affiliation</a>
* @since 4.2
*/
public PubSub modifyAffiliationAsOwner(List<Affiliation> affiliations) throws NoResponseException,
XMPPErrorException, NotConnectedException, InterruptedException {
for (Affiliation affiliation : affiliations) {
if (affiliation.getPubSubNamespace() != PubSubNamespace.OWNER) {
throw new IllegalArgumentException("Must use Affiliation(BareJid, Type) affiliations");
}
}
PubSub pubSub = createPubsubPacket(Type.set, new AffiliationsExtension(affiliations, getId()),
PubSubNamespace.OWNER);
return sendPubsubPacket(pubSub);
}
/**
* The user subscribes to the node using the supplied jid. The
* bare jid portion of this one must match the jid for the connection.