001/** 002 * 003 * Copyright 2016 Fernando Ramirez 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.muclight.element; 018 019import java.util.Iterator; 020import java.util.Map; 021 022import javax.xml.namespace.QName; 023 024import org.jivesoftware.smack.packet.Element; 025import org.jivesoftware.smack.packet.ExtensionElement; 026import org.jivesoftware.smack.packet.Message; 027import org.jivesoftware.smack.util.XmlStringBuilder; 028 029import org.jivesoftware.smackx.muclight.MUCLightAffiliation; 030import org.jivesoftware.smackx.muclight.MUCLightRoomConfiguration; 031import org.jivesoftware.smackx.muclight.MultiUserChatLight; 032import org.jivesoftware.smackx.xdata.packet.DataForm; 033 034import org.jxmpp.jid.Jid; 035 036public abstract class MUCLightElements { 037 038 /** 039 * Affiliations change extension element class. 040 * 041 * @author Fernando Ramirez 042 * 043 */ 044 public static class AffiliationsChangeExtension implements ExtensionElement { 045 046 public static final String ELEMENT = DataForm.ELEMENT; 047 public static final String NAMESPACE = MultiUserChatLight.NAMESPACE + MultiUserChatLight.AFFILIATIONS; 048 public static final QName QNAME = new QName(NAMESPACE, ELEMENT); 049 050 private final Map<Jid, MUCLightAffiliation> affiliations; 051 private final String prevVersion; 052 private final String version; 053 054 public AffiliationsChangeExtension(Map<Jid, MUCLightAffiliation> affiliations, String prevVersion, 055 String version) { 056 this.affiliations = affiliations; 057 this.prevVersion = prevVersion; 058 this.version = version; 059 } 060 061 @Override 062 public String getElementName() { 063 return ELEMENT; 064 } 065 066 @Override 067 public String getNamespace() { 068 return NAMESPACE; 069 } 070 071 /** 072 * Get the affiliations. 073 * 074 * @return the affiliations 075 */ 076 public Map<Jid, MUCLightAffiliation> getAffiliations() { 077 return affiliations; 078 } 079 080 /** 081 * Get the previous version. 082 * 083 * @return the previous version 084 */ 085 public String getPrevVersion() { 086 return prevVersion; 087 } 088 089 /** 090 * Get the version. 091 * 092 * @return the version 093 */ 094 public String getVersion() { 095 return version; 096 } 097 098 @Override 099 public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { 100 XmlStringBuilder xml = new XmlStringBuilder(this); 101 xml.rightAngleBracket(); 102 103 xml.optElement("prev-version", prevVersion); 104 xml.optElement("version", version); 105 106 Iterator<Map.Entry<Jid, MUCLightAffiliation>> it = affiliations.entrySet().iterator(); 107 while (it.hasNext()) { 108 Map.Entry<Jid, MUCLightAffiliation> pair = it.next(); 109 xml.append(new UserWithAffiliationElement(pair.getKey(), pair.getValue())); 110 } 111 112 xml.closeElement(this); 113 return xml; 114 } 115 116 public static AffiliationsChangeExtension from(Message message) { 117 return message.getExtension(AffiliationsChangeExtension.class); 118 } 119 120 } 121 122 /** 123 * Configurations change extension element class. 124 * 125 * @author Fernando Ramirez 126 * 127 */ 128 public static class ConfigurationsChangeExtension implements ExtensionElement { 129 130 public static final String ELEMENT = DataForm.ELEMENT; 131 public static final String NAMESPACE = MultiUserChatLight.NAMESPACE + MultiUserChatLight.CONFIGURATION; 132 133 private final String prevVersion; 134 private final String version; 135 private final String roomName; 136 private final String subject; 137 private final Map<String, String> customConfigs; 138 139 /** 140 * Configurations change extension constructor. 141 * 142 * @param prevVersion TODO javadoc me please 143 * @param version TODO javadoc me please 144 * @param roomName TODO javadoc me please 145 * @param subject TODO javadoc me please 146 * @param customConfigs TODO javadoc me please 147 */ 148 public ConfigurationsChangeExtension(String prevVersion, String version, String roomName, String subject, 149 Map<String, String> customConfigs) { 150 this.prevVersion = prevVersion; 151 this.version = version; 152 this.roomName = roomName; 153 this.subject = subject; 154 this.customConfigs = customConfigs; 155 } 156 157 @Override 158 public String getElementName() { 159 return ELEMENT; 160 } 161 162 @Override 163 public String getNamespace() { 164 return NAMESPACE; 165 } 166 167 /** 168 * Get the previous version. 169 * 170 * @return the previous version 171 */ 172 public String getPrevVersion() { 173 return prevVersion; 174 } 175 176 /** 177 * Get the version. 178 * 179 * @return the version 180 */ 181 public String getVersion() { 182 return version; 183 } 184 185 /** 186 * Get the room name. 187 * 188 * @return the room name 189 */ 190 public String getRoomName() { 191 return roomName; 192 } 193 194 /** 195 * Get the room subject. 196 * 197 * @return the room subject 198 */ 199 public String getSubject() { 200 return subject; 201 } 202 203 /** 204 * Get the room custom configurations. 205 * 206 * @return the room custom configurations 207 */ 208 public Map<String, String> getCustomConfigs() { 209 return customConfigs; 210 } 211 212 @Override 213 public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { 214 XmlStringBuilder xml = new XmlStringBuilder(this); 215 xml.rightAngleBracket(); 216 217 xml.optElement("prev-version", prevVersion); 218 xml.optElement("version", version); 219 xml.optElement("roomname", roomName); 220 xml.optElement("subject", subject); 221 222 if (customConfigs != null) { 223 Iterator<Map.Entry<String, String>> it = customConfigs.entrySet().iterator(); 224 while (it.hasNext()) { 225 Map.Entry<String, String> pair = it.next(); 226 xml.element(pair.getKey(), pair.getValue()); 227 } 228 } 229 230 xml.closeElement(this); 231 return xml; 232 } 233 234 public static ConfigurationsChangeExtension from(Message message) { 235 return (ConfigurationsChangeExtension) message.getExtensionElement(ConfigurationsChangeExtension.ELEMENT, ConfigurationsChangeExtension.NAMESPACE); 236 } 237 238 } 239 240 /** 241 * Configuration element class. 242 * 243 * @author Fernando Ramirez 244 * 245 */ 246 public static class ConfigurationElement implements Element { 247 248 private MUCLightRoomConfiguration configuration; 249 250 /** 251 * Configuration element constructor. 252 * 253 * @param configuration TODO javadoc me please 254 */ 255 public ConfigurationElement(MUCLightRoomConfiguration configuration) { 256 this.configuration = configuration; 257 } 258 259 @Override 260 public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { 261 XmlStringBuilder xml = new XmlStringBuilder(); 262 xml.openElement("configuration"); 263 264 xml.element("roomname", configuration.getRoomName()); 265 xml.optElement("subject", configuration.getSubject()); 266 267 if (configuration.getCustomConfigs() != null) { 268 Iterator<Map.Entry<String, String>> it = configuration.getCustomConfigs().entrySet().iterator(); 269 while (it.hasNext()) { 270 Map.Entry<String, String> pair = it.next(); 271 xml.element(pair.getKey(), pair.getValue()); 272 } 273 } 274 275 xml.closeElement("configuration"); 276 return xml; 277 } 278 279 } 280 281 /** 282 * Occupants element class. 283 * 284 * @author Fernando Ramirez 285 * 286 */ 287 public static class OccupantsElement implements Element { 288 289 private Map<Jid, MUCLightAffiliation> occupants; 290 291 /** 292 * Occupants element constructor. 293 * 294 * @param occupants TODO javadoc me please 295 */ 296 public OccupantsElement(Map<Jid, MUCLightAffiliation> occupants) { 297 this.occupants = occupants; 298 } 299 300 @Override 301 public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { 302 XmlStringBuilder xml = new XmlStringBuilder(); 303 xml.openElement("occupants"); 304 305 Iterator<Map.Entry<Jid, MUCLightAffiliation>> it = occupants.entrySet().iterator(); 306 while (it.hasNext()) { 307 Map.Entry<Jid, MUCLightAffiliation> pair = it.next(); 308 xml.append(new UserWithAffiliationElement(pair.getKey(), pair.getValue())); 309 } 310 311 xml.closeElement("occupants"); 312 return xml; 313 } 314 315 } 316 317 /** 318 * User with affiliation element class. 319 * 320 * @author Fernando Ramirez 321 * 322 */ 323 public static class UserWithAffiliationElement implements Element { 324 325 private Jid user; 326 private MUCLightAffiliation affiliation; 327 328 /** 329 * User with affiliations element constructor. 330 * 331 * @param user TODO javadoc me please 332 * @param affiliation TODO javadoc me please 333 */ 334 public UserWithAffiliationElement(Jid user, MUCLightAffiliation affiliation) { 335 this.user = user; 336 this.affiliation = affiliation; 337 } 338 339 @Override 340 public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { 341 XmlStringBuilder xml = new XmlStringBuilder(); 342 xml.halfOpenElement("user"); 343 xml.attribute("affiliation", affiliation); 344 xml.rightAngleBracket(); 345 xml.escape(user); 346 xml.closeElement("user"); 347 return xml; 348 } 349 350 } 351 352 /** 353 * Blocking element class. 354 * 355 * @author Fernando Ramirez 356 * 357 */ 358 public static class BlockingElement implements Element { 359 360 private Jid jid; 361 private Boolean allow; 362 private Boolean isRoom; 363 364 /** 365 * Blocking element constructor. 366 * 367 * @param jid TODO javadoc me please 368 * @param allow TODO javadoc me please 369 * @param isRoom TODO javadoc me please 370 */ 371 public BlockingElement(Jid jid, Boolean allow, Boolean isRoom) { 372 this.jid = jid; 373 this.allow = allow; 374 this.isRoom = isRoom; 375 } 376 377 @Override 378 public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { 379 XmlStringBuilder xml = new XmlStringBuilder(); 380 381 String tag = isRoom ? "room" : "user"; 382 xml.halfOpenElement(tag); 383 384 String action = allow ? "allow" : "deny"; 385 xml.attribute("action", action); 386 xml.rightAngleBracket(); 387 388 xml.escape(jid); 389 390 xml.closeElement(tag); 391 return xml; 392 } 393 394 } 395 396}