001/**
002 *
003 * Copyright the original author or authors
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.pubsub;
018
019/**
020 * Represents a subscription to node for both requests and replies.
021 * 
022 * @author Robin Collier
023 */
024public class Subscription extends NodeExtension
025{
026        protected String jid;
027        protected String id;
028        protected State state;
029        protected boolean configRequired = false;
030        
031        public enum State
032        {
033                subscribed, unconfigured, pending, none 
034        }
035
036        /**
037         * Used to constructs a subscription request to the root node with the specified
038         * JID.
039         * 
040         * @param subscriptionJid The subscriber JID
041         */
042        public Subscription(String subscriptionJid)
043        {
044                this(subscriptionJid, null, null, null);
045        }
046        
047        /**
048         * Used to constructs a subscription request to the specified node with the specified
049         * JID.
050         * 
051         * @param subscriptionJid The subscriber JID
052         * @param nodeId The node id
053         */
054        public Subscription(String subscriptionJid, String nodeId)
055        {
056                this(subscriptionJid, nodeId, null, null);
057        }
058        
059        /**
060         * Constructs a representation of a subscription reply to the specified node 
061         * and JID.  The server will have supplied the subscription id and current state.
062         * 
063         * @param jid The JID the request was made under
064         * @param nodeId The node subscribed to
065         * @param subscriptionId The id of this subscription
066         * @param state The current state of the subscription
067         */
068        public Subscription(String jid, String nodeId, String subscriptionId, State state)
069        {
070                super(PubSubElementType.SUBSCRIPTION, nodeId);
071                this.jid = jid;
072                id = subscriptionId;
073                this.state = state;
074        }
075        
076        /**
077         * Constructs a representation of a subscription reply to the specified node 
078         * and JID.  The server will have supplied the subscription id and current state
079         * and whether the subscription need to be configured.
080         * 
081         * @param jid The JID the request was made under
082         * @param nodeId The node subscribed to
083         * @param subscriptionId The id of this subscription
084         * @param state The current state of the subscription
085         * @param configRequired Is configuration required to complete the subscription 
086         */
087        public Subscription(String jid, String nodeId, String subscriptionId, State state, boolean configRequired)
088        {
089                super(PubSubElementType.SUBSCRIPTION, nodeId);
090                this.jid = jid;
091                id = subscriptionId;
092                this.state = state;
093                this.configRequired = configRequired;
094        }
095        
096        /**
097         * Gets the JID the subscription is created for
098         * 
099         * @return The JID
100         */
101        public String getJid()
102        {
103                return jid;
104        }
105        
106        /**
107         * Gets the subscription id
108         * 
109         * @return The subscription id
110         */
111        public String getId()
112        {
113                return id;
114        }
115        
116        /**
117         * Gets the current subscription state.
118         * 
119         * @return Current subscription state
120         */
121        public State getState()
122        {
123                return state;
124        }
125
126        /**
127         * This value is only relevant when the {@link #getState()} is {@link State#unconfigured}
128         * 
129         * @return true if configuration is required, false otherwise
130         */
131        public boolean isConfigRequired()
132        {
133                return configRequired;
134        }
135        
136        public String toXML()
137        {
138                StringBuilder builder = new StringBuilder("<subscription");
139                appendAttribute(builder, "jid", jid);
140                
141                if (getNode() != null)
142                        appendAttribute(builder, "node", getNode());
143                
144                if (id != null)
145                        appendAttribute(builder, "subid", id);
146                
147                if (state != null)
148                        appendAttribute(builder, "subscription", state.toString());
149                
150                builder.append("/>");
151                return builder.toString();
152        }
153
154        private void appendAttribute(StringBuilder builder, String att, String value)
155        {
156                builder.append(" ");
157                builder.append(att);
158                builder.append("='");
159                builder.append(value);
160                builder.append("'");
161        }
162
163}