1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-22 20:12:07 +01:00

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.XMPPConnection;
import org.jivesoftware.smack.packet.ExtensionElement; 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. * the type of affiliation.
* *
* Affiliations are retrieved from the {@link PubSubManager#getAffiliations()} method, which * 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 public class Affiliation implements ExtensionElement
{ {
protected String node; public static final String ELEMENT = "affiliation";
protected Type type;
private final BareJid jid;
private final String node;
private final Type affiliation;
private final PubSubNamespace namespace;
public enum Type public enum Type
{ {
@ -42,52 +50,93 @@ public class Affiliation implements ExtensionElement
/** /**
* Constructs an affiliation. * Constructs an affiliation.
* *
* @param nodeId The node the user is affiliated with. * @param node The node the user is affiliated with.
* @param affiliation The type of affiliation. * @param affiliation the optional affiliation.
*/ */
public Affiliation(String nodeId, Type affiliation) public Affiliation(String node, Type affiliation) {
{ this.node = StringUtils.requireNotNullOrEmpty(node, "node must not be null or empty");
node = nodeId; this.affiliation = affiliation;
type = affiliation; this.jid = null;
if (affiliation != null) {
namespace = PubSubNamespace.BASIC;
} else {
namespace = PubSubNamespace.OWNER;
}
} }
public String getNodeId() /**
{ * Construct a affiliation modification request.
*
* @param jid
* @param affiliation
*/
public Affiliation(BareJid jid, Type affiliation) {
this(jid, affiliation, PubSubNamespace.OWNER);
}
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;
}
/**
* Get the node.
*
* @return the node.
* @deprecated use {@link #getNode} instead.
*/
@Deprecated
public String getNodeId() {
return getNode();
}
public String getNode() {
return node; return node;
} }
public Type getType() /**
{ * Get the type.
return type; *
* @return the type.
* @deprecated use {@link #getAffiliation()} instead.
*/
@Deprecated
public Type getType() {
return getAffiliation();
} }
public String getElementName() public Type getAffiliation() {
{ return affiliation;
return "subscription";
} }
public String getNamespace() public BareJid getJid() {
{ return jid;
return null;
} }
public String toXML() @Override
{ public String getElementName() {
StringBuilder builder = new StringBuilder("<"); return ELEMENT;
builder.append(getElementName());
appendAttribute(builder, "node", node);
appendAttribute(builder, "affiliation", type.toString());
builder.append("/>");
return builder.toString();
} }
private static void appendAttribute(StringBuilder builder, String att, String value) public String getNamespace() {
{ return namespace.getXmlns();
builder.append(" "); }
builder.append(att);
builder.append("='"); public PubSubNamespace getPubSubNamespace() {
builder.append(value); return namespace;
builder.append("'"); }
@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; package org.jivesoftware.smackx.pubsub.provider;
import java.util.List; import org.jivesoftware.smack.SmackException;
import java.util.Map; import org.jivesoftware.smack.provider.ExtensionElementProvider;
import org.jivesoftware.smack.util.ParserUtils;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.provider.EmbeddedExtensionProvider;
import org.jivesoftware.smackx.pubsub.Affiliation; 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 * 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 * @author Robin Collier
*/ */
public class AffiliationProvider extends EmbeddedExtensionProvider<Affiliation> public class AffiliationProvider extends ExtensionElementProvider<Affiliation> {
{
@Override @Override
protected Affiliation createReturnExtension(String currentElement, String currentNamespace, Map<String, String> attributeMap, List<? extends ExtensionElement> content) public Affiliation parse(XmlPullParser parser, int initialDepth)
{ throws Exception {
return new Affiliation(attributeMap.get("node"), Affiliation.Type.valueOf(attributeMap.get("affiliation"))); 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> </extensionProvider>
<!-- XEP-0060 pubsub#owner --> <!-- 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> <iqProvider>
<elementName>pubsub</elementName> <elementName>pubsub</elementName>
<namespace>http://jabber.org/protocol/pubsub#owner</namespace> <namespace>http://jabber.org/protocol/pubsub#owner</namespace>