Example usage for org.dom4j Element getNamespaceURI

List of usage examples for org.dom4j Element getNamespaceURI

Introduction

In this page you can find the example usage for org.dom4j Element getNamespaceURI.

Prototype

String getNamespaceURI();

Source Link

Document

Returns the URI mapped to the namespace of this element if one exists otherwise an empty String is returned.

Usage

From source file:org.jivesoftware.openfire.plugin.spark.SparkManager.java

License:Open Source License

/**
 * Client features are detected using Service Discovery, allowing
 * for ease of use within the client.  When a client "discovers" the
 * manager, they can query for related features within that discovered item.
 *
 * @param packet the packet/*from  www  .java  2s  .c  o m*/
 */
public void processPacket(Packet packet) {
    if (packet instanceof IQ) {
        IQ iqPacket = (IQ) packet;

        Element childElement = (iqPacket).getChildElement();
        String namespace = null;
        if (childElement != null) {
            namespace = childElement.getNamespaceURI();
        }

        if (IQ.Type.get == iqPacket.getType()) {
            // Handle any disco info requests.
            if ("http://jabber.org/protocol/disco#info".equals(namespace)) {
                handleDiscoInfo(iqPacket);
            }

            // Handle any disco item requests.
            else if ("http://jabber.org/protocol/disco#items".equals(namespace)) {
                handleDiscoItems(iqPacket);
            } else if ("jabber:iq:version".equals(namespace)) {
                IQ reply = IQ.createResultIQ(iqPacket);
                Element version = reply.setChildElement("query", "jabber:iq:version");
                version.addElement("name").setText("Client Control Manager");
                version.addElement("version").setText("3.5");
                sendPacket(reply);
            } else {
                // Return error since this is an unknown service request
                IQ reply = IQ.createResultIQ(iqPacket);
                reply.setError(PacketError.Condition.service_unavailable);
                sendPacket(reply);
            }
        } else if (IQ.Type.error == iqPacket.getType() || IQ.Type.result == iqPacket.getType()) {
            if ("jabber:iq:version".equals(namespace)) {
                handleClientVersion(iqPacket);
            }
        } else {
            // Return error since this is an unknown service request
            IQ reply = IQ.createResultIQ(iqPacket);
            reply.setError(PacketError.Condition.service_unavailable);
            sendPacket(reply);
        }
    }
}

From source file:org.jivesoftware.openfire.plugin.Xep227Exporter.java

License:Apache License

/**
 * @param user/* w ww .ja v  a2 s  .  c  om*/
 * @param previousDomain
 * @param isUserProviderReadOnly
 * @param invalidUsers
 */
@SuppressWarnings("unchecked")
private void importUser(Element user, String previousDomain, boolean isUserProviderReadOnly,
        List<String> invalidUsers) {
    Log.debug("importUser");

    List<RosterItem> rosterItems = new ArrayList<RosterItem>();
    List<OfflineMessage> offlineMessages = new ArrayList<OfflineMessage>();
    Element vCardElement = null;

    String userName = user.attributeValue(NAME_NAME);
    String password = user.attributeValue(PASSWORD_NAME);

    Iterator<Element> userElements = user.elementIterator();
    while (userElements.hasNext()) {
        Element userElement = userElements.next();

        String nameElement = userElement.getName();
        if (OFFLINE_MESSAGES_ELEMENT_NAME.equals(nameElement)) {

            importOffLineMessages(userElement, offlineMessages);

        } else if (QUERY_ELEMENT_NAME.equals(nameElement)
                && JABBER_IQ_ROSTER_NS.equals(userElement.getNamespaceURI())) {

            importUserRoster(userElement, rosterItems, previousDomain);

        } else if (V_CARD_NAME.equals(nameElement) && VCARD_TEMP_NS.equals(userElement.getNamespaceURI())) {

            vCardElement = userElement;

        }
    }

    if (userName != null) {
        try {
            userName = Stringprep.nodeprep(userName);

            if (!isUserProviderReadOnly && (password != null)) {
                userManager.createUser(userName, password, userName, null);
            }

            if (!isUserProviderReadOnly && vCardElement != null) {
                try {
                    vCardManager.setVCard(userName, vCardElement);
                } catch (Exception e) {
                    Log.warn("Error updating VCard:" + userName + ":" + e.getMessage());
                    Log.debug("", e);

                }
            }

            // Check to see user exists before adding their roster, this is for
            // read-only user providers.
            userManager.getUser(userName);
            for (RosterItem ri : rosterItems) {
                rosterItemProvider.createItem(userName, ri);
            }
            for (OfflineMessage offlineMessage : offlineMessages) {
                offlineMessagesStore.addMessage(offlineMessage);
            }
        } catch (StringprepException se) {
            Log.info("Invalid username " + userName);
            invalidUsers.add(userName);
        } catch (UserAlreadyExistsException e) {
            Log.info("User already exists " + userName);
            invalidUsers.add(userName);
        } catch (UserNotFoundException e) {
            Log.info("User not found " + userName);
            invalidUsers.add(userName);
        } catch (Exception e) {
            Log.warn("Error updating User:" + userName + ":" + e.getLocalizedMessage());
            invalidUsers.add(userName);
        }
    }

}

From source file:org.jivesoftware.openfire.PrivateStorage.java

License:Open Source License

/**
 * Stores private data. If the name and namespace of the element matches another
 * stored private data XML document, then replace it with the new one.
 *
 * @param data the data to store (XML element)
 * @param username the username of the account where private data is being stored
 *//*  w ww  .  ja v  a 2s . co  m*/
public void add(String username, Element data) {
    if (enabled) {
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            StringWriter writer = new StringWriter();
            data.write(writer);
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(LOAD_PRIVATE);
            pstmt.setString(1, username);
            pstmt.setString(2, data.getNamespaceURI());
            rs = pstmt.executeQuery();
            boolean update = false;
            if (rs.next()) {
                update = true;
            }
            DbConnectionManager.fastcloseStmt(rs, pstmt);
            if (update) {
                pstmt = con.prepareStatement(UPDATE_PRIVATE);
            } else {
                pstmt = con.prepareStatement(INSERT_PRIVATE);
            }
            pstmt.setString(1, writer.toString());
            pstmt.setString(2, data.getName());
            pstmt.setString(3, username);
            pstmt.setString(4, data.getNamespaceURI());
            pstmt.executeUpdate();
        } catch (Exception e) {
            Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
        } finally {
            DbConnectionManager.closeConnection(rs, pstmt, con);
        }
    }
}

From source file:org.jivesoftware.openfire.PrivateStorage.java

License:Open Source License

/**
 * Returns the data stored under a key corresponding to the name and namespace
 * of the given element. The Element must be in the form:<p>
 *
 * <code>&lt;name xmlns='namespace'/&gt;</code><p>
 *
 * If no data is currently stored under the given key, an empty element will be
 * returned.//from   w  w w.  j a  v  a  2  s  .c  om
 *
 * @param data an XML document who's element name and namespace is used to
 *      match previously stored private data.
 * @param username the username of the account where private data is being stored.
 * @return the data stored under the given key or the data element.
 */
public Element get(String username, Element data) {
    if (enabled) {
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        SAXReader xmlReader = null;
        try {
            // Get a sax reader from the pool
            xmlReader = xmlReaders.take();
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(LOAD_PRIVATE);
            pstmt.setString(1, username);
            pstmt.setString(2, data.getNamespaceURI());
            rs = pstmt.executeQuery();
            if (rs.next()) {
                data.clearContent();
                String result = rs.getString(1).trim();
                Document doc = xmlReader.read(new StringReader(result));
                data = doc.getRootElement();
            }
        } catch (Exception e) {
            Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
        } finally {
            DbConnectionManager.closeConnection(rs, pstmt, con);
            // Return the sax reader to the pool
            if (xmlReader != null) {
                xmlReaders.add(xmlReader);
            }
        }
    }
    return data;
}

From source file:org.jivesoftware.openfire.pubsub.PubSubEngine.java

License:Open Source License

/**
 * Handles IQ packets sent to the pubsub service. Requests of disco#info and disco#items
 * are not being handled by the engine. Instead the service itself should handle disco packets.
 *
 * @param service the PubSub service this action is to be performed for.
 * @param iq the IQ packet sent to the pubsub service.
 * @return true if the IQ packet was handled by the engine.
 *//*from   w w w.j ava  2  s . co m*/
public boolean process(PubSubService service, IQ iq) {
    // Ignore IQs of type ERROR or RESULT
    if (IQ.Type.error == iq.getType() || IQ.Type.result == iq.getType()) {
        return true;
    }
    Element childElement = iq.getChildElement();
    String namespace = null;

    if (childElement != null) {
        namespace = childElement.getNamespaceURI();
    }
    if ("http://jabber.org/protocol/pubsub".equals(namespace)) {
        Element action = childElement.element("publish");
        if (action != null) {
            // Entity publishes an item
            publishItemsToNode(service, iq, action);
            return true;
        }
        action = childElement.element("subscribe");
        if (action != null) {
            // Entity subscribes to a node
            subscribeNode(service, iq, childElement, action);
            return true;
        }
        action = childElement.element("options");
        if (action != null) {
            if (IQ.Type.get == iq.getType()) {
                // Subscriber requests subscription options form
                getSubscriptionConfiguration(service, iq, childElement, action);
            } else {
                // Subscriber submits completed options form
                configureSubscription(service, iq, action);
            }
            return true;
        }
        action = childElement.element("create");
        if (action != null) {
            // Entity is requesting to create a new node
            createNode(service, iq, childElement, action);
            return true;
        }
        action = childElement.element("unsubscribe");
        if (action != null) {
            // Entity unsubscribes from a node
            unsubscribeNode(service, iq, action);
            return true;
        }
        action = childElement.element("subscriptions");
        if (action != null) {
            // Entity requests all current subscriptions
            getSubscriptions(service, iq, childElement);
            return true;
        }
        action = childElement.element("affiliations");
        if (action != null) {
            // Entity requests all current affiliations
            getAffiliations(service, iq, childElement);
            return true;
        }
        action = childElement.element("items");
        if (action != null) {
            // Subscriber requests all active items
            getPublishedItems(service, iq, action);
            return true;
        }
        action = childElement.element("retract");
        if (action != null) {
            // Entity deletes an item
            deleteItems(service, iq, action);
            return true;
        }
        // Unknown action requested
        sendErrorPacket(iq, PacketError.Condition.bad_request, null);
        return true;
    } else if ("http://jabber.org/protocol/pubsub#owner".equals(namespace)) {
        Element action = childElement.element("configure");
        if (action != null) {
            String nodeID = action.attributeValue("node");
            if (nodeID == null) {
                // if user is not sysadmin then return nodeid-required error
                if (!service.isServiceAdmin(iq.getFrom()) || !service.isCollectionNodesSupported()) {
                    // Configure elements must have a node attribute so answer an error
                    Element pubsubError = DocumentHelper.createElement(
                            QName.get("nodeid-required", "http://jabber.org/protocol/pubsub#errors"));
                    sendErrorPacket(iq, PacketError.Condition.bad_request, pubsubError);
                    return true;
                } else {
                    // Sysadmin is trying to configure root collection node
                    nodeID = service.getRootCollectionNode().getNodeID();
                }
            }
            if (IQ.Type.get == iq.getType()) {
                // Owner requests configuration form of a node
                getNodeConfiguration(service, iq, childElement, nodeID);
            } else {
                // Owner submits or cancels node configuration form
                configureNode(service, iq, action, nodeID);
            }
            return true;
        }
        action = childElement.element("default");
        if (action != null) {
            // Owner requests default configuration options for
            // leaf or collection nodes
            getDefaultNodeConfiguration(service, iq, childElement, action);
            return true;
        }
        action = childElement.element("delete");
        if (action != null) {
            // Owner deletes a node
            deleteNode(service, iq, action);
            return true;
        }
        action = childElement.element("subscriptions");
        if (action != null) {
            if (IQ.Type.get == iq.getType()) {
                // Owner requests all affiliated entities
                getNodeSubscriptions(service, iq, action);
            } else {
                modifyNodeSubscriptions(service, iq, action);
            }
            return true;
        }
        action = childElement.element("affiliations");
        if (action != null) {
            if (IQ.Type.get == iq.getType()) {
                // Owner requests all affiliated entities
                getNodeAffiliations(service, iq, action);
            } else {
                modifyNodeAffiliations(service, iq, action);
            }
            return true;
        }
        action = childElement.element("purge");
        if (action != null) {
            // Owner purges items from a node
            purgeNode(service, iq, action);
            return true;
        }
        // Unknown action requested so return error to sender
        sendErrorPacket(iq, PacketError.Condition.bad_request, null);
        return true;
    } else if ("http://jabber.org/protocol/commands".equals(namespace)) {
        // Process ad-hoc command
        IQ reply = service.getManager().process(iq);
        router.route(reply);
        return true;
    }
    return false;
}

From source file:org.jivesoftware.openfire.pubsub.PubSubModule.java

License:Open Source License

private void process(IQ iq) {
    // Ignore IQs of type ERROR
    if (IQ.Type.error == iq.getType()) {
        return;//from w  w w  .  j a va 2  s  .  c  om
    }
    Element childElement = iq.getChildElement();
    String namespace = null;

    if (childElement != null) {
        namespace = childElement.getNamespaceURI();
    }
    if ("http://jabber.org/protocol/disco#info".equals(namespace)) {
        // TODO PubSub should have an IQDiscoInfoHandler of its own when PubSub becomes
        // a component
        IQ reply = XMPPServer.getInstance().getIQDiscoInfoHandler().handleIQ(iq);
        router.route(reply);
    } else if ("http://jabber.org/protocol/disco#items".equals(namespace)) {
        // TODO PubSub should have an IQDiscoItemsHandler of its own when PubSub becomes
        // a component
        IQ reply = XMPPServer.getInstance().getIQDiscoItemsHandler().handleIQ(iq);
        router.route(reply);
    } else {
        // Unknown namespace requested so return error to sender
        engine.sendErrorPacket(iq, PacketError.Condition.service_unavailable, null);
    }
}

From source file:org.jivesoftware.openfire.sip.log.LogComponent.java

License:Open Source License

private void processIQ(IQ iq) {
    IQ reply = IQ.createResultIQ(iq);//  ww  w .j av a  2  s  . co  m
    Element childElement = iq.getChildElement();
    String namespace = childElement.getNamespaceURI();
    Element childElementCopy = iq.getChildElement().createCopy();
    reply.setChildElement(childElementCopy);

    if ("http://jabber.org/protocol/disco#info".equals(namespace)) {
        if (iq.getTo().getNode() == null) {
            // Return service identity and features
            Element identity = childElementCopy.addElement("identity");
            identity.addAttribute("category", "component");
            identity.addAttribute("type", "generic");
            identity.addAttribute("name", "Remote Logger");
            childElementCopy.addElement("feature").addAttribute("var", "http://jabber.org/protocol/disco#info");
            childElementCopy.addElement("feature").addAttribute("var", NAMESPACE);
        }
    } else if (NAMESPACE.equals(namespace)) {
        if (iq.getTo().getNode() == null && iq.getFrom() != null) {

            reply = logListener.logReceived(reply);
            //reply.setTo(reply.getFrom());

        } else {
            reply.getChildElement().addAttribute("type", "unregistered");

        }
    }
    try {
        componentManager.sendPacket(this, reply);
    } catch (Exception e) {
        Log.error(e.getMessage(), e);
    }

}

From source file:org.jivesoftware.phone.xmpp.PacketHandler.java

License:Open Source License

public void processPacket(IQ iq) {

    Element element = iq.getChildElement();
    String namespace = element.getNamespaceURI();

    if (NAMESPACE.equals(namespace)) {
        String type = element.attributeValue("type");

        if (PhoneAction.Type.DIAL.name().equals(type)) {
            handleDial(iq);/*from  ww w .  ja v a2s  .  co m*/
        } else if (PhoneAction.Type.FORWARD.name().equals(type)) {
            handleForward(iq);
        }

    } else if ("http://jabber.org/protocol/disco#info".equals(namespace)) {
        handleDisco(iq);
    } else {
        // We were given a packet we don't know how to handle, send an error back
        IQ reply = IQ.createResultIQ(iq);
        reply.setType(IQ.Type.error);
        PacketError error = new PacketError(PacketError.Condition.feature_not_implemented,
                PacketError.Type.cancel, "Unknown operation");
        reply.setError(error);
        send(reply);
    }

}

From source file:org.jivesoftware.smack.PacketReader.java

License:Open Source License

private IQ parseIQ(Element doc) {
    Element pingEl = doc.element(Ping.ELEMENT);
    if (pingEl != null && pingEl.getNamespace().getURI().equals(Ping.NAMESPACE)) {
        return new Ping(doc);
    }//w  ww .  jav  a  2s  . c om

    Element bindEl = doc.element(Bind.ELEMENT);
    if (bindEl != null && bindEl.getNamespace().getURI().equals(Bind.NAMESPACE)) {
        return new Bind(doc);
    }

    Element query = doc.element("query");
    if (query != null) {
        if ("jabber:iq:roster".equals(query.getNamespaceURI())) {
            return new Roster(doc);
        }
        if ("jabber:iq:auth".equals(query.getNamespaceURI())) {
            return new Authentication(doc);
        }
    }

    return new IQ(doc);
}

From source file:org.jivesoftware.xmpp.workgroup.search.IQChatSearchHandler.java

License:Open Source License

public void handleIQ(IQ packet) {
    try {//from   w  w w.  j  a v  a  2 s .co  m
        // Check that the sender of this IQ is an agent
        workgroupManager.getAgentManager().getAgent(packet.getFrom());

        Element iq = packet.getChildElement();

        IQ reply = IQ.createResultIQ(packet);

        if (iq.elements().isEmpty()) {
            reply.setChildElement(iq.createCopy());
            // Send the search form to the agent
            reply.addExtension(searchForm.createCopy());
            workgroupManager.send(reply);
        } else {
            // Send the result of the search to the agent
            Date startDate = null;
            Date endDate = null;
            Collection<Workgroup> workgroups = WorkgroupManager.getInstance().getWorkgroups();
            JID agentJID = null;
            String queryString = null;

            // Get the search parameters from the completed form
            DataForm submitedForm = (DataForm) packet.getExtension(DataForm.ELEMENT_NAME, DataForm.NAMESPACE);
            for (FormField field : submitedForm.getFields()) {
                if ("date/start".equals(field.getVariable())) {
                    try {
                        startDate = DataForm.parseDate(field.getValues().get(0));
                    } catch (ParseException e) {
                        Log.debug("Invalid startDate " + field.getValues().get(0), e);
                    }
                } else if ("date/end".equals(field.getVariable())) {
                    try {
                        endDate = DataForm.parseDate(field.getValues().get(0));
                    } catch (ParseException e) {
                        Log.debug("Invalid endDate " + field.getValues().get(0), e);
                    }
                } else if ("workgroups".equals(field.getVariable())) {
                    if (!field.getValues().isEmpty()) {
                        workgroups = new ArrayList<Workgroup>();
                        for (String value : field.getValues()) {
                            try {
                                workgroups.add(WorkgroupManager.getInstance().getWorkgroup(new JID(value)));
                            } catch (UserNotFoundException e) {
                                Log.debug("Invalid workgroup JID " + value, e);
                            }
                        }
                    } else {
                        // Search in all the workgroups since no one was specified
                        workgroups = WorkgroupManager.getInstance().getWorkgroups();
                    }
                } else if ("agent".equals(field.getVariable())) {
                    agentJID = new JID(field.getValues().get(0));
                } else if ("queryString".equals(field.getVariable())) {
                    queryString = field.getValues().get(0);
                }
            }

            // Build the response
            DataForm searchResults = resultForm.createCopy();
            // Perform the search
            for (Workgroup workgroup : workgroups) {
                ChatSearch search = new ChatSearch(workgroup, startDate, endDate, agentJID, queryString);
                for (QueryResult result : search.getResults()) {
                    Map<String, Object> fields = new LinkedHashMap<String, Object>();
                    fields.put("workgroup", result.getWorkgroup().getJID().toBareJID());
                    fields.put("sessionID", result.getSessionID());
                    fields.put("startDate", result.getStartDate());
                    fields.put("agentJIDs", result.getAgentJIDs());
                    fields.put("relevance", result.getRelevance());

                    // Add Metadata
                    Map<String, String> metadata = getMetadataMap(result.getSessionID());
                    if (metadata.containsKey("question")) {
                        fields.put("question", metadata.get("question"));
                    }

                    if (metadata.containsKey("email")) {
                        fields.put("email", metadata.get("email"));
                    }

                    if (metadata.containsKey("username")) {
                        fields.put("username", metadata.get("username"));
                    }

                    searchResults.addItemFields(fields);
                }
            }
            reply.setChildElement(iq.getName(), iq.getNamespaceURI());
            reply.addExtension(searchResults);
            workgroupManager.send(reply);
        }
    } catch (AgentNotFoundException e) {
        IQ reply = IQ.createResultIQ(packet);
        reply.setChildElement(packet.getChildElement().createCopy());
        reply.setError(new PacketError(PacketError.Condition.not_authorized));
        workgroupManager.send(reply);
    }
}