1 20 21 package org.jivesoftware.smackx.packet; 22 23 import org.jivesoftware.smack.PacketCollector; 24 import org.jivesoftware.smack.SmackConfiguration; 25 import org.jivesoftware.smack.XMPPConnection; 26 import org.jivesoftware.smack.XMPPException; 27 import org.jivesoftware.smack.filter.PacketIDFilter; 28 import org.jivesoftware.smack.packet.IQ; 29 import org.jivesoftware.smack.packet.XMPPError; 30 31 import java.lang.reflect.Field ; 32 import java.lang.reflect.Modifier ; 33 import java.util.HashMap ; 34 import java.util.Iterator ; 35 import java.util.Map ; 36 37 77 public class VCard extends IQ { 78 79 83 private Map homePhones = new HashMap (); 84 private Map workPhones = new HashMap (); 85 86 87 92 private Map homeAddr = new HashMap (); 93 private Map workAddr = new HashMap (); 94 95 private String firstName; 96 private String lastName; 97 private String middleName; 98 99 private String emailHome; 100 private String emailWork; 101 102 private String organization; 103 private String organizationUnit; 104 105 108 private Map otherSimpleFields = new HashMap (); 109 110 public VCard() { 111 } 112 113 119 public String getField(String field) { 120 return (String ) otherSimpleFields.get(field); 121 } 122 123 130 public void setField(String field, String value) { 131 otherSimpleFields.put(field, value); 132 } 133 134 public String getFirstName() { 135 return firstName; 136 } 137 138 public void setFirstName(String firstName) { 139 this.firstName = firstName; 140 } 141 142 public String getLastName() { 143 return lastName; 144 } 145 146 public void setLastName(String lastName) { 147 this.lastName = lastName; 148 } 149 150 public String getMiddleName() { 151 return middleName; 152 } 153 154 public void setMiddleName(String middleName) { 155 this.middleName = middleName; 156 } 157 158 public String getNickName() { 159 return (String ) otherSimpleFields.get("NICKNAME"); 160 } 161 162 public void setNickName(String nickName) { 163 otherSimpleFields.put("NICKNAME", nickName); 164 } 165 166 public String getEmailHome() { 167 return emailHome; 168 } 169 170 public void setEmailHome(String email) { 171 this.emailHome = email; 172 } 173 174 public String getEmailWork() { 175 return emailWork; 176 } 177 178 public void setEmailWork(String emailWork) { 179 this.emailWork = emailWork; 180 } 181 182 public String getJabberId() { 183 return (String ) otherSimpleFields.get("JABBERID"); 184 } 185 186 public void setJabberId(String jabberId) { 187 otherSimpleFields.put("JABBERID", jabberId); 188 } 189 190 public String getOrganization() { 191 return organization; 192 } 193 194 public void setOrganization(String organization) { 195 this.organization = organization; 196 } 197 198 public String getOrganizationUnit() { 199 return organizationUnit; 200 } 201 202 public void setOrganizationUnit(String organizationUnit) { 203 this.organizationUnit = organizationUnit; 204 } 205 206 212 public String getAddressFieldHome(String addrField) { 213 return (String ) homeAddr.get(addrField); 214 } 215 216 222 public void setAddressFieldHome(String addrField, String value) { 223 homeAddr.put(addrField, value); 224 } 225 226 232 public String getAddressFieldWork(String addrField) { 233 return (String ) workAddr.get(addrField); 234 } 235 236 242 public void setAddressFieldWork(String addrField, String value) { 243 workAddr.put(addrField, value); 244 } 245 246 247 253 public void setPhoneHome(String phoneType, String phoneNum) { 254 homePhones.put(phoneType, phoneNum); 255 } 256 257 262 public String getPhoneHome(String phoneType) { 263 return (String ) homePhones.get(phoneType); 264 } 265 266 272 public void setPhoneWork(String phoneType, String phoneNum) { 273 workPhones.put(phoneType, phoneNum); 274 } 275 276 281 public String getPhoneWork(String phoneType) { 282 return (String ) workPhones.get(phoneType); 283 } 284 285 291 public void save(XMPPConnection connection) { 292 checkAuthenticated(connection); 293 294 setType(IQ.Type.SET); 295 setFrom(connection.getUser()); 296 connection.sendPacket(this); 297 } 298 299 303 public void load(XMPPConnection connection) throws XMPPException { 304 checkAuthenticated(connection); 305 306 setFrom(connection.getUser()); 307 doLoad(connection, connection.getUser()); 308 } 309 310 313 public void load(XMPPConnection connection, String user) throws XMPPException { 314 checkAuthenticated(connection); 315 316 setTo(user); 317 doLoad(connection, user); 318 } 319 320 private void doLoad(XMPPConnection connection, String user) throws XMPPException { 321 setType(Type.GET); 322 PacketCollector collector = connection.createPacketCollector( 323 new PacketIDFilter(getPacketID())); 324 connection.sendPacket(this); 325 326 VCard result = null; 327 try { 328 result = (VCard) collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); 329 330 if (result == null) { 331 throw new XMPPException(new XMPPError(408, "Timeout getting VCard information")); 332 } 333 if (result.getError() != null) { 334 throw new XMPPException(result.getError()); 335 } 336 } catch (ClassCastException e) { 337 System.out.println("No VCard for " + user); 338 } 339 340 copyFieldsFrom(result); 341 } 342 343 public String getChildElementXML() { 344 StringBuffer sb = new StringBuffer (); 345 new VCardWriter(sb).write(); 346 return sb.toString(); 347 } 348 349 private void copyFieldsFrom(VCard result) { 350 if (result == null) result = new VCard(); 351 352 Field [] fields = VCard.class.getDeclaredFields(); 353 for (int i = 0; i < fields.length; i++) { 354 Field field = fields[i]; 355 if (field.getDeclaringClass() == VCard.class && 356 !Modifier.isFinal(field.getModifiers())) { 357 try { 358 field.setAccessible(true); 359 field.set(this, field.get(result)); 360 } catch (IllegalAccessException e) { 361 throw new RuntimeException ("This cannot happen:" + field, e); 362 } 363 } 364 } 365 } 366 367 private void checkAuthenticated(XMPPConnection connection) { 368 if (connection == null) { 369 new IllegalArgumentException ("No connection was provided"); 370 } 371 if (!connection.isAuthenticated()) { 372 new IllegalArgumentException ("Connection is not authenticated"); 373 } 374 if (connection.isAnonymous()) { 375 new IllegalArgumentException ("Connection cannot be anonymous"); 376 } 377 } 378 379 private boolean hasContent() { 380 return hasNameField() 382 || hasOrganizationFields() 383 || emailHome != null 384 || emailWork != null 385 || otherSimpleFields.size() > 0 386 || homeAddr.size() > 0 387 || homePhones.size() > 0 388 || workAddr.size() > 0 389 || workPhones.size() > 0 390 ; 391 } 392 393 private boolean hasNameField() { 394 return firstName != null || lastName != null || middleName != null; 395 } 396 397 private boolean hasOrganizationFields() { 398 return organization != null || organizationUnit != null; 399 } 400 401 403 public boolean equals(Object o) { 404 if (this == o) return true; 405 if (o == null || getClass() != o.getClass()) return false; 406 407 final VCard vCard = (VCard) o; 408 409 if (emailHome != null ? !emailHome.equals(vCard.emailHome) : vCard.emailHome != null) { 410 return false; 411 } 412 if (emailWork != null ? !emailWork.equals(vCard.emailWork) : vCard.emailWork != null) { 413 return false; 414 } 415 if (firstName != null ? !firstName.equals(vCard.firstName) : vCard.firstName != null) { 416 return false; 417 } 418 if (!homeAddr.equals(vCard.homeAddr)) { 419 return false; 420 } 421 if (!homePhones.equals(vCard.homePhones)) { 422 return false; 423 } 424 if (lastName != null ? !lastName.equals(vCard.lastName) : vCard.lastName != null) { 425 return false; 426 } 427 if (middleName != null ? !middleName.equals(vCard.middleName) : vCard.middleName != null) { 428 return false; 429 } 430 if (organization != null ? 431 !organization.equals(vCard.organization) : vCard.organization != null) { 432 return false; 433 } 434 if (organizationUnit != null ? 435 !organizationUnit.equals(vCard.organizationUnit) : vCard.organizationUnit != null) { 436 return false; 437 } 438 if (!otherSimpleFields.equals(vCard.otherSimpleFields)) { 439 return false; 440 } 441 if (!workAddr.equals(vCard.workAddr)) { 442 return false; 443 } 444 if (!workPhones.equals(vCard.workPhones)) { 445 return false; 446 } 447 448 return true; 449 } 450 451 public int hashCode() { 452 int result; 453 result = homePhones.hashCode(); 454 result = 29 * result + workPhones.hashCode(); 455 result = 29 * result + homeAddr.hashCode(); 456 result = 29 * result + workAddr.hashCode(); 457 result = 29 * result + (firstName != null ? firstName.hashCode() : 0); 458 result = 29 * result + (lastName != null ? lastName.hashCode() : 0); 459 result = 29 * result + (middleName != null ? middleName.hashCode() : 0); 460 result = 29 * result + (emailHome != null ? emailHome.hashCode() : 0); 461 result = 29 * result + (emailWork != null ? emailWork.hashCode() : 0); 462 result = 29 * result + (organization != null ? organization.hashCode() : 0); 463 result = 29 * result + (organizationUnit != null ? organizationUnit.hashCode() : 0); 464 result = 29 * result + otherSimpleFields.hashCode(); 465 return result; 466 } 467 468 public String toString() { 469 return getChildElementXML(); 470 } 471 472 474 private class VCardWriter { 475 private final StringBuffer sb; 476 477 VCardWriter(StringBuffer sb) { 478 this.sb = sb; 479 } 480 481 public void write() { 482 appendTag("vCard", "xmlns", "vcard-temp", hasContent(), new ContentBuilder() { 483 public void addTagContent() { 484 buildActualContent(); 485 } 486 }); 487 } 488 489 private void buildActualContent() { 490 if (hasNameField()) { 491 appendFN(); 492 appendN(); 493 } 494 495 appendOrganization(); 496 appendGenericFields(); 497 498 appendEmail(emailWork, "WORK"); 499 appendEmail(emailHome, "HOME"); 500 501 appendPhones(workPhones, "WORK"); 502 appendPhones(homePhones, "HOME"); 503 504 appendAddress(workAddr, "WORK"); 505 appendAddress(homeAddr, "HOME"); 506 } 507 508 private void appendEmail(final String email, final String type) { 509 if (email != null) { 510 appendTag("EMAIL", true, new ContentBuilder() { 511 public void addTagContent() { 512 appendEmptyTag(type); 513 appendEmptyTag("INTERNET"); 514 appendEmptyTag("PREF"); 515 appendTag("USERID", email); 516 } 517 }); 518 } 519 } 520 521 private void appendPhones(Map phones, final String code) { 522 Iterator it = phones.entrySet().iterator(); 523 while (it.hasNext()) { 524 final Map.Entry entry = (Map.Entry ) it.next(); 525 appendTag("TEL", true, new ContentBuilder() { 526 public void addTagContent() { 527 appendEmptyTag(entry.getKey()); 528 appendEmptyTag(code); 529 appendTag("NUMBER", (String ) entry.getValue()); 530 } 531 }); 532 } 533 } 534 535 private void appendAddress(final Map addr, final String code) { 536 if (addr.size() > 0) { 537 appendTag("ADR", true, new ContentBuilder() { 538 public void addTagContent() { 539 appendEmptyTag(code); 540 541 Iterator it = addr.entrySet().iterator(); 542 while (it.hasNext()) { 543 final Map.Entry entry = (Map.Entry ) it.next(); 544 appendTag((String ) entry.getKey(), (String ) entry.getValue()); 545 } 546 } 547 }); 548 } 549 } 550 551 private void appendEmptyTag(Object tag) { 552 sb.append('<').append(tag).append("/>"); 553 } 554 555 private void appendGenericFields() { 556 Iterator it = otherSimpleFields.entrySet().iterator(); 557 while (it.hasNext()) { 558 Map.Entry entry = (Map.Entry ) it.next(); 559 appendTag(entry.getKey().toString(), (String ) entry.getValue()); 560 } 561 } 562 563 private void appendOrganization() { 564 if (hasOrganizationFields()) { 565 appendTag("ORG", true, new ContentBuilder() { 566 public void addTagContent() { 567 appendTag("ORGNAME", organization); 568 appendTag("ORGUNIT", organizationUnit); 569 } 570 }); 571 } 572 } 573 574 private void appendField(String tag) { 575 String value = (String ) otherSimpleFields.get(tag); 576 appendTag(tag, value); 577 } 578 579 private void appendFN() { 580 final ContentBuilder contentBuilder = new ContentBuilder() { 581 public void addTagContent() { 582 if (firstName != null) { 583 sb.append(firstName + ' '); 584 } 585 if (middleName != null) { 586 sb.append(middleName + ' '); 587 } 588 if (lastName != null) { 589 sb.append(lastName); 590 } 591 } 592 }; 593 appendTag("FN", true, contentBuilder); 594 } 595 596 private void appendN() { 597 appendTag("N", true, new ContentBuilder() { 598 public void addTagContent() { 599 appendTag("FAMILY", lastName); 600 appendTag("GIVEN", firstName); 601 appendTag("MIDDLE", middleName); 602 } 603 }); 604 } 605 606 private void appendTag(String tag, String attr, String attrValue, boolean hasContent, 607 ContentBuilder builder) { 608 sb.append('<').append(tag); 609 if (attr != null) { 610 sb.append(' ').append(attr).append('=').append('\'').append(attrValue).append('\''); 611 } 612 613 if (hasContent) { 614 sb.append('>'); 615 builder.addTagContent(); 616 sb.append("</").append(tag).append(">\n"); 617 } else { 618 sb.append("/>\n"); 619 } 620 } 621 622 private void appendTag(String tag, boolean hasContent, ContentBuilder builder) { 623 appendTag(tag, null, null, hasContent, builder); 624 } 625 626 private void appendTag(String tag, final String tagText) { 627 if (tagText == null) return; 628 final ContentBuilder contentBuilder = new ContentBuilder() { 629 public void addTagContent() { 630 sb.append(tagText.trim()); 631 } 632 }; 633 appendTag(tag, true, contentBuilder); 634 } 635 636 } 637 638 640 private interface ContentBuilder { 641 void addTagContent(); 642 } 643 644 } 646 647 | Popular Tags |