Affiliation.java

  1. /**
  2.  *
  3.  * Copyright the original author or authors
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.pubsub;

  18. import org.jivesoftware.smack.XMPPConnection;
  19. import org.jivesoftware.smack.packet.XmlElement;
  20. import org.jivesoftware.smack.util.Objects;
  21. import org.jivesoftware.smack.util.StringUtils;
  22. import org.jivesoftware.smack.util.XmlStringBuilder;

  23. import org.jivesoftware.smackx.pubsub.packet.PubSubNamespace;

  24. import org.jxmpp.jid.BareJid;

  25. /**
  26.  * Represents a affiliation between a user and a node, where the {@link Type} defines
  27.  * the type of affiliation.
  28.  *
  29.  * Affiliations are retrieved from the {@link PubSubManager#getAffiliations()} method, which
  30.  * gets affiliations for the calling user, based on the identity that is associated with
  31.  * the {@link XMPPConnection}.
  32.  *
  33.  * @author Robin Collier
  34.  */
  35. public class Affiliation implements XmlElement {
  36.     public static final String ELEMENT = "affiliation";

  37.     public enum AffiliationNamespace {
  38.         basic(PubSubElementType.AFFILIATIONS),
  39.         owner(PubSubElementType.AFFILIATIONS_OWNER),
  40.         ;
  41.         public final PubSubElementType type;

  42.         AffiliationNamespace(PubSubElementType type) {
  43.             this.type = type;
  44.         }

  45.         public static AffiliationNamespace fromXmlns(String xmlns) {
  46.             for (AffiliationNamespace affiliationsNamespace : AffiliationNamespace.values()) {
  47.                 if (affiliationsNamespace.type.getNamespace().getXmlns().equals(xmlns)) {
  48.                     return affiliationsNamespace;
  49.                 }
  50.             }
  51.             throw new IllegalArgumentException("Invalid affiliations namespace: " + xmlns);
  52.         }
  53.     }

  54.     private final BareJid jid;
  55.     private final String node;
  56.     private final Type affiliation;
  57.     private final AffiliationNamespace namespace;

  58.     public enum Type {
  59.         member, none, outcast, owner, publisher
  60.     }

  61.     /**
  62.      * Constructs an affiliation.
  63.      *
  64.      * @param node The node the user is affiliated with.
  65.      * @param affiliation the optional affiliation.
  66.      */
  67.     public Affiliation(String node, Type affiliation) {
  68.         this(node, affiliation, affiliation == null ? AffiliationNamespace.basic : AffiliationNamespace.owner);
  69.     }

  70.     /**
  71.      * Constructs an affiliation.
  72.      *
  73.      * @param node The node the user is affiliated with.
  74.      * @param affiliation the optional affiliation.
  75.      * @param namespace the affiliation's namespace.
  76.      */
  77.     public Affiliation(String node, Type affiliation, AffiliationNamespace namespace) {
  78.         this.node = StringUtils.requireNotNullNorEmpty(node, "node must not be null nor empty");
  79.         this.affiliation = affiliation;
  80.         this.jid = null;
  81.         this.namespace = Objects.requireNonNull(namespace);
  82.     }

  83.     /**
  84.      * Construct a affiliation modification request.
  85.      *
  86.      * @param jid TODO javadoc me please
  87.      * @param affiliation TODO javadoc me please
  88.      */
  89.     public Affiliation(BareJid jid, Type affiliation) {
  90.         this(jid, affiliation, AffiliationNamespace.owner);
  91.     }

  92.     public Affiliation(BareJid jid, Type affiliation, AffiliationNamespace namespace) {
  93.         this.jid = jid;
  94.         this.affiliation = affiliation;
  95.         this.node = null;
  96.         // This is usually the pubsub#owner namespace, but see xep60 example 208 where just 'pubsub' is used
  97.         // ("notification of affiliation change")
  98.         this.namespace = namespace;
  99.     }

  100.     /**
  101.      * Get the node.
  102.      *
  103.      * @return the node.
  104.      * @deprecated use {@link #getNode} instead.
  105.      */
  106.     @Deprecated
  107.     public String getNodeId() {
  108.         return getNode();
  109.     }

  110.     public String getNode() {
  111.         return node;
  112.     }

  113.     /**
  114.      * Get the type.
  115.      *
  116.      * @return the type.
  117.      * @deprecated use {@link #getAffiliation()} instead.
  118.      */
  119.     @Deprecated
  120.     public Type getType() {
  121.         return getAffiliation();
  122.     }

  123.     public Type getAffiliation() {
  124.         return affiliation;
  125.     }

  126.     public BareJid getJid() {
  127.         return jid;
  128.     }

  129.     @Override
  130.     public String getElementName() {
  131.         return ELEMENT;
  132.     }

  133.     @Override
  134.     public String getNamespace() {
  135.         return getPubSubNamespace().getXmlns();
  136.     }

  137.     public PubSubNamespace getPubSubNamespace() {
  138.         return namespace.type.getNamespace();
  139.     }

  140.     /**
  141.      * Check if this is an affiliation element to modify affiliations on a node.
  142.      *
  143.      * @return <code>true</code> if this is an affiliation element to modify affiliations on a node, <code>false</code> otherwise.
  144.      * @since 4.2
  145.      */
  146.     public boolean isAffiliationModification() {
  147.         if (jid != null && affiliation != null) {
  148.             assert node == null && namespace == AffiliationNamespace.owner;
  149.             return true;
  150.         }
  151.         return false;
  152.     }

  153.     @Override
  154.     public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  155.         XmlStringBuilder xml = new XmlStringBuilder(this);
  156.         xml.optAttribute("node", node);
  157.         xml.optAttribute("jid", jid);
  158.         xml.optAttribute("affiliation", affiliation);
  159.         xml.closeEmptyElement();
  160.         return xml;
  161.     }
  162. }