mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-22 22:32:06 +01:00
Extend PubSub Affiliation class by 'jid'
to allow pubsub#owner affiliation operations. SMACK-674.
This commit is contained in:
parent
4a857fe67a
commit
f7782aa466
3 changed files with 132 additions and 55 deletions
|
@ -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.
|
||||||
return node;
|
*
|
||||||
}
|
* @param jid
|
||||||
|
* @param affiliation
|
||||||
|
*/
|
||||||
|
public Affiliation(BareJid jid, Type affiliation) {
|
||||||
|
this(jid, affiliation, PubSubNamespace.OWNER);
|
||||||
|
}
|
||||||
|
|
||||||
public Type getType()
|
public Affiliation(BareJid jid, Type affiliation, PubSubNamespace namespace) {
|
||||||
{
|
this.jid = jid;
|
||||||
return type;
|
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()
|
/**
|
||||||
{
|
* Get the node.
|
||||||
return "subscription";
|
*
|
||||||
}
|
* @return the node.
|
||||||
|
* @deprecated use {@link #getNode} instead.
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
public String getNodeId() {
|
||||||
|
return getNode();
|
||||||
|
}
|
||||||
|
|
||||||
public String getNamespace()
|
public String getNode() {
|
||||||
{
|
return node;
|
||||||
return null;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public String toXML()
|
/**
|
||||||
{
|
* Get the type.
|
||||||
StringBuilder builder = new StringBuilder("<");
|
*
|
||||||
builder.append(getElementName());
|
* @return the type.
|
||||||
appendAttribute(builder, "node", node);
|
* @deprecated use {@link #getAffiliation()} instead.
|
||||||
appendAttribute(builder, "affiliation", type.toString());
|
*/
|
||||||
|
@Deprecated
|
||||||
|
public Type getType() {
|
||||||
|
return getAffiliation();
|
||||||
|
}
|
||||||
|
|
||||||
builder.append("/>");
|
public Type getAffiliation() {
|
||||||
return builder.toString();
|
return affiliation;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void appendAttribute(StringBuilder builder, String att, String value)
|
public BareJid getJid() {
|
||||||
{
|
return jid;
|
||||||
builder.append(" ");
|
}
|
||||||
builder.append(att);
|
|
||||||
builder.append("='");
|
@Override
|
||||||
builder.append(value);
|
public String getElementName() {
|
||||||
builder.append("'");
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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>
|
||||||
|
|
Loading…
Reference in a new issue