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