Subscription.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.util.XmlStringBuilder;

  19. import org.jxmpp.jid.Jid;

  20. /**
  21.  * Represents a subscription to node for both requests and replies.
  22.  *
  23.  * @author Robin Collier
  24.  */
  25. public class Subscription extends NodeExtension {
  26.     protected Jid jid;
  27.     protected String id;
  28.     protected State state;
  29.     protected boolean configRequired = false;

  30.     public enum State {
  31.         subscribed, unconfigured, pending, none
  32.     }

  33.     /**
  34.      * Used to constructs a subscription request to the root node with the specified
  35.      * JID.
  36.      *
  37.      * @param subscriptionJid The subscriber JID
  38.      */
  39.     public Subscription(Jid subscriptionJid) {
  40.         this(subscriptionJid, null, null, null);
  41.     }

  42.     /**
  43.      * Used to constructs a subscription request to the specified node with the specified
  44.      * JID.
  45.      *
  46.      * @param subscriptionJid The subscriber JID
  47.      * @param nodeId The node id
  48.      */
  49.     public Subscription(Jid subscriptionJid, String nodeId) {
  50.         this(subscriptionJid, nodeId, null, null);
  51.     }

  52.     /**
  53.      * Construct a subscription change request to the specified state.
  54.      *
  55.      * @param subscriptionJid The subscriber JID
  56.      * @param state The requested new state
  57.      */
  58.     public Subscription(Jid subscriptionJid, State state) {
  59.         this(subscriptionJid, null, null, state);
  60.     }

  61.     /**
  62.      * Constructs a representation of a subscription reply to the specified node
  63.      * and JID.  The server will have supplied the subscription id and current state.
  64.      *
  65.      * @param jid The JID the request was made under
  66.      * @param nodeId The node subscribed to
  67.      * @param subscriptionId The id of this subscription
  68.      * @param state The current state of the subscription
  69.      */
  70.     public Subscription(Jid jid, String nodeId, String subscriptionId, State state) {
  71.         super(PubSubElementType.SUBSCRIPTION, nodeId);
  72.         this.jid = jid;
  73.         id = subscriptionId;
  74.         this.state = state;
  75.     }

  76.     /**
  77.      * Constructs a representation of a subscription reply to the specified node
  78.      * and JID.  The server will have supplied the subscription id and current state
  79.      * and whether the subscription need to be configured.
  80.      *
  81.      * @param jid The JID the request was made under
  82.      * @param nodeId The node subscribed to
  83.      * @param subscriptionId The id of this subscription
  84.      * @param state The current state of the subscription
  85.      * @param configRequired Is configuration required to complete the subscription
  86.      */
  87.     public Subscription(Jid jid, String nodeId, String subscriptionId, State state, boolean configRequired) {
  88.         super(PubSubElementType.SUBSCRIPTION, nodeId);
  89.         this.jid = jid;
  90.         id = subscriptionId;
  91.         this.state = state;
  92.         this.configRequired = configRequired;
  93.     }

  94.     /**
  95.      * Gets the JID the subscription is created for.
  96.      *
  97.      * @return The JID
  98.      */
  99.     public Jid getJid() {
  100.         return jid;
  101.     }

  102.     /**
  103.      * Gets the subscription id.
  104.      *
  105.      * @return The subscription id
  106.      */
  107.     public String getId() {
  108.         return id;
  109.     }

  110.     /**
  111.      * Gets the current subscription state.
  112.      *
  113.      * @return Current subscription state
  114.      */
  115.     public State getState() {
  116.         return state;
  117.     }

  118.     /**
  119.      * This value is only relevant when the {@link #getState()} is {@link State#unconfigured}.
  120.      *
  121.      * @return true if configuration is required, false otherwise
  122.      */
  123.     public boolean isConfigRequired() {
  124.         return configRequired;
  125.     }

  126.     @Override
  127.     protected void addXml(XmlStringBuilder xml) {
  128.         xml.attribute("jid", jid);
  129.         xml.optAttribute("subid", id);
  130.         xml.optAttribute("subscription", state);
  131.         xml.closeEmptyElement();
  132.     }

  133. }