Home | History | Annotate | Download | only in pubsub
      1 /**
      2  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
      3  * you may not use this file except in compliance with the License.
      4  * You may obtain a copy of the License at
      5  *
      6  *     http://www.apache.org/licenses/LICENSE-2.0
      7  *
      8  * Unless required by applicable law or agreed to in writing, software
      9  * distributed under the License is distributed on an "AS IS" BASIS,
     10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     11  * See the License for the specific language governing permissions and
     12  * limitations under the License.
     13  */
     14 package org.jivesoftware.smackx.pubsub;
     15 
     16 /**
     17  * Represents a subscription to node for both requests and replies.
     18  *
     19  * @author Robin Collier
     20  */
     21 public class Subscription extends NodeExtension
     22 {
     23 	protected String jid;
     24 	protected String id;
     25 	protected State state;
     26 	protected boolean configRequired = false;
     27 
     28 	public enum State
     29 	{
     30 		subscribed, unconfigured, pending, none
     31 	}
     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(String subscriptionJid)
     40 	{
     41 		this(subscriptionJid, null, null, null);
     42 	}
     43 
     44 	/**
     45 	 * Used to constructs a subscription request to the specified node with the specified
     46 	 * JID.
     47 	 *
     48 	 * @param subscriptionJid The subscriber JID
     49 	 * @param nodeId The node id
     50 	 */
     51 	public Subscription(String subscriptionJid, String nodeId)
     52 	{
     53 		this(subscriptionJid, nodeId, null, null);
     54 	}
     55 
     56 	/**
     57 	 * Constructs a representation of a subscription reply to the specified node
     58 	 * and JID.  The server	will have supplied the subscription id and current state.
     59 	 *
     60 	 * @param jid The JID the request was made under
     61 	 * @param nodeId The node subscribed to
     62 	 * @param subscriptionId The id of this subscription
     63 	 * @param state The current state of the subscription
     64 	 */
     65 	public Subscription(String jid, String nodeId, String subscriptionId, State state)
     66 	{
     67 		super(PubSubElementType.SUBSCRIPTION, nodeId);
     68 		this.jid = jid;
     69 		id = subscriptionId;
     70 		this.state = state;
     71 	}
     72 
     73 	/**
     74 	 * Constructs a representation of a subscription reply to the specified node
     75 	 * and JID.  The server	will have supplied the subscription id and current state
     76 	 * and whether the subscription need to be configured.
     77 	 *
     78 	 * @param jid The JID the request was made under
     79 	 * @param nodeId The node subscribed to
     80 	 * @param subscriptionId The id of this subscription
     81 	 * @param state The current state of the subscription
     82 	 * @param configRequired Is configuration required to complete the subscription
     83 	 */
     84 	public Subscription(String jid, String nodeId, String subscriptionId, State state, boolean configRequired)
     85 	{
     86 		super(PubSubElementType.SUBSCRIPTION, nodeId);
     87 		this.jid = jid;
     88 		id = subscriptionId;
     89 		this.state = state;
     90 		this.configRequired = configRequired;
     91 	}
     92 
     93 	/**
     94 	 * Gets the JID the subscription is created for
     95 	 *
     96 	 * @return The JID
     97 	 */
     98 	public String getJid()
     99 	{
    100 		return jid;
    101 	}
    102 
    103 	/**
    104 	 * Gets the subscription id
    105 	 *
    106 	 * @return The subscription id
    107 	 */
    108 	public String getId()
    109 	{
    110 		return id;
    111 	}
    112 
    113 	/**
    114 	 * Gets the current subscription state.
    115 	 *
    116 	 * @return Current subscription state
    117 	 */
    118 	public State getState()
    119 	{
    120 		return state;
    121 	}
    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 
    151 	private void appendAttribute(StringBuilder builder, String att, String value)
    152 	{
    153 		builder.append(" ");
    154 		builder.append(att);
    155 		builder.append("='");
    156 		builder.append(value);
    157 		builder.append("'");
    158 	}
    159 
    160 }
    161