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