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. /**
  19.  * Represents a subscription to node for both requests and replies.
  20.  *
  21.  * @author Robin Collier
  22.  */
  23. public class Subscription extends NodeExtension
  24. {
  25.     protected String jid;
  26.     protected String id;
  27.     protected State state;
  28.     protected boolean configRequired = false;
  29.    
  30.     public enum State
  31.     {
  32.         subscribed, unconfigured, pending, none
  33.     }

  34.     /**
  35.      * Used to constructs a subscription request to the root node with the specified
  36.      * JID.
  37.      *
  38.      * @param subscriptionJid The subscriber JID
  39.      */
  40.     public Subscription(String subscriptionJid)
  41.     {
  42.         this(subscriptionJid, null, null, null);
  43.     }
  44.    
  45.     /**
  46.      * Used to constructs a subscription request to the specified node with the specified
  47.      * JID.
  48.      *
  49.      * @param subscriptionJid The subscriber JID
  50.      * @param nodeId The node id
  51.      */
  52.     public Subscription(String subscriptionJid, String nodeId)
  53.     {
  54.         this(subscriptionJid, nodeId, null, null);
  55.     }
  56.    
  57.     /**
  58.      * Constructs a representation of a subscription reply to the specified node
  59.      * and JID.  The server will have supplied the subscription id and current state.
  60.      *
  61.      * @param jid The JID the request was made under
  62.      * @param nodeId The node subscribed to
  63.      * @param subscriptionId The id of this subscription
  64.      * @param state The current state of the subscription
  65.      */
  66.     public Subscription(String jid, String nodeId, String subscriptionId, State state)
  67.     {
  68.         super(PubSubElementType.SUBSCRIPTION, nodeId);
  69.         this.jid = jid;
  70.         id = subscriptionId;
  71.         this.state = state;
  72.     }
  73.    
  74.     /**
  75.      * Constructs a representation of a subscription reply to the specified node
  76.      * and JID.  The server will have supplied the subscription id and current state
  77.      * and whether the subscription need to be configured.
  78.      *
  79.      * @param jid The JID the request was made under
  80.      * @param nodeId The node subscribed to
  81.      * @param subscriptionId The id of this subscription
  82.      * @param state The current state of the subscription
  83.      * @param configRequired Is configuration required to complete the subscription
  84.      */
  85.     public Subscription(String jid, String nodeId, String subscriptionId, State state, boolean configRequired)
  86.     {
  87.         super(PubSubElementType.SUBSCRIPTION, nodeId);
  88.         this.jid = jid;
  89.         id = subscriptionId;
  90.         this.state = state;
  91.         this.configRequired = configRequired;
  92.     }
  93.    
  94.     /**
  95.      * Gets the JID the subscription is created for
  96.      *
  97.      * @return The JID
  98.      */
  99.     public String getJid()
  100.     {
  101.         return jid;
  102.     }
  103.    
  104.     /**
  105.      * Gets the subscription id
  106.      *
  107.      * @return The subscription id
  108.      */
  109.     public String getId()
  110.     {
  111.         return id;
  112.     }
  113.    
  114.     /**
  115.      * Gets the current subscription state.
  116.      *
  117.      * @return Current subscription state
  118.      */
  119.     public State getState()
  120.     {
  121.         return state;
  122.     }

  123.     /**
  124.      * This value is only relevant when the {@link #getState()} is {@link State#unconfigured}
  125.      *
  126.      * @return true if configuration is required, false otherwise
  127.      */
  128.     public boolean isConfigRequired()
  129.     {
  130.         return configRequired;
  131.     }
  132.    
  133.     public String toXML()
  134.     {
  135.         StringBuilder builder = new StringBuilder("<subscription");
  136.         appendAttribute(builder, "jid", jid);
  137.        
  138.         if (getNode() != null)
  139.             appendAttribute(builder, "node", getNode());
  140.        
  141.         if (id != null)
  142.             appendAttribute(builder, "subid", id);
  143.        
  144.         if (state != null)
  145.             appendAttribute(builder, "subscription", state.toString());
  146.        
  147.         builder.append("/>");
  148.         return builder.toString();
  149.     }

  150.     private void appendAttribute(StringBuilder builder, String att, String value)
  151.     {
  152.         builder.append(" ");
  153.         builder.append(att);
  154.         builder.append("='");
  155.         builder.append(value);
  156.         builder.append("'");
  157.     }

  158. }