LdapUserProfile.java 20 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/**
 * $RCSfile$
 * $Revision: $
 * $Date: $
 *
 * Copyright (C) 2006 Jive Software. All rights reserved.
 *
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
 */

package org.jivesoftware.admin;

14 15
import org.dom4j.*;
import org.dom4j.io.OutputFormat;
16
import org.jivesoftware.util.JiveGlobals;
17 18
import org.jivesoftware.util.Log;
import org.jivesoftware.util.XMLWriter;
19 20
import org.jivesoftware.wildfire.ldap.LdapManager;
import org.jivesoftware.wildfire.ldap.LdapVCardProvider;
21 22 23

import java.io.IOException;
import java.io.StringWriter;
24
import java.util.Iterator;
25 26 27 28 29 30 31 32 33

/**
 * Bean that stores the vcard mapping. It is also responsible for saving the mapping
 * as an XML property and retrieving it.
 *
 * @author Gaston Dombiak
 */
public class LdapUserProfile {

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    private String name = "";
    private String email = "";
    private String fullName = "";
    private String nickname = "";
    private String birthday = "";
    private String homeStreet = "";
    private String homeCity = "";
    private String homeState = "";
    private String homeZip = "";
    private String homeCountry = "";
    private String homePhone = "";
    private String homeMobile = "";
    private String homeFax = "";
    private String homePager = "";
    private String businessStreet = "";
    private String businessCity = "";
    private String businessState = "";
    private String businessZip = "";
    private String businessCountry = "";
    private String businessJobTitle = "";
    private String businessDepartment = "";
    private String businessPhone = "";
    private String businessMobile = "";
    private String businessFax = "";
    private String businessPager = "";
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

100 101
    public String getHomeStreet() {
        return homeStreet;
102 103
    }

104 105
    public void setHomeStreet(String homeStreet) {
        this.homeStreet = homeStreet;
106 107
    }

108 109
    public String getHomeCity() {
        return homeCity;
110 111
    }

112 113
    public void setHomeCity(String homeCity) {
        this.homeCity = homeCity;
114 115
    }

116 117
    public String getHomeState() {
        return homeState;
118 119
    }

120 121
    public void setHomeState(String homeState) {
        this.homeState = homeState;
122 123
    }

124 125
    public String getHomeZip() {
        return homeZip;
126 127
    }

128 129
    public void setHomeZip(String homeZip) {
        this.homeZip = homeZip;
130 131
    }

132 133
    public String getHomeCountry() {
        return homeCountry;
134 135
    }

136 137
    public void setHomeCountry(String homeCountry) {
        this.homeCountry = homeCountry;
138 139
    }

140 141
    public String getHomePhone() {
        return homePhone;
142 143
    }

144 145
    public void setHomePhone(String homePhone) {
        this.homePhone = homePhone;
146 147
    }

148 149
    public String getHomeMobile() {
        return homeMobile;
150 151
    }

152 153
    public void setHomeMobile(String homeMobile) {
        this.homeMobile = homeMobile;
154 155
    }

156 157
    public String getHomeFax() {
        return homeFax;
158 159
    }

160 161
    public void setHomeFax(String homeFax) {
        this.homeFax = homeFax;
162 163
    }

164 165
    public String getHomePager() {
        return homePager;
166 167
    }

168 169
    public void setHomePager(String homePager) {
        this.homePager = homePager;
170 171
    }

172 173
    public String getBusinessStreet() {
        return businessStreet;
174 175
    }

176 177
    public void setBusinessStreet(String businessStreet) {
        this.businessStreet = businessStreet;
178 179
    }

180 181
    public String getBusinessCity() {
        return businessCity;
182 183
    }

184 185
    public void setBusinessCity(String businessCity) {
        this.businessCity = businessCity;
186 187
    }

188 189
    public String getBusinessState() {
        return businessState;
190 191
    }

192 193
    public void setBusinessState(String businessState) {
        this.businessState = businessState;
194 195
    }

196 197
    public String getBusinessZip() {
        return businessZip;
198 199
    }

200 201
    public void setBusinessZip(String businessZip) {
        this.businessZip = businessZip;
202 203
    }

204 205
    public String getBusinessCountry() {
        return businessCountry;
206 207
    }

208 209
    public void setBusinessCountry(String businessCountry) {
        this.businessCountry = businessCountry;
210 211
    }

212 213
    public String getBusinessJobTitle() {
        return businessJobTitle;
214 215
    }

216 217
    public void setBusinessJobTitle(String businessJobTitle) {
        this.businessJobTitle = businessJobTitle;
218 219
    }

220 221
    public String getBusinessDepartment() {
        return businessDepartment;
222 223
    }

224 225
    public void setBusinessDepartment(String businessDepartment) {
        this.businessDepartment = businessDepartment;
226 227
    }

228 229
    public String getBusinessPhone() {
        return businessPhone;
230 231
    }

232 233
    public void setBusinessPhone(String businessPhone) {
        this.businessPhone = businessPhone;
234 235
    }

236 237
    public String getBusinessMobile() {
        return businessMobile;
238 239
    }

240 241
    public void setBusinessMobile(String businessMobile) {
        this.businessMobile = businessMobile;
242 243
    }

244 245
    public String getBusinessFax() {
        return businessFax;
246 247
    }

248 249
    public void setBusinessFax(String businessFax) {
        this.businessFax = businessFax;
250 251
    }

252 253
    public String getBusinessPager() {
        return businessPager;
254 255
    }

256 257
    public void setBusinessPager(String businessPager) {
        this.businessPager = businessPager;
258 259 260 261 262 263 264 265 266 267 268
    }

    /**
     * Sets default mapping values when using an Active Directory server.
     */
    public void initForActiveDirectory() {
        name = "{cn}";
        email = "{mail}";
        fullName = "{displayName}";
        nickname = "";
        birthday = "";
269 270 271 272
        homeStreet = "{homePostalAddress}";
        homeCity = "";
        homeState = "";
        homeZip = "{homeZip}";
273
        homeCountry = "{co}";
274
        homePhone = "{homePhone}";
275
        homeMobile = "{mobile}";
276 277
        homeFax = "";
        homePager = "";
278
        businessStreet = "{streetAddress}";
279 280 281
        businessCity = "{l}";
        businessState = "{st}";
        businessZip = "{postalCode}";
282
        businessCountry = "{co}";
283 284
        businessJobTitle = "{title}";
        businessDepartment = "{department}";
285
        businessPhone = "{telephoneNumber}";
286 287 288
        businessMobile = "{mobile}";
        businessFax = "{facsimileTelephoneNumber}";
        businessPager = "{pager}";
289 290 291 292 293 294 295 296 297 298 299
    }

    /**
     * Sets default mapping values when using an Active Directory server.
     */
    public void initForOpenLDAP() {
        name = "{cn}";
        email = "{mail}";
        fullName = "{displayName}";
        nickname = "{uid}";
        birthday = "";
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
        homeStreet = "{homePostalAddress}";
        homeCity = "";
        homeState = "";
        homeZip = "";
        homeCountry = "";
        homePhone = "{homePhone}";
        homeMobile = "";
        homeFax = "";
        homePager = "";
        businessStreet = "{postalAddress}";
        businessCity = "{l}";
        businessState = "{st}";
        businessZip = "{postalCode}";
        businessCountry = "";
        businessJobTitle = "{title}";
        businessDepartment = "{departmentNumber}";
        businessPhone = "{telephoneNumber}";
        businessMobile = "{mobile}";
        businessFax = "";
        businessPager = "{pager}";
320 321 322 323 324
    }

    /**
     * Saves current configuration as XML properties.
     */
325
    public void saveProperties() {
326 327 328 329 330 331
        Element vCard = DocumentHelper.createElement(QName.get("vCard", "vcard-temp"));
        Element subelement;

        // Add name
        if (name != null && name.trim().length() > 0) {
            subelement = vCard.addElement("N");
332
            subelement.addElement("GIVEN").setText(name.trim());
333 334 335 336 337
        }
        // Add email
        if (email != null && email.trim().length() > 0) {
            subelement = vCard.addElement("EMAIL");
            subelement.addElement("INTERNET");
338
            subelement.addElement("USERID").setText(email.trim());
339 340
        }
        // Add Full Name
341
        vCard.addElement("FN").setText(fullName.trim());
342 343
        // Add nickname
        if (nickname != null && nickname.trim().length() > 0) {
344
            vCard.addElement("NICKNAME").setText(nickname.trim());
345 346 347
        }
        // Add birthday
        if (birthday != null && birthday.trim().length() > 0) {
348
            vCard.addElement("BDAY").setText(birthday.trim());
349 350 351 352
        }
        // Add home address
        subelement = vCard.addElement("ADR");
        subelement.addElement("HOME");
353 354
        if (homeStreet != null && homeStreet.trim().length() > 0) {
            subelement.addElement("STREET").setText(homeStreet.trim());
355
        }
356 357
        if (homeCity != null && homeCity.trim().length() > 0) {
            subelement.addElement("LOCALITY").setText(homeCity.trim());
358
        }
359 360
        if (homeState != null && homeState.trim().length() > 0) {
            subelement.addElement("REGION").setText(homeState.trim());
361
        }
362 363
        if (homeZip != null && homeZip.trim().length() > 0) {
            subelement.addElement("PCODE").setText(homeZip.trim());
364
        }
365 366
        if (homeCountry != null && homeCountry.trim().length() > 0) {
            subelement.addElement("CTRY").setText(homeCountry.trim());
367 368 369 370
        }
        // Add business address
        subelement = vCard.addElement("ADR");
        subelement.addElement("WORK");
371 372
        if (businessStreet != null && businessStreet.trim().length() > 0) {
            subelement.addElement("STREET").setText(businessStreet.trim());
373
        }
374 375
        if (businessCity != null && businessCity.trim().length() > 0) {
            subelement.addElement("LOCALITY").setText(businessCity.trim());
376
        }
377 378
        if (businessState != null && businessState.trim().length() > 0) {
            subelement.addElement("REGION").setText(businessState.trim());
379
        }
380
        if (businessZip != null && businessZip.trim().length() > 0) {
381
            subelement.addElement("PCODE").setText(businessZip.trim());
382
        }
383 384
        if (businessCountry != null && businessCountry.trim().length() > 0) {
            subelement.addElement("CTRY").setText(businessCountry.trim());
385 386
        }
        // Add home phone
387
        if (homePhone != null && homePhone.trim().length() > 0) {
388 389 390
            subelement = vCard.addElement("TEL");
            subelement.addElement("HOME");
            subelement.addElement("VOICE");
391
            subelement.addElement("NUMBER").setText(homePhone.trim());
392 393
        }
        // Add home mobile
394
        if (homeMobile != null && homeMobile.trim().length() > 0) {
395 396 397
            subelement = vCard.addElement("TEL");
            subelement.addElement("HOME");
            subelement.addElement("CELL");
398
            subelement.addElement("NUMBER").setText(homeMobile.trim());
399 400
        }
        // Add home fax
401
        if (homeFax != null && homeFax.trim().length() > 0) {
402 403 404
            subelement = vCard.addElement("TEL");
            subelement.addElement("HOME");
            subelement.addElement("FAX");
405
            subelement.addElement("NUMBER").setText(homeFax.trim());
406 407
        }
        // Add home pager
408
        if (homePager != null && homePager.trim().length() > 0) {
409 410 411
            subelement = vCard.addElement("TEL");
            subelement.addElement("HOME");
            subelement.addElement("PAGER");
412
            subelement.addElement("NUMBER").setText(homePager.trim());
413 414
        }
        // Add business phone
415
        if (businessPhone != null && businessPhone.trim().length() > 0) {
416 417 418
            subelement = vCard.addElement("TEL");
            subelement.addElement("WORK");
            subelement.addElement("VOICE");
419
            subelement.addElement("NUMBER").setText(businessPhone.trim());
420 421
        }
        // Add business mobile
422
        if (businessMobile != null && businessMobile.trim().length() > 0) {
423 424 425
            subelement = vCard.addElement("TEL");
            subelement.addElement("WORK");
            subelement.addElement("CELL");
426
            subelement.addElement("NUMBER").setText(businessMobile.trim());
427 428
        }
        // Add business fax
429
        if (businessFax != null && businessFax.trim().length() > 0) {
430 431 432
            subelement = vCard.addElement("TEL");
            subelement.addElement("WORK");
            subelement.addElement("FAX");
433
            subelement.addElement("NUMBER").setText(businessFax.trim());
434 435
        }
        // Add business pager
436
        if (businessPager != null && businessPager.trim().length() > 0) {
437 438 439
            subelement = vCard.addElement("TEL");
            subelement.addElement("WORK");
            subelement.addElement("PAGER");
440
            subelement.addElement("NUMBER").setText(businessPager.trim());
441 442
        }
        // Add job title
443 444
        if (businessJobTitle != null && businessJobTitle.trim().length() > 0) {
            vCard.addElement("TITLE").setText(businessJobTitle.trim());
445
        }
446 447 448 449
        // Add job department
        if (businessDepartment != null && businessDepartment.trim().length() > 0) {
            vCard.addElement("ORG").addElement("ORGUNIT").setText(businessDepartment.trim());
        }
450 451 452 453 454 455 456 457 458 459 460 461 462 463
        // Generate content to store in property
        String vcardXML;
        StringWriter writer = new StringWriter();
        OutputFormat prettyPrinter = OutputFormat.createPrettyPrint();
        XMLWriter xmlWriter = new XMLWriter(writer, prettyPrinter);
        try {
            xmlWriter.write(vCard);
            vcardXML = writer.toString();
        }
        catch (IOException e) {
            Log.error("Error pretty formating XML", e);
            vcardXML = vCard.asXML();
        }

464 465
        StringBuilder sb = new StringBuilder(vcardXML.length());
        sb.append("<![CDATA[").append(vcardXML).append("]]>");
466
        // Save mapping as an XML property
467
        JiveGlobals.setXMLProperty("ldap.vcard-mapping", sb.toString());
468 469 470 471 472

        // Set that the vcard provider is LdapVCardProvider
        JiveGlobals.setXMLProperty("provider.vcard.className", LdapVCardProvider.class.getName());

        // Save duplicated fields in LdapManager (should be removed in the future)
473 474
        LdapManager.getInstance().setNameField(name.replaceAll("(\\{)([\\d\\D&&[^}]]+)(})", "$2"));
        LdapManager.getInstance().setEmailField(email.replaceAll("(\\{)([\\d\\D&&[^}]]+)(})", "$2"));
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
    }

    /**
     * Returns true if the vCard mappings where successfully loaded from the XML
     * property.
     *
     * @return true if mappings where loaded from saved property.
     */
    public boolean loadFromProperties() {
        String xmlProperty = JiveGlobals.getXMLProperty("ldap.vcard-mapping");
        if (xmlProperty == null || xmlProperty.trim().length() == 0) {
            return false;
        }

        try {
490 491 492 493 494
            // Remove CDATA wrapping element
            if (xmlProperty.startsWith("<![CDATA[")) {
                xmlProperty = xmlProperty.substring(9, xmlProperty.length()-3);
            }
            // Parse XML
495 496 497 498 499 500 501 502 503 504 505 506 507
            Document document = DocumentHelper.parseText(xmlProperty);
            Element vCard = document.getRootElement();

            Element element = vCard.element("N");
            if (element != null) {
                name = element.elementTextTrim("GIVEN");
            }
            element = vCard.element("EMAIL");
            if (element != null) {
                email = element.elementTextTrim("USERID");
            }
            element = vCard.element("FN");
            if (element != null) {
508
                fullName = element.getTextTrim();
509 510 511
            }
            element = vCard.element("NICKNAME");
            if (element != null) {
512
                nickname = element.getTextTrim();
513 514 515
            }
            element = vCard.element("BDAY");
            if (element != null) {
516
                birthday = element.getTextTrim();
517
            }
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
            // Parse addresses
            Iterator addresses = vCard.elementIterator("ADR");
            while (addresses.hasNext()) {
                element = (Element) addresses.next();
                if (element.element("HOME") != null) {
                    if (element.element("STREET") != null) {
                        homeStreet = element.elementTextTrim("STREET");
                    }
                    if (element.element("LOCALITY") != null) {
                        homeCity = element.elementTextTrim("LOCALITY");
                    }
                    if (element.element("REGION") != null) {
                        homeState = element.elementTextTrim("REGION");
                    }
                    if (element.element("PCODE") != null) {
                        homeZip = element.elementTextTrim("PCODE");
                    }
                    if (element.element("CTRY") != null) {
                        homeCountry = element.elementTextTrim("CTRY");
                    }
                }
                else if (element.element("WORK") != null) {
                    if (element.element("STREET") != null) {
                        businessStreet = element.elementTextTrim("STREET");
                    }
                    if (element.element("LOCALITY") != null) {
                        businessCity = element.elementTextTrim("LOCALITY");
                    }
                    if (element.element("REGION") != null) {
                        businessState = element.elementTextTrim("REGION");
                    }
                    if (element.element("PCODE") != null) {
                        businessZip = element.elementTextTrim("PCODE");
                    }
                    if (element.element("CTRY") != null) {
                        businessCountry = element.elementTextTrim("CTRY");
                    }
                }
            }
            // Parse telephones
            Iterator telephones = vCard.elementIterator("TEL");
            while (telephones.hasNext()) {
                element = (Element) telephones.next();
                if (element.element("HOME") != null) {
                    if (element.element("VOICE") != null) {
                        homePhone = element.elementTextTrim("NUMBER");
                    }
                    else if (element.element("CELL") != null) {
                        homeMobile = element.elementTextTrim("NUMBER");
                    }
                    else if (element.element("FAX") != null) {
                        homeFax = element.elementTextTrim("NUMBER");
                    }
                    else if (element.element("PAGER") != null) {
                        homePager = element.elementTextTrim("NUMBER");
                    }
                }
                else if (element.element("WORK") != null) {
                    if (element.element("VOICE") != null) {
                        businessPhone = element.elementTextTrim("NUMBER");
                    }
                    else if (element.element("CELL") != null) {
                        businessMobile = element.elementTextTrim("NUMBER");
                    }
                    else if (element.element("FAX") != null) {
                        businessFax = element.elementTextTrim("NUMBER");
                    }
                    else if (element.element("PAGER") != null) {
                        businessPager = element.elementTextTrim("NUMBER");
                    }
                }
            }
            element = vCard.element("TITLE");
            if (element != null) {
                businessJobTitle = element.getTextTrim();
            }
            element = vCard.element("ORG");
            if (element != null) {
                if (element.element("ORGUNIT") != null) {
                    businessDepartment = element.elementTextTrim("ORGUNIT");
                }
            }
600 601 602 603 604
        }
        catch (DocumentException e) {
            Log.error("Error loading vcard mappings from property", e);
            return false;
        }
605

606
        return true;
607 608
    }
}