Search in sources :

Example 1 with XMLWriter

use of org.jivesoftware.util.XMLWriter 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)

Example 2 with XMLWriter

use of org.jivesoftware.util.XMLWriter in project Openfire by igniterealtime.

the class UpdateManager method saveAvailablePluginsInfo.

/**
     * Saves to conf/available-plugins.xml the list of plugins that are available
     * at igniterealtime.org.
     */
private void saveAvailablePluginsInfo() {
    //  XML to store in the file
    Element xml = docFactory.createDocument().addElement("available");
    for (AvailablePlugin plugin : availablePlugins.values()) {
        Element component = xml.addElement("plugin");
        component.addAttribute("name", plugin.getName());
        component.addAttribute("latest", plugin.getLatestVersion());
        component.addAttribute("changelog", plugin.getChangelog());
        component.addAttribute("url", plugin.getURL());
        component.addAttribute("author", plugin.getAuthor());
        component.addAttribute("description", plugin.getDescription());
        component.addAttribute("icon", plugin.getIcon());
        component.addAttribute("minServerVersion", plugin.getMinServerVersion());
        component.addAttribute("readme", plugin.getReadme());
        component.addAttribute("licenseType", plugin.getLicenseType());
        component.addAttribute("fileSize", Long.toString(plugin.getFileSize()));
    }
    // Write data out to conf/available-plugins.xml file.
    Writer writer = null;
    try {
        // Create the conf folder if required
        File file = new File(JiveGlobals.getHomeDirectory(), "conf");
        if (!file.exists()) {
            file.mkdir();
        }
        file = new File(JiveGlobals.getHomeDirectory() + File.separator + "conf", "available-plugins.xml");
        // Delete the old version.xml file if it exists
        if (file.exists()) {
            file.delete();
        }
        // Create new version.xml with returned data
        writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8));
        OutputFormat prettyPrinter = OutputFormat.createPrettyPrint();
        XMLWriter xmlWriter = new XMLWriter(writer, prettyPrinter);
        xmlWriter.write(xml);
    } catch (Exception e) {
        Log.error(e.getMessage(), e);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e1) {
                Log.error(e1.getMessage(), e1);
            }
        }
    }
}
Also used : Element(org.dom4j.Element) FileOutputStream(java.io.FileOutputStream) OutputFormat(org.dom4j.io.OutputFormat) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) File(java.io.File) XMLWriter(org.jivesoftware.util.XMLWriter) XMLWriter(org.jivesoftware.util.XMLWriter) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) DocumentException(org.dom4j.DocumentException) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter)

Example 3 with XMLWriter

use of org.jivesoftware.util.XMLWriter in project Openfire by igniterealtime.

the class UpdateManager method saveLatestServerInfo.

/**
     * Saves to conf/server-update.xml information about the latest Openfire release that is
     * available for download.
     */
private void saveLatestServerInfo() {
    Element xmlResponse = docFactory.createDocument().addElement("version");
    if (serverUpdate != null) {
        Element component = xmlResponse.addElement("openfire");
        component.addAttribute("latest", serverUpdate.getLatestVersion());
        component.addAttribute("changelog", serverUpdate.getChangelog());
        component.addAttribute("url", serverUpdate.getURL());
    }
    // Write data out to conf/server-update.xml file.
    try {
        // Create the conf folder if required
        File file = new File(JiveGlobals.getHomeDirectory(), "conf");
        if (!file.exists()) {
            file.mkdir();
        }
        file = new File(JiveGlobals.getHomeDirectory() + File.separator + "conf", "server-update.xml");
        // Delete the old server-update.xml file if it exists
        if (file.exists()) {
            file.delete();
        }
        // Create new version.xml with returned data
        try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8))) {
            OutputFormat prettyPrinter = OutputFormat.createPrettyPrint();
            XMLWriter xmlWriter = new XMLWriter(writer, prettyPrinter);
            xmlWriter.write(xmlResponse);
        }
    } catch (Exception e) {
        Log.error(e.getMessage(), e);
    }
}
Also used : Element(org.dom4j.Element) FileOutputStream(java.io.FileOutputStream) OutputFormat(org.dom4j.io.OutputFormat) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) XMLWriter(org.jivesoftware.util.XMLWriter) XMLWriter(org.jivesoftware.util.XMLWriter) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) DocumentException(org.dom4j.DocumentException) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter)

Aggregations

IOException (java.io.IOException)3 OutputFormat (org.dom4j.io.OutputFormat)3 XMLWriter (org.jivesoftware.util.XMLWriter)3 BufferedWriter (java.io.BufferedWriter)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Writer (java.io.Writer)2 DocumentException (org.dom4j.DocumentException)2 Element (org.dom4j.Element)2 StringWriter (java.io.StringWriter)1 LdapVCardProvider (org.jivesoftware.openfire.ldap.LdapVCardProvider)1