Example usage for org.dom4j DocumentHelper createElement

List of usage examples for org.dom4j DocumentHelper createElement

Introduction

In this page you can find the example usage for org.dom4j DocumentHelper createElement.

Prototype

public static Element createElement(String name) 

Source Link

Usage

From source file:org.jivesoftware.admin.LdapUserProfile.java

License:Open Source License

/**
 * Saves current configuration as XML/DB properties.
 *//*w  ww .  ja  va2s.  c om*/
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());
}

From source file:org.jivesoftware.multiplexer.net.http.HttpBindServlet.java

License:Open Source License

private String createErrorBody(String type, String condition) {
    Element body = DocumentHelper.createElement("body");
    body.addNamespace("", "http://jabber.org/protocol/httpbind");
    body.addAttribute("type", type);
    body.addAttribute("condition", condition);
    return body.asXML();
}

From source file:org.jivesoftware.multiplexer.net.http.HttpBindServlet.java

License:Open Source License

private static String createEmptyBody() {
    Element body = DocumentHelper.createElement("body");
    body.addNamespace("", "http://jabber.org/protocol/httpbind");
    return body.asXML();
}

From source file:org.jivesoftware.multiplexer.net.http.HttpSession.java

License:Open Source License

/**
 * Returns the stream features which are available for this session.
 *
 * @return the stream features which are available for this session.
 *//*w w  w  .  ja  v a  2 s . co m*/
public Collection<Element> getAvailableStreamFeaturesElements() {
    List<Element> elements = new ArrayList<Element>();

    Element sasl = connectionManager.getServerSurrogate().getSASLMechanismsElement(this);
    if (sasl != null) {
        elements.add(sasl);
    }

    Element bind = DocumentHelper
            .createElement(new QName("bind", new Namespace("", "urn:ietf:params:xml:ns:xmpp-bind")));
    elements.add(bind);

    Element session = DocumentHelper
            .createElement(new QName("session", new Namespace("", "urn:ietf:params:xml:ns:xmpp-session")));
    elements.add(session);
    return elements;
}

From source file:org.jivesoftware.multiplexer.net.http.HttpSessionManager.java

License:Open Source License

private String createSessionCreationResponse(HttpSession session) throws DocumentException {
    Element response = DocumentHelper.createElement("body");
    response.addNamespace("", "http://jabber.org/protocol/httpbind");
    response.addNamespace("stream", "http://etherx.jabber.org/streams");
    response.addAttribute("authid", session.getStreamID());
    response.addAttribute("sid", session.getStreamID());
    response.addAttribute("secure", Boolean.TRUE.toString());
    response.addAttribute("requests", String.valueOf(session.getMaxRequests()));
    response.addAttribute("inactivity", String.valueOf(session.getInactivityTimeout()));
    response.addAttribute("polling", String.valueOf(session.getMaxPollingInterval()));
    response.addAttribute("wait", String.valueOf(session.getWait()));
    if (session.getVersion() >= 1.6) {
        response.addAttribute("ver", String.valueOf(session.getVersion()));
    }/*  w  ww  . j  a  v  a 2s  .c  o  m*/

    Element features = response.addElement("stream:features");
    for (Element feature : session.getAvailableStreamFeaturesElements()) {
        features.add(feature.createCopy());
    }

    return response.asXML();
}

From source file:org.jivesoftware.openfire.commands.AdHocCommandHandler.java

License:Open Source License

public Iterator<Element> getIdentities(String name, String node, JID senderJID) {
    ArrayList<Element> identities = new ArrayList<Element>();
    Element identity = DocumentHelper.createElement("identity");
    identity.addAttribute("category", "automation");
    identity.addAttribute("type", NAMESPACE.equals(node) ? "command-list" : "command-node");
    identities.add(identity);/*  w  w  w .j a v  a  2 s  . c  o  m*/
    return identities.iterator();
}

From source file:org.jivesoftware.openfire.disco.IQDiscoInfoHandler.java

License:Open Source License

/**
 * Returns the DiscoInfoProvider responsible for providing information at the server level. This
 * means that this DiscoInfoProvider will provide information whenever a disco request whose
 * recipient JID is the server (e.g. localhost) is made.
 *
 * @return the DiscoInfoProvider responsible for providing information at the server level.
 *///from  www .ja v  a 2 s .  c  o  m
private DiscoInfoProvider getServerInfoProvider() {
    return new DiscoInfoProvider() {
        final ArrayList<Element> identities = new ArrayList<Element>();

        public Iterator<Element> getIdentities(String name, String node, JID senderJID) {
            if (node != null && serverNodeProviders.get(node) != null) {
                // Redirect the request to the disco info provider of the specified node
                return serverNodeProviders.get(node).getIdentities(name, node, senderJID);
            }
            if (name == null) {
                // Answer identity of the server
                synchronized (identities) {
                    if (identities.isEmpty()) {
                        Element identity = DocumentHelper.createElement("identity");
                        identity.addAttribute("category", "server");
                        identity.addAttribute("name",
                                JiveGlobals.getProperty("xmpp.server.name", "Openfire Server"));
                        identity.addAttribute("type", "im");

                        identities.add(identity);

                        // Include identities from modules that implement ServerIdentitiesProvider
                        for (Element identityElement : serverIdentities) {
                            identities.add(identityElement);
                        }
                    }
                }
                return identities.iterator();
            } else {
                if (SessionManager.getInstance().isAnonymousRoute(name)) {
                    // Answer identity of an anonymous user.
                    return anonymousUserIdentities.iterator();
                } else {
                    // Answer identity of a registered user.
                    // Note: We know that this user exists because #hasInfo returned true
                    return registeredUserIdentities.iterator();
                }
            }
        }

        public Iterator<String> getFeatures(String name, String node, JID senderJID) {
            if (node != null && serverNodeProviders.get(node) != null) {
                // Redirect the request to the disco info provider of the specified node
                return serverNodeProviders.get(node).getFeatures(name, node, senderJID);
            }
            if (name == null) {
                // Answer features of the server
                return new HashSet<String>(serverFeatures.keySet()).iterator();
            } else {
                // Answer features of the user
                return userFeatures.iterator();
            }
        }

        public boolean hasInfo(String name, String node, JID senderJID) {
            if (node != null) {
                if (serverNodeProviders.get(node) != null) {
                    // Redirect the request to the disco info provider of the specified node
                    return serverNodeProviders.get(node).hasInfo(name, node, senderJID);
                }
                // Unknown node
                return false;
            }
            try {
                // True if it is an info request of the server, a registered user or an
                // anonymous user. We now support disco of user's bare JIDs
                return name == null || UserManager.getInstance().getUser(name) != null
                        || SessionManager.getInstance().isAnonymousRoute(name);
            } catch (UserNotFoundException e) {
                return false;
            }
        }

        public DataForm getExtendedInfo(String name, String node, JID senderJID) {
            if (node != null && serverNodeProviders.get(node) != null) {
                // Redirect the request to the disco info provider of the specified node
                return serverNodeProviders.get(node).getExtendedInfo(name, node, senderJID);
            }
            return null;
        }
    };
}

From source file:org.jivesoftware.openfire.disco.IQDiscoItemsHandler.java

License:Open Source License

/**
 * Registers a new disco item for a component. The jid attribute of the item will match the jid
 * of the component and the name should be the name of the component discovered using disco.
 *
 * @param jid the jid of the component.//from  w w w .  java 2 s  . c  o  m
 * @param node the node that complements the jid address.
 * @param name the discovered name of the component.
 */
public void addComponentItem(String jid, String node, String name) {
    Lock lock = CacheFactory.getLock(jid, serverItems);
    try {
        lock.lock();
        ClusteredServerItem item = serverItems.get(jid);
        if (item == null) {
            // First time a node registers a server item for this component
            item = new ClusteredServerItem();

            Element element = DocumentHelper.createElement("item");
            element.addAttribute("jid", jid);
            element.addAttribute("node", node);
            element.addAttribute("name", name);
            item.element = element;
        }
        if (item.nodes.add(XMPPServer.getInstance().getNodeID())) {
            // Update the cache with latest info
            serverItems.put(jid, item);
        }
        // Keep track of the new server item added by this JVM
        localServerItems.put(jid, item.element);
    } finally {
        lock.unlock();
    }
}

From source file:org.jivesoftware.openfire.disco.IQDiscoItemsHandler.java

License:Open Source License

public Iterator<Element> getUserItems(String name, JID senderJID) {
    List<Element> answer = new ArrayList<Element>();
    try {//from w  w  w  .ja  v  a2s  . c o m
        User user = UserManager.getInstance().getUser(name);
        RosterItem item = user.getRoster().getRosterItem(senderJID);
        // If the requesting entity is subscribed to the account's presence then
        // answer the user's "available resources"
        if (item.getSubStatus() == RosterItem.SUB_FROM || item.getSubStatus() == RosterItem.SUB_BOTH) {
            for (Session session : SessionManager.getInstance().getSessions(name)) {
                Element element = DocumentHelper.createElement("item");
                element.addAttribute("jid", session.getAddress().toString());
                answer.add(element);
            }
        }
        return answer.iterator();
    } catch (UserNotFoundException e) {
        return answer.iterator();
    }
}

From source file:org.jivesoftware.openfire.fastpath.providers.ChatMetadataProvider.java

License:Open Source License

public Element getMetaDataElement(Map<String, String> metaData) {
    QName qName = DocumentHelper.createQName("metadata",
            DocumentHelper.createNamespace("", "http://jivesoftware.com/protocol/workgroup"));
    Element metaDataElement = DocumentHelper.createElement(qName);

    for (Map.Entry<String, String> entry : metaData.entrySet()) {
        String name = entry.getKey();
        String value = entry.getValue();
        Element elem = metaDataElement.addElement("value");
        elem.addAttribute("name", name).setText(value);
    }//from  www . jav  a  2s  .c  o m
    return metaDataElement;
}