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.XmlElement; 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 XmlElement { 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 149 if (description == null && transport == null) { 150 return xml.closeEmptyElement(); 151 } 152 153 xml.rightAngleBracket(); 154 155 xml.optAppend(description); 156 xml.optElement(transport); 157 158 xml.closeElement(this); 159 return xml; 160 } 161 162 public static Builder getBuilder() { 163 return new Builder(); 164 } 165 166 public static final class Builder { 167 private Creator creator; 168 169 private String disposition; 170 171 private String name; 172 173 private Senders senders; 174 175 private JingleContentDescription description; 176 177 private JingleContentTransport transport; 178 179 private Builder() { 180 } 181 182 public Builder setCreator(Creator creator) { 183 this.creator = creator; 184 return this; 185 } 186 187 public Builder setDisposition(String disposition) { 188 this.disposition = disposition; 189 return this; 190 } 191 192 public Builder setName(String name) { 193 this.name = name; 194 return this; 195 } 196 197 public Builder setSenders(Senders senders) { 198 this.senders = senders; 199 return this; 200 } 201 202 public Builder setDescription(JingleContentDescription description) { 203 if (this.description != null) { 204 throw new IllegalStateException("Jingle content description already set"); 205 } 206 this.description = description; 207 return this; 208 } 209 210 public Builder setTransport(JingleContentTransport transport) { 211 this.transport = transport; 212 return this; 213 } 214 215 public JingleContent build() { 216 return new JingleContent(creator, disposition, name, senders, description, transport); 217 } 218 } 219}