aboutsummaryrefslogtreecommitdiff
path: root/src/org/jivesoftware/smackx/pubsub/Subscription.java
blob: 19ad8a8f1a57abeea4bedc703a3e63d1f25abc6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jivesoftware.smackx.pubsub;

/**
 * Represents a subscription to node for both requests and replies.
 * 
 * @author Robin Collier
 */
public class Subscription extends NodeExtension
{
	protected String jid;
	protected String id;
	protected State state;
	protected boolean configRequired = false;
	
	public enum State
	{
		subscribed, unconfigured, pending, none 
	}

	/**
	 * Used to constructs a subscription request to the root node with the specified
	 * JID.
	 * 
	 * @param subscriptionJid The subscriber JID
	 */
	public Subscription(String subscriptionJid)
	{
		this(subscriptionJid, null, null, null);
	}
	
	/**
	 * Used to constructs a subscription request to the specified node with the specified
	 * JID.
	 * 
	 * @param subscriptionJid The subscriber JID
	 * @param nodeId The node id
	 */
	public Subscription(String subscriptionJid, String nodeId)
	{
		this(subscriptionJid, nodeId, null, null);
	}
	
	/**
	 * Constructs a representation of a subscription reply to the specified node 
	 * and JID.  The server	will have supplied the subscription id and current state.
	 * 
	 * @param jid The JID the request was made under
	 * @param nodeId The node subscribed to
	 * @param subscriptionId The id of this subscription
	 * @param state The current state of the subscription
	 */
	public Subscription(String jid, String nodeId, String subscriptionId, State state)
	{
		super(PubSubElementType.SUBSCRIPTION, nodeId);
		this.jid = jid;
		id = subscriptionId;
		this.state = state;
	}
	
	/**
	 * Constructs a representation of a subscription reply to the specified node 
	 * and JID.  The server	will have supplied the subscription id and current state
	 * and whether the subscription need to be configured.
	 * 
	 * @param jid The JID the request was made under
	 * @param nodeId The node subscribed to
	 * @param subscriptionId The id of this subscription
	 * @param state The current state of the subscription
	 * @param configRequired Is configuration required to complete the subscription 
	 */
	public Subscription(String jid, String nodeId, String subscriptionId, State state, boolean configRequired)
	{
		super(PubSubElementType.SUBSCRIPTION, nodeId);
		this.jid = jid;
		id = subscriptionId;
		this.state = state;
		this.configRequired = configRequired;
	}
	
	/**
	 * Gets the JID the subscription is created for
	 * 
	 * @return The JID
	 */
	public String getJid()
	{
		return jid;
	}
	
	/**
	 * Gets the subscription id
	 * 
	 * @return The subscription id
	 */
	public String getId()
	{
		return id;
	}
	
	/**
	 * Gets the current subscription state.
	 * 
	 * @return Current subscription state
	 */
	public State getState()
	{
		return state;
	}

	/**
	 * This value is only relevant when the {@link #getState()} is {@link State#unconfigured}
	 * 
	 * @return true if configuration is required, false otherwise
	 */
	public boolean isConfigRequired()
	{
		return configRequired;
	}
	
	public String toXML()
	{
		StringBuilder builder = new StringBuilder("<subscription");
		appendAttribute(builder, "jid", jid);
		
		if (getNode() != null)
			appendAttribute(builder, "node", getNode());
		
		if (id != null)
			appendAttribute(builder, "subid", id);
		
		if (state != null)
			appendAttribute(builder, "subscription", state.toString());
		
		builder.append("/>");
		return builder.toString();
	}

	private void appendAttribute(StringBuilder builder, String att, String value)
	{
		builder.append(" ");
		builder.append(att);
		builder.append("='");
		builder.append(value);
		builder.append("'");
	}

}