001/**
002 *
003 * Copyright 2017-2019 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 */
017package org.jivesoftware.smackx.jingle.element;
018
019import org.jivesoftware.smack.packet.FullyQualifiedElement;
020import org.jivesoftware.smack.packet.XmlEnvironment;
021import org.jivesoftware.smack.util.Objects;
022import org.jivesoftware.smack.util.StringUtils;
023import org.jivesoftware.smack.util.XmlStringBuilder;
024
025/**
026 * Jingle content element.
027 */
028public final class JingleContent implements FullyQualifiedElement {
029
030    public static final String ELEMENT = "content";
031    public static final String NAMESPACE = Jingle.NAMESPACE;
032
033    public static final String CREATOR_ATTRIBUTE_NAME = "creator";
034
035    public enum Creator {
036        initiator,
037        responder,
038    }
039
040    /**
041     * Which party originally generated the content type. Defined values are 'initiator' and 'responder'. Default is
042     * 'initiator'.
043     */
044    private final Creator creator;
045
046    public static final String DISPOSITION_ATTRIBUTE_NAME = "disposition";
047
048    private final String disposition;
049
050    public static final String NAME_ATTRIBUTE_NAME = "name";
051
052    private final String name;
053
054    public static final String SENDERS_ATTRIBUTE_NAME = "senders";
055
056    public enum Senders {
057        both,
058        initiator,
059        none,
060        responder,
061    }
062
063    /**
064     * Which parties in the session will be generation the content. Defined values are 'both', 'initiator', 'none' and
065     * 'responder. Default is 'both'.
066     */
067    private final Senders senders;
068
069    private final JingleContentDescription description;
070
071    private final JingleContentTransport transport;
072
073    /**
074     * Creates a content description..
075     */
076    private JingleContent(Creator creator, String disposition, String name, Senders senders,
077                    JingleContentDescription description, JingleContentTransport transport) {
078        this.creator = Objects.requireNonNull(creator, "Jingle content creator must not be null");
079        this.disposition = disposition;
080        this.name = StringUtils.requireNotNullNorEmpty(name, "Jingle content name must not be null nor empty");
081        this.senders = senders;
082        this.description = description;
083        this.transport = transport;
084    }
085
086    public Creator getCreator() {
087        return creator;
088    }
089
090    public String getDisposition() {
091        return disposition;
092    }
093
094    public String getName() {
095        return name;
096    }
097
098    public Senders getSenders() {
099        return senders;
100    }
101
102    /**
103     * Gets the description for this Jingle content.
104     *
105     * @return The description.
106     */
107    public JingleContentDescription getDescription() {
108        return description;
109    }
110
111    /**
112     * Returns an Iterator for the JingleTransports in the packet.
113     *
114     * @return an Iterator for the JingleTransports in the packet.
115     * @deprecated use {@link #getTransport()} instead.
116     */
117    @Deprecated
118    public JingleContentTransport getJingleTransport() {
119        return getTransport();
120    }
121
122    /**
123     * Returns an Iterator for the JingleTransports in the packet.
124     *
125     * @return an Iterator for the JingleTransports in the packet.
126     */
127    public JingleContentTransport getTransport() {
128        return transport;
129    }
130
131    @Override
132    public String getElementName() {
133        return ELEMENT;
134    }
135
136    @Override
137    public String getNamespace() {
138        return NAMESPACE;
139    }
140
141    @Override
142    public XmlStringBuilder toXML(XmlEnvironment enclosingXmlEnvironment) {
143        XmlStringBuilder xml = new XmlStringBuilder(this, enclosingXmlEnvironment);
144        xml.attribute(CREATOR_ATTRIBUTE_NAME, creator);
145        xml.optAttribute(DISPOSITION_ATTRIBUTE_NAME, disposition);
146        xml.attribute(NAME_ATTRIBUTE_NAME, name);
147        xml.optAttribute(SENDERS_ATTRIBUTE_NAME, senders);
148        xml.rightAngleBracket();
149
150        xml.optAppend(description);
151        xml.optElement(transport);
152
153        xml.closeElement(this);
154        return xml;
155    }
156
157    public static Builder getBuilder() {
158        return new Builder();
159    }
160
161    public static final class Builder {
162        private Creator creator;
163
164        private String disposition;
165
166        private String name;
167
168        private Senders senders;
169
170        private JingleContentDescription description;
171
172        private JingleContentTransport transport;
173
174        private Builder() {
175        }
176
177        public Builder setCreator(Creator creator) {
178            this.creator = creator;
179            return this;
180        }
181
182        public Builder setDisposition(String disposition) {
183            this.disposition = disposition;
184            return this;
185        }
186
187        public Builder setName(String name) {
188            this.name = name;
189            return this;
190        }
191
192        public Builder setSenders(Senders senders) {
193            this.senders = senders;
194            return this;
195        }
196
197        public Builder setDescription(JingleContentDescription description) {
198            if (this.description != null) {
199                throw new IllegalStateException("Jingle content description already set");
200            }
201            this.description = description;
202            return this;
203        }
204
205        public Builder setTransport(JingleContentTransport transport) {
206            this.transport = transport;
207            return this;
208        }
209
210        public JingleContent build() {
211            return new JingleContent(creator, disposition, name, senders, description, transport);
212        }
213    }
214}