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