Search in sources :

Example 1 with LdapVCardProvider

use of org.jivesoftware.openfire.ldap.LdapVCardProvider in project Openfire by igniterealtime.

the class LdapUserProfile method saveProperties.

/**
     * Saves current configuration as XML/DB properties.
     */
public void saveProperties() {
    Element vCard = DocumentHelper.createElement(QName.get("vCard", "vcard-temp"));
    Element subelement;
    // Add name
    if (name != null && name.trim().length() > 0) {
        subelement = vCard.addElement("N");
        subelement.addElement("GIVEN").setText(name.trim());
    }
    // Add email
    if (email != null && email.trim().length() > 0) {
        subelement = vCard.addElement("EMAIL");
        subelement.addElement("INTERNET");
        subelement.addElement("USERID").setText(email.trim());
    }
    // Add Full Name
    vCard.addElement("FN").setText(fullName.trim());
    // Add nickname
    if (nickname != null && nickname.trim().length() > 0) {
        vCard.addElement("NICKNAME").setText(nickname.trim());
    }
    // Add birthday
    if (birthday != null && birthday.trim().length() > 0) {
        vCard.addElement("BDAY").setText(birthday.trim());
    }
    // Add photo/avatar
    if (photo != null && photo.trim().length() > 0) {
        Element element = vCard.addElement("PHOTO");
        element.addElement("TYPE").setText("image/jpeg");
        element.addElement("BINVAL").setText(photo.trim());
    }
    // Add home address
    subelement = vCard.addElement("ADR");
    subelement.addElement("HOME");
    if (homeStreet != null && homeStreet.trim().length() > 0) {
        subelement.addElement("STREET").setText(homeStreet.trim());
    }
    if (homeCity != null && homeCity.trim().length() > 0) {
        subelement.addElement("LOCALITY").setText(homeCity.trim());
    }
    if (homeState != null && homeState.trim().length() > 0) {
        subelement.addElement("REGION").setText(homeState.trim());
    }
    if (homeZip != null && homeZip.trim().length() > 0) {
        subelement.addElement("PCODE").setText(homeZip.trim());
    }
    if (homeCountry != null && homeCountry.trim().length() > 0) {
        subelement.addElement("CTRY").setText(homeCountry.trim());
    }
    // Add business address
    subelement = vCard.addElement("ADR");
    subelement.addElement("WORK");
    if (businessStreet != null && businessStreet.trim().length() > 0) {
        subelement.addElement("STREET").setText(businessStreet.trim());
    }
    if (businessCity != null && businessCity.trim().length() > 0) {
        subelement.addElement("LOCALITY").setText(businessCity.trim());
    }
    if (businessState != null && businessState.trim().length() > 0) {
        subelement.addElement("REGION").setText(businessState.trim());
    }
    if (businessZip != null && businessZip.trim().length() > 0) {
        subelement.addElement("PCODE").setText(businessZip.trim());
    }
    if (businessCountry != null && businessCountry.trim().length() > 0) {
        subelement.addElement("CTRY").setText(businessCountry.trim());
    }
    // Add home phone
    if (homePhone != null && homePhone.trim().length() > 0) {
        subelement = vCard.addElement("TEL");
        subelement.addElement("HOME");
        subelement.addElement("VOICE");
        subelement.addElement("NUMBER").setText(homePhone.trim());
    }
    // Add home mobile
    if (homeMobile != null && homeMobile.trim().length() > 0) {
        subelement = vCard.addElement("TEL");
        subelement.addElement("HOME");
        subelement.addElement("CELL");
        subelement.addElement("NUMBER").setText(homeMobile.trim());
    }
    // Add home fax
    if (homeFax != null && homeFax.trim().length() > 0) {
        subelement = vCard.addElement("TEL");
        subelement.addElement("HOME");
        subelement.addElement("FAX");
        subelement.addElement("NUMBER").setText(homeFax.trim());
    }
    // Add home pager
    if (homePager != null && homePager.trim().length() > 0) {
        subelement = vCard.addElement("TEL");
        subelement.addElement("HOME");
        subelement.addElement("PAGER");
        subelement.addElement("NUMBER").setText(homePager.trim());
    }
    // Add business phone
    if (businessPhone != null && businessPhone.trim().length() > 0) {
        subelement = vCard.addElement("TEL");
        subelement.addElement("WORK");
        subelement.addElement("VOICE");
        subelement.addElement("NUMBER").setText(businessPhone.trim());
    }
    // Add business mobile
    if (businessMobile != null && businessMobile.trim().length() > 0) {
        subelement = vCard.addElement("TEL");
        subelement.addElement("WORK");
        subelement.addElement("CELL");
        subelement.addElement("NUMBER").setText(businessMobile.trim());
    }
    // Add business fax
    if (businessFax != null && businessFax.trim().length() > 0) {
        subelement = vCard.addElement("TEL");
        subelement.addElement("WORK");
        subelement.addElement("FAX");
        subelement.addElement("NUMBER").setText(businessFax.trim());
    }
    // Add business pager
    if (businessPager != null && businessPager.trim().length() > 0) {
        subelement = vCard.addElement("TEL");
        subelement.addElement("WORK");
        subelement.addElement("PAGER");
        subelement.addElement("NUMBER").setText(businessPager.trim());
    }
    // Add job title
    if (businessJobTitle != null && businessJobTitle.trim().length() > 0) {
        vCard.addElement("TITLE").setText(businessJobTitle.trim());
    }
    // Add job department
    if (businessDepartment != null && businessDepartment.trim().length() > 0) {
        vCard.addElement("ORG").addElement("ORGUNIT").setText(businessDepartment.trim());
    }
    // 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();
    }
    StringBuilder sb = new StringBuilder(vcardXML.length());
    sb.append("<![CDATA[").append(vcardXML).append("]]>");
    // Save mapping as an XML property
    JiveGlobals.setProperty("ldap.vcard-mapping", sb.toString());
    // Set that the vcard provider is LdapVCardProvider
    JiveGlobals.setProperty("provider.vcard.className", LdapVCardProvider.class.getName());
    // Save duplicated fields in LdapManager (should be removed in the future)
    LdapManager.getInstance().setNameField(name.replaceAll("(\\{)([\\d\\D&&[^}]]+)(})", "$2"));
    LdapManager.getInstance().setEmailField(email.replaceAll("(\\{)([\\d\\D&&[^}]]+)(})", "$2"));
    // Store the DB storage variable in the actual database.
    JiveGlobals.setProperty("ldap.override.avatar", avatarStoredInDB.toString());
}
Also used : StringWriter(java.io.StringWriter) OutputFormat(org.dom4j.io.OutputFormat) IOException(java.io.IOException) LdapVCardProvider(org.jivesoftware.openfire.ldap.LdapVCardProvider) XMLWriter(org.jivesoftware.util.XMLWriter)

Aggregations

IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 OutputFormat (org.dom4j.io.OutputFormat)1 LdapVCardProvider (org.jivesoftware.openfire.ldap.LdapVCardProvider)1 XMLWriter (org.jivesoftware.util.XMLWriter)1