Extend PubSub Affiliation class by 'jid'

to allow pubsub#owner affiliation operations. SMACK-674.
This commit is contained in:
Florian Schmaus 2015-06-06 11:13:42 +02:00
parent 4a857fe67a
commit f7782aa466
3 changed files with 132 additions and 55 deletions

View File

@ -18,9 +18,13 @@ package org.jivesoftware.smackx.pubsub;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smackx.pubsub.packet.PubSubNamespace;
import org.jxmpp.jid.BareJid;
/**
* Represents a affiliation between a user and a node, where the {@link #type} defines
* Represents a affiliation between a user and a node, where the {@link Type} defines
* the type of affiliation.
*
* Affiliations are retrieved from the {@link PubSubManager#getAffiliations()} method, which
@ -31,8 +35,12 @@ import org.jivesoftware.smack.packet.ExtensionElement;
*/
public class Affiliation implements ExtensionElement
{
protected String node;
protected Type type;
public static final String ELEMENT = "affiliation";
private final BareJid jid;
private final String node;
private final Type affiliation;
private final PubSubNamespace namespace;
public enum Type
{
@ -42,52 +50,93 @@ public class Affiliation implements ExtensionElement
/**
* Constructs an affiliation.
*
* @param nodeId The node the user is affiliated with.
* @param affiliation The type of affiliation.
* @param node The node the user is affiliated with.
* @param affiliation the optional affiliation.
*/
public Affiliation(String nodeId, Type affiliation)
{
node = nodeId;
type = affiliation;
}
public Affiliation(String node, Type affiliation) {
this.node = StringUtils.requireNotNullOrEmpty(node, "node must not be null or empty");
this.affiliation = affiliation;
this.jid = null;
if (affiliation != null) {
namespace = PubSubNamespace.BASIC;
} else {
namespace = PubSubNamespace.OWNER;
}
}
public String getNodeId()
{
return node;
}
/**
* Construct a affiliation modification request.
*
* @param jid
* @param affiliation
*/
public Affiliation(BareJid jid, Type affiliation) {
this(jid, affiliation, PubSubNamespace.OWNER);
}
public Type getType()
{
return type;
}
public Affiliation(BareJid jid, Type affiliation, PubSubNamespace namespace) {
this.jid = jid;
this.affiliation = affiliation;
this.node = null;
// This is usually the pubsub#owner namesapce, but see xep60 example 208 where just 'pubsub' is used
// ("notification of affilliation change")
this.namespace = namespace;
}
public String getElementName()
{
return "subscription";
}
/**
* Get the node.
*
* @return the node.
* @deprecated use {@link #getNode} instead.
*/
@Deprecated
public String getNodeId() {
return getNode();
}
public String getNamespace()
{
return null;
}
public String getNode() {
return node;
}
public String toXML()
{
StringBuilder builder = new StringBuilder("<");
builder.append(getElementName());
appendAttribute(builder, "node", node);
appendAttribute(builder, "affiliation", type.toString());
/**
* Get the type.
*
* @return the type.
* @deprecated use {@link #getAffiliation()} instead.
*/
@Deprecated
public Type getType() {
return getAffiliation();
}
builder.append("/>");
return builder.toString();
}
public Type getAffiliation() {
return affiliation;
}
private static void appendAttribute(StringBuilder builder, String att, String value)
{
builder.append(" ");
builder.append(att);
builder.append("='");
builder.append(value);
builder.append("'");
}
public BareJid getJid() {
return jid;
}
@Override
public String getElementName() {
return ELEMENT;
}
public String getNamespace() {
return namespace.getXmlns();
}
public PubSubNamespace getPubSubNamespace() {
return namespace;
}
@Override
public XmlStringBuilder toXML() {
XmlStringBuilder xml = new XmlStringBuilder(this);
xml.optAttribute("node", node);
xml.optAttribute("jid", jid);
xml.optAttribute("affiliation", affiliation);
xml.closeEmptyElement();
return xml;
}
}

View File

@ -16,12 +16,13 @@
*/
package org.jivesoftware.smackx.pubsub.provider;
import java.util.List;
import java.util.Map;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.provider.EmbeddedExtensionProvider;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.provider.ExtensionElementProvider;
import org.jivesoftware.smack.util.ParserUtils;
import org.jivesoftware.smackx.pubsub.Affiliation;
import org.jivesoftware.smackx.pubsub.packet.PubSubNamespace;
import org.jxmpp.jid.BareJid;
import org.xmlpull.v1.XmlPullParser;
/**
* Parses the affiliation element out of the reply stanza from the server
@ -29,12 +30,32 @@ import org.jivesoftware.smackx.pubsub.Affiliation;
*
* @author Robin Collier
*/
public class AffiliationProvider extends EmbeddedExtensionProvider<Affiliation>
{
@Override
protected Affiliation createReturnExtension(String currentElement, String currentNamespace, Map<String, String> attributeMap, List<? extends ExtensionElement> content)
{
return new Affiliation(attributeMap.get("node"), Affiliation.Type.valueOf(attributeMap.get("affiliation")));
}
public class AffiliationProvider extends ExtensionElementProvider<Affiliation> {
@Override
public Affiliation parse(XmlPullParser parser, int initialDepth)
throws Exception {
String node = parser.getAttributeValue(null, "node");
BareJid jid = ParserUtils.getBareJidAttribute(parser);
String affiliationString = parser.getAttributeValue(null, "affiliation");
Affiliation.Type affiliationType = null;
if (affiliationString != null) {
affiliationType = Affiliation.Type.valueOf(affiliationString);
}
Affiliation affiliation;
if (node != null && jid == null) {
// affiliationType may be empty
affiliation = new Affiliation(node, affiliationType);
}
else if (node == null && jid != null) {
PubSubNamespace namespace = null; // TODO
affiliation = new Affiliation(jid, affiliationType, namespace);
}
else {
throw new SmackException("Invalid affililation");
}
return affiliation;
}
}

View File

@ -321,6 +321,13 @@
</extensionProvider>
<!-- XEP-0060 pubsub#owner -->
<extensionProvider>
<elementName>affiliation</elementName>
<namespace>http://jabber.org/protocol/pubsub#owner</namespace>
<className>org.jivesoftware.smackx.pubsub.provider.AffiliationProvider</className>
</extensionProvider>
<iqProvider>
<elementName>pubsub</elementName>
<namespace>http://jabber.org/protocol/pubsub#owner</namespace>