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
019import org.jivesoftware.smack.util.XmlStringBuilder;
020
021import org.jxmpp.jid.Jid;
022
023/**
024 * Represents a subscription to node for both requests and replies.
025 *
026 * @author Robin Collier
027 */
028public class Subscription extends NodeExtension {
029    protected Jid jid;
030    protected String id;
031    protected State state;
032    protected boolean configRequired = false;
033
034    public enum State {
035        subscribed, unconfigured, pending, none
036    }
037
038    /**
039     * Used to constructs a subscription request to the root node with the specified
040     * JID.
041     *
042     * @param subscriptionJid The subscriber JID
043     */
044    public Subscription(Jid subscriptionJid) {
045        this(subscriptionJid, null, null, null);
046    }
047
048    /**
049     * Used to constructs a subscription request to the specified node with the specified
050     * JID.
051     *
052     * @param subscriptionJid The subscriber JID
053     * @param nodeId The node id
054     */
055    public Subscription(Jid subscriptionJid, String nodeId) {
056        this(subscriptionJid, nodeId, null, null);
057    }
058
059    /**
060     * Construct a subscription change request to the specified state.
061     *
062     * @param subscriptionJid The subscriber JID
063     * @param state The requested new state
064     */
065    public Subscription(Jid subscriptionJid, State state) {
066        this(subscriptionJid, null, null, state);
067    }
068
069    /**
070     * Constructs a representation of a subscription reply to the specified node
071     * and JID.  The server will have supplied the subscription id and current state.
072     *
073     * @param jid The JID the request was made under
074     * @param nodeId The node subscribed to
075     * @param subscriptionId The id of this subscription
076     * @param state The current state of the subscription
077     */
078    public Subscription(Jid jid, String nodeId, String subscriptionId, State state) {
079        super(PubSubElementType.SUBSCRIPTION, nodeId);
080        this.jid = jid;
081        id = subscriptionId;
082        this.state = state;
083    }
084
085    /**
086     * Constructs a representation of a subscription reply to the specified node
087     * and JID.  The server will have supplied the subscription id and current state
088     * and whether the subscription need to be configured.
089     *
090     * @param jid The JID the request was made under
091     * @param nodeId The node subscribed to
092     * @param subscriptionId The id of this subscription
093     * @param state The current state of the subscription
094     * @param configRequired Is configuration required to complete the subscription
095     */
096    public Subscription(Jid jid, String nodeId, String subscriptionId, State state, boolean configRequired) {
097        super(PubSubElementType.SUBSCRIPTION, nodeId);
098        this.jid = jid;
099        id = subscriptionId;
100        this.state = state;
101        this.configRequired = configRequired;
102    }
103
104    /**
105     * Gets the JID the subscription is created for.
106     *
107     * @return The JID
108     */
109    public Jid getJid() {
110        return jid;
111    }
112
113    /**
114     * Gets the subscription id.
115     *
116     * @return The subscription id
117     */
118    public String getId() {
119        return id;
120    }
121
122    /**
123     * Gets the current subscription state.
124     *
125     * @return Current subscription state
126     */
127    public State getState() {
128        return state;
129    }
130
131    /**
132     * This value is only relevant when the {@link #getState()} is {@link State#unconfigured}.
133     *
134     * @return true if configuration is required, false otherwise
135     */
136    public boolean isConfigRequired() {
137        return configRequired;
138    }
139
140    @Override
141    public XmlStringBuilder toXML(String enclosingNamespace) {
142        XmlStringBuilder builder = new XmlStringBuilder(this);
143        builder.attribute("jid", jid);
144
145        builder.optAttribute("node", getNode());
146        builder.optAttribute("subid", id);
147        builder.optAttribute("subscription", state.toString());
148
149        builder.closeEmptyElement();
150        return builder;
151    }
152
153    private static void appendAttribute(StringBuilder builder, String att, String value) {
154        builder.append(' ');
155        builder.append(att);
156        builder.append("='");
157        builder.append(value);
158        builder.append('\'');
159    }
160
161}