001/**
002 *
003 * Copyright 2003-2007 Jive Software, 2014-2021 Florian Schmaus
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 */
017
018package org.jivesoftware.smackx.jingle.element;
019
020import java.util.ArrayList;
021import java.util.Collections;
022import java.util.List;
023
024import org.jivesoftware.smack.XMPPConnection;
025import org.jivesoftware.smack.packet.IQ;
026import org.jivesoftware.smack.packet.IqBuilder;
027import org.jivesoftware.smack.packet.IqData;
028import org.jivesoftware.smack.packet.id.StandardStanzaIdSource;
029import org.jivesoftware.smack.util.Objects;
030import org.jivesoftware.smack.util.StringUtils;
031
032import org.jxmpp.jid.FullJid;
033
034/**
035 * The Jingle element.
036 *
037 * @author Florian Schmaus
038 */
039public final class Jingle extends IQ {
040
041    public static final String NAMESPACE = "urn:xmpp:jingle:1";
042
043    public static final String ACTION_ATTRIBUTE_NAME = "action";
044
045    public static final String INITIATOR_ATTRIBUTE_NAME = "initiator";
046
047    public static final String RESPONDER_ATTRIBUTE_NAME = "responder";
048
049    public static final String SESSION_ID_ATTRIBUTE_NAME = "sid";
050
051    public static final String ELEMENT = "jingle";
052
053    /**
054     * The session ID related to this session. The session ID is a unique identifier generated by the initiator. This
055     * should match the XML Nmtoken production so that XML character escaping is not needed for characters such as &.
056     */
057    private final String sessionId;
058
059    /**
060     * The jingle action. This attribute is required.
061     */
062    private final JingleAction action;
063
064    private final FullJid initiator;
065
066    private final FullJid responder;
067
068    private final JingleReason reason;
069
070    private final List<JingleContent> contents;
071
072    private Jingle(Builder builder, String sessionId, JingleAction action, FullJid initiator, FullJid responder, JingleReason reason,
073                    List<JingleContent> contents) {
074        super(builder, ELEMENT, NAMESPACE);
075        this.sessionId = StringUtils.requireNotNullNorEmpty(sessionId, "Jingle session ID must not be null");
076        this.action = Objects.requireNonNull(action, "Jingle action must not be null");
077        this.initiator = initiator;
078        this.responder = responder;
079        this.reason = reason;
080        if (contents != null) {
081            this.contents = Collections.unmodifiableList(contents);
082        }
083        else {
084            this.contents = Collections.emptyList();
085        }
086        setType(Type.set);
087    }
088
089    /**
090     * Get the initiator. The initiator will be the full JID of the entity that has initiated the flow (which may be
091     * different to the "from" address in the IQ)
092     *
093     * @return the initiator
094     */
095    public FullJid getInitiator() {
096        return initiator;
097    }
098
099    /**
100     * Get the responder. The responder is the full JID of the entity that has replied to the initiation (which may be
101     * different to the "to" address in the IQ).
102     *
103     * @return the responder
104     */
105    public FullJid getResponder() {
106        return responder;
107    }
108
109    /**
110     * Returns the session ID related to the session. The session ID is a unique identifier generated by the initiator.
111     * This should match the XML Nmtoken production so that XML character escaping is not needed for characters such as
112     * &amp;.
113     *
114     * @return Returns the session ID related to the session.
115     */
116    public String getSid() {
117        return sessionId;
118    }
119
120    /**
121     * Get the action specified in the jingle IQ.
122     *
123     * @return the action.
124     */
125    public JingleAction getAction() {
126        return action;
127    }
128
129    public JingleReason getReason() {
130        return reason;
131    }
132
133    /**
134     * Get a List of the contents.
135     *
136     * @return the contents.
137     */
138    public List<JingleContent> getContents() {
139        return contents;
140    }
141
142    /**
143     * Get the only jingle content if one exists, or <code>null</code>. This method will throw an
144     * {@link IllegalStateException} if there is more than one jingle content.
145     *
146     * @return a JingleContent instance or <code>null</code>.
147     * @throws IllegalStateException if there is more than one jingle content.
148     */
149    public JingleContent getSoleContentOrThrow() {
150        if (contents.isEmpty()) {
151            return null;
152        }
153
154        if (contents.size() > 1) {
155            throw new IllegalStateException();
156        }
157
158        return contents.get(0);
159    }
160
161    @Override
162    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
163        xml.optAttribute(INITIATOR_ATTRIBUTE_NAME, getInitiator());
164        xml.optAttribute(RESPONDER_ATTRIBUTE_NAME, getResponder());
165        xml.optAttribute(ACTION_ATTRIBUTE_NAME, getAction());
166        xml.optAttribute(SESSION_ID_ATTRIBUTE_NAME, getSid());
167        xml.rightAngleBracket();
168
169        xml.optElement(reason);
170
171        xml.append(contents);
172
173        return xml;
174    }
175
176    /**
177     * Deprecated, do not use.
178     *
179     * @return a builder.
180     * @deprecated use {@link #builder(XMPPConnection)} instead.
181     */
182    @Deprecated
183    // TODO: Remove in Smack 4.6.
184    public static Builder getBuilder() {
185        return builder(StandardStanzaIdSource.DEFAULT.getNewStanzaId());
186    }
187
188    public static Builder builder(XMPPConnection connection) {
189        return new Builder(connection);
190    }
191
192    public static Builder builder(IqData iqData) {
193        return new Builder(iqData);
194    }
195
196    public static Builder builder(String stanzaId) {
197        return new Builder(stanzaId);
198    }
199
200    public static final class Builder extends IqBuilder<Builder, Jingle> {
201        private String sid;
202
203        private JingleAction action;
204
205        private FullJid initiator;
206
207        private FullJid responder;
208
209        private JingleReason reason;
210
211        private List<JingleContent> contents;
212
213        Builder(IqData iqCommon) {
214            super(iqCommon);
215        }
216
217        Builder(XMPPConnection connection) {
218            super(connection);
219        }
220
221        Builder(String stanzaId) {
222            super(stanzaId);
223        }
224
225        public Builder setSessionId(String sessionId) {
226            StringUtils.requireNotNullNorEmpty(sessionId, "Session ID must not be null nor empty");
227            this.sid = sessionId;
228            return this;
229        }
230
231        public Builder setAction(JingleAction action) {
232            this.action = action;
233            return this;
234        }
235
236        public Builder setInitiator(FullJid initator) {
237            this.initiator = initator;
238            return this;
239        }
240
241        public Builder setResponder(FullJid responder) {
242            this.responder = responder;
243            return this;
244        }
245
246        public Builder addJingleContent(JingleContent content) {
247            if (contents == null) {
248                contents = new ArrayList<>(1);
249            }
250            contents.add(content);
251            return this;
252        }
253
254        public Builder setReason(JingleReason.Reason reason) {
255            this.reason = new JingleReason(reason);
256            return this;
257        }
258
259        public Builder setReason(JingleReason reason) {
260            this.reason = reason;
261            return this;
262        }
263
264        @Override
265        public Jingle build() {
266            return new Jingle(this, sid, action, initiator, responder, reason, contents);
267        }
268
269        @Override
270        public Builder getThis() {
271            return this;
272        }
273    }
274}