1 20 21 package org.jivesoftware.smack.packet; 22 23 import org.jivesoftware.smack.util.StringUtils; 24 25 import java.util.*; 26 import java.io.*; 27 28 40 public abstract class Packet { 41 42 47 public static final String ID_NOT_AVAILABLE = "ID_NOT_AVAILABLE"; 48 49 52 private static String prefix = StringUtils.randomString(5) + "-"; 53 54 58 private static long id = 0; 59 60 66 private static synchronized String nextID() { 67 return prefix + Long.toString(id++); 68 } 69 70 private String packetID = null; 71 private String to = null; 72 private String from = null; 73 private List packetExtensions = null; 74 private Map properties = null; 75 private XMPPError error = null; 76 77 83 public String getPacketID() { 84 if (ID_NOT_AVAILABLE.equals(packetID)) { 85 return null; 86 } 87 88 if (packetID == null) { 89 packetID = nextID(); 90 } 91 return packetID; 92 } 93 94 100 public void setPacketID(String packetID) { 101 this.packetID = packetID; 102 } 103 104 112 public String getTo() { 113 return to; 114 } 115 116 122 public void setTo(String to) { 123 this.to = to; 124 } 125 126 134 public String getFrom() { 135 return from; 136 } 137 138 145 public void setFrom(String from) { 146 this.from = from; 147 } 148 149 155 public XMPPError getError() { 156 return error; 157 } 158 159 164 public void setError(XMPPError error) { 165 this.error = error; 166 } 167 168 173 public synchronized Iterator getExtensions() { 174 if (packetExtensions == null) { 175 return Collections.EMPTY_LIST.iterator(); 176 } 177 return Collections.unmodifiableList(new ArrayList(packetExtensions)).iterator(); 178 } 179 180 194 public synchronized PacketExtension getExtension(String elementName, String namespace) { 195 if (packetExtensions == null || elementName == null || namespace == null) { 196 return null; 197 } 198 for (Iterator i=packetExtensions.iterator(); i.hasNext(); ) { 199 PacketExtension ext = (PacketExtension)i.next(); 200 if (elementName.equals(ext.getElementName()) && namespace.equals(ext.getNamespace())) { 201 return ext; 202 } 203 } 204 return null; 205 } 206 207 212 public synchronized void addExtension(PacketExtension extension) { 213 if (packetExtensions == null) { 214 packetExtensions = new ArrayList(); 215 } 216 packetExtensions.add(extension); 217 } 218 219 224 public synchronized void removeExtension(PacketExtension extension) { 225 if (packetExtensions != null) { 226 packetExtensions.remove(extension); 227 } 228 } 229 230 239 public synchronized Object getProperty(String name) { 240 if (properties == null) { 241 return null; 242 } 243 return properties.get(name); 244 } 245 246 252 public void setProperty(String name, int value) { 253 setProperty(name, new Integer (value)); 254 } 255 256 262 public void setProperty(String name, long value) { 263 setProperty(name, new Long (value)); 264 } 265 266 272 public void setProperty(String name, float value) { 273 setProperty(name, new Float (value)); 274 } 275 276 282 public void setProperty(String name, double value) { 283 setProperty(name, new Double (value)); 284 } 285 286 292 public void setProperty(String name, boolean value) { 293 setProperty(name, new Boolean (value)); 294 } 295 296 303 public synchronized void setProperty(String name, Object value) { 304 if (!(value instanceof Serializable)) { 305 throw new IllegalArgumentException ("Value must be serialiazble"); 306 } 307 if (properties == null) { 308 properties = new HashMap(); 309 } 310 properties.put(name, value); 311 } 312 313 318 public synchronized void deleteProperty(String name) { 319 if (properties == null) { 320 return; 321 } 322 properties.remove(name); 323 } 324 325 330 public synchronized Iterator getPropertyNames() { 331 if (properties == null) { 332 return Collections.EMPTY_LIST.iterator(); 333 } 334 return properties.keySet().iterator(); 335 } 336 337 344 public abstract String toXML(); 345 346 353 protected synchronized String getExtensionsXML() { 354 StringBuffer buf = new StringBuffer (); 355 Iterator extensions = getExtensions(); 357 while (extensions.hasNext()) { 358 PacketExtension extension = (PacketExtension)extensions.next(); 359 buf.append(extension.toXML()); 360 } 361 if (properties != null && !properties.isEmpty()) { 363 buf.append("<properties xmlns=\"http://www.jivesoftware.com/xmlns/xmpp/properties\">"); 364 for (Iterator i=getPropertyNames(); i.hasNext(); ) { 366 String name = (String )i.next(); 367 Object value = getProperty(name); 368 buf.append("<property>"); 369 buf.append("<name>").append(StringUtils.escapeForXML(name)).append("</name>"); 370 buf.append("<value type=\""); 371 if (value instanceof Integer ) { 372 buf.append("integer\">").append(value).append("</value>"); 373 } 374 else if (value instanceof Long ) { 375 buf.append("long\">").append(value).append("</value>"); 376 } 377 else if (value instanceof Float ) { 378 buf.append("float\">").append(value).append("</value>"); 379 } 380 else if (value instanceof Double ) { 381 buf.append("double\">").append(value).append("</value>"); 382 } 383 else if (value instanceof Boolean ) { 384 buf.append("boolean\">").append(value).append("</value>"); 385 } 386 else if (value instanceof String ) { 387 buf.append("string\">"); 388 buf.append(StringUtils.escapeForXML((String )value)); 389 buf.append("</value>"); 390 } 391 else { 395 ByteArrayOutputStream byteStream = null; 396 ObjectOutputStream out = null; 397 try { 398 byteStream = new ByteArrayOutputStream(); 399 out = new ObjectOutputStream(byteStream); 400 out.writeObject(value); 401 buf.append("java-object\">"); 402 String encodedVal = StringUtils.encodeBase64(byteStream.toByteArray()); 403 buf.append(encodedVal).append("</value>"); 404 } 405 catch (Exception e) { 406 e.printStackTrace(); 407 } 408 finally { 409 if (out != null) { 410 try { out.close(); } catch (Exception e) { } 411 } 412 if (byteStream != null) { 413 try { byteStream.close(); } catch (Exception e) { } 414 } 415 } 416 } 417 buf.append("</property>"); 418 } 419 buf.append("</properties>"); 420 } 421 return buf.toString(); 422 } 423 } | Popular Tags |