001: /**
002: * $RCSfile$
003: * $Revision: $
004: * $Date: $
005: *
006: * Copyright (C) 2006 Jive Software. All rights reserved.
007: *
008: * This software is published under the terms of the GNU Public License (GPL),
009: * a copy of which is included in this distribution.
010: */package org.jivesoftware.admin;
011:
012: import org.dom4j.*;
013: import org.dom4j.io.OutputFormat;
014: import org.jivesoftware.openfire.ldap.LdapManager;
015: import org.jivesoftware.openfire.ldap.LdapVCardProvider;
016: import org.jivesoftware.util.JiveGlobals;
017: import org.jivesoftware.util.Log;
018: import org.jivesoftware.util.XMLWriter;
019:
020: import java.io.IOException;
021: import java.io.StringWriter;
022: import java.util.Iterator;
023:
024: /**
025: * Bean that stores the vcard mapping. It is also responsible for saving the mapping
026: * as an XML property and retrieving it.
027: *
028: * @author Gaston Dombiak
029: */
030: public class LdapUserProfile {
031:
032: private String name = "";
033: private String email = "";
034: private String fullName = "";
035: private String nickname = "";
036: private String birthday = "";
037: private String photo = "";
038: private String homeStreet = "";
039: private String homeCity = "";
040: private String homeState = "";
041: private String homeZip = "";
042: private String homeCountry = "";
043: private String homePhone = "";
044: private String homeMobile = "";
045: private String homeFax = "";
046: private String homePager = "";
047: private String businessStreet = "";
048: private String businessCity = "";
049: private String businessState = "";
050: private String businessZip = "";
051: private String businessCountry = "";
052: private String businessJobTitle = "";
053: private String businessDepartment = "";
054: private String businessPhone = "";
055: private String businessMobile = "";
056: private String businessFax = "";
057: private String businessPager = "";
058: private Boolean avatarStoredInDB = false;
059:
060: public String getName() {
061: return name;
062: }
063:
064: public void setName(String name) {
065: this .name = name;
066: }
067:
068: public String getEmail() {
069: return email;
070: }
071:
072: public void setEmail(String email) {
073: this .email = email;
074: }
075:
076: public String getFullName() {
077: return fullName;
078: }
079:
080: public void setFullName(String fullName) {
081: this .fullName = fullName;
082: }
083:
084: public String getNickname() {
085: return nickname;
086: }
087:
088: public void setNickname(String nickname) {
089: this .nickname = nickname;
090: }
091:
092: public String getBirthday() {
093: return birthday;
094: }
095:
096: public void setBirthday(String birthday) {
097: this .birthday = birthday;
098: }
099:
100: public String getPhoto() {
101: return photo;
102: }
103:
104: public void setPhoto(String photo) {
105: this .photo = photo;
106: }
107:
108: public String getHomeStreet() {
109: return homeStreet;
110: }
111:
112: public void setHomeStreet(String homeStreet) {
113: this .homeStreet = homeStreet;
114: }
115:
116: public String getHomeCity() {
117: return homeCity;
118: }
119:
120: public void setHomeCity(String homeCity) {
121: this .homeCity = homeCity;
122: }
123:
124: public String getHomeState() {
125: return homeState;
126: }
127:
128: public void setHomeState(String homeState) {
129: this .homeState = homeState;
130: }
131:
132: public String getHomeZip() {
133: return homeZip;
134: }
135:
136: public void setHomeZip(String homeZip) {
137: this .homeZip = homeZip;
138: }
139:
140: public String getHomeCountry() {
141: return homeCountry;
142: }
143:
144: public void setHomeCountry(String homeCountry) {
145: this .homeCountry = homeCountry;
146: }
147:
148: public String getHomePhone() {
149: return homePhone;
150: }
151:
152: public void setHomePhone(String homePhone) {
153: this .homePhone = homePhone;
154: }
155:
156: public String getHomeMobile() {
157: return homeMobile;
158: }
159:
160: public void setHomeMobile(String homeMobile) {
161: this .homeMobile = homeMobile;
162: }
163:
164: public String getHomeFax() {
165: return homeFax;
166: }
167:
168: public void setHomeFax(String homeFax) {
169: this .homeFax = homeFax;
170: }
171:
172: public String getHomePager() {
173: return homePager;
174: }
175:
176: public void setHomePager(String homePager) {
177: this .homePager = homePager;
178: }
179:
180: public String getBusinessStreet() {
181: return businessStreet;
182: }
183:
184: public void setBusinessStreet(String businessStreet) {
185: this .businessStreet = businessStreet;
186: }
187:
188: public String getBusinessCity() {
189: return businessCity;
190: }
191:
192: public void setBusinessCity(String businessCity) {
193: this .businessCity = businessCity;
194: }
195:
196: public String getBusinessState() {
197: return businessState;
198: }
199:
200: public void setBusinessState(String businessState) {
201: this .businessState = businessState;
202: }
203:
204: public String getBusinessZip() {
205: return businessZip;
206: }
207:
208: public void setBusinessZip(String businessZip) {
209: this .businessZip = businessZip;
210: }
211:
212: public String getBusinessCountry() {
213: return businessCountry;
214: }
215:
216: public void setBusinessCountry(String businessCountry) {
217: this .businessCountry = businessCountry;
218: }
219:
220: public String getBusinessJobTitle() {
221: return businessJobTitle;
222: }
223:
224: public void setBusinessJobTitle(String businessJobTitle) {
225: this .businessJobTitle = businessJobTitle;
226: }
227:
228: public String getBusinessDepartment() {
229: return businessDepartment;
230: }
231:
232: public void setBusinessDepartment(String businessDepartment) {
233: this .businessDepartment = businessDepartment;
234: }
235:
236: public String getBusinessPhone() {
237: return businessPhone;
238: }
239:
240: public void setBusinessPhone(String businessPhone) {
241: this .businessPhone = businessPhone;
242: }
243:
244: public String getBusinessMobile() {
245: return businessMobile;
246: }
247:
248: public void setBusinessMobile(String businessMobile) {
249: this .businessMobile = businessMobile;
250: }
251:
252: public String getBusinessFax() {
253: return businessFax;
254: }
255:
256: public void setBusinessFax(String businessFax) {
257: this .businessFax = businessFax;
258: }
259:
260: public String getBusinessPager() {
261: return businessPager;
262: }
263:
264: public void setBusinessPager(String businessPager) {
265: this .businessPager = businessPager;
266: }
267:
268: public Boolean getAvatarStoredInDB() {
269: return avatarStoredInDB;
270: }
271:
272: public void setAvatarStoredInDB(Boolean avatarStoredInDB) {
273: if (avatarStoredInDB == null) {
274: this .avatarStoredInDB = false;
275: } else {
276: this .avatarStoredInDB = avatarStoredInDB;
277: }
278: }
279:
280: /**
281: * Sets default mapping values when using an Active Directory server.
282: */
283: public void initForActiveDirectory() {
284: name = "{cn}";
285: email = "{mail}";
286: fullName = "{displayName}";
287: nickname = "";
288: birthday = "";
289: photo = "{jpegPhoto}";
290: homeStreet = "{homePostalAddress}";
291: homeCity = "";
292: homeState = "";
293: homeZip = "{homeZip}";
294: homeCountry = "{co}";
295: homePhone = "{homePhone}";
296: homeMobile = "{mobile}";
297: homeFax = "";
298: homePager = "";
299: businessStreet = "{streetAddress}";
300: businessCity = "{l}";
301: businessState = "{st}";
302: businessZip = "{postalCode}";
303: businessCountry = "{co}";
304: businessJobTitle = "{title}";
305: businessDepartment = "{department}";
306: businessPhone = "{telephoneNumber}";
307: businessMobile = "{mobile}";
308: businessFax = "{facsimileTelephoneNumber}";
309: businessPager = "{pager}";
310: avatarStoredInDB = false;
311: }
312:
313: /**
314: * Sets default mapping values when using an Active Directory server.
315: */
316: public void initForOpenLDAP() {
317: name = "{cn}";
318: email = "{mail}";
319: fullName = "{displayName}";
320: nickname = "{uid}";
321: birthday = "";
322: photo = "{jpegPhoto}";
323: homeStreet = "{homePostalAddress}";
324: homeCity = "";
325: homeState = "";
326: homeZip = "";
327: homeCountry = "";
328: homePhone = "{homePhone}";
329: homeMobile = "";
330: homeFax = "";
331: homePager = "";
332: businessStreet = "{postalAddress}";
333: businessCity = "{l}";
334: businessState = "{st}";
335: businessZip = "{postalCode}";
336: businessCountry = "";
337: businessJobTitle = "{title}";
338: businessDepartment = "{departmentNumber}";
339: businessPhone = "{telephoneNumber}";
340: businessMobile = "{mobile}";
341: businessFax = "";
342: businessPager = "{pager}";
343: avatarStoredInDB = false;
344: }
345:
346: /**
347: * Saves current configuration as XML/DB properties.
348: */
349: public void saveProperties() {
350: Element vCard = DocumentHelper.createElement(QName.get("vCard",
351: "vcard-temp"));
352: Element subelement;
353:
354: // Add name
355: if (name != null && name.trim().length() > 0) {
356: subelement = vCard.addElement("N");
357: subelement.addElement("GIVEN").setText(name.trim());
358: }
359: // Add email
360: if (email != null && email.trim().length() > 0) {
361: subelement = vCard.addElement("EMAIL");
362: subelement.addElement("INTERNET");
363: subelement.addElement("USERID").setText(email.trim());
364: }
365: // Add Full Name
366: vCard.addElement("FN").setText(fullName.trim());
367: // Add nickname
368: if (nickname != null && nickname.trim().length() > 0) {
369: vCard.addElement("NICKNAME").setText(nickname.trim());
370: }
371: // Add birthday
372: if (birthday != null && birthday.trim().length() > 0) {
373: vCard.addElement("BDAY").setText(birthday.trim());
374: }
375: // Add photo/avatar
376: if (photo != null && photo.trim().length() > 0) {
377: Element element = vCard.addElement("PHOTO");
378: element.addElement("TYPE").setText("image/jpeg");
379: element.addElement("BINVAL").setText(photo.trim());
380: }
381: // Add home address
382: subelement = vCard.addElement("ADR");
383: subelement.addElement("HOME");
384: if (homeStreet != null && homeStreet.trim().length() > 0) {
385: subelement.addElement("STREET").setText(homeStreet.trim());
386: }
387: if (homeCity != null && homeCity.trim().length() > 0) {
388: subelement.addElement("LOCALITY").setText(homeCity.trim());
389: }
390: if (homeState != null && homeState.trim().length() > 0) {
391: subelement.addElement("REGION").setText(homeState.trim());
392: }
393: if (homeZip != null && homeZip.trim().length() > 0) {
394: subelement.addElement("PCODE").setText(homeZip.trim());
395: }
396: if (homeCountry != null && homeCountry.trim().length() > 0) {
397: subelement.addElement("CTRY").setText(homeCountry.trim());
398: }
399: // Add business address
400: subelement = vCard.addElement("ADR");
401: subelement.addElement("WORK");
402: if (businessStreet != null
403: && businessStreet.trim().length() > 0) {
404: subelement.addElement("STREET").setText(
405: businessStreet.trim());
406: }
407: if (businessCity != null && businessCity.trim().length() > 0) {
408: subelement.addElement("LOCALITY").setText(
409: businessCity.trim());
410: }
411: if (businessState != null && businessState.trim().length() > 0) {
412: subelement.addElement("REGION").setText(
413: businessState.trim());
414: }
415: if (businessZip != null && businessZip.trim().length() > 0) {
416: subelement.addElement("PCODE").setText(businessZip.trim());
417: }
418: if (businessCountry != null
419: && businessCountry.trim().length() > 0) {
420: subelement.addElement("CTRY").setText(
421: businessCountry.trim());
422: }
423: // Add home phone
424: if (homePhone != null && homePhone.trim().length() > 0) {
425: subelement = vCard.addElement("TEL");
426: subelement.addElement("HOME");
427: subelement.addElement("VOICE");
428: subelement.addElement("NUMBER").setText(homePhone.trim());
429: }
430: // Add home mobile
431: if (homeMobile != null && homeMobile.trim().length() > 0) {
432: subelement = vCard.addElement("TEL");
433: subelement.addElement("HOME");
434: subelement.addElement("CELL");
435: subelement.addElement("NUMBER").setText(homeMobile.trim());
436: }
437: // Add home fax
438: if (homeFax != null && homeFax.trim().length() > 0) {
439: subelement = vCard.addElement("TEL");
440: subelement.addElement("HOME");
441: subelement.addElement("FAX");
442: subelement.addElement("NUMBER").setText(homeFax.trim());
443: }
444: // Add home pager
445: if (homePager != null && homePager.trim().length() > 0) {
446: subelement = vCard.addElement("TEL");
447: subelement.addElement("HOME");
448: subelement.addElement("PAGER");
449: subelement.addElement("NUMBER").setText(homePager.trim());
450: }
451: // Add business phone
452: if (businessPhone != null && businessPhone.trim().length() > 0) {
453: subelement = vCard.addElement("TEL");
454: subelement.addElement("WORK");
455: subelement.addElement("VOICE");
456: subelement.addElement("NUMBER").setText(
457: businessPhone.trim());
458: }
459: // Add business mobile
460: if (businessMobile != null
461: && businessMobile.trim().length() > 0) {
462: subelement = vCard.addElement("TEL");
463: subelement.addElement("WORK");
464: subelement.addElement("CELL");
465: subelement.addElement("NUMBER").setText(
466: businessMobile.trim());
467: }
468: // Add business fax
469: if (businessFax != null && businessFax.trim().length() > 0) {
470: subelement = vCard.addElement("TEL");
471: subelement.addElement("WORK");
472: subelement.addElement("FAX");
473: subelement.addElement("NUMBER").setText(businessFax.trim());
474: }
475: // Add business pager
476: if (businessPager != null && businessPager.trim().length() > 0) {
477: subelement = vCard.addElement("TEL");
478: subelement.addElement("WORK");
479: subelement.addElement("PAGER");
480: subelement.addElement("NUMBER").setText(
481: businessPager.trim());
482: }
483: // Add job title
484: if (businessJobTitle != null
485: && businessJobTitle.trim().length() > 0) {
486: vCard.addElement("TITLE").setText(businessJobTitle.trim());
487: }
488: // Add job department
489: if (businessDepartment != null
490: && businessDepartment.trim().length() > 0) {
491: vCard.addElement("ORG").addElement("ORGUNIT").setText(
492: businessDepartment.trim());
493: }
494: // Generate content to store in property
495: String vcardXML;
496: StringWriter writer = new StringWriter();
497: OutputFormat prettyPrinter = OutputFormat.createPrettyPrint();
498: XMLWriter xmlWriter = new XMLWriter(writer, prettyPrinter);
499: try {
500: xmlWriter.write(vCard);
501: vcardXML = writer.toString();
502: } catch (IOException e) {
503: Log.error("Error pretty formating XML", e);
504: vcardXML = vCard.asXML();
505: }
506:
507: StringBuilder sb = new StringBuilder(vcardXML.length());
508: sb.append("<![CDATA[").append(vcardXML).append("]]>");
509: // Save mapping as an XML property
510: JiveGlobals.setXMLProperty("ldap.vcard-mapping", sb.toString());
511:
512: // Set that the vcard provider is LdapVCardProvider
513: JiveGlobals.setXMLProperty("provider.vcard.className",
514: LdapVCardProvider.class.getName());
515:
516: // Save duplicated fields in LdapManager (should be removed in the future)
517: LdapManager.getInstance().setNameField(
518: name.replaceAll("(\\{)([\\d\\D&&[^}]]+)(})", "$2"));
519: LdapManager.getInstance().setEmailField(
520: email.replaceAll("(\\{)([\\d\\D&&[^}]]+)(})", "$2"));
521:
522: // Store the DB storage variable in the actual database.
523: JiveGlobals.setProperty("ldap.override.avatar",
524: avatarStoredInDB.toString());
525: }
526:
527: /**
528: * Returns true if the vCard mappings where successfully loaded from the XML/DB
529: * properties.
530: *
531: * @return true if mappings where loaded from saved property.
532: */
533: public boolean loadFromProperties() {
534: String xmlProperty = JiveGlobals
535: .getXMLProperty("ldap.vcard-mapping");
536: if (xmlProperty == null || xmlProperty.trim().length() == 0) {
537: return false;
538: }
539:
540: try {
541: // Remove CDATA wrapping element
542: if (xmlProperty.startsWith("<![CDATA[")) {
543: xmlProperty = xmlProperty.substring(9, xmlProperty
544: .length() - 3);
545: }
546: // Parse XML
547: Document document = DocumentHelper.parseText(xmlProperty);
548: Element vCard = document.getRootElement();
549:
550: Element element = vCard.element("N");
551: if (element != null) {
552: name = element.elementTextTrim("GIVEN");
553: }
554: element = vCard.element("EMAIL");
555: if (element != null) {
556: email = element.elementTextTrim("USERID");
557: }
558: element = vCard.element("FN");
559: if (element != null) {
560: fullName = element.getTextTrim();
561: }
562: element = vCard.element("NICKNAME");
563: if (element != null) {
564: nickname = element.getTextTrim();
565: }
566: element = vCard.element("BDAY");
567: if (element != null) {
568: birthday = element.getTextTrim();
569: }
570: // Parse addresses
571: Iterator addresses = vCard.elementIterator("ADR");
572: while (addresses.hasNext()) {
573: element = (Element) addresses.next();
574: if (element.element("HOME") != null) {
575: if (element.element("STREET") != null) {
576: homeStreet = element.elementTextTrim("STREET");
577: }
578: if (element.element("LOCALITY") != null) {
579: homeCity = element.elementTextTrim("LOCALITY");
580: }
581: if (element.element("REGION") != null) {
582: homeState = element.elementTextTrim("REGION");
583: }
584: if (element.element("PCODE") != null) {
585: homeZip = element.elementTextTrim("PCODE");
586: }
587: if (element.element("CTRY") != null) {
588: homeCountry = element.elementTextTrim("CTRY");
589: }
590: } else if (element.element("WORK") != null) {
591: if (element.element("STREET") != null) {
592: businessStreet = element
593: .elementTextTrim("STREET");
594: }
595: if (element.element("LOCALITY") != null) {
596: businessCity = element
597: .elementTextTrim("LOCALITY");
598: }
599: if (element.element("REGION") != null) {
600: businessState = element
601: .elementTextTrim("REGION");
602: }
603: if (element.element("PCODE") != null) {
604: businessZip = element.elementTextTrim("PCODE");
605: }
606: if (element.element("CTRY") != null) {
607: businessCountry = element
608: .elementTextTrim("CTRY");
609: }
610: }
611: }
612: // Parse telephones
613: Iterator telephones = vCard.elementIterator("TEL");
614: while (telephones.hasNext()) {
615: element = (Element) telephones.next();
616: if (element.element("HOME") != null) {
617: if (element.element("VOICE") != null) {
618: homePhone = element.elementTextTrim("NUMBER");
619: } else if (element.element("CELL") != null) {
620: homeMobile = element.elementTextTrim("NUMBER");
621: } else if (element.element("FAX") != null) {
622: homeFax = element.elementTextTrim("NUMBER");
623: } else if (element.element("PAGER") != null) {
624: homePager = element.elementTextTrim("NUMBER");
625: }
626: } else if (element.element("WORK") != null) {
627: if (element.element("VOICE") != null) {
628: businessPhone = element
629: .elementTextTrim("NUMBER");
630: } else if (element.element("CELL") != null) {
631: businessMobile = element
632: .elementTextTrim("NUMBER");
633: } else if (element.element("FAX") != null) {
634: businessFax = element.elementTextTrim("NUMBER");
635: } else if (element.element("PAGER") != null) {
636: businessPager = element
637: .elementTextTrim("NUMBER");
638: }
639: }
640: }
641: element = vCard.element("TITLE");
642: if (element != null) {
643: businessJobTitle = element.getTextTrim();
644: }
645: element = vCard.element("ORG");
646: if (element != null) {
647: if (element.element("ORGUNIT") != null) {
648: businessDepartment = element
649: .elementTextTrim("ORGUNIT");
650: }
651: }
652: avatarStoredInDB = JiveGlobals.getBooleanProperty(
653: "ldap.override.avatar", false);
654: } catch (DocumentException e) {
655: Log.error("Error loading vcard mappings from property", e);
656: return false;
657: }
658:
659: return true;
660: }
661: }
|